Coding Style

- Use better termstr contructor.
This commit is contained in:
Andrew Hamilton 2022-10-20 16:26:09 +10:00
parent 54814ac0d3
commit 8efb2aaa69
6 changed files with 17 additions and 17 deletions

View file

@ -815,7 +815,7 @@ class Screen:
self._log.log_message([in_green("Refreshing all results of "), tool_name, in_green("")]) self._log.log_message([in_green("Refreshing all results of "), tool_name, in_green("")])
self._summary.refresh_tool(selection.tool) self._summary.refresh_tool(selection.tool)
_DIMMED_BORDER = [termstr.TermStr(part).fg_color(termstr.Color.grey_100) _DIMMED_BORDER = [termstr.TermStr(part, fg_color=termstr.Color.grey_100)
for part in fill3.Border.THIN] for part in fill3.Border.THIN]
def _set_focus(self): def _set_focus(self):
@ -920,7 +920,7 @@ class Screen:
@functools.cache @functools.cache
def _get_partial_bar_chars(self, bar_transparency): def _get_partial_bar_chars(self, bar_transparency):
bar_color = termstr.blend_color(termstr.Color.black, termstr.Color.white, bar_transparency) bar_color = termstr.blend_color(termstr.Color.black, termstr.Color.white, bar_transparency)
return [termstr.TermStr(char).fg_color(bar_color).bg_color(termstr.Color.black) return [termstr.TermStr(char, fg_color=bar_color, bg_color=termstr.Color.black)
for char in fill3.ScrollBar._PARTIAL_CHARS[1]] for char in fill3.ScrollBar._PARTIAL_CHARS[1]]
def _get_status_bar_appearance(self, width, progress_bar_size): def _get_status_bar_appearance(self, width, progress_bar_size):
@ -1113,8 +1113,8 @@ def print_tool_info():
for tool in tools_: for tool in tools_:
extensions_for_tool.setdefault(tool, {extension}).add(extension) extensions_for_tool.setdefault(tool, {extension}).add(extension)
for tool in sorted(tools.tools_all(), key=lambda t: t.__name__): for tool in sorted(tools.tools_all(), key=lambda t: t.__name__):
print(termstr.TermStr(tool.__name__).bold() if tools.is_tool_available(tool) print(termstr.TermStr(tool.__name__, is_bold=True) if tools.is_tool_available(tool)
else termstr.TermStr(tool.__name__).fg_color(termstr.Color.red) + " (not available)") else termstr.TermStr(tool.__name__, fg_color=termstr.Color.red) + " (not available)")
if hasattr(tool, "command"): if hasattr(tool, "command"):
print(f"command: {tool.command} foo.{extensions[0]}") print(f"command: {tool.command} foo.{extensions[0]}")
else: else:

View file

@ -10,7 +10,7 @@ import termstr
class TermDoc(pydoc.TextDoc): class TermDoc(pydoc.TextDoc):
def bold(self, text): def bold(self, text):
return str(termstr.TermStr(text).bold()) return str(termstr.TermStr(text, is_bold=True))
def main(): def main():

View file

@ -64,7 +64,7 @@ STATUS_MEANINGS = [(Status.ok, "Ok"), (Status.problem, "Problem"),
(Status.error, "Error")] (Status.error, "Error")]
STATUS_TO_TERMSTR = {status: termstr.TermStr(" ", termstr.CharStyle(bg_color=color)) STATUS_TO_TERMSTR = {status: termstr.TermStr(" ", termstr.CharStyle(bg_color=color))
for status, color in _STATUS_COLORS.items()} for status, color in _STATUS_COLORS.items()}
STATUS_TO_TERMSTR[Status.pending] = termstr.TermStr(".").fg_color(termstr.Color.grey_100) STATUS_TO_TERMSTR[Status.pending] = termstr.TermStr(".", fg_color=termstr.Color.grey_100)
TIMEOUT = 60 TIMEOUT = 60
@ -130,7 +130,7 @@ def _syntax_highlight(text, lexer, style):
text = termstr.join("", [termstr.TermStr(text, _char_style_for_token_type( text = termstr.join("", [termstr.TermStr(text, _char_style_for_token_type(
token_type, default_bg_color, default_style)) token_type, default_bg_color, default_style))
for token_type, text in pygments.lex(text, lexer)]) for token_type, text in pygments.lex(text, lexer)])
text_widget = fill3.Text(text, pad_char=termstr.TermStr(" ").bg_color(default_bg_color)) text_widget = fill3.Text(text, pad_char=termstr.TermStr(" ", bg_color=default_bg_color))
return termstr.join("\n", text_widget.text) return termstr.join("\n", text_widget.text)
@ -170,7 +170,7 @@ def metadata(path):
def detail(value, unit): def detail(value, unit):
result = f" ({value})" if unit is None else f" ({value} {unit})" result = f" ({value})" if unit is None else f" ({value} {unit})"
return termstr.TermStr(result).fg_color(termstr.Color.grey_100) return termstr.TermStr(result, fg_color=termstr.Color.grey_100)
is_symlink = "yes" if os.path.islink(path) else "no" is_symlink = "yes" if os.path.islink(path) else "no"
stat_result = os.stat(path) stat_result = os.stat(path)
permissions = stat.filemode(stat_result.st_mode) permissions = stat.filemode(stat_result.st_mode)
@ -197,7 +197,7 @@ def metadata(path):
text.append("\n") text.append("\n")
else: else:
name, value = line name, value = line
name = termstr.TermStr(name + ":").fg_color(termstr.Color.blue).ljust(16) name = termstr.TermStr(name + ":", fg_color=termstr.Color.blue).ljust(16)
text.append(name + termstr.join("", value) + "\n") text.append(name + termstr.join("", value) + "\n")
return Status.ok, termstr.join("", text) return Status.ok, termstr.join("", text)
@ -268,7 +268,7 @@ def mypy(path):
def _colorize_coverage_report(lines): def _colorize_coverage_report(lines):
line_color = {"> ": termstr.Color.green, "! ": termstr.Color.grey_150, " ": None} line_color = {"> ": termstr.Color.green, "! ": termstr.Color.grey_150, " ": None}
return termstr.join("", [termstr.TermStr(line).fg_color(line_color[line[:2]]) for line in lines]) return termstr.join("", [termstr.TermStr(line, fg_color=line_color[line[:2]]) for line in lines])
@deps(deps={"python3-coverage"}, url="https://coverage.readthedocs.io/") @deps(deps={"python3-coverage"}, url="https://coverage.readthedocs.io/")
@ -329,7 +329,7 @@ def _get_mccabe_line_score(line):
def _colorize_mccabe(text): def _colorize_mccabe(text):
return termstr.join("", [ return termstr.join("", [
termstr.TermStr(line).fg_color(termstr.Color.yellow) termstr.TermStr(line, fg_color=termstr.Color.yellow)
if _get_mccabe_line_score(line) > 10 else line if _get_mccabe_line_score(line) > 10 else line
for line in text.splitlines(keepends=True)]) for line in text.splitlines(keepends=True)])

View file

@ -48,7 +48,7 @@ def make_listing_page(url_path):
tool = getattr(tools, tool_name) tool = getattr(tools, tool_name)
tool_name_colored = tools.tool_name_colored(tool, path) tool_name_colored = tools.tool_name_colored(tool, path)
header = fill3.appearance_as_html([lscolors.path_colored(path) + " - " + tool_name_colored, header = fill3.appearance_as_html([lscolors.path_colored(path) + " - " + tool_name_colored,
termstr.TermStr(" ").underline() * 100]) termstr.TermStr(" " * 100, is_underlined=True)])
body = fill3.appearance_as_html(result.appearance()) body = fill3.appearance_as_html(result.appearance())
return make_page(header + body, f"{path} - {tool_name}") return make_page(header + body, f"{path} - {tool_name}")

View file

@ -165,13 +165,13 @@ class ScrollBar:
self.interval = interval self.interval = interval
bar_color = bar_color or ScrollBar.DEFAULT_BAR_COLOR bar_color = bar_color or ScrollBar.DEFAULT_BAR_COLOR
background_color = background_color or ScrollBar.DEFAULT_BACKGROUND_COLOR background_color = background_color or ScrollBar.DEFAULT_BACKGROUND_COLOR
self._bar_char = termstr.TermStr("").fg_color(bar_color) self._bar_char = termstr.TermStr("", fg_color=bar_color)
self._background_char = termstr.TermStr(" ").bg_color(background_color) self._background_char = termstr.TermStr(" ", bg_color=background_color)
if self._is_horizontal: if self._is_horizontal:
bar_color, background_color = background_color, bar_color bar_color, background_color = background_color, bar_color
self._partial_chars = [ self._partial_chars = [
(termstr.TermStr(char).fg_color(bar_color).bg_color(background_color), (termstr.TermStr(char, fg_color=bar_color, bg_color=background_color),
termstr.TermStr(char).fg_color(background_color).bg_color(bar_color)) termstr.TermStr(char, fg_color=background_color, bg_color=bar_color))
for char in self._PARTIAL_CHARS[self._is_horizontal]] for char in self._PARTIAL_CHARS[self._is_horizontal]]
def appearance_for(self, dimensions): def appearance_for(self, dimensions):

View file

@ -164,6 +164,6 @@ def path_colored(path):
dirname = dirname + os.path.sep dirname = dirname + os.path.sep
dir_style = _charstyle_of_path(os.path.sep) dir_style = _charstyle_of_path(os.path.sep)
parts = [termstr.TermStr(part, dir_style) for part in dirname.split(os.path.sep)] parts = [termstr.TermStr(part, dir_style) for part in dirname.split(os.path.sep)]
path_sep = termstr.TermStr(os.path.sep).fg_color(termstr.Color.grey_100) path_sep = termstr.TermStr(os.path.sep, fg_color=termstr.Color.grey_100)
dir_name = termstr.join(path_sep, parts) dir_name = termstr.join(path_sep, parts)
return dir_name + termstr.TermStr(basename, char_style) return dir_name + termstr.TermStr(basename, char_style)