33from axelrod .action import Action
44from axelrod .player import Player
55from prompt_toolkit import prompt
6- from prompt_toolkit .styles import style_from_dict
7- from prompt_toolkit .token import Token
86from prompt_toolkit .validation import ValidationError , Validator
97
8+ try : # pragma: no cover
9+ from prompt_toolkit .styles import style_from_dict
10+ from prompt_toolkit .token import Token
11+
12+ token_toolbar = Token .Toolbar
13+ bottom_toolbar_name = "get_bottom_toolbar_tokens"
14+ PROMPT2 = False
15+
16+ except ImportError : # prompt_toolkit v2
17+ from prompt_toolkit .styles import Style
18+
19+ style_from_dict = Style .from_dict
20+ token_toolbar = "pygments.toolbar"
21+ bottom_toolbar_name = "bottom_toolbar"
22+ PROMPT2 = True
23+
1024C , D = Action .C , Action .D
1125
12- toolbar_style = style_from_dict ({Token . Toolbar : "#ffffff bg:#333333" })
26+ toolbar_style = style_from_dict ({token_toolbar : "#ffffff bg:#333333" })
1327
1428
1529class ActionValidator (Validator ):
@@ -65,7 +79,7 @@ def __init__(self, name="human", c_symbol="C", d_symbol="D"):
6579 self .symbols = {C : c_symbol , D : d_symbol }
6680 self .opponent_history = []
6781
68- def _history_toolbar (self , cli ):
82+ def _history_toolbar (self ):
6983 """
7084 A prompt-toolkit function to define the bottom toolbar.
7185 Described at http://python-prompt-toolkit.readthedocs.io/en/latest/pages/building_prompts.html#adding-a-bottom-toolbar
@@ -77,7 +91,7 @@ def _history_toolbar(self, cli):
7791 content = "History ({}, opponent): {}" .format (self .human_name , history )
7892 else :
7993 content = ""
80- return [( Token . Toolbar , content )]
94+ return content
8195
8296 def _status_messages (self ):
8397 """
@@ -95,7 +109,11 @@ def _status_messages(self):
95109 mapping print or toolbar to the relevant string
96110 """
97111 if self .history :
98- toolbar = self ._history_toolbar
112+ toolbar = (
113+ self ._history_toolbar
114+ if PROMPT2
115+ else lambda cli : [(token_toolbar , self ._history_toolbar ())]
116+ )
99117 print_statement = "{}Turn {}: {} played {}, opponent played {}" .format (
100118 linesep ,
101119 len (self .history ),
@@ -124,8 +142,8 @@ def _get_human_input(self) -> Action: # pragma: no cover
124142 len (self .history ) + 1 , self .human_name
125143 ),
126144 validator = ActionValidator (),
127- get_bottom_toolbar_tokens = self .status_messages ["toolbar" ],
128145 style = toolbar_style ,
146+ ** {bottom_toolbar_name : self .status_messages ["toolbar" ]},
129147 )
130148
131149 return Action .from_char (action .upper ())
0 commit comments