Coding style

- Rename appearance to appearance_for.
This commit is contained in:
Andrew Hamilton 2022-01-18 14:32:11 +10:00
parent 62ebbd562b
commit 6681101b1c
4 changed files with 65 additions and 64 deletions

View file

@ -432,14 +432,14 @@ class Summary:
+ appearance[highlighted_y][-1]) + appearance[highlighted_y][-1])
return appearance return appearance
def appearance(self, dimensions): def appearance_for(self, dimensions):
width, height = dimensions width, height = dimensions
if len(self._entries) == 0: if len(self._entries) == 0:
return [" " * width for row in range(height)] return [" " * width for row in range(height)]
cursor_x, cursor_y = self.cursor_position() cursor_x, cursor_y = self.cursor_position()
width, height = width - 1, height - 1 # Minus one for the scrollbars width, height = width - 1, height - 1 # Minus one for the scrollbars
self._set_scroll_position(cursor_x, cursor_y, height) self._set_scroll_position(cursor_x, cursor_y, height)
return self._highlight_cursor_row(self._view_widget.appearance(dimensions), cursor_y) return self._highlight_cursor_row(self._view_widget.appearance_for(dimensions), cursor_y)
def scroll(self, dx, dy): def scroll(self, dx, dy):
scroll_x, scroll_y = self._view_widget.position scroll_x, scroll_y = self._view_widget.position
@ -579,7 +579,7 @@ class Log:
def log_command(self, message, timestamp=None): def log_command(self, message, timestamp=None):
self.log_message(message, char_style=Log._GREEN_STYLE) self.log_message(message, char_style=Log._GREEN_STYLE)
def appearance(self, dimensions): def appearance_for(self, dimensions):
if self._appearance is None or fill3.appearance_dimensions(self._appearance) != dimensions: if self._appearance is None or fill3.appearance_dimensions(self._appearance) != dimensions:
width, height = dimensions width, height = dimensions
del self.lines[:-height] del self.lines[:-height]
@ -632,8 +632,8 @@ class Help:
action() action()
fill3.APPEARANCE_CHANGED_EVENT.set() fill3.APPEARANCE_CHANGED_EVENT.set()
def appearance(self, dimensions): def appearance_for(self, dimensions):
return self.widget.appearance(dimensions) return self.widget.appearance_for(dimensions)
class Listing: class Listing:
@ -642,9 +642,9 @@ class Listing:
self.view = view self.view = view
self.last_dimensions = None self.last_dimensions = None
def appearance(self, dimensions): def appearance_for(self, dimensions):
self.last_dimensions = dimensions self.last_dimensions = dimensions
return self.view.appearance(dimensions) return self.view.appearance_for(dimensions)
class Screen: class Screen:
@ -946,7 +946,7 @@ class Screen:
max(0, width * incomplete / self._summary.result_total)) max(0, width * incomplete / self._summary.result_total))
return self._get_status_bar_appearance(width, progress_bar_size) return self._get_status_bar_appearance(width, progress_bar_size)
def appearance(self, dimensions): def appearance_for(self, dimensions):
if len(self._summary._entries) > 0: if len(self._summary._entries) > 0:
self._fix_listing() self._fix_listing()
if self._is_help_visible: if self._is_help_visible:
@ -956,7 +956,7 @@ class Screen:
else: else:
body = self._layouts[self._is_log_visible][self._is_listing_portrait] body = self._layouts[self._is_log_visible][self._is_listing_portrait]
width, height = max(dimensions[0], 10), max(dimensions[1], 20) width, height = max(dimensions[0], 10), max(dimensions[1], 20)
result = body.appearance((width, height-1)) + self._get_status_bar(width) result = body.appearance_for((width, height-1)) + self._get_status_bar(width)
return (result if (width, height) == dimensions return (result if (width, height) == dimensions
else fill3.appearance_resize(result, dimensions)) else fill3.appearance_resize(result, dimensions))

View file

@ -22,7 +22,8 @@ _DIMENSIONS = (100, 60)
def _widget_to_string(widget, dimensions=_DIMENSIONS): def _widget_to_string(widget, dimensions=_DIMENSIONS):
appearance = widget.appearance_min() if dimensions is None else widget.appearance(dimensions) appearance = (widget.appearance_min() if dimensions is None
else widget.appearance_for(dimensions))
return str(fill3.join("\n", appearance)) return str(fill3.join("\n", appearance))

View file

@ -81,11 +81,11 @@ class Row:
self.widgets = widgets self.widgets = widgets
self.widths_func = widths_func self.widths_func = widths_func
def appearance(self, dimensions): def appearance_for(self, dimensions):
width, height = dimensions width, height = dimensions
widths = self.widths_func(self.widgets, width) widths = self.widths_func(self.widgets, width)
assert sum(widths) == width, (sum(widths), width) assert sum(widths) == width, (sum(widths), width)
return join_horizontal([column_widget.appearance((item_width, height)) return join_horizontal([column_widget.appearance_for((item_width, height))
for column_widget, item_width in zip(self.widgets, widths)]) for column_widget, item_width in zip(self.widgets, widths)])
def appearance_min(self): def appearance_min(self):
@ -120,13 +120,13 @@ class Column:
self.partition_func = partition_func self.partition_func = partition_func
self.background_char = background_char self.background_char = background_char
def appearance(self, dimensions): def appearance_for(self, dimensions):
width, height = dimensions width, height = dimensions
if len(self.widgets) == 0: # FIX: Really allow zero widgets? if len(self.widgets) == 0: # FIX: Really allow zero widgets?
return [self.background_char * width] * height return [self.background_char * width] * height
heights = self.partition_func(self.widgets, height) heights = self.partition_func(self.widgets, height)
assert sum(heights) == height, (sum(heights), height) assert sum(heights) == height, (sum(heights), height)
return join_vertical([row_widget.appearance((width, item_height)) return join_vertical([row_widget.appearance_for((width, item_height))
for row_widget, item_height in zip(self.widgets, heights)]) for row_widget, item_height in zip(self.widgets, heights)])
def _appearance_list(self, widgets): def _appearance_list(self, widgets):
@ -155,7 +155,7 @@ class Filler:
def __init__(self, widget): def __init__(self, widget):
self.widget = widget self.widget = widget
def appearance(self, dimensions): def appearance_for(self, dimensions):
return appearance_resize(self.widget.appearance_min(), dimensions) return appearance_resize(self.widget.appearance_min(), dimensions)
@ -180,7 +180,7 @@ class ScrollBar:
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(self, dimensions): def appearance_for(self, dimensions):
width, height = dimensions width, height = dimensions
assert width == 1 or height == 1, (width, height) assert width == 1 or height == 1, (width, height)
length = width if self._is_horizontal else height length = width if self._is_horizontal else height
@ -225,7 +225,7 @@ class Portal:
def scroll_right(self): def scroll_right(self):
self._scroll_half_pages(1, 0) self._scroll_half_pages(1, 0)
def appearance(self, dimensions): def appearance_for(self, dimensions):
width, height = dimensions width, height = dimensions
x, y = self.position x, y = self.position
try: try:
@ -265,7 +265,7 @@ class View:
def widget(self, widget): def widget(self, widget):
self.portal.widget = widget self.portal.widget = widget
def appearance(self, dimensions): def appearance_for(self, dimensions):
width, height = dimensions width, height = dimensions
try: try:
full_width, full_height = self.portal.widget.appearance_dimensions() full_width, full_height = self.portal.widget.appearance_dimensions()
@ -273,7 +273,7 @@ class View:
full_appearance = self.portal.widget.appearance_min() full_appearance = self.portal.widget.appearance_min()
full_width, full_height = appearance_dimensions(full_appearance) full_width, full_height = appearance_dimensions(full_appearance)
if full_width == 0 or full_height == 0: if full_width == 0 or full_height == 0:
return self.portal.appearance(dimensions) return self.portal.appearance_for(dimensions)
x, y = self.portal.position x, y = self.portal.position
hide_scrollbar_vertical = self.hide_scrollbars and full_height <= height and y == 0 hide_scrollbar_vertical = self.hide_scrollbars and full_height <= height and y == 0
hide_scrollbar_horizontal = self.hide_scrollbars and full_width <= width and x == 0 hide_scrollbar_horizontal = self.hide_scrollbars and full_width <= width and x == 0
@ -285,14 +285,14 @@ class View:
full_height = max(full_height, y + height) full_height = max(full_height, y + height)
self.vertical_scrollbar.interval = (y / full_height, (y + height) / full_height) self.vertical_scrollbar.interval = (y / full_height, (y + height) / full_height)
width -= 1 width -= 1
portal_appearance = self.portal.appearance((width, height)) portal_appearance = self.portal.appearance_for((width, height))
if hide_scrollbar_vertical: if hide_scrollbar_vertical:
result = portal_appearance result = portal_appearance
else: else:
scrollbar_v_appearance = self.vertical_scrollbar.appearance((1, height)) scrollbar_v_appearance = self.vertical_scrollbar.appearance_for((1, height))
result = join_horizontal([portal_appearance, scrollbar_v_appearance]) result = join_horizontal([portal_appearance, scrollbar_v_appearance])
if not hide_scrollbar_horizontal: if not hide_scrollbar_horizontal:
scrollbar_h_appearance = self.horizontal_scrollbar.appearance((width, 1)) scrollbar_h_appearance = self.horizontal_scrollbar.appearance_for((width, 1))
result.append(scrollbar_h_appearance[0] + ("" if hide_scrollbar_vertical else " ")) result.append(scrollbar_h_appearance[0] + ("" if hide_scrollbar_vertical else " "))
return result return result
@ -314,7 +314,7 @@ class Text:
def appearance_min(self): def appearance_min(self):
return self.text return self.text
def appearance(self, dimensions): def appearance_for(self, dimensions):
return appearance_resize(self.appearance_min(), dimensions) return appearance_resize(self.appearance_min(), dimensions)
@ -379,9 +379,9 @@ class Border:
def appearance_min(self): def appearance_min(self):
return self._add_border(self.widget.appearance_min()) return self._add_border(self.widget.appearance_min())
def appearance(self, dimensions): def appearance_for(self, dimensions):
width, height = dimensions width, height = dimensions
return self._add_border(self.widget.appearance((width-2, height-2))) return self._add_border(self.widget.appearance_for((width-2, height-2)))
class Placeholder: class Placeholder:
@ -392,8 +392,8 @@ class Placeholder:
def appearance_min(self): def appearance_min(self):
return self.widget.appearance_min() return self.widget.appearance_min()
def appearance(self, dimensions): def appearance_for(self, dimensions):
return self.widget.appearance(dimensions) return self.widget.appearance_for(dimensions)
class Fixed: class Fixed:
@ -430,7 +430,7 @@ def handle_exception(func):
@handle_exception @handle_exception
def draw_screen(widget): def draw_screen(widget):
global _LAST_APPEARANCE global _LAST_APPEARANCE
appearance = widget.appearance(os.get_terminal_size()) appearance = widget.appearance_for(os.get_terminal_size())
print(terminal.move(0, 0), *appearance, sep="", end="", flush=True) print(terminal.move(0, 0), *appearance, sep="", end="", flush=True)
_LAST_APPEARANCE = appearance _LAST_APPEARANCE = appearance
@ -438,7 +438,7 @@ def draw_screen(widget):
@handle_exception @handle_exception
def patch_screen(widget): def patch_screen(widget):
global _LAST_APPEARANCE global _LAST_APPEARANCE
appearance = widget.appearance(os.get_terminal_size()) appearance = widget.appearance_for(os.get_terminal_size())
zip_func = (itertools.zip_longest if len(appearance) > len(_LAST_APPEARANCE) else zip) zip_func = (itertools.zip_longest if len(appearance) > len(_LAST_APPEARANCE) else zip)
changed_lines = (str(terminal.move(0, row_index)) + line for row_index, (line, old_line) changed_lines = (str(terminal.move(0, row_index)) + line for row_index, (line, old_line)
in enumerate(zip_func(appearance, _LAST_APPEARANCE)) if line != old_line) in enumerate(zip_func(appearance, _LAST_APPEARANCE)) if line != old_line)
@ -504,8 +504,8 @@ class _Screen:
def __init__(self): def __init__(self):
self.content = Filler(Text("Hello World")) self.content = Filler(Text("Hello World"))
def appearance(self, dimensions): def appearance_for(self, dimensions):
return self.content.appearance(dimensions) return self.content.appearance_for(dimensions)
def on_keyboard_input(self, term_code): def on_keyboard_input(self, term_code):
if term_code in ["q", terminal.ESC, terminal.CTRL_C]: if term_code in ["q", terminal.ESC, terminal.CTRL_C]:

View file

@ -19,7 +19,7 @@ class WidgetTests(unittest.TestCase):
rows = fill3.Row([self.TEXT_A, self.TEXT_B]) rows = fill3.Row([self.TEXT_A, self.TEXT_B])
self.assert_string(rows.appearance_min(), "AB") self.assert_string(rows.appearance_min(), "AB")
rows = fill3.Row([fill3.Filler(self.TEXT_A), fill3.Filler(self.TEXT_B)]) rows = fill3.Row([fill3.Filler(self.TEXT_A), fill3.Filler(self.TEXT_B)])
self.assert_string(rows.appearance((4, 1)), "A B ") self.assert_string(rows.appearance_for((4, 1)), "A B ")
def test_columns_widget(self): def test_columns_widget(self):
columns = fill3.Column([self.TEXT_A, self.TEXT_B]) columns = fill3.Column([self.TEXT_A, self.TEXT_B])
@ -35,30 +35,30 @@ class WidgetTests(unittest.TestCase):
def test_portal_widget(self): def test_portal_widget(self):
row = fill3.Row([fill3.Text("foo"), fill3.Text("bar")]) row = fill3.Row([fill3.Text("foo"), fill3.Text("bar")])
portal = fill3.Portal(row, (1, 0)) portal = fill3.Portal(row, (1, 0))
self.assert_string(portal.appearance((5, 1)), "oobar") self.assert_string(portal.appearance_for((5, 1)), "oobar")
portal.position = (0, 10) portal.position = (0, 10)
self.assert_string(portal.appearance((1, 1)), " ") self.assert_string(portal.appearance_for((1, 1)), " ")
def test_border_widget(self): def test_border_widget(self):
contents = fill3.Filler(self.TEXT_A) contents = fill3.Filler(self.TEXT_A)
self.assert_string(fill3.Border(contents).appearance((3, 3)), "┌─┐\n" self.assert_string(fill3.Border(contents).appearance_for((3, 3)), "┌─┐\n"
"│A│\n" "│A│\n"
"└─┘") "└─┘")
for empty_contents in [fill3.Filler(fill3.Text("")), fill3.Column([])]: for empty_contents in [fill3.Filler(fill3.Text("")), fill3.Column([])]:
self.assert_string(fill3.Border(empty_contents).appearance((2, 2)), "┌┐\n" self.assert_string(fill3.Border(empty_contents).appearance_for((2, 2)), "┌┐\n"
"└┘") "└┘")
self.assert_string(fill3.Border(fill3.Column([])).appearance_min(), "┌┐\n" self.assert_string(fill3.Border(fill3.Column([])).appearance_min(), "┌┐\n"
"└┘") "└┘")
self.assert_string(fill3.Border(empty_contents).appearance((3, 3)), "┌─┐\n" self.assert_string(fill3.Border(empty_contents).appearance_for((3, 3)), "┌─┐\n"
"│ │\n" "│ │\n"
"└─┘") "└─┘")
text = fill3.Text("abcdef") text = fill3.Text("abcdef")
self.assert_string(fill3.Border(text, title="AB").appearance((8, 3)), "┌─ AB ─┐\n" self.assert_string(fill3.Border(text, title="AB").appearance_for((8, 3)), "┌─ AB ─┐\n"
"│abcdef│\n" "│abcdef│\n"
"└──────┘") "└──────┘")
self.assert_string(fill3.Border(text, title="ABC").appearance((6, 3)), "┌ …C ┐\n" self.assert_string(fill3.Border(text, title="ABC").appearance_for((6, 3)), "┌ …C ┐\n"
"│abcd│\n" "│abcd│\n"
"└────┘") "└────┘")
def test_placeholder_widget(self): def test_placeholder_widget(self):
placeholder = fill3.Placeholder(self.TEXT_A) placeholder = fill3.Placeholder(self.TEXT_A)
@ -74,32 +74,32 @@ class WidgetTests(unittest.TestCase):
def test_scroll_bar(self): def test_scroll_bar(self):
scroll_bar = fill3.ScrollBar(is_horizontal=True) scroll_bar = fill3.ScrollBar(is_horizontal=True)
self.assertEqual(scroll_bar.interval, (0, 0)) self.assertEqual(scroll_bar.interval, (0, 0))
self.assert_string2(scroll_bar.appearance((1, 1)), (" ", "i")) self.assert_string2(scroll_bar.appearance_for((1, 1)), (" ", "i"))
scroll_bar.interval = (0, 0.5) scroll_bar.interval = (0, 0.5)
self.assert_string2(scroll_bar.appearance((2, 1)), (" ", "i ")) self.assert_string2(scroll_bar.appearance_for((2, 1)), (" ", "i "))
scroll_bar.interval = (0, 0.1) scroll_bar.interval = (0, 0.1)
self.assert_string2(scroll_bar.appearance((2, 1)), (" ", "i ")) self.assert_string2(scroll_bar.appearance_for((2, 1)), (" ", "i "))
scroll_bar.interval = (0.25, 0.75) scroll_bar.interval = (0.25, 0.75)
self.assert_string2(scroll_bar.appearance((4, 1)), ("", " i ")) self.assert_string2(scroll_bar.appearance_for((4, 1)), ("", " i "))
scroll_bar.interval = (0, 0.75) scroll_bar.interval = (0, 0.75)
self.assert_string2(scroll_bar.appearance((2, 1)), ("", "i ")) self.assert_string2(scroll_bar.appearance_for((2, 1)), ("", "i "))
scroll_bar = fill3.ScrollBar(is_horizontal=False) scroll_bar = fill3.ScrollBar(is_horizontal=False)
self.assertEqual(scroll_bar.interval, (0, 0)) self.assertEqual(scroll_bar.interval, (0, 0))
self.assert_string2(scroll_bar.appearance((1, 1)), ("", " ")) self.assert_string2(scroll_bar.appearance_for((1, 1)), ("", " "))
scroll_bar.interval = (0, 0.5) scroll_bar.interval = (0, 0.5)
self.assert_string2(scroll_bar.appearance((1, 2)), ("\n" self.assert_string2(scroll_bar.appearance_for((1, 2)), ("\n"
"", " i")) "", " i"))
scroll_bar.interval = (0, 0.1) scroll_bar.interval = (0, 0.1)
self.assert_string2(scroll_bar.appearance((1, 2)), ("\n" self.assert_string2(scroll_bar.appearance_for((1, 2)), ("\n"
"", " i")) "", " i"))
scroll_bar.interval = (0.25, 0.75) scroll_bar.interval = (0.25, 0.75)
self.assert_string2(scroll_bar.appearance((1, 4)), (" \n" self.assert_string2(scroll_bar.appearance_for((1, 4)), (" \n"
"\n" "\n"
"\n" "\n"
"", " i")) "", " i"))
scroll_bar.interval = (0, 0.75) scroll_bar.interval = (0, 0.75)
self.assert_string2(scroll_bar.appearance((1, 2)), ("\n" self.assert_string2(scroll_bar.appearance_for((1, 2)), ("\n"
"", " i")) "", " i"))
def test_table_widget(self): def test_table_widget(self):
table = fill3.Table([]) table = fill3.Table([])