You've already forked RadioPlayer
mirror of
https://github.com/radio95-rnt/RadioPlayer.git
synced 2026-02-26 21:53:54 +01:00
optimize a bit
This commit is contained in:
138
radioPlaylist.py
138
radioPlaylist.py
@@ -358,78 +358,40 @@ class DisplayManager:
|
||||
self.stats = stats_calc
|
||||
self.config = config
|
||||
|
||||
def draw_header(self, playlists: Dict, current_day: str, current_day_idx: int,
|
||||
days: List[str], term_width: int, force_redraw: bool = False,
|
||||
def draw_header(self, playlists: Dict, current_day: str, current_day_idx: int,
|
||||
days: List[str], term_width: int, force_redraw: bool = False,
|
||||
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)
|
||||
percentages, polskie_percentages, total_pl = result or ({}, {}, 0)
|
||||
|
||||
|
||||
if self.config.is_custom_mode:
|
||||
# Custom mode header - simpler display
|
||||
category_bar = f"Custom Playlist: {self.config.custom_playlist_file} | "
|
||||
# Custom mode header
|
||||
custom_percent = percentages.get("custom", 0)
|
||||
polskie_percent = polskie_percentages.get("custom", 0)
|
||||
category_bar += f"Selected: {custom_percent:.1f}% | Polish: {polskie_percent:.1f}%"
|
||||
|
||||
if len(category_bar) > term_width - 2:
|
||||
category_bar = category_bar[:term_width - 5] + "..."
|
||||
|
||||
header_content = (category_bar, "") # No day bar in custom mode
|
||||
category_bar = f"Custom Playlist: {self.config.custom_playlist_file} | Selected: {custom_percent:.1f}% | Polish: {polskie_percent:.1f}%"
|
||||
header_content = (category_bar, "")
|
||||
else:
|
||||
# Original weekly mode header
|
||||
category_bar = ""
|
||||
for category in ['late_night', 'morning', 'day', 'night']:
|
||||
percent = percentages.get(category, 0)
|
||||
polskie_percent = polskie_percentages.get(category, 0)
|
||||
category_bar += f"{category[:4].capitalize()}: {percent:.1f}% (P:{polskie_percent:.1f}%) | "
|
||||
category_bar += f"TP:{total_pl:0.1f}% | "
|
||||
|
||||
# Calculate unassigned files
|
||||
assigned_files = set()
|
||||
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):
|
||||
# Weekly mode header
|
||||
category_bar = " | ".join([
|
||||
f"{cat[:4].capitalize()}: {percentages.get(cat, 0):.1f}% (P:{polskie_percentages.get(cat, 0):.1f}%)"
|
||||
for cat in ['late_night', 'morning', 'day', 'night']
|
||||
])
|
||||
# ... (rest of your header logic for weekly mode is fine)
|
||||
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)
|
||||
|
||||
# Optimization: Only redraw if content has changed or if forced
|
||||
if force_redraw or state.last_header != header_content:
|
||||
self.terminal.move_cursor(1)
|
||||
self.terminal.clear_line()
|
||||
if self.config.is_custom_mode:
|
||||
print("\033[1;37mCustom Playlist Mode:\033[0m".center(term_width), end="", flush=True)
|
||||
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)
|
||||
|
||||
# ... (your printing logic for the header)
|
||||
print(header_content[0].center(term_width))
|
||||
if not self.config.is_custom_mode:
|
||||
self.terminal.move_cursor(3)
|
||||
self.terminal.clear_line()
|
||||
print(header_content[1], end="", flush=True)
|
||||
|
||||
if state:
|
||||
state.last_header = header_content
|
||||
print(header_content[1])
|
||||
state.last_header = header_content
|
||||
|
||||
def get_header_height(self) -> int:
|
||||
"""Get the height of the header section."""
|
||||
@@ -437,33 +399,35 @@ class DisplayManager:
|
||||
|
||||
def draw_search_bar(self, search_term: str, term_width: int, force_redraw: bool = False,
|
||||
state: InterfaceState = None):
|
||||
"""Draw the search bar."""
|
||||
search_row = self.get_header_height() + 3 # After header + keybinds
|
||||
|
||||
if force_redraw or (state and state.last_search != search_term):
|
||||
"""Draw the search bar, only if the search term has changed."""
|
||||
# Optimization: Only redraw if search term changes
|
||||
if force_redraw or state.last_search != search_term:
|
||||
search_row = self.get_header_height() + 3
|
||||
self.terminal.move_cursor(search_row)
|
||||
self.terminal.clear_line()
|
||||
search_display = f"Search: {search_term}"
|
||||
if len(search_display) > term_width - 2:
|
||||
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
|
||||
print(f"\033[1;33m{search_display}\033[0m")
|
||||
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,
|
||||
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()
|
||||
available_lines = 4 + header_height # header + keybinds + search + position + message
|
||||
start_idx = max(0, min(scroll_offset, len(audio_files) - available_lines))
|
||||
content_start_row = header_height + 6
|
||||
available_lines = term_height - content_start_row
|
||||
|
||||
start_idx = scroll_offset
|
||||
end_idx = min(start_idx + available_lines, len(audio_files))
|
||||
|
||||
# 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])
|
||||
)
|
||||
|
||||
files_display_state = (start_idx, end_idx, selected_idx, current_day)
|
||||
|
||||
if force_redraw or (state and (state.last_files_display != files_display_state or
|
||||
state.last_selected_idx != selected_idx)):
|
||||
if force_redraw or state.last_files_display != files_display_state:
|
||||
|
||||
# Position info line
|
||||
position_row = header_height + 4
|
||||
@@ -490,7 +454,9 @@ class DisplayManager:
|
||||
# File list
|
||||
for display_row, idx in enumerate(range(start_idx, end_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:
|
||||
# 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)
|
||||
|
||||
# Clear remaining lines
|
||||
for clear_row in range(header_height + 6 + (end_idx - start_idx), term_height):
|
||||
self.terminal.move_cursor(clear_row)
|
||||
self.terminal.clear_line()
|
||||
|
||||
if state:
|
||||
state.last_files_display = files_display_state
|
||||
state.last_selected_idx = selected_idxbar[:term_width - 5] + "..."
|
||||
last_end_idx = state.last_files_display[1] if state.last_files_display else 0
|
||||
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()
|
||||
|
||||
state.last_files_display = files_display_state
|
||||
|
||||
# Day bar
|
||||
day_bar = ""
|
||||
|
||||
Reference in New Issue
Block a user