Coding style
- Rename appearance to appearance_for.
This commit is contained in:
parent
62ebbd562b
commit
6681101b1c
4 changed files with 65 additions and 64 deletions
|
|
@ -81,11 +81,11 @@ class Row:
|
|||
self.widgets = widgets
|
||||
self.widths_func = widths_func
|
||||
|
||||
def appearance(self, dimensions):
|
||||
def appearance_for(self, dimensions):
|
||||
width, height = dimensions
|
||||
widths = self.widths_func(self.widgets, 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)])
|
||||
|
||||
def appearance_min(self):
|
||||
|
|
@ -120,13 +120,13 @@ class Column:
|
|||
self.partition_func = partition_func
|
||||
self.background_char = background_char
|
||||
|
||||
def appearance(self, dimensions):
|
||||
def appearance_for(self, dimensions):
|
||||
width, height = dimensions
|
||||
if len(self.widgets) == 0: # FIX: Really allow zero widgets?
|
||||
return [self.background_char * width] * height
|
||||
heights = self.partition_func(self.widgets, 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)])
|
||||
|
||||
def _appearance_list(self, widgets):
|
||||
|
|
@ -155,7 +155,7 @@ class Filler:
|
|||
def __init__(self, widget):
|
||||
self.widget = widget
|
||||
|
||||
def appearance(self, dimensions):
|
||||
def appearance_for(self, 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))
|
||||
for char in self._PARTIAL_CHARS[self._is_horizontal]]
|
||||
|
||||
def appearance(self, dimensions):
|
||||
def appearance_for(self, dimensions):
|
||||
width, height = dimensions
|
||||
assert width == 1 or height == 1, (width, height)
|
||||
length = width if self._is_horizontal else height
|
||||
|
|
@ -225,7 +225,7 @@ class Portal:
|
|||
def scroll_right(self):
|
||||
self._scroll_half_pages(1, 0)
|
||||
|
||||
def appearance(self, dimensions):
|
||||
def appearance_for(self, dimensions):
|
||||
width, height = dimensions
|
||||
x, y = self.position
|
||||
try:
|
||||
|
|
@ -265,7 +265,7 @@ class View:
|
|||
def widget(self, widget):
|
||||
self.portal.widget = widget
|
||||
|
||||
def appearance(self, dimensions):
|
||||
def appearance_for(self, dimensions):
|
||||
width, height = dimensions
|
||||
try:
|
||||
full_width, full_height = self.portal.widget.appearance_dimensions()
|
||||
|
|
@ -273,7 +273,7 @@ class View:
|
|||
full_appearance = self.portal.widget.appearance_min()
|
||||
full_width, full_height = appearance_dimensions(full_appearance)
|
||||
if full_width == 0 or full_height == 0:
|
||||
return self.portal.appearance(dimensions)
|
||||
return self.portal.appearance_for(dimensions)
|
||||
x, y = self.portal.position
|
||||
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
|
||||
|
|
@ -285,14 +285,14 @@ class View:
|
|||
full_height = max(full_height, y + height)
|
||||
self.vertical_scrollbar.interval = (y / full_height, (y + height) / full_height)
|
||||
width -= 1
|
||||
portal_appearance = self.portal.appearance((width, height))
|
||||
portal_appearance = self.portal.appearance_for((width, height))
|
||||
if hide_scrollbar_vertical:
|
||||
result = portal_appearance
|
||||
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])
|
||||
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 " "))
|
||||
return result
|
||||
|
||||
|
|
@ -314,7 +314,7 @@ class Text:
|
|||
def appearance_min(self):
|
||||
return self.text
|
||||
|
||||
def appearance(self, dimensions):
|
||||
def appearance_for(self, dimensions):
|
||||
return appearance_resize(self.appearance_min(), dimensions)
|
||||
|
||||
|
||||
|
|
@ -379,9 +379,9 @@ class Border:
|
|||
def appearance_min(self):
|
||||
return self._add_border(self.widget.appearance_min())
|
||||
|
||||
def appearance(self, dimensions):
|
||||
def appearance_for(self, 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:
|
||||
|
|
@ -392,8 +392,8 @@ class Placeholder:
|
|||
def appearance_min(self):
|
||||
return self.widget.appearance_min()
|
||||
|
||||
def appearance(self, dimensions):
|
||||
return self.widget.appearance(dimensions)
|
||||
def appearance_for(self, dimensions):
|
||||
return self.widget.appearance_for(dimensions)
|
||||
|
||||
|
||||
class Fixed:
|
||||
|
|
@ -430,7 +430,7 @@ def handle_exception(func):
|
|||
@handle_exception
|
||||
def draw_screen(widget):
|
||||
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)
|
||||
_LAST_APPEARANCE = appearance
|
||||
|
||||
|
|
@ -438,7 +438,7 @@ def draw_screen(widget):
|
|||
@handle_exception
|
||||
def patch_screen(widget):
|
||||
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)
|
||||
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)
|
||||
|
|
@ -504,8 +504,8 @@ class _Screen:
|
|||
def __init__(self):
|
||||
self.content = Filler(Text("Hello World"))
|
||||
|
||||
def appearance(self, dimensions):
|
||||
return self.content.appearance(dimensions)
|
||||
def appearance_for(self, dimensions):
|
||||
return self.content.appearance_for(dimensions)
|
||||
|
||||
def on_keyboard_input(self, term_code):
|
||||
if term_code in ["q", terminal.ESC, terminal.CTRL_C]:
|
||||
|
|
|
|||
|
|
@ -19,7 +19,7 @@ class WidgetTests(unittest.TestCase):
|
|||
rows = fill3.Row([self.TEXT_A, self.TEXT_B])
|
||||
self.assert_string(rows.appearance_min(), "AB")
|
||||
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):
|
||||
columns = fill3.Column([self.TEXT_A, self.TEXT_B])
|
||||
|
|
@ -35,30 +35,30 @@ class WidgetTests(unittest.TestCase):
|
|||
def test_portal_widget(self):
|
||||
row = fill3.Row([fill3.Text("foo"), fill3.Text("bar")])
|
||||
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)
|
||||
self.assert_string(portal.appearance((1, 1)), " ")
|
||||
self.assert_string(portal.appearance_for((1, 1)), " ")
|
||||
|
||||
def test_border_widget(self):
|
||||
contents = fill3.Filler(self.TEXT_A)
|
||||
self.assert_string(fill3.Border(contents).appearance((3, 3)), "┌─┐\n"
|
||||
"│A│\n"
|
||||
"└─┘")
|
||||
self.assert_string(fill3.Border(contents).appearance_for((3, 3)), "┌─┐\n"
|
||||
"│A│\n"
|
||||
"└─┘")
|
||||
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(empty_contents).appearance((3, 3)), "┌─┐\n"
|
||||
"│ │\n"
|
||||
"└─┘")
|
||||
self.assert_string(fill3.Border(empty_contents).appearance_for((3, 3)), "┌─┐\n"
|
||||
"│ │\n"
|
||||
"└─┘")
|
||||
text = fill3.Text("abcdef")
|
||||
self.assert_string(fill3.Border(text, title="AB").appearance((8, 3)), "┌─ AB ─┐\n"
|
||||
"│abcdef│\n"
|
||||
"└──────┘")
|
||||
self.assert_string(fill3.Border(text, title="ABC").appearance((6, 3)), "┌ …C ┐\n"
|
||||
"│abcd│\n"
|
||||
"└────┘")
|
||||
self.assert_string(fill3.Border(text, title="AB").appearance_for((8, 3)), "┌─ AB ─┐\n"
|
||||
"│abcdef│\n"
|
||||
"└──────┘")
|
||||
self.assert_string(fill3.Border(text, title="ABC").appearance_for((6, 3)), "┌ …C ┐\n"
|
||||
"│abcd│\n"
|
||||
"└────┘")
|
||||
|
||||
def test_placeholder_widget(self):
|
||||
placeholder = fill3.Placeholder(self.TEXT_A)
|
||||
|
|
@ -74,32 +74,32 @@ class WidgetTests(unittest.TestCase):
|
|||
def test_scroll_bar(self):
|
||||
scroll_bar = fill3.ScrollBar(is_horizontal=True)
|
||||
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)
|
||||
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)
|
||||
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)
|
||||
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)
|
||||
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)
|
||||
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)
|
||||
self.assert_string2(scroll_bar.appearance((1, 2)), ("█\n"
|
||||
"█", " i"))
|
||||
self.assert_string2(scroll_bar.appearance_for((1, 2)), ("█\n"
|
||||
"█", " i"))
|
||||
scroll_bar.interval = (0, 0.1)
|
||||
self.assert_string2(scroll_bar.appearance((1, 2)), ("█\n"
|
||||
"█", " i"))
|
||||
self.assert_string2(scroll_bar.appearance_for((1, 2)), ("█\n"
|
||||
"█", " i"))
|
||||
scroll_bar.interval = (0.25, 0.75)
|
||||
self.assert_string2(scroll_bar.appearance((1, 4)), (" \n"
|
||||
"█\n"
|
||||
"█\n"
|
||||
"█", " i"))
|
||||
self.assert_string2(scroll_bar.appearance_for((1, 4)), (" \n"
|
||||
"█\n"
|
||||
"█\n"
|
||||
"█", " i"))
|
||||
scroll_bar.interval = (0, 0.75)
|
||||
self.assert_string2(scroll_bar.appearance((1, 2)), ("█\n"
|
||||
"▄", " i"))
|
||||
self.assert_string2(scroll_bar.appearance_for((1, 2)), ("█\n"
|
||||
"▄", " i"))
|
||||
|
||||
def test_table_widget(self):
|
||||
table = fill3.Table([])
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue