3
3
from axelrod .action import Action
4
4
from axelrod .player import Player
5
5
from prompt_toolkit import prompt
6
- from prompt_toolkit .styles import style_from_dict
7
- from prompt_toolkit .token import Token
8
6
from prompt_toolkit .validation import ValidationError , Validator
9
7
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
+
10
24
C , D = Action .C , Action .D
11
25
12
- toolbar_style = style_from_dict ({Token . Toolbar : "#ffffff bg:#333333" })
26
+ toolbar_style = style_from_dict ({token_toolbar : "#ffffff bg:#333333" })
13
27
14
28
15
29
class ActionValidator (Validator ):
@@ -65,7 +79,7 @@ def __init__(self, name="human", c_symbol="C", d_symbol="D"):
65
79
self .symbols = {C : c_symbol , D : d_symbol }
66
80
self .opponent_history = []
67
81
68
- def _history_toolbar (self , cli ):
82
+ def _history_toolbar (self ):
69
83
"""
70
84
A prompt-toolkit function to define the bottom toolbar.
71
85
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):
77
91
content = "History ({}, opponent): {}" .format (self .human_name , history )
78
92
else :
79
93
content = ""
80
- return [( Token . Toolbar , content )]
94
+ return content
81
95
82
96
def _status_messages (self ):
83
97
"""
@@ -95,7 +109,11 @@ def _status_messages(self):
95
109
mapping print or toolbar to the relevant string
96
110
"""
97
111
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
+ )
99
117
print_statement = "{}Turn {}: {} played {}, opponent played {}" .format (
100
118
linesep ,
101
119
len (self .history ),
@@ -124,8 +142,8 @@ def _get_human_input(self) -> Action: # pragma: no cover
124
142
len (self .history ) + 1 , self .human_name
125
143
),
126
144
validator = ActionValidator (),
127
- get_bottom_toolbar_tokens = self .status_messages ["toolbar" ],
128
145
style = toolbar_style ,
146
+ ** {bottom_toolbar_name : self .status_messages ["toolbar" ]},
129
147
)
130
148
131
149
return Action .from_char (action .upper ())
0 commit comments