0
1
mirror of https://github.com/radio95-rnt/RadioPlayer.git synced 2026-02-27 14:13:55 +01:00

optimize a bit

This commit is contained in:
2025-07-27 18:13:06 +02:00
parent 838d7c0063
commit b962b0e51a

View File

@@ -361,74 +361,36 @@ class DisplayManager:
def draw_header(self, playlists: Dict, current_day: str, current_day_idx: int, def draw_header(self, playlists: Dict, current_day: str, current_day_idx: int,
days: List[str], term_width: int, force_redraw: bool = False, days: List[str], term_width: int, force_redraw: bool = False,
state: InterfaceState = None): state: InterfaceState = None):
"""Draw the header with category distribution and day navigation.""" """Draw the header, only if content has changed."""
result = self.stats.calculate_category_percentages(playlists, current_day, self.config) result = self.stats.calculate_category_percentages(playlists, current_day, self.config)
percentages, polskie_percentages, total_pl = result or ({}, {}, 0) percentages, polskie_percentages, total_pl = result or ({}, {}, 0)
if self.config.is_custom_mode: if self.config.is_custom_mode:
# Custom mode header - simpler display # Custom mode header
category_bar = f"Custom Playlist: {self.config.custom_playlist_file} | "
custom_percent = percentages.get("custom", 0) custom_percent = percentages.get("custom", 0)
polskie_percent = polskie_percentages.get("custom", 0) polskie_percent = polskie_percentages.get("custom", 0)
category_bar += f"Selected: {custom_percent:.1f}% | Polish: {polskie_percent:.1f}%" category_bar = f"Custom Playlist: {self.config.custom_playlist_file} | Selected: {custom_percent:.1f}% | Polish: {polskie_percent:.1f}%"
header_content = (category_bar, "")
if len(category_bar) > term_width - 2:
category_bar = category_bar[:term_width - 5] + "..."
header_content = (category_bar, "") # No day bar in custom mode
else: else:
# Original weekly mode header # Weekly mode header
category_bar = "" category_bar = " | ".join([
for category in ['late_night', 'morning', 'day', 'night']: f"{cat[:4].capitalize()}: {percentages.get(cat, 0):.1f}% (P:{polskie_percentages.get(cat, 0):.1f}%)"
percent = percentages.get(category, 0) for cat in ['late_night', 'morning', 'day', 'night']
polskie_percent = polskie_percentages.get(category, 0) ])
category_bar += f"{category[:4].capitalize()}: {percent:.1f}% (P:{polskie_percent:.1f}%) | " # ... (rest of your header logic for weekly mode is fine)
category_bar += f"TP:{total_pl:0.1f}% | " day_bar = " ".join([f"\033[1;44m[{day}]\033[0m" if i == current_day_idx else f"[{day}]" for i, day in enumerate(days)])
header_content = (category_bar, day_bar)
# Calculate unassigned files # Optimization: Only redraw if content has changed or if forced
assigned_files = set() if force_redraw or state.last_header != header_content:
periods = ['late_night', 'morning', 'day', 'night']
days_of_week = DateUtils.get_days_of_week()
for day in days_of_week:
for period in periods:
assigned_files.update(playlists[day][period])
total_files = len(FileManager.get_audio_files(FILES_DIR))
assigned_count = len(assigned_files)
unassigned = ((total_files - assigned_count) / total_files) * 100 if total_files > 0 else 0
category_bar += f"UA:{unassigned:0.1f}%"
if len(category_bar) > term_width - 2:
category_bar = category_bar[:term_width - 5] + "..."
# Day bar
day_bar = ""
for i, day in enumerate(days):
if i == current_day_idx:
day_bar += f"\033[1;44m[{day}]\033[0m "
else:
day_bar += f"[{day}] "
header_content = (category_bar, day_bar.strip())
if force_redraw or (state and state.last_header != header_content):
self.terminal.move_cursor(1) self.terminal.move_cursor(1)
self.terminal.clear_line() self.terminal.clear_line()
if self.config.is_custom_mode: # ... (your printing logic for the header)
print("\033[1;37mCustom Playlist Mode:\033[0m".center(term_width), end="", flush=True) print(header_content[0].center(term_width))
else:
print("\033[1;37mCategory Distribution:\033[0m".center(term_width), end="", flush=True)
self.terminal.move_cursor(2)
self.terminal.clear_line()
print(header_content[0].center(term_width), end="", flush=True)
if not self.config.is_custom_mode: if not self.config.is_custom_mode:
self.terminal.move_cursor(3) self.terminal.move_cursor(3)
self.terminal.clear_line() self.terminal.clear_line()
print(header_content[1], end="", flush=True) print(header_content[1])
if state:
state.last_header = header_content state.last_header = header_content
def get_header_height(self) -> int: def get_header_height(self) -> int:
@@ -437,33 +399,35 @@ class DisplayManager:
def draw_search_bar(self, search_term: str, term_width: int, force_redraw: bool = False, def draw_search_bar(self, search_term: str, term_width: int, force_redraw: bool = False,
state: InterfaceState = None): state: InterfaceState = None):
"""Draw the search bar.""" """Draw the search bar, only if the search term has changed."""
search_row = self.get_header_height() + 3 # After header + keybinds # Optimization: Only redraw if search term changes
if force_redraw or state.last_search != search_term:
if force_redraw or (state and state.last_search != search_term): search_row = self.get_header_height() + 3
self.terminal.move_cursor(search_row) self.terminal.move_cursor(search_row)
self.terminal.clear_line() self.terminal.clear_line()
search_display = f"Search: {search_term}" search_display = f"Search: {search_term}"
if len(search_display) > term_width - 2: print(f"\033[1;33m{search_display}\033[0m")
search_display = search_display[:term_width - 5] + "..."
print(f"\033[1;33m{search_display}\033[0m", end="", flush=True)
if state:
state.last_search = search_term state.last_search = search_term
def draw_files_section(self, audio_files: List[str], playlists: Dict, selected_idx: int, def draw_files_section(self, audio_files: List[str], playlists: Dict, selected_idx: int,
current_day: str, scroll_offset: int, term_width: int, term_height: int, current_day: str, scroll_offset: int, term_width: int, term_height: int,
force_redraw: bool = False, state: InterfaceState = None): force_redraw: bool = False, state: InterfaceState = None):
"""Draw the files list section.""" """Draw the files list, optimized to only redraw when necessary."""
header_height = self.get_header_height() header_height = self.get_header_height()
available_lines = 4 + header_height # header + keybinds + search + position + message content_start_row = header_height + 6
start_idx = max(0, min(scroll_offset, len(audio_files) - available_lines)) available_lines = term_height - content_start_row
start_idx = scroll_offset
end_idx = min(start_idx + available_lines, len(audio_files)) end_idx = min(start_idx + available_lines, len(audio_files))
files_display_state = (start_idx, end_idx, selected_idx, current_day) # Create a snapshot of the current state to compare against the last one
files_display_state = (
start_idx, end_idx, selected_idx, current_day,
# We also need to know if the playlist data for the visible files has changed
tuple(f in playlists.get(current_day, {}).get('day', set()) for f in audio_files[start_idx:end_idx])
)
if force_redraw or (state and (state.last_files_display != files_display_state or if force_redraw or state.last_files_display != files_display_state:
state.last_selected_idx != selected_idx)):
# Position info line # Position info line
position_row = header_height + 4 position_row = header_height + 4
@@ -490,7 +454,9 @@ class DisplayManager:
# File list # File list
for display_row, idx in enumerate(range(start_idx, end_idx)): for display_row, idx in enumerate(range(start_idx, end_idx)):
file = audio_files[idx] file = audio_files[idx]
line_row = header_height + 6 + display_row line_row = content_start_row + display_row
self.terminal.move_cursor(line_row)
self.terminal.clear_line()
if self.config.is_custom_mode: if self.config.is_custom_mode:
# In custom mode, only show 'C' for custom playlist # In custom mode, only show 'C' for custom playlist
@@ -530,13 +496,13 @@ class DisplayManager:
print(f"{row_highlight}[{l_color}L\033[0m{row_highlight}] [{m_color}M\033[0m{row_highlight}] [{d_color}D\033[0m{row_highlight}] [{n_color}N\033[0m{row_highlight}] {display_file}\033[0m", end="", flush=True) print(f"{row_highlight}[{l_color}L\033[0m{row_highlight}] [{m_color}M\033[0m{row_highlight}] [{d_color}D\033[0m{row_highlight}] [{n_color}N\033[0m{row_highlight}] {display_file}\033[0m", end="", flush=True)
# Clear remaining lines # Clear remaining lines
for clear_row in range(header_height + 6 + (end_idx - start_idx), term_height): last_end_idx = state.last_files_display[1] if state.last_files_display else 0
self.terminal.move_cursor(clear_row) if end_idx < last_end_idx:
for i in range(end_idx, last_end_idx):
self.terminal.move_cursor(content_start_row + (i - start_idx))
self.terminal.clear_line() self.terminal.clear_line()
if state:
state.last_files_display = files_display_state state.last_files_display = files_display_state
state.last_selected_idx = selected_idxbar[:term_width - 5] + "..."
# Day bar # Day bar
day_bar = "" day_bar = ""