[Console] Refactor single letter variables

- Replace usage of `s` for variable names to make it easier to read the code.
- Remove unneeded and unused encoding parameter from parse_color_string
  It should not be encoded by this function, only on output.
This commit is contained in:
Calum Lind 2018-10-22 15:55:09 +01:00
parent 1838403e3b
commit 9ab2a50097
3 changed files with 21 additions and 23 deletions

View File

@ -273,7 +273,7 @@ def add_string(
"""
try:
parsed = colors.parse_color_string(fstring, encoding)
parsed = colors.parse_color_string(fstring)
except colors.BadColorString as ex:
log.error('Cannot add bad color string %s: %s', fstring, ex)
return

View File

@ -538,21 +538,21 @@ class CmdLine(BaseMode, Commander):
"""
col = 0
try:
parsed = colors.parse_color_string(string, self.encoding)
parsed = colors.parse_color_string(string)
except colors.BadColorString as ex:
log.error('Cannot add bad color string %s: %s', string, ex)
return
for index, (color, s) in enumerate(parsed):
for index, (color, p_str) in enumerate(parsed):
if index + 1 == len(parsed):
# This is the last string so lets append some " " to it
s += ' ' * (self.cols - (col + strwidth(s)) - 1)
p_str += ' ' * (self.cols - (col + strwidth(p_str)) - 1)
try:
self.stdscr.addstr(row, col, s.encode(self.encoding), color)
self.stdscr.addstr(row, col, p_str.encode(self.encoding), color)
except curses.error:
pass
col += strwidth(s)
col += strwidth(p_str)
def set_batch_write(self, batch):
"""

View File

@ -175,37 +175,35 @@ def get_line_width(line):
return format_utils.strwidth(line)
def parse_color_string(s, encoding='UTF-8'):
"""
Parses a string and returns a list of 2-tuples (color, string).
:param s:, string to parse
:param encoding: the encoding to use on output
def parse_color_string(string):
"""Parses a string and returns a list of 2-tuples (color, string).
Args:
string (str): The string to parse.
"""
check_tag_count(s)
check_tag_count(string)
ret = []
last_color_attr = None
# Keep track of where the strings
while s.find(color_tag_start) != -1:
begin = s.find(color_tag_start)
while string.find(color_tag_start) != -1:
begin = string.find(color_tag_start)
if begin > 0:
ret.append(
(
curses.color_pair(
color_pairs[(schemes['input'][0], schemes['input'][1])]
),
s[:begin],
string[:begin],
)
)
end = s.find(color_tag_end)
end = string.find(color_tag_end)
if end == -1:
raise BadColorString('Missing closing "!}"')
# Get a list of attributes in the bracketed section
attrs = s[begin + 2 : end].split(',')
attrs = string[begin + 2 : end].split(',')
if len(attrs) == 1 and not attrs[0].strip(' '):
raise BadColorString('No description in {! !}')
@ -279,18 +277,18 @@ def parse_color_string(s, encoding='UTF-8'):
last_color_attr = color_pair
# We need to find the text now, so lets try to find another {! and if
# there isn't one, then it's the rest of the string
next_begin = s.find(color_tag_start, end)
next_begin = string.find(color_tag_start, end)
if next_begin == -1:
ret.append((color_pair, replace_tabs(s[end + 2 :])))
ret.append((color_pair, replace_tabs(string[end + 2 :])))
break
else:
ret.append((color_pair, replace_tabs(s[end + 2 : next_begin])))
s = s[next_begin:]
ret.append((color_pair, replace_tabs(string[end + 2 : next_begin])))
string = string[next_begin:]
if not ret:
# There was no color scheme so we add it with a 0 for white on black
ret = [(0, s)]
ret = [(0, string)]
return ret