Skip to content

Commit 97dfe5b

Browse files
authored
Merge pull request #295 from mischif/color-string
Use a function for colored strings
2 parents 8998621 + e1b4218 commit 97dfe5b

File tree

4 files changed

+302
-277
lines changed

4 files changed

+302
-277
lines changed

software/script/chameleon_cli_main.py

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import prompt_toolkit
1111
from prompt_toolkit.formatted_text import ANSI
1212
from prompt_toolkit.history import FileHistory
13-
from chameleon_utils import CR, CG, CY, C0
13+
from chameleon_utils import CR, CG, CY, color_string
1414

1515
ULTRA = r"""
1616
╦ ╦╦ ╔╦╗╦═╗╔═╗
@@ -68,10 +68,12 @@ def get_prompt(self):
6868
6969
:return: current cmd prompt
7070
"""
71-
device_string = f"{CG}USB" if self.device_com.isOpen(
72-
) else f"{CR}Offline"
73-
status = f"[{device_string}{C0}] chameleon --> "
74-
return status
71+
if self.device_com.isOpen():
72+
status = color_string((CG, 'USB'))
73+
else:
74+
status = color_string((CR, 'Offline'))
75+
76+
return ANSI(f"[{status}] chameleon --> ")
7577

7678
@staticmethod
7779
def print_banner():
@@ -80,7 +82,7 @@ def print_banner():
8082
8183
:return:
8284
"""
83-
print(f"{CY}{BANNER}{C0}")
85+
print(color_string((CY, BANNER)))
8486

8587
def exec_cmd(self, cmd_str):
8688
if cmd_str == '':
@@ -102,7 +104,7 @@ def exec_cmd(self, cmd_str):
102104
# Found tree node is a group without an implementation, print children
103105
print("".ljust(18, "-") + "".ljust(10) + "".ljust(30, "-"))
104106
for child in tree_node.children:
105-
cmd_title = f"{CG}{child.name}{C0}"
107+
cmd_title = color_string((CG, child.name))
106108
if not child.cls:
107109
help_line = (f" - {cmd_title}".ljust(37)) + f"{{ {child.help_text}... }}"
108110
else:
@@ -123,7 +125,7 @@ def exec_cmd(self, cmd_str):
123125
return
124126
except chameleon_utils.ArgsParserError as e:
125127
args.print_help()
126-
print(f'{CY}'+str(e).strip()+f'{C0}', end="\n\n")
128+
print(color_string((CY, str(e).strip())))
127129
return
128130
except chameleon_utils.ParserExitIntercept:
129131
# don't exit process.
@@ -144,10 +146,9 @@ def exec_cmd(self, cmd_str):
144146
raise error
145147

146148
except (chameleon_utils.UnexpectedResponseError, chameleon_utils.ArgsParserError) as e:
147-
print(f"{CR}{str(e)}{C0}")
149+
print(color_string((CR, str(e))))
148150
except Exception:
149-
print(
150-
f"CLI exception: {CR}{traceback.format_exc()}{C0}")
151+
print(f"CLI exception: {color_string((CR, traceback.format_exc()))}")
151152

152153
def startCLI(self):
153154
"""
@@ -169,7 +170,7 @@ def startCLI(self):
169170
# wait user input
170171
try:
171172
cmd_str = self.session.prompt(
172-
ANSI(self.get_prompt())).strip()
173+
self.get_prompt()).strip()
173174
cmd_strs = cmd_str.replace(
174175
"\r\n", "\n").replace("\r", "\n").split("\n")
175176
cmd_str = cmd_strs.pop(0)

0 commit comments

Comments
 (0)