From 4b457452d4b183c95cf5227495d2f9b4a1c0f3dc Mon Sep 17 00:00:00 2001 From: Kharec Date: Tue, 16 Dec 2025 02:55:15 +0100 Subject: [PATCH] refactor: move table-related utilities to table_utils.py --- auditui/app.py | 37 ++++++++++---------------------- auditui/table_utils.py | 48 +++++++++++++++++++++++++++++++++++++++++- 2 files changed, 58 insertions(+), 27 deletions(-) diff --git a/auditui/app.py b/auditui/app.py index 768f48e..a81636f 100644 --- a/auditui/app.py +++ b/auditui/app.py @@ -14,7 +14,12 @@ from .constants import * from .downloads import DownloadManager from .library import LibraryClient from .playback import PlaybackController -from .table_utils import create_progress_sort_key, create_title_sort_key +from .table_utils import ( + create_progress_sort_key, + create_title_sort_key, + filter_unfinished_items, + format_item_as_row, +) if TYPE_CHECKING: from textual.widgets._data_table import ColumnKey @@ -166,28 +171,9 @@ class Auditui(App): return for item in items: - title = self.library_client.extract_title(item) - author_names = self.library_client.extract_authors(item) - if author_names and len(author_names) > AUTHOR_NAME_MAX_LENGTH: - author_names = f"{author_names[:AUTHOR_NAME_DISPLAY_LENGTH]}..." - minutes = self.library_client.extract_runtime_minutes(item) - runtime_str = self.library_client.format_duration( - minutes, unit="minutes", default_none="Unknown length" - ) - percent_complete = self.library_client.extract_progress_info(item) - progress_str = ( - f"{percent_complete:.1f}%" - if percent_complete and percent_complete > 0 - else "0%" - ) - - table.add_row( - title, - author_names or "Unknown", - runtime_str or "Unknown", - progress_str, - key=title, - ) + title, author, runtime, progress = format_item_as_row( + item, self.library_client) + table.add_row(title, author, runtime, progress, key=title) self.current_items = items mode = "all" if self.show_all_mode else "unfinished" @@ -206,9 +192,8 @@ class Auditui(App): return self.show_all_mode = False - unfinished_items = [ - item for item in self.all_items if not self.library_client.is_finished(item) - ] + unfinished_items = filter_unfinished_items( + self.all_items, self.library_client) self._populate_table(unfinished_items) def action_toggle_dark(self) -> None: diff --git a/auditui/table_utils.py b/auditui/table_utils.py index 3066bae..0b85aa4 100644 --- a/auditui/table_utils.py +++ b/auditui/table_utils.py @@ -3,7 +3,11 @@ import unicodedata from typing import Callable -from .constants import PROGRESS_COLUMN_INDEX +from .constants import ( + AUTHOR_NAME_DISPLAY_LENGTH, + AUTHOR_NAME_MAX_LENGTH, + PROGRESS_COLUMN_INDEX, +) def create_title_sort_key(reverse: bool = False) -> tuple[Callable, bool]: @@ -30,3 +34,45 @@ def create_progress_sort_key(progress_column_index: int = PROGRESS_COLUMN_INDEX, return 0.0 return progress_key, reverse + + +def truncate_author_name(author_names: str) -> str: + """Truncate author name if it exceeds maximum length.""" + if author_names and len(author_names) > AUTHOR_NAME_MAX_LENGTH: + return f"{author_names[:AUTHOR_NAME_DISPLAY_LENGTH]}..." + return author_names + + +def format_item_as_row(item: dict, library_client) -> tuple[str, str, str, str]: + """Format a library item into table row data. + + Returns: + Tuple of (title, author, runtime, progress) strings + """ + title = library_client.extract_title(item) + + author_names = library_client.extract_authors(item) + author_names = truncate_author_name(author_names) + author_display = author_names or "Unknown" + + minutes = library_client.extract_runtime_minutes(item) + runtime_str = library_client.format_duration( + minutes, unit="minutes", default_none="Unknown length" + ) or "Unknown" + + percent_complete = library_client.extract_progress_info(item) + progress_str = ( + f"{percent_complete:.1f}%" + if percent_complete and percent_complete > 0 + else "0%" + ) + + return (title, author_display, runtime_str, progress_str) + + +def filter_unfinished_items(items: list[dict], library_client) -> list[dict]: + """Filter out finished items from the list.""" + return [ + item for item in items + if not library_client.is_finished(item) + ]