Skip to content
Snippets Groups Projects

Refactor Cart into checkerboard table

Merged Mateusz Kudela requested to merge mkudela/issue-173 into develop
1 file
+ 29
23
Compare changes
  • Side-by-side
  • Inline
@@ -196,10 +196,34 @@ class CliveCheckerboardTable(CliveWidget):
self.update_previous_state(content)
await self.rebuild(content)
async def _rebuild_header(self) -> None:
await self.query("*")[1].remove()
await self.mount(self._header, after=self._title)
async def _rebuild_rows(
self,
row_type: Any, # noqa: ANN401
start_index: int = 0,
end_index: int | None = None,
) -> None:
rows = self.query(row_type)
assert start_index < len(
rows
), "Starting_from_element parameter has higher value than number of elements in the table"
if end_index:
assert (
start_index < end_index
), "Starting_from_element parameter has higher value than ending_with_element argument"
for index in range(start_index, end_index + 1 if end_index else len(rows)):
await rows[index].remove()
self._mount_static_rows(start_index, end_index)
async def rebuild(
self,
content: Content | None = None,
row_type: Any = None, # noqa: ANN401
rebuild_header: bool = False,
starting_from_element: int = 0,
ending_with_element: int | None = None,
) -> None:
@@ -213,31 +237,13 @@ class CliveCheckerboardTable(CliveWidget):
if not self.should_be_dynamic and content is not None: # content is given, but table is static
raise RebuildStaticTableWithContentError
with self.app.batch_update():
if starting_from_element == 0 and ending_with_element is None:
await self.query("*").remove()
await self.mount_all(self._create_table_content(content))
if rebuild_header:
await self._rebuild_header()
if self.should_be_dynamic:
self._create_table_content(content)
else:
rows = self.query(row_type)
assert row_type, "You have to set row_type."
assert starting_from_element < len(
rows
), "Starting_from_element parameter has higher value than number of elements in the table"
if ending_with_element:
assert (
starting_from_element < ending_with_element
), "Starting_from_element parameter has higher value than ending_with_element argument"
# remove elements
for index in range(
starting_from_element, ending_with_element + 1 if ending_with_element else len(rows)
):
await rows[index].remove()
# mount again - rebuild only rows after index
(
self._mount_static_rows(starting_from_element, ending_with_element)
if not self.should_be_dynamic
else self._create_table_content(content)
)
await self._rebuild_rows(row_type, starting_from_element, ending_with_element)
def _create_table_content(
self, content: Content | None = None, rows_from_index: int = 0, rows_to_index: int | None = None
Loading