From 00cdeee5c6b8925c70410d598b97802fcb62b0f8 Mon Sep 17 00:00:00 2001 From: Alex Clarke Date: Wed, 4 Dec 2024 17:41:30 -0700 Subject: [PATCH] feat(ui): Full Sonarr system tab support Signed-off-by: Alex Clarke --- src/app/sonarr/sonarr_context_clues.rs | 5 + src/app/sonarr/sonarr_context_clues_tests.rs | 17 ++ src/ui/radarr_ui/mod.rs | 5 - src/ui/radarr_ui/radarr_ui_utils.rs | 46 ---- src/ui/radarr_ui/radarr_ui_utils_tests.rs | 70 ------ src/ui/radarr_ui/system/mod.rs | 5 +- src/ui/radarr_ui/system/system_details_ui.rs | 3 +- src/ui/sonarr_ui/mod.rs | 3 + src/ui/sonarr_ui/system/mod.rs | 232 ++++++++++++++++++ src/ui/sonarr_ui/system/system_details_ui.rs | 180 ++++++++++++++ .../system/system_details_ui_tests.rs | 21 ++ src/ui/sonarr_ui/system/system_ui_tests.rs | 25 ++ src/ui/utils.rs | 40 ++- src/ui/utils_tests.rs | 80 +++++- 14 files changed, 598 insertions(+), 134 deletions(-) delete mode 100644 src/ui/radarr_ui/radarr_ui_utils.rs delete mode 100644 src/ui/radarr_ui/radarr_ui_utils_tests.rs create mode 100644 src/ui/sonarr_ui/system/mod.rs create mode 100644 src/ui/sonarr_ui/system/system_details_ui.rs create mode 100644 src/ui/sonarr_ui/system/system_details_ui_tests.rs create mode 100644 src/ui/sonarr_ui/system/system_ui_tests.rs diff --git a/src/app/sonarr/sonarr_context_clues.rs b/src/app/sonarr/sonarr_context_clues.rs index 9aacf69..f9c64a0 100644 --- a/src/app/sonarr/sonarr_context_clues.rs +++ b/src/app/sonarr/sonarr_context_clues.rs @@ -90,3 +90,8 @@ pub static EPISODE_DETAILS_CONTEXT_CLUES: [ContextClue; 3] = [ (DEFAULT_KEYBINDINGS.search, "auto search"), (DEFAULT_KEYBINDINGS.esc, DEFAULT_KEYBINDINGS.esc.desc), ]; + +pub static SYSTEM_TASKS_CONTEXT_CLUES: [ContextClue; 2] = [ + (DEFAULT_KEYBINDINGS.submit, "start task"), + (DEFAULT_KEYBINDINGS.esc, DEFAULT_KEYBINDINGS.esc.desc), +]; diff --git a/src/app/sonarr/sonarr_context_clues_tests.rs b/src/app/sonarr/sonarr_context_clues_tests.rs index c72b0b2..2c8b485 100644 --- a/src/app/sonarr/sonarr_context_clues_tests.rs +++ b/src/app/sonarr/sonarr_context_clues_tests.rs @@ -9,6 +9,7 @@ mod tests { HISTORY_CONTEXT_CLUES, MANUAL_EPISODE_SEARCH_CONTEXTUAL_CONTEXT_CLUES, MANUAL_EPISODE_SEARCH_CONTEXT_CLUES, MANUAL_SEASON_SEARCH_CONTEXT_CLUES, SEASON_DETAILS_CONTEXT_CLUES, SERIES_CONTEXT_CLUES, SERIES_DETAILS_CONTEXT_CLUES, + SYSTEM_TASKS_CONTEXT_CLUES, }, }; @@ -266,4 +267,20 @@ mod tests { assert_str_eq!(*description, DEFAULT_KEYBINDINGS.esc.desc); assert_eq!(episode_details_context_clues_iter.next(), None); } + + #[test] + fn test_system_tasks_context_clues() { + let mut system_tasks_context_clues_iter = SYSTEM_TASKS_CONTEXT_CLUES.iter(); + + let (key_binding, description) = system_tasks_context_clues_iter.next().unwrap(); + + assert_eq!(*key_binding, DEFAULT_KEYBINDINGS.submit); + assert_str_eq!(*description, "start task"); + + let (key_binding, description) = system_tasks_context_clues_iter.next().unwrap(); + + assert_eq!(*key_binding, DEFAULT_KEYBINDINGS.esc); + assert_str_eq!(*description, DEFAULT_KEYBINDINGS.esc.desc); + assert_eq!(system_tasks_context_clues_iter.next(), None); + } } diff --git a/src/ui/radarr_ui/mod.rs b/src/ui/radarr_ui/mod.rs index 36d6337..18a9f11 100644 --- a/src/ui/radarr_ui/mod.rs +++ b/src/ui/radarr_ui/mod.rs @@ -34,14 +34,9 @@ mod collections; mod downloads; mod indexers; mod library; -mod radarr_ui_utils; mod root_folders; mod system; -#[cfg(test)] -#[path = "radarr_ui_tests.rs"] -mod radarr_ui_tests; - pub(super) struct RadarrUi; impl DrawUi for RadarrUi { diff --git a/src/ui/radarr_ui/radarr_ui_utils.rs b/src/ui/radarr_ui/radarr_ui_utils.rs deleted file mode 100644 index 9cbadeb..0000000 --- a/src/ui/radarr_ui/radarr_ui_utils.rs +++ /dev/null @@ -1,46 +0,0 @@ -use ratatui::style::{Style, Stylize}; -use ratatui::widgets::ListItem; - -use crate::ui::styles::ManagarrStyle; - -#[cfg(test)] -#[path = "radarr_ui_utils_tests.rs"] -mod radarr_ui_utils_tests; - -pub(super) fn style_log_list_item(list_item: ListItem<'_>, level: String) -> ListItem<'_> { - match level.to_lowercase().as_str() { - "trace" => list_item.gray(), - "debug" => list_item.blue(), - "info" => list_item.style(Style::new().default()), - "warn" => list_item.style(Style::new().secondary()), - "error" => list_item.style(Style::new().failure()), - "fatal" => list_item.style(Style::new().failure().bold()), - _ => list_item.style(Style::new().default()), - } -} - -pub(super) fn convert_to_minutes_hours_days(time: i64) -> String { - if time < 60 { - if time == 0 { - "now".to_owned() - } else if time == 1 { - format!("{time} minute") - } else { - format!("{time} minutes") - } - } else if time / 60 < 24 { - let hours = time / 60; - if hours == 1 { - format!("{hours} hour") - } else { - format!("{hours} hours") - } - } else { - let days = time / (60 * 24); - if days == 1 { - format!("{days} day") - } else { - format!("{days} days") - } - } -} diff --git a/src/ui/radarr_ui/radarr_ui_utils_tests.rs b/src/ui/radarr_ui/radarr_ui_utils_tests.rs deleted file mode 100644 index ddcecf8..0000000 --- a/src/ui/radarr_ui/radarr_ui_utils_tests.rs +++ /dev/null @@ -1,70 +0,0 @@ -#[cfg(test)] -mod tests { - use super::super::*; - use pretty_assertions::assert_str_eq; - use ratatui::prelude::Text; - use ratatui::text::Span; - - #[test] - fn test_determine_log_style_by_level() { - let list_item = ListItem::new(Text::from(Span::raw("test"))); - - assert_eq!( - style_log_list_item(list_item.clone(), "trace".to_string()), - list_item.clone().gray() - ); - assert_eq!( - style_log_list_item(list_item.clone(), "debug".to_string()), - list_item.clone().blue() - ); - assert_eq!( - style_log_list_item(list_item.clone(), "info".to_string()), - list_item.clone().style(Style::new().default()) - ); - assert_eq!( - style_log_list_item(list_item.clone(), "warn".to_string()), - list_item.clone().style(Style::new().secondary()) - ); - assert_eq!( - style_log_list_item(list_item.clone(), "error".to_string()), - list_item.clone().style(Style::new().failure()) - ); - assert_eq!( - style_log_list_item(list_item.clone(), "fatal".to_string()), - list_item.clone().style(Style::new().failure().bold()) - ); - assert_eq!( - style_log_list_item(list_item.clone(), "".to_string()), - list_item.style(Style::new().default()) - ); - } - - #[test] - fn test_determine_log_style_by_level_case_insensitive() { - let list_item = ListItem::new(Text::from(Span::raw("test"))); - - assert_eq!( - style_log_list_item(list_item.clone(), "TrAcE".to_string()), - list_item.gray() - ); - } - - #[test] - fn test_convert_to_minutes_hours_days_minutes() { - assert_str_eq!(convert_to_minutes_hours_days(0), "now"); - assert_str_eq!(convert_to_minutes_hours_days(1), "1 minute"); - assert_str_eq!(convert_to_minutes_hours_days(2), "2 minutes"); - } - - #[test] - fn test_convert_to_minutes_hours_days_hours() { - assert_str_eq!(convert_to_minutes_hours_days(60), "1 hour"); - assert_str_eq!(convert_to_minutes_hours_days(120), "2 hours"); - } - - #[test] - fn test_convert_to_minutes_hours_days_days() { - assert_str_eq!(convert_to_minutes_hours_days(1440), "1 day"); - assert_str_eq!(convert_to_minutes_hours_days(2880), "2 days"); - } -} diff --git a/src/ui/radarr_ui/system/mod.rs b/src/ui/radarr_ui/system/mod.rs index 016f474..db50e4b 100644 --- a/src/ui/radarr_ui/system/mod.rs +++ b/src/ui/radarr_ui/system/mod.rs @@ -15,10 +15,11 @@ use crate::app::App; use crate::models::radarr_models::RadarrTask; use crate::models::servarr_data::radarr::radarr_data::ActiveRadarrBlock; use crate::models::servarr_models::QueueEvent; -use crate::ui::radarr_ui::radarr_ui_utils::{convert_to_minutes_hours_days, style_log_list_item}; use crate::ui::radarr_ui::system::system_details_ui::SystemDetailsUi; use crate::ui::styles::ManagarrStyle; -use crate::ui::utils::layout_block_top_border; +use crate::ui::utils::{ + convert_to_minutes_hours_days, layout_block_top_border, style_log_list_item, +}; use crate::ui::widgets::loading_block::LoadingBlock; use crate::ui::widgets::managarr_table::ManagarrTable; use crate::ui::widgets::selectable_list::SelectableList; diff --git a/src/ui/radarr_ui/system/system_details_ui.rs b/src/ui/radarr_ui/system/system_details_ui.rs index 6198c86..41a5437 100644 --- a/src/ui/radarr_ui/system/system_details_ui.rs +++ b/src/ui/radarr_ui/system/system_details_ui.rs @@ -9,13 +9,12 @@ use crate::app::App; use crate::models::radarr_models::RadarrTask; use crate::models::servarr_data::radarr::radarr_data::{ActiveRadarrBlock, SYSTEM_DETAILS_BLOCKS}; use crate::models::Route; -use crate::ui::radarr_ui::radarr_ui_utils::style_log_list_item; use crate::ui::radarr_ui::system::{ draw_queued_events, draw_system_ui_layout, extract_task_props, TASK_TABLE_CONSTRAINTS, TASK_TABLE_HEADERS, }; use crate::ui::styles::ManagarrStyle; -use crate::ui::utils::{borderless_block, title_block}; +use crate::ui::utils::{borderless_block, style_log_list_item, title_block}; use crate::ui::widgets::confirmation_prompt::ConfirmationPrompt; use crate::ui::widgets::loading_block::LoadingBlock; use crate::ui::widgets::managarr_table::ManagarrTable; diff --git a/src/ui/sonarr_ui/mod.rs b/src/ui/sonarr_ui/mod.rs index 4dc47f4..c871d17 100644 --- a/src/ui/sonarr_ui/mod.rs +++ b/src/ui/sonarr_ui/mod.rs @@ -14,6 +14,7 @@ use ratatui::{ Frame, }; use root_folders::RootFoldersUi; +use system::SystemUi; use crate::{ app::App, @@ -43,6 +44,7 @@ mod history; mod indexers; mod library; mod root_folders; +mod system; #[cfg(test)] #[path = "sonarr_ui_tests.rs"] @@ -66,6 +68,7 @@ impl DrawUi for SonarrUi { _ if HistoryUi::accepts(route) => HistoryUi::draw(f, app, content_area), _ if RootFoldersUi::accepts(route) => RootFoldersUi::draw(f, app, content_area), _ if IndexersUi::accepts(route) => IndexersUi::draw(f, app, content_area), + _ if SystemUi::accepts(route) => SystemUi::draw(f, app, content_area), _ => (), } } diff --git a/src/ui/sonarr_ui/system/mod.rs b/src/ui/sonarr_ui/system/mod.rs new file mode 100644 index 0000000..f9e6c88 --- /dev/null +++ b/src/ui/sonarr_ui/system/mod.rs @@ -0,0 +1,232 @@ +use std::ops::Sub; + +use chrono::Utc; +use ratatui::layout::Layout; +use ratatui::style::Style; +use ratatui::text::{Span, Text}; +use ratatui::widgets::{Cell, Paragraph, Row}; +use ratatui::{ + layout::{Constraint, Rect}, + widgets::ListItem, + Frame, +}; + +use crate::app::App; +use crate::models::servarr_data::sonarr::sonarr_data::ActiveSonarrBlock; +use crate::models::servarr_models::QueueEvent; +use crate::models::sonarr_models::SonarrTask; +use crate::ui::sonarr_ui::system::system_details_ui::SystemDetailsUi; +use crate::ui::styles::ManagarrStyle; +use crate::ui::utils::{ + convert_to_minutes_hours_days, layout_block_top_border, style_log_list_item, +}; +use crate::ui::widgets::loading_block::LoadingBlock; +use crate::ui::widgets::managarr_table::ManagarrTable; +use crate::ui::widgets::selectable_list::SelectableList; +use crate::{ + models::Route, + ui::{utils::title_block, DrawUi}, +}; + +mod system_details_ui; + +#[cfg(test)] +#[path = "system_ui_tests.rs"] +mod system_ui_tests; + +pub(super) const TASK_TABLE_HEADERS: [&str; 4] = + ["Name", "Interval", "Last Execution", "Next Execution"]; + +pub(super) const TASK_TABLE_CONSTRAINTS: [Constraint; 4] = [ + Constraint::Percentage(30), + Constraint::Percentage(23), + Constraint::Percentage(23), + Constraint::Percentage(23), +]; + +pub(super) struct SystemUi; + +impl DrawUi for SystemUi { + fn accepts(route: Route) -> bool { + if let Route::Sonarr(active_sonarr_block, _) = route { + return SystemDetailsUi::accepts(route) || active_sonarr_block == ActiveSonarrBlock::System; + } + + false + } + + fn draw(f: &mut Frame<'_>, app: &mut App<'_>, area: Rect) { + let route = app.get_current_route(); + + match route { + _ if SystemDetailsUi::accepts(route) => SystemDetailsUi::draw(f, app, area), + _ if matches!(route, Route::Sonarr(ActiveSonarrBlock::System, _)) => { + draw_system_ui_layout(f, app, area) + } + _ => (), + } + } +} + +pub(super) fn draw_system_ui_layout(f: &mut Frame<'_>, app: &mut App<'_>, area: Rect) { + let [activities_area, logs_area, help_area] = Layout::vertical([ + Constraint::Ratio(1, 2), + Constraint::Ratio(1, 2), + Constraint::Min(2), + ]) + .areas(area); + + let [tasks_area, events_area] = + Layout::horizontal([Constraint::Ratio(1, 2), Constraint::Ratio(1, 2)]).areas(activities_area); + + draw_tasks(f, app, tasks_area); + draw_queued_events(f, app, events_area); + draw_logs(f, app, logs_area); + draw_help(f, app, help_area); +} + +fn draw_tasks(f: &mut Frame<'_>, app: &mut App<'_>, area: Rect) { + let tasks_row_mapping = |task: &SonarrTask| { + let task_props = extract_task_props(task); + + Row::new(vec![ + Cell::from(task_props.name), + Cell::from(task_props.interval), + Cell::from(task_props.last_execution), + Cell::from(task_props.next_execution), + ]) + .primary() + }; + let tasks_table = ManagarrTable::new(Some(&mut app.data.sonarr_data.tasks), tasks_row_mapping) + .block(title_block("Tasks")) + .loading(app.is_loading) + .highlight_rows(false) + .headers(TASK_TABLE_HEADERS) + .constraints(TASK_TABLE_CONSTRAINTS); + + f.render_widget(tasks_table, area); +} + +pub(super) fn draw_queued_events(f: &mut Frame<'_>, app: &mut App<'_>, area: Rect) { + let events_row_mapping = |event: &QueueEvent| { + let queued = convert_to_minutes_hours_days(Utc::now().sub(event.queued).num_minutes()); + let queued_string = if queued != "now" { + format!("{queued} ago") + } else { + queued + }; + let started_string = if event.started.is_some() { + let started = + convert_to_minutes_hours_days(Utc::now().sub(event.started.unwrap()).num_minutes()); + + if started != "now" { + format!("{started} ago") + } else { + started + } + } else { + String::new() + }; + + let duration = if event.duration.is_some() { + &event.duration.as_ref().unwrap()[..8] + } else { + "" + }; + + Row::new(vec![ + Cell::from(event.trigger.clone()), + Cell::from(event.status.clone()), + Cell::from(event.command_name.clone()), + Cell::from(queued_string), + Cell::from(started_string), + Cell::from(duration.to_owned()), + ]) + .primary() + }; + let events_table = ManagarrTable::new( + Some(&mut app.data.sonarr_data.queued_events), + events_row_mapping, + ) + .block(title_block("Queued Events")) + .loading(app.is_loading) + .highlight_rows(false) + .headers(["Trigger", "Status", "Name", "Queued", "Started", "Duration"]) + .constraints([ + Constraint::Percentage(13), + Constraint::Percentage(13), + Constraint::Percentage(30), + Constraint::Percentage(16), + Constraint::Percentage(14), + Constraint::Percentage(14), + ]); + + f.render_widget(events_table, area); +} + +fn draw_logs(f: &mut Frame<'_>, app: &mut App<'_>, area: Rect) { + let block = title_block("Logs"); + + if app.data.sonarr_data.logs.items.is_empty() { + f.render_widget(LoadingBlock::new(app.is_loading, block), area); + return; + } + + let logs_box = SelectableList::new(&mut app.data.sonarr_data.logs, |log| { + let log_line = log.to_string(); + let level = log_line.split('|').collect::>()[1].to_string(); + + style_log_list_item(ListItem::new(Text::from(Span::raw(log_line))), level) + }) + .block(block) + .highlight_style(Style::new().default()); + + f.render_widget(logs_box, area); +} + +fn draw_help(f: &mut Frame<'_>, app: &mut App<'_>, area: Rect) { + let help_text = Text::from( + format!( + " {}", + app + .data + .sonarr_data + .main_tabs + .get_active_tab_contextual_help() + .unwrap() + ) + .help(), + ); + let help_paragraph = Paragraph::new(help_text) + .block(layout_block_top_border()) + .left_aligned(); + + f.render_widget(help_paragraph, area); +} + +pub(super) struct TaskProps { + pub(super) name: String, + pub(super) interval: String, + pub(super) last_execution: String, + pub(super) next_execution: String, +} + +pub(super) fn extract_task_props(task: &SonarrTask) -> TaskProps { + let interval = convert_to_minutes_hours_days(task.interval); + let next_execution = + convert_to_minutes_hours_days((task.next_execution - Utc::now()).num_minutes()); + let last_execution = + convert_to_minutes_hours_days((Utc::now() - task.last_execution).num_minutes()); + let last_execution_string = if last_execution != "now" { + format!("{last_execution} ago") + } else { + last_execution + }; + + TaskProps { + name: task.name.clone(), + interval, + last_execution: last_execution_string, + next_execution, + } +} diff --git a/src/ui/sonarr_ui/system/system_details_ui.rs b/src/ui/sonarr_ui/system/system_details_ui.rs new file mode 100644 index 0000000..7aa79a2 --- /dev/null +++ b/src/ui/sonarr_ui/system/system_details_ui.rs @@ -0,0 +1,180 @@ +use ratatui::layout::{Alignment, Rect}; +use ratatui::text::{Span, Text}; +use ratatui::widgets::{Cell, ListItem, Paragraph, Row}; +use ratatui::Frame; + +use crate::app::context_clues::{build_context_clue_string, BARE_POPUP_CONTEXT_CLUES}; +use crate::app::sonarr::sonarr_context_clues::SYSTEM_TASKS_CONTEXT_CLUES; +use crate::app::App; +use crate::models::servarr_data::sonarr::sonarr_data::{ActiveSonarrBlock, SYSTEM_DETAILS_BLOCKS}; +use crate::models::sonarr_models::SonarrTask; +use crate::models::Route; +use crate::ui::sonarr_ui::system::{ + draw_queued_events, draw_system_ui_layout, extract_task_props, TASK_TABLE_CONSTRAINTS, + TASK_TABLE_HEADERS, +}; +use crate::ui::styles::ManagarrStyle; +use crate::ui::utils::{borderless_block, style_log_list_item, title_block}; +use crate::ui::widgets::confirmation_prompt::ConfirmationPrompt; +use crate::ui::widgets::loading_block::LoadingBlock; +use crate::ui::widgets::managarr_table::ManagarrTable; +use crate::ui::widgets::popup::{Popup, Size}; +use crate::ui::widgets::selectable_list::SelectableList; +use crate::ui::{draw_popup_over, DrawUi}; + +#[cfg(test)] +#[path = "system_details_ui_tests.rs"] +mod system_details_ui_tests; + +pub(super) struct SystemDetailsUi; + +impl DrawUi for SystemDetailsUi { + fn accepts(route: Route) -> bool { + if let Route::Sonarr(active_sonarr_block, _) = route { + return SYSTEM_DETAILS_BLOCKS.contains(&active_sonarr_block); + } + + false + } + + fn draw(f: &mut Frame<'_>, app: &mut App<'_>, area: Rect) { + if let Route::Sonarr(active_sonarr_block, _) = app.get_current_route() { + match active_sonarr_block { + ActiveSonarrBlock::SystemLogs => { + draw_system_ui_layout(f, app, area); + draw_logs_popup(f, app); + } + ActiveSonarrBlock::SystemTasks | ActiveSonarrBlock::SystemTaskStartConfirmPrompt => { + draw_popup_over( + f, + app, + area, + draw_system_ui_layout, + draw_tasks_popup, + Size::Large, + ) + } + ActiveSonarrBlock::SystemQueuedEvents => draw_popup_over( + f, + app, + area, + draw_system_ui_layout, + draw_queued_events, + Size::Medium, + ), + ActiveSonarrBlock::SystemUpdates => { + draw_system_ui_layout(f, app, area); + draw_updates_popup(f, app); + } + _ => (), + } + } + } +} + +fn draw_logs_popup(f: &mut Frame<'_>, app: &mut App<'_>) { + let block = title_block("Log Details"); + let help_footer = format!( + "<↑↓←→> scroll | {}", + build_context_clue_string(&BARE_POPUP_CONTEXT_CLUES) + ); + + if app.data.sonarr_data.log_details.items.is_empty() { + let loading = LoadingBlock::new(app.is_loading, borderless_block()); + let popup = Popup::new(loading) + .size(Size::Large) + .block(block) + .footer(&help_footer); + + f.render_widget(popup, f.area()); + return; + } + + let logs_list = SelectableList::new(&mut app.data.sonarr_data.log_details, |log| { + let log_line = log.to_string(); + let level = log.text.split('|').collect::>()[1].to_string(); + + style_log_list_item(ListItem::new(Text::from(Span::raw(log_line))), level) + }) + .block(borderless_block()); + let popup = Popup::new(logs_list) + .size(Size::Large) + .block(block) + .footer(&help_footer); + + f.render_widget(popup, f.area()); +} + +fn draw_tasks_popup(f: &mut Frame<'_>, app: &mut App<'_>, area: Rect) { + let help_footer = Some(build_context_clue_string(&SYSTEM_TASKS_CONTEXT_CLUES)); + let tasks_row_mapping = |task: &SonarrTask| { + let task_props = extract_task_props(task); + + Row::new(vec![ + Cell::from(task_props.name), + Cell::from(task_props.interval), + Cell::from(task_props.last_execution), + Cell::from(task_props.next_execution), + ]) + .primary() + }; + let tasks_table = ManagarrTable::new(Some(&mut app.data.sonarr_data.tasks), tasks_row_mapping) + .block(borderless_block()) + .loading(app.is_loading) + .margin(1) + .footer(help_footer) + .footer_alignment(Alignment::Center) + .headers(TASK_TABLE_HEADERS) + .constraints(TASK_TABLE_CONSTRAINTS); + + f.render_widget(title_block("Tasks"), area); + f.render_widget(tasks_table, area); + + if matches!( + app.get_current_route(), + Route::Sonarr(ActiveSonarrBlock::SystemTaskStartConfirmPrompt, _) + ) { + let prompt = format!( + "Do you want to manually start this task: {}?", + app.data.sonarr_data.tasks.current_selection().name + ); + let confirmation_prompt = ConfirmationPrompt::new() + .title("Start Task") + .prompt(&prompt) + .yes_no_value(app.data.sonarr_data.prompt_confirm); + + f.render_widget( + Popup::new(confirmation_prompt).size(Size::MediumPrompt), + f.area(), + ); + } +} + +fn draw_updates_popup(f: &mut Frame<'_>, app: &mut App<'_>) { + let help_footer = format!( + "<↑↓> scroll | {}", + build_context_clue_string(&BARE_POPUP_CONTEXT_CLUES) + ); + let updates = app.data.sonarr_data.updates.get_text(); + let block = title_block("Updates"); + + if !updates.is_empty() { + let updates_paragraph = Paragraph::new(Text::from(updates)) + .block(borderless_block()) + .scroll((app.data.sonarr_data.updates.offset, 0)); + let popup = Popup::new(updates_paragraph) + .size(Size::Large) + .block(block) + .footer(&help_footer); + + f.render_widget(popup, f.area()); + } else { + let loading = LoadingBlock::new(app.is_loading, borderless_block()); + let popup = Popup::new(loading) + .size(Size::Large) + .block(block) + .footer(&help_footer); + + f.render_widget(popup, f.area()); + } +} diff --git a/src/ui/sonarr_ui/system/system_details_ui_tests.rs b/src/ui/sonarr_ui/system/system_details_ui_tests.rs new file mode 100644 index 0000000..80dc239 --- /dev/null +++ b/src/ui/sonarr_ui/system/system_details_ui_tests.rs @@ -0,0 +1,21 @@ +#[cfg(test)] +mod tests { + use strum::IntoEnumIterator; + + use crate::models::servarr_data::sonarr::sonarr_data::{ + ActiveSonarrBlock, SYSTEM_DETAILS_BLOCKS, + }; + use crate::ui::sonarr_ui::system::system_details_ui::SystemDetailsUi; + use crate::ui::DrawUi; + + #[test] + fn test_system_details_ui_accepts() { + ActiveSonarrBlock::iter().for_each(|active_sonarr_block| { + if SYSTEM_DETAILS_BLOCKS.contains(&active_sonarr_block) { + assert!(SystemDetailsUi::accepts(active_sonarr_block.into())); + } else { + assert!(!SystemDetailsUi::accepts(active_sonarr_block.into())); + } + }); + } +} diff --git a/src/ui/sonarr_ui/system/system_ui_tests.rs b/src/ui/sonarr_ui/system/system_ui_tests.rs new file mode 100644 index 0000000..dc43bdc --- /dev/null +++ b/src/ui/sonarr_ui/system/system_ui_tests.rs @@ -0,0 +1,25 @@ +#[cfg(test)] +mod tests { + use strum::IntoEnumIterator; + + use crate::models::servarr_data::sonarr::sonarr_data::{ + ActiveSonarrBlock, SYSTEM_DETAILS_BLOCKS, + }; + use crate::ui::sonarr_ui::system::SystemUi; + use crate::ui::DrawUi; + + #[test] + fn test_system_ui_accepts() { + let mut system_ui_blocks = Vec::new(); + system_ui_blocks.push(ActiveSonarrBlock::System); + system_ui_blocks.extend(SYSTEM_DETAILS_BLOCKS); + + ActiveSonarrBlock::iter().for_each(|active_sonarr_block| { + if system_ui_blocks.contains(&active_sonarr_block) { + assert!(SystemUi::accepts(active_sonarr_block.into())); + } else { + assert!(!SystemUi::accepts(active_sonarr_block.into())); + } + }); + } +} diff --git a/src/ui/utils.rs b/src/ui/utils.rs index bf1e24b..3b206db 100644 --- a/src/ui/utils.rs +++ b/src/ui/utils.rs @@ -3,7 +3,7 @@ use ratatui::layout::{Alignment, Constraint, Layout, Rect}; use ratatui::style::{Color, Style, Stylize}; use ratatui::symbols; use ratatui::text::{Line, Span, Text}; -use ratatui::widgets::{Block, BorderType, Borders, LineGauge, Paragraph, Wrap}; +use ratatui::widgets::{Block, BorderType, Borders, LineGauge, ListItem, Paragraph, Wrap}; pub const COLOR_TEAL: Color = Color::Rgb(35, 50, 55); @@ -116,3 +116,41 @@ pub fn line_gauge_with_label(title: &str, ratio: f64) -> LineGauge<'_> { pub fn get_width_from_percentage(area: Rect, percentage: u16) -> usize { (area.width as f64 * (percentage as f64 / 100.0)) as usize } + +pub(super) fn style_log_list_item(list_item: ListItem<'_>, level: String) -> ListItem<'_> { + match level.to_lowercase().as_str() { + "trace" => list_item.gray(), + "debug" => list_item.blue(), + "info" => list_item.style(Style::new().default()), + "warn" => list_item.style(Style::new().secondary()), + "error" => list_item.style(Style::new().failure()), + "fatal" => list_item.style(Style::new().failure().bold()), + _ => list_item.style(Style::new().default()), + } +} + +pub(super) fn convert_to_minutes_hours_days(time: i64) -> String { + if time < 60 { + if time == 0 { + "now".to_owned() + } else if time == 1 { + format!("{time} minute") + } else { + format!("{time} minutes") + } + } else if time / 60 < 24 { + let hours = time / 60; + if hours == 1 { + format!("{hours} hour") + } else { + format!("{hours} hours") + } + } else { + let days = time / (60 * 24); + if days == 1 { + format!("{days} day") + } else { + format!("{days} days") + } + } +} diff --git a/src/ui/utils_tests.rs b/src/ui/utils_tests.rs index 888e6bd..47ab277 100644 --- a/src/ui/utils_tests.rs +++ b/src/ui/utils_tests.rs @@ -1,16 +1,16 @@ #[cfg(test)] mod test { - use pretty_assertions::assert_eq; + use pretty_assertions::{assert_eq, assert_str_eq}; use ratatui::layout::{Alignment, Rect}; - use ratatui::style::{Color, Modifier, Style}; - use ratatui::text::Span; - use ratatui::widgets::{Block, BorderType, Borders}; + use ratatui::style::{Color, Modifier, Style, Stylize}; + use ratatui::text::{Span, Text}; + use ratatui::widgets::{Block, BorderType, Borders, ListItem}; use crate::ui::utils::{ - borderless_block, centered_rect, get_width_from_percentage, layout_block, - layout_block_bottom_border, layout_block_top_border, layout_block_top_border_with_title, - layout_block_with_title, logo_block, style_block_highlight, title_block, title_block_centered, - title_style, + borderless_block, centered_rect, convert_to_minutes_hours_days, get_width_from_percentage, + layout_block, layout_block_bottom_border, layout_block_top_border, + layout_block_top_border_with_title, layout_block_with_title, logo_block, style_block_highlight, + style_log_list_item, title_block, title_block_centered, title_style, }; #[test] @@ -174,6 +174,70 @@ mod test { ); } + #[test] + fn test_determine_log_style_by_level() { + use crate::ui::styles::ManagarrStyle; + let list_item = ListItem::new(Text::from(Span::raw("test"))); + + assert_eq!( + style_log_list_item(list_item.clone(), "trace".to_string()), + list_item.clone().gray() + ); + assert_eq!( + style_log_list_item(list_item.clone(), "debug".to_string()), + list_item.clone().blue() + ); + assert_eq!( + style_log_list_item(list_item.clone(), "info".to_string()), + list_item.clone().style(Style::new().default()) + ); + assert_eq!( + style_log_list_item(list_item.clone(), "warn".to_string()), + list_item.clone().style(Style::new().secondary()) + ); + assert_eq!( + style_log_list_item(list_item.clone(), "error".to_string()), + list_item.clone().style(Style::new().failure()) + ); + assert_eq!( + style_log_list_item(list_item.clone(), "fatal".to_string()), + list_item.clone().style(Style::new().failure().bold()) + ); + assert_eq!( + style_log_list_item(list_item.clone(), "".to_string()), + list_item.style(Style::new().default()) + ); + } + + #[test] + fn test_determine_log_style_by_level_case_insensitive() { + let list_item = ListItem::new(Text::from(Span::raw("test"))); + + assert_eq!( + style_log_list_item(list_item.clone(), "TrAcE".to_string()), + list_item.gray() + ); + } + + #[test] + fn test_convert_to_minutes_hours_days_minutes() { + assert_str_eq!(convert_to_minutes_hours_days(0), "now"); + assert_str_eq!(convert_to_minutes_hours_days(1), "1 minute"); + assert_str_eq!(convert_to_minutes_hours_days(2), "2 minutes"); + } + + #[test] + fn test_convert_to_minutes_hours_days_hours() { + assert_str_eq!(convert_to_minutes_hours_days(60), "1 hour"); + assert_str_eq!(convert_to_minutes_hours_days(120), "2 hours"); + } + + #[test] + fn test_convert_to_minutes_hours_days_days() { + assert_str_eq!(convert_to_minutes_hours_days(1440), "1 day"); + assert_str_eq!(convert_to_minutes_hours_days(2880), "2 days"); + } + fn rect() -> Rect { Rect { x: 0,