refactor: move table-related utilities to table_utils.py
This commit is contained in:
@@ -14,7 +14,12 @@ from .constants import *
|
|||||||
from .downloads import DownloadManager
|
from .downloads import DownloadManager
|
||||||
from .library import LibraryClient
|
from .library import LibraryClient
|
||||||
from .playback import PlaybackController
|
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:
|
if TYPE_CHECKING:
|
||||||
from textual.widgets._data_table import ColumnKey
|
from textual.widgets._data_table import ColumnKey
|
||||||
@@ -166,28 +171,9 @@ class Auditui(App):
|
|||||||
return
|
return
|
||||||
|
|
||||||
for item in items:
|
for item in items:
|
||||||
title = self.library_client.extract_title(item)
|
title, author, runtime, progress = format_item_as_row(
|
||||||
author_names = self.library_client.extract_authors(item)
|
item, self.library_client)
|
||||||
if author_names and len(author_names) > AUTHOR_NAME_MAX_LENGTH:
|
table.add_row(title, author, runtime, progress, key=title)
|
||||||
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,
|
|
||||||
)
|
|
||||||
|
|
||||||
self.current_items = items
|
self.current_items = items
|
||||||
mode = "all" if self.show_all_mode else "unfinished"
|
mode = "all" if self.show_all_mode else "unfinished"
|
||||||
@@ -206,9 +192,8 @@ class Auditui(App):
|
|||||||
return
|
return
|
||||||
|
|
||||||
self.show_all_mode = False
|
self.show_all_mode = False
|
||||||
unfinished_items = [
|
unfinished_items = filter_unfinished_items(
|
||||||
item for item in self.all_items if not self.library_client.is_finished(item)
|
self.all_items, self.library_client)
|
||||||
]
|
|
||||||
self._populate_table(unfinished_items)
|
self._populate_table(unfinished_items)
|
||||||
|
|
||||||
def action_toggle_dark(self) -> None:
|
def action_toggle_dark(self) -> None:
|
||||||
|
|||||||
@@ -3,7 +3,11 @@
|
|||||||
import unicodedata
|
import unicodedata
|
||||||
from typing import Callable
|
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]:
|
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 0.0
|
||||||
|
|
||||||
return progress_key, reverse
|
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)
|
||||||
|
]
|
||||||
|
|||||||
Reference in New Issue
Block a user