Refactored the UI module and the handlers module to do a more chain-of-responsibility method to manage the UI's and handlers for different key events. Also, initial work for indexer settings as well
This commit is contained in:
@@ -0,0 +1,256 @@
|
||||
use std::ops::Sub;
|
||||
|
||||
use chrono::Utc;
|
||||
use tui::layout::Alignment;
|
||||
use tui::text::{Span, Text};
|
||||
use tui::widgets::{Cell, Paragraph, Row};
|
||||
use tui::{
|
||||
backend::Backend,
|
||||
layout::{Constraint, Rect},
|
||||
widgets::ListItem,
|
||||
Frame,
|
||||
};
|
||||
|
||||
use crate::models::radarr_models::Task;
|
||||
use crate::ui::radarr_ui::radarr_ui_utils::{
|
||||
convert_to_minutes_hours_days, determine_log_style_by_level,
|
||||
};
|
||||
use crate::ui::radarr_ui::system::system_details_ui::SystemDetailsUi;
|
||||
use crate::ui::utils::{layout_block_top_border, style_help, style_primary};
|
||||
use crate::ui::{draw_table, ListProps, TableProps};
|
||||
use crate::{
|
||||
app::{radarr::ActiveRadarrBlock, App},
|
||||
models::Route,
|
||||
ui::{
|
||||
draw_list_box,
|
||||
utils::{horizontal_chunks, title_block, vertical_chunks},
|
||||
DrawUi,
|
||||
},
|
||||
};
|
||||
|
||||
mod system_details_ui;
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "system_ui_tests.rs"]
|
||||
mod system_ui_tests;
|
||||
|
||||
pub(super) const TASK_TABLE_HEADERS: [&str; 5] = [
|
||||
"Name",
|
||||
"Interval",
|
||||
"Last Execution",
|
||||
"Last Duration",
|
||||
"Next Execution",
|
||||
];
|
||||
|
||||
pub(super) const TASK_TABLE_CONSTRAINTS: [Constraint; 5] = [
|
||||
Constraint::Percentage(30),
|
||||
Constraint::Percentage(12),
|
||||
Constraint::Percentage(18),
|
||||
Constraint::Percentage(18),
|
||||
Constraint::Percentage(22),
|
||||
];
|
||||
|
||||
pub(super) struct SystemUi {}
|
||||
|
||||
impl DrawUi for SystemUi {
|
||||
fn accepts(route: Route) -> bool {
|
||||
if let Route::Radarr(active_radarr_block, _) = route {
|
||||
return SystemDetailsUi::accepts(route) || active_radarr_block == ActiveRadarrBlock::System;
|
||||
}
|
||||
|
||||
false
|
||||
}
|
||||
|
||||
fn draw<B: Backend>(f: &mut Frame<'_, B>, app: &mut App<'_>, content_rect: Rect) {
|
||||
let route = *app.get_current_route();
|
||||
|
||||
match route {
|
||||
_ if SystemDetailsUi::accepts(route) => SystemDetailsUi::draw(f, app, content_rect),
|
||||
_ if matches!(route, Route::Radarr(ActiveRadarrBlock::System, _)) => {
|
||||
draw_system_ui_layout(f, app, content_rect)
|
||||
}
|
||||
_ => (),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn draw_system_ui_layout<B: Backend>(
|
||||
f: &mut Frame<'_, B>,
|
||||
app: &mut App<'_>,
|
||||
area: Rect,
|
||||
) {
|
||||
let vertical_chunks = vertical_chunks(
|
||||
vec![
|
||||
Constraint::Ratio(1, 2),
|
||||
Constraint::Ratio(1, 2),
|
||||
Constraint::Min(2),
|
||||
],
|
||||
area,
|
||||
);
|
||||
|
||||
let horizontal_chunks = horizontal_chunks(
|
||||
vec![Constraint::Ratio(1, 2), Constraint::Ratio(1, 2)],
|
||||
vertical_chunks[0],
|
||||
);
|
||||
|
||||
draw_tasks(f, app, horizontal_chunks[0]);
|
||||
draw_queued_events(f, app, horizontal_chunks[1]);
|
||||
draw_logs(f, app, vertical_chunks[1]);
|
||||
draw_help(f, app, vertical_chunks[2]);
|
||||
}
|
||||
|
||||
fn draw_tasks<B: Backend>(f: &mut Frame<'_, B>, app: &mut App<'_>, area: Rect) {
|
||||
draw_table(
|
||||
f,
|
||||
area,
|
||||
title_block("Tasks"),
|
||||
TableProps {
|
||||
content: &mut app.data.radarr_data.tasks,
|
||||
table_headers: TASK_TABLE_HEADERS.to_vec(),
|
||||
constraints: TASK_TABLE_CONSTRAINTS.to_vec(),
|
||||
help: None,
|
||||
},
|
||||
|task| {
|
||||
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.last_duration),
|
||||
Cell::from(task_props.next_execution),
|
||||
])
|
||||
.style(style_primary())
|
||||
},
|
||||
app.is_loading,
|
||||
false,
|
||||
);
|
||||
}
|
||||
|
||||
pub(super) fn draw_queued_events<B: Backend>(f: &mut Frame<'_, B>, app: &mut App<'_>, area: Rect) {
|
||||
draw_table(
|
||||
f,
|
||||
area,
|
||||
title_block("Queued Events"),
|
||||
TableProps {
|
||||
content: &mut app.data.radarr_data.queued_events,
|
||||
table_headers: vec!["Trigger", "Status", "Name", "Queued", "Started", "Duration"],
|
||||
constraints: vec![
|
||||
Constraint::Percentage(13),
|
||||
Constraint::Percentage(13),
|
||||
Constraint::Percentage(30),
|
||||
Constraint::Percentage(16),
|
||||
Constraint::Percentage(14),
|
||||
Constraint::Percentage(14),
|
||||
],
|
||||
help: None,
|
||||
},
|
||||
|event| {
|
||||
let queued = convert_to_minutes_hours_days(Utc::now().sub(event.queued).num_minutes());
|
||||
let queued_string = if queued != "now" {
|
||||
format!("{} ago", queued)
|
||||
} 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!("{} ago", started)
|
||||
} 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()),
|
||||
])
|
||||
.style(style_primary())
|
||||
},
|
||||
app.is_loading,
|
||||
false,
|
||||
);
|
||||
}
|
||||
|
||||
fn draw_logs<B: Backend>(f: &mut Frame<'_, B>, app: &mut App<'_>, area: Rect) {
|
||||
draw_list_box(
|
||||
f,
|
||||
area,
|
||||
|log| {
|
||||
let log_line = log.to_string();
|
||||
let level = log_line.split('|').collect::<Vec<&str>>()[1];
|
||||
let style = determine_log_style_by_level(level);
|
||||
|
||||
ListItem::new(Text::from(Span::raw(log_line))).style(style)
|
||||
},
|
||||
ListProps {
|
||||
content: &mut app.data.radarr_data.logs,
|
||||
title: "Logs",
|
||||
is_loading: app.is_loading,
|
||||
is_popup: false,
|
||||
help: None,
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
fn draw_help<B: Backend>(f: &mut Frame<'_, B>, app: &mut App<'_>, area: Rect) {
|
||||
let mut help_text = Text::from(format!(
|
||||
" {}",
|
||||
app
|
||||
.data
|
||||
.radarr_data
|
||||
.main_tabs
|
||||
.get_active_tab_contextual_help()
|
||||
.unwrap()
|
||||
));
|
||||
help_text.patch_style(style_help());
|
||||
let help_paragraph = Paragraph::new(help_text)
|
||||
.block(layout_block_top_border())
|
||||
.alignment(Alignment::Left);
|
||||
|
||||
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) last_duration: String,
|
||||
pub(super) next_execution: String,
|
||||
}
|
||||
|
||||
pub(super) fn extract_task_props(task: &Task) -> TaskProps {
|
||||
let interval = convert_to_minutes_hours_days(*task.interval.as_i64().as_ref().unwrap());
|
||||
let last_duration = &task.last_duration[..8];
|
||||
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!("{} ago", last_execution)
|
||||
} else {
|
||||
last_execution
|
||||
};
|
||||
|
||||
TaskProps {
|
||||
name: task.name.clone(),
|
||||
interval,
|
||||
last_execution: last_execution_string,
|
||||
last_duration: last_duration.to_owned(),
|
||||
next_execution,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,166 @@
|
||||
use tui::backend::Backend;
|
||||
use tui::layout::Rect;
|
||||
use tui::text::{Span, Text};
|
||||
use tui::widgets::{Cell, ListItem, Paragraph, Row};
|
||||
use tui::Frame;
|
||||
|
||||
use crate::app::radarr::{ActiveRadarrBlock, SYSTEM_DETAILS_BLOCKS};
|
||||
use crate::app::App;
|
||||
use crate::models::Route;
|
||||
use crate::ui::radarr_ui::radarr_ui_utils::determine_log_style_by_level;
|
||||
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::utils::{borderless_block, style_primary, title_block};
|
||||
use crate::ui::{
|
||||
draw_help_and_get_content_rect, draw_large_popup_over, draw_list_box, draw_medium_popup_over,
|
||||
draw_prompt_box, draw_prompt_popup_over, draw_table, loading, DrawUi, ListProps, TableProps,
|
||||
};
|
||||
|
||||
#[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::Radarr(active_radarr_block, _) = route {
|
||||
return SYSTEM_DETAILS_BLOCKS.contains(&active_radarr_block);
|
||||
}
|
||||
|
||||
false
|
||||
}
|
||||
|
||||
fn draw<B: Backend>(f: &mut Frame<'_, B>, app: &mut App<'_>, content_rect: Rect) {
|
||||
if let Route::Radarr(active_radarr_block, _) = *app.get_current_route() {
|
||||
match active_radarr_block {
|
||||
ActiveRadarrBlock::SystemLogs => {
|
||||
draw_large_popup_over(f, app, content_rect, draw_system_ui_layout, draw_logs_popup)
|
||||
}
|
||||
ActiveRadarrBlock::SystemTasks | ActiveRadarrBlock::SystemTaskStartConfirmPrompt => {
|
||||
draw_large_popup_over(
|
||||
f,
|
||||
app,
|
||||
content_rect,
|
||||
draw_system_ui_layout,
|
||||
draw_tasks_popup,
|
||||
)
|
||||
}
|
||||
ActiveRadarrBlock::SystemQueuedEvents => draw_medium_popup_over(
|
||||
f,
|
||||
app,
|
||||
content_rect,
|
||||
draw_system_ui_layout,
|
||||
draw_queued_events,
|
||||
),
|
||||
ActiveRadarrBlock::SystemUpdates => draw_large_popup_over(
|
||||
f,
|
||||
app,
|
||||
content_rect,
|
||||
draw_system_ui_layout,
|
||||
draw_updates_popup,
|
||||
),
|
||||
_ => (),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn draw_logs_popup<B: Backend>(f: &mut Frame<'_, B>, app: &mut App<'_>, area: Rect) {
|
||||
draw_list_box(
|
||||
f,
|
||||
area,
|
||||
|log| {
|
||||
let log_line = log.to_string();
|
||||
let level = log.text.split('|').collect::<Vec<&str>>()[1];
|
||||
let style = determine_log_style_by_level(level);
|
||||
|
||||
ListItem::new(Text::from(Span::raw(log_line))).style(style)
|
||||
},
|
||||
ListProps {
|
||||
content: &mut app.data.radarr_data.log_details,
|
||||
title: "Log Details",
|
||||
is_loading: app.is_loading,
|
||||
is_popup: true,
|
||||
help: Some("<↑↓←→> scroll | <esc> close"),
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
fn draw_tasks_popup<B: Backend>(f: &mut Frame<'_, B>, app: &mut App<'_>, area: Rect) {
|
||||
let tasks_popup_table = |f: &mut Frame<'_, B>, app: &mut App<'_>, area: Rect| {
|
||||
f.render_widget(title_block("Tasks"), area);
|
||||
|
||||
let context_area =
|
||||
draw_help_and_get_content_rect(f, area, Some("<enter> start task | <esc> close"));
|
||||
|
||||
draw_table(
|
||||
f,
|
||||
context_area,
|
||||
borderless_block(),
|
||||
TableProps {
|
||||
content: &mut app.data.radarr_data.tasks,
|
||||
table_headers: TASK_TABLE_HEADERS.to_vec(),
|
||||
constraints: TASK_TABLE_CONSTRAINTS.to_vec(),
|
||||
help: None,
|
||||
},
|
||||
|task| {
|
||||
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.last_duration),
|
||||
Cell::from(task_props.next_execution),
|
||||
])
|
||||
.style(style_primary())
|
||||
},
|
||||
app.is_loading,
|
||||
true,
|
||||
)
|
||||
};
|
||||
|
||||
if matches!(
|
||||
app.get_current_route(),
|
||||
Route::Radarr(ActiveRadarrBlock::SystemTaskStartConfirmPrompt, _)
|
||||
) {
|
||||
draw_prompt_popup_over(f, app, area, tasks_popup_table, draw_start_task_prompt)
|
||||
} else {
|
||||
tasks_popup_table(f, app, area);
|
||||
}
|
||||
}
|
||||
|
||||
fn draw_start_task_prompt<B: Backend>(f: &mut Frame<'_, B>, app: &mut App<'_>, prompt_area: Rect) {
|
||||
draw_prompt_box(
|
||||
f,
|
||||
prompt_area,
|
||||
"Start Task",
|
||||
format!(
|
||||
"Do you want to manually start this task: {}?",
|
||||
app.data.radarr_data.tasks.current_selection().name
|
||||
)
|
||||
.as_str(),
|
||||
app.data.radarr_data.prompt_confirm,
|
||||
);
|
||||
}
|
||||
|
||||
fn draw_updates_popup<B: Backend>(f: &mut Frame<'_, B>, app: &mut App<'_>, area: Rect) {
|
||||
f.render_widget(title_block("Updates"), area);
|
||||
|
||||
let content_rect = draw_help_and_get_content_rect(f, area, Some("<↑↓> scroll | <esc> close"));
|
||||
let updates = app.data.radarr_data.updates.get_text();
|
||||
let block = borderless_block();
|
||||
|
||||
if !updates.is_empty() {
|
||||
let updates_paragraph = Paragraph::new(Text::from(updates))
|
||||
.block(block)
|
||||
.scroll((app.data.radarr_data.updates.offset, 0));
|
||||
|
||||
f.render_widget(updates_paragraph, content_rect);
|
||||
} else {
|
||||
loading(f, block, content_rect, app.is_loading);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use strum::IntoEnumIterator;
|
||||
|
||||
use crate::app::radarr::{ActiveRadarrBlock, SYSTEM_DETAILS_BLOCKS};
|
||||
use crate::ui::radarr_ui::system::system_details_ui::SystemDetailsUi;
|
||||
use crate::ui::DrawUi;
|
||||
|
||||
#[test]
|
||||
fn test_system_details_ui_accepts() {
|
||||
ActiveRadarrBlock::iter().for_each(|active_radarr_block| {
|
||||
if SYSTEM_DETAILS_BLOCKS.contains(&active_radarr_block) {
|
||||
assert!(SystemDetailsUi::accepts(active_radarr_block.into()));
|
||||
} else {
|
||||
assert!(!SystemDetailsUi::accepts(active_radarr_block.into()));
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use strum::IntoEnumIterator;
|
||||
|
||||
use crate::app::radarr::{ActiveRadarrBlock, SYSTEM_DETAILS_BLOCKS};
|
||||
use crate::ui::radarr_ui::system::SystemUi;
|
||||
use crate::ui::DrawUi;
|
||||
|
||||
#[test]
|
||||
fn test_system_ui_accepts() {
|
||||
let mut system_ui_blocks = Vec::new();
|
||||
system_ui_blocks.push(ActiveRadarrBlock::System);
|
||||
system_ui_blocks.extend(SYSTEM_DETAILS_BLOCKS);
|
||||
|
||||
ActiveRadarrBlock::iter().for_each(|active_radarr_block| {
|
||||
if system_ui_blocks.contains(&active_radarr_block) {
|
||||
assert!(SystemUi::accepts(active_radarr_block.into()));
|
||||
} else {
|
||||
assert!(!SystemUi::accepts(active_radarr_block.into()));
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user