Partial implementation of Tasks and Logs and test refactor
This commit is contained in:
+21
-1
@@ -566,7 +566,7 @@ pub fn draw_drop_down_menu_button<B: Backend>(
|
||||
draw_button_with_icon(f, horizontal_chunks[1], selection, "▼", is_selected);
|
||||
}
|
||||
|
||||
pub fn draw_drop_down_list<'a, B: Backend, T>(
|
||||
pub fn draw_selectable_list<'a, B: Backend, T>(
|
||||
f: &mut Frame<'_, B>,
|
||||
area: Rect,
|
||||
content: &'a mut StatefulList<T>,
|
||||
@@ -580,6 +580,26 @@ pub fn draw_drop_down_list<'a, B: Backend, T>(
|
||||
f.render_stateful_widget(list, area, &mut content.state);
|
||||
}
|
||||
|
||||
pub fn draw_list_box<'a, B: Backend, T>(
|
||||
f: &mut Frame<'_, B>,
|
||||
area: Rect,
|
||||
content: &'a mut StatefulList<T>,
|
||||
title: &str,
|
||||
item_mapper: impl Fn(&T) -> ListItem<'a>,
|
||||
is_loading: bool,
|
||||
) {
|
||||
let block = title_block(title);
|
||||
|
||||
if !content.items.is_empty() {
|
||||
let items: Vec<ListItem<'_>> = content.items.iter().map(item_mapper).collect();
|
||||
let list = List::new(items).block(title_block(title));
|
||||
|
||||
f.render_stateful_widget(list, area, &mut content.state);
|
||||
} else {
|
||||
loading(f, block, area, is_loading);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn draw_text_box<B: Backend>(
|
||||
f: &mut Frame<'_, B>,
|
||||
text_box_area: Rect,
|
||||
|
||||
@@ -20,8 +20,8 @@ use crate::ui::utils::{
|
||||
vertical_chunks_with_margin,
|
||||
};
|
||||
use crate::ui::{
|
||||
draw_button, draw_drop_down_list, draw_drop_down_menu_button, draw_drop_down_popup,
|
||||
draw_error_popup, draw_error_popup_over, draw_large_popup_over, draw_medium_popup_over,
|
||||
draw_button, draw_drop_down_menu_button, draw_drop_down_popup, draw_error_popup,
|
||||
draw_error_popup_over, draw_large_popup_over, draw_medium_popup_over, draw_selectable_list,
|
||||
draw_table, draw_text_box, draw_text_box_with_label, DrawUi, TableProps,
|
||||
};
|
||||
use crate::utils::convert_runtime;
|
||||
@@ -311,7 +311,7 @@ fn draw_select_monitor_popup<B: Backend>(
|
||||
app: &mut App<'_>,
|
||||
popup_area: Rect,
|
||||
) {
|
||||
draw_drop_down_list(
|
||||
draw_selectable_list(
|
||||
f,
|
||||
popup_area,
|
||||
&mut app.data.radarr_data.monitor_list,
|
||||
|
||||
@@ -18,9 +18,10 @@ use crate::app::App;
|
||||
use crate::logos::RADARR_LOGO;
|
||||
use crate::models::radarr_models::{DiskSpace, DownloadRecord, Movie, RootFolder};
|
||||
use crate::models::Route;
|
||||
use crate::ui::draw_drop_down_list;
|
||||
use crate::ui::draw_selectable_list;
|
||||
use crate::ui::draw_tabs;
|
||||
use crate::ui::loading;
|
||||
use crate::ui::radarr_ui::system_ui::SystemUi;
|
||||
use crate::ui::radarr_ui::{
|
||||
add_movie_ui::AddMoviesUi, collection_details_ui::CollectionDetailsUi,
|
||||
collections_ui::CollectionsUi, delete_movie_ui::DeleteMovieUi, downloads_ui::DownloadsUi,
|
||||
@@ -45,6 +46,7 @@ mod edit_movie_ui;
|
||||
mod library_ui;
|
||||
mod movie_details_ui;
|
||||
mod root_folders_ui;
|
||||
mod system_ui;
|
||||
|
||||
pub(super) struct RadarrUi {}
|
||||
|
||||
@@ -70,6 +72,7 @@ impl DrawUi for RadarrUi {
|
||||
ActiveRadarrBlock::RootFolders
|
||||
| ActiveRadarrBlock::AddRootFolderPrompt
|
||||
| ActiveRadarrBlock::DeleteRootFolderPrompt => RootFoldersUi::draw(f, app, content_rect),
|
||||
ActiveRadarrBlock::System => SystemUi::draw(f, app, content_rect),
|
||||
_ if MOVIE_DETAILS_BLOCKS.contains(&active_radarr_block) => {
|
||||
MovieDetailsUi::draw(f, app, content_rect)
|
||||
}
|
||||
@@ -265,7 +268,7 @@ fn draw_select_minimum_availability_popup<B: Backend>(
|
||||
app: &mut App<'_>,
|
||||
popup_area: Rect,
|
||||
) {
|
||||
draw_drop_down_list(
|
||||
draw_selectable_list(
|
||||
f,
|
||||
popup_area,
|
||||
&mut app.data.radarr_data.minimum_availability_list,
|
||||
@@ -278,7 +281,7 @@ fn draw_select_quality_profile_popup<B: Backend>(
|
||||
app: &mut App<'_>,
|
||||
popup_area: Rect,
|
||||
) {
|
||||
draw_drop_down_list(
|
||||
draw_selectable_list(
|
||||
f,
|
||||
popup_area,
|
||||
&mut app.data.radarr_data.quality_profile_list,
|
||||
@@ -291,7 +294,7 @@ fn draw_select_root_folder_popup<B: Backend>(
|
||||
app: &mut App<'_>,
|
||||
popup_area: Rect,
|
||||
) {
|
||||
draw_drop_down_list(
|
||||
draw_selectable_list(
|
||||
f,
|
||||
popup_area,
|
||||
&mut app.data.radarr_data.root_folder_list,
|
||||
|
||||
@@ -18,9 +18,9 @@ use crate::ui::utils::{
|
||||
style_primary, style_success, style_warning, vertical_chunks,
|
||||
};
|
||||
use crate::ui::{
|
||||
draw_drop_down_list, draw_drop_down_popup, draw_large_popup_over, draw_prompt_box,
|
||||
draw_prompt_box_with_content, draw_prompt_popup_over, draw_small_popup_over, draw_table,
|
||||
draw_tabs, loading, DrawUi, TableProps,
|
||||
draw_drop_down_popup, draw_large_popup_over, draw_prompt_box, draw_prompt_box_with_content,
|
||||
draw_prompt_popup_over, draw_selectable_list, draw_small_popup_over, draw_table, draw_tabs,
|
||||
loading, DrawUi, TableProps,
|
||||
};
|
||||
use crate::utils::convert_to_gb;
|
||||
|
||||
@@ -58,7 +58,7 @@ impl DrawUi for MovieDetailsUi {
|
||||
content_area,
|
||||
draw_movie_info,
|
||||
|f, app, content_area| {
|
||||
draw_drop_down_list(
|
||||
draw_selectable_list(
|
||||
f,
|
||||
content_area,
|
||||
&mut app.data.radarr_data.movie_releases_sort,
|
||||
|
||||
@@ -0,0 +1,147 @@
|
||||
use crate::ui::utils::{style_primary, style_secondary};
|
||||
use crate::ui::{draw_table, TableProps};
|
||||
use crate::{
|
||||
app::{radarr::ActiveRadarrBlock, App},
|
||||
models::Route,
|
||||
ui::{
|
||||
draw_list_box,
|
||||
utils::{horizontal_chunks, style_default, style_failure, title_block, vertical_chunks},
|
||||
DrawUi,
|
||||
},
|
||||
};
|
||||
use chrono::DateTime;
|
||||
use std::ops::Sub;
|
||||
use tui::style::Modifier;
|
||||
use tui::text::{Span, Text};
|
||||
use tui::widgets::{Cell, Row};
|
||||
use tui::{
|
||||
backend::Backend,
|
||||
layout::{Constraint, Rect},
|
||||
style::{Color, Style},
|
||||
widgets::ListItem,
|
||||
Frame,
|
||||
};
|
||||
|
||||
pub(super) struct SystemUi {}
|
||||
|
||||
impl DrawUi for SystemUi {
|
||||
fn draw<B: Backend>(f: &mut Frame<'_, B>, app: &mut App<'_>, content_rect: Rect) {
|
||||
if matches!(
|
||||
*app.get_current_route(),
|
||||
Route::Radarr(ActiveRadarrBlock::System, _)
|
||||
) {
|
||||
draw_system_ui_layout(f, app, content_rect)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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)], 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]);
|
||||
f.render_widget(title_block("Queue"), horizontal_chunks[1]);
|
||||
draw_logs(f, app, vertical_chunks[1]);
|
||||
}
|
||||
|
||||
fn draw_tasks<B: Backend>(f: &mut Frame<'_, B>, app: &mut App<'_>, area: Rect) {
|
||||
let block = title_block("Tasks");
|
||||
draw_table(
|
||||
f,
|
||||
area,
|
||||
block,
|
||||
TableProps {
|
||||
content: &mut app.data.radarr_data.tasks,
|
||||
table_headers: vec![
|
||||
"Name",
|
||||
"Interval",
|
||||
"Last Execution",
|
||||
"Last Duration",
|
||||
"Next Duration",
|
||||
],
|
||||
constraints: vec![
|
||||
Constraint::Percentage(30),
|
||||
Constraint::Percentage(12),
|
||||
Constraint::Percentage(16),
|
||||
Constraint::Percentage(16),
|
||||
Constraint::Percentage(16),
|
||||
],
|
||||
help: None,
|
||||
},
|
||||
|task| {
|
||||
let interval = format!("{} hours", task.interval.as_u64().as_ref().unwrap() / 60);
|
||||
let last_duration = &task.last_duration[..8];
|
||||
let next_execution = task.next_execution.sub(DateTime::default()).num_minutes();
|
||||
let next_execution_string = if next_execution > 60 {
|
||||
format!("{} hours", next_execution / 60)
|
||||
} else {
|
||||
format!("{} minutes", next_execution)
|
||||
};
|
||||
let last_execution = task.last_execution.sub(DateTime::default()).num_minutes();
|
||||
let last_execution_string = if last_execution > 60 {
|
||||
format!("{} hours", last_execution / 60)
|
||||
} else {
|
||||
format!("{} minutes", last_execution)
|
||||
};
|
||||
|
||||
Row::new(vec![
|
||||
Cell::from(task.name.clone()),
|
||||
Cell::from(interval),
|
||||
Cell::from(last_execution_string),
|
||||
Cell::from(last_duration.to_owned()),
|
||||
Cell::from(next_execution_string),
|
||||
])
|
||||
.style(style_primary())
|
||||
},
|
||||
app.is_loading,
|
||||
);
|
||||
}
|
||||
|
||||
fn draw_logs<B: Backend>(f: &mut Frame<'_, B>, app: &mut App<'_>, area: Rect) {
|
||||
draw_list_box(
|
||||
f,
|
||||
area,
|
||||
&mut app.data.radarr_data.logs,
|
||||
"Logs",
|
||||
|log| {
|
||||
let log_line = if log.exception.is_some() {
|
||||
Text::from(Span::raw(format!(
|
||||
"{}|{}|{}|{}|{}",
|
||||
log.time,
|
||||
log.level.as_ref().unwrap().to_uppercase(),
|
||||
log.logger.as_ref().unwrap(),
|
||||
log.exception_type.as_ref().unwrap(),
|
||||
log.exception.as_ref().unwrap()
|
||||
)))
|
||||
} else {
|
||||
Text::from(Span::raw(format!(
|
||||
"{}|{}|{}|{}",
|
||||
log.time,
|
||||
log.level.as_ref().unwrap().to_uppercase(),
|
||||
log.logger.as_ref().unwrap(),
|
||||
log.message.as_ref().unwrap()
|
||||
)))
|
||||
};
|
||||
|
||||
ListItem::new(log_line).style(determine_log_style_by_level(log.level.as_ref().unwrap()))
|
||||
},
|
||||
app.is_loading,
|
||||
);
|
||||
}
|
||||
|
||||
fn determine_log_style_by_level(level: &str) -> Style {
|
||||
match level.to_lowercase().as_str() {
|
||||
"trace" => Style::default().fg(Color::Gray),
|
||||
"debug" => Style::default().fg(Color::Blue),
|
||||
"info" => style_default(),
|
||||
"warn" => style_secondary(),
|
||||
"error" => style_failure(),
|
||||
"fatal" => style_failure().add_modifier(Modifier::BOLD),
|
||||
_ => style_default(),
|
||||
}
|
||||
}
|
||||
+4
-410
@@ -16,6 +16,10 @@ pub const COLOR_ORANGE: Color = Color::Rgb(255, 170, 66);
|
||||
pub const COLOR_WHITE: Color = Color::White;
|
||||
pub const COLOR_MAGENTA: Color = Color::Magenta;
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "utils_tests.rs"]
|
||||
mod utils_tests;
|
||||
|
||||
pub fn horizontal_chunks(constraints: Vec<Constraint>, area: Rect) -> Rc<[Rect]> {
|
||||
layout_with_constraints(constraints)
|
||||
.direction(Direction::Horizontal)
|
||||
@@ -271,413 +275,3 @@ pub fn show_cursor<B: Backend>(f: &mut Frame<'_, B>, area: Rect, offset: usize,
|
||||
pub fn get_width_from_percentage(area: Rect, percentage: u16) -> usize {
|
||||
(area.width as f64 * (percentage as f64 / 100.0)) as usize
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use pretty_assertions::assert_eq;
|
||||
use tui::layout::{Alignment, Constraint, Direction, Layout, Rect};
|
||||
use tui::style::{Color, Modifier, Style};
|
||||
use tui::text::{Span, Spans};
|
||||
use tui::widgets::{Block, BorderType, Borders};
|
||||
|
||||
use crate::ui::utils::{
|
||||
borderless_block, centered_rect, get_width_from_percentage, horizontal_chunks,
|
||||
horizontal_chunks_with_margin, layout_block, layout_block_bottom_border,
|
||||
layout_block_top_border, layout_block_top_border_with_title, layout_block_with_title,
|
||||
layout_with_constraints, logo_block, spans_info_default, spans_info_primary,
|
||||
spans_info_with_style, style_block_highlight, style_bold, style_default, style_default_bold,
|
||||
style_failure, style_help, style_highlight, style_primary, style_secondary, style_success,
|
||||
style_system_function, style_unmonitored, style_warning, title_block, title_block_centered,
|
||||
title_style, vertical_chunks, vertical_chunks_with_margin,
|
||||
};
|
||||
|
||||
#[test]
|
||||
fn test_horizontal_chunks() {
|
||||
let constraints = [
|
||||
Constraint::Percentage(10),
|
||||
Constraint::Max(20),
|
||||
Constraint::Min(10),
|
||||
Constraint::Length(30),
|
||||
Constraint::Ratio(3, 4),
|
||||
];
|
||||
let area = rect();
|
||||
let expected_layout = Layout::default()
|
||||
.constraints(constraints)
|
||||
.direction(Direction::Horizontal)
|
||||
.split(area);
|
||||
|
||||
assert_eq!(horizontal_chunks(constraints.into(), area), expected_layout);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_horizontal_chunks_with_margin() {
|
||||
let constraints = [
|
||||
Constraint::Percentage(10),
|
||||
Constraint::Max(20),
|
||||
Constraint::Min(10),
|
||||
Constraint::Length(30),
|
||||
Constraint::Ratio(3, 4),
|
||||
];
|
||||
let area = rect();
|
||||
let expected_layout = Layout::default()
|
||||
.constraints(constraints)
|
||||
.direction(Direction::Horizontal)
|
||||
.margin(1)
|
||||
.split(area);
|
||||
|
||||
assert_eq!(
|
||||
horizontal_chunks_with_margin(constraints.into(), area, 1),
|
||||
expected_layout
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_vertical_chunks() {
|
||||
let constraints = [
|
||||
Constraint::Percentage(10),
|
||||
Constraint::Max(20),
|
||||
Constraint::Min(10),
|
||||
Constraint::Length(30),
|
||||
Constraint::Ratio(3, 4),
|
||||
];
|
||||
let area = rect();
|
||||
let expected_layout = Layout::default()
|
||||
.constraints(constraints)
|
||||
.direction(Direction::Vertical)
|
||||
.split(area);
|
||||
|
||||
assert_eq!(vertical_chunks(constraints.into(), area), expected_layout);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_vertical_chunks_with_margin() {
|
||||
let constraints = [
|
||||
Constraint::Percentage(10),
|
||||
Constraint::Max(20),
|
||||
Constraint::Min(10),
|
||||
Constraint::Length(30),
|
||||
Constraint::Ratio(3, 4),
|
||||
];
|
||||
let area = rect();
|
||||
let expected_layout = Layout::default()
|
||||
.constraints(constraints)
|
||||
.direction(Direction::Vertical)
|
||||
.margin(1)
|
||||
.split(area);
|
||||
|
||||
assert_eq!(
|
||||
vertical_chunks_with_margin(constraints.into(), area, 1),
|
||||
expected_layout
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_layout_with_constraints() {
|
||||
let constraints = [
|
||||
Constraint::Percentage(10),
|
||||
Constraint::Max(20),
|
||||
Constraint::Min(10),
|
||||
Constraint::Length(30),
|
||||
Constraint::Ratio(3, 4),
|
||||
];
|
||||
let expected_layout = Layout::default().constraints(constraints);
|
||||
|
||||
assert_eq!(layout_with_constraints(constraints.into()), expected_layout);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_layout_block() {
|
||||
assert_eq!(
|
||||
layout_block(),
|
||||
Block::default()
|
||||
.borders(Borders::ALL)
|
||||
.border_type(BorderType::Rounded)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_layout_block_with_title() {
|
||||
let title_span = Span::styled(
|
||||
"title",
|
||||
Style::default()
|
||||
.fg(Color::DarkGray)
|
||||
.add_modifier(Modifier::BOLD),
|
||||
);
|
||||
let expected_block = Block::default()
|
||||
.borders(Borders::ALL)
|
||||
.border_type(BorderType::Rounded)
|
||||
.title(title_span.clone());
|
||||
|
||||
assert_eq!(layout_block_with_title(title_span), expected_block);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_layout_block_top_border_with_title() {
|
||||
let title_span = Span::styled(
|
||||
"title",
|
||||
Style::default()
|
||||
.fg(Color::DarkGray)
|
||||
.add_modifier(Modifier::BOLD),
|
||||
);
|
||||
let expected_block = Block::default()
|
||||
.borders(Borders::TOP)
|
||||
.title(title_span.clone());
|
||||
|
||||
assert_eq!(
|
||||
layout_block_top_border_with_title(title_span),
|
||||
expected_block
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_layout_block_top_border() {
|
||||
assert_eq!(
|
||||
layout_block_top_border(),
|
||||
Block::default().borders(Borders::TOP)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_layout_block_bottom_border() {
|
||||
assert_eq!(
|
||||
layout_block_bottom_border(),
|
||||
Block::default().borders(Borders::BOTTOM)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_borderless_block() {
|
||||
assert_eq!(borderless_block(), Block::default());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_spans_info_with_style() {
|
||||
let first_style = Style::default()
|
||||
.fg(Color::DarkGray)
|
||||
.add_modifier(Modifier::BOLD);
|
||||
let second_style = Style::default()
|
||||
.fg(Color::LightYellow)
|
||||
.add_modifier(Modifier::ITALIC);
|
||||
let expected_spans = Spans::from(vec![
|
||||
Span::styled("title".to_owned(), first_style),
|
||||
Span::styled("content".to_owned(), second_style),
|
||||
]);
|
||||
|
||||
assert_eq!(
|
||||
spans_info_with_style(
|
||||
"title".to_owned(),
|
||||
"content".to_owned(),
|
||||
first_style,
|
||||
second_style
|
||||
),
|
||||
expected_spans
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_spans_info_default() {
|
||||
let expected_spans = Spans::from(vec![
|
||||
Span::styled(
|
||||
"title".to_owned(),
|
||||
Style::default().add_modifier(Modifier::BOLD),
|
||||
),
|
||||
Span::styled("content".to_owned(), Style::default().fg(Color::White)),
|
||||
]);
|
||||
|
||||
assert_eq!(
|
||||
spans_info_default("title".to_owned(), "content".to_owned()),
|
||||
expected_spans
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_spans_info_primary() {
|
||||
let expected_spans = Spans::from(vec![
|
||||
Span::styled(
|
||||
"title".to_owned(),
|
||||
Style::default()
|
||||
.fg(Color::Cyan)
|
||||
.add_modifier(Modifier::BOLD),
|
||||
),
|
||||
Span::styled("content".to_owned(), Style::default().fg(Color::White)),
|
||||
]);
|
||||
|
||||
assert_eq!(
|
||||
spans_info_primary("title".to_owned(), "content".to_owned()),
|
||||
expected_spans
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_style_bold() {
|
||||
assert_eq!(style_bold(), Style::default().add_modifier(Modifier::BOLD));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_style_highlight() {
|
||||
assert_eq!(
|
||||
style_highlight(),
|
||||
Style::default().add_modifier(Modifier::REVERSED)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_style_unmonitored() {
|
||||
assert_eq!(style_unmonitored(), Style::default().fg(Color::White));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_style_default() {
|
||||
assert_eq!(style_default(), Style::default().fg(Color::White));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_style_default_bold() {
|
||||
assert_eq!(
|
||||
style_default_bold(),
|
||||
Style::default()
|
||||
.fg(Color::White)
|
||||
.add_modifier(Modifier::BOLD)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_style_primary() {
|
||||
assert_eq!(style_primary(), Style::default().fg(Color::Cyan));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_style_secondary() {
|
||||
assert_eq!(style_secondary(), Style::default().fg(Color::Yellow));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_style_system_function() {
|
||||
assert_eq!(style_system_function(), Style::default().fg(Color::Yellow));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_style_success() {
|
||||
assert_eq!(style_success(), Style::default().fg(Color::Green));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_style_warning() {
|
||||
assert_eq!(style_warning(), Style::default().fg(Color::Magenta));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_style_failure() {
|
||||
assert_eq!(style_failure(), Style::default().fg(Color::Red));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_style_help() {
|
||||
assert_eq!(style_help(), Style::default().fg(Color::LightBlue));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_style_button_highlight_selected() {
|
||||
let expected_style = Style::default()
|
||||
.fg(Color::Yellow)
|
||||
.add_modifier(Modifier::BOLD);
|
||||
|
||||
assert_eq!(style_block_highlight(true), expected_style);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_style_button_highlight_unselected() {
|
||||
let expected_style = Style::default()
|
||||
.fg(Color::White)
|
||||
.add_modifier(Modifier::BOLD);
|
||||
|
||||
assert_eq!(style_block_highlight(false), expected_style);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_title_style() {
|
||||
let expected_span = Span::styled(" test ", Style::default().add_modifier(Modifier::BOLD));
|
||||
|
||||
assert_eq!(title_style("test"), expected_span);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_title_block() {
|
||||
let expected_block = Block::default()
|
||||
.borders(Borders::ALL)
|
||||
.border_type(BorderType::Rounded)
|
||||
.title(Span::styled(
|
||||
" test ",
|
||||
Style::default().add_modifier(Modifier::BOLD),
|
||||
));
|
||||
|
||||
assert_eq!(title_block("test"), expected_block);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_title_block_centered() {
|
||||
let expected_block = Block::default()
|
||||
.borders(Borders::ALL)
|
||||
.border_type(BorderType::Rounded)
|
||||
.title(Span::styled(
|
||||
" test ",
|
||||
Style::default().add_modifier(Modifier::BOLD),
|
||||
))
|
||||
.title_alignment(Alignment::Center);
|
||||
|
||||
assert_eq!(title_block_centered("test"), expected_block);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_logo_block() {
|
||||
let expected_block = Block::default()
|
||||
.borders(Borders::ALL)
|
||||
.border_type(BorderType::Rounded)
|
||||
.title(Span::styled(
|
||||
" Managarr - A Servarr management TUI ",
|
||||
Style::default()
|
||||
.fg(Color::Magenta)
|
||||
.add_modifier(Modifier::BOLD)
|
||||
.add_modifier(Modifier::ITALIC),
|
||||
));
|
||||
|
||||
assert_eq!(logo_block(), expected_block);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_centered_rect() {
|
||||
let expected_rect = Rect {
|
||||
x: 30,
|
||||
y: 45,
|
||||
width: 60,
|
||||
height: 90,
|
||||
};
|
||||
|
||||
assert_eq!(centered_rect(50, 50, rect()), expected_rect);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_get_width_from_percentage() {
|
||||
assert_eq!(
|
||||
get_width_from_percentage(
|
||||
Rect {
|
||||
x: 0,
|
||||
y: 0,
|
||||
width: 100,
|
||||
height: 10
|
||||
},
|
||||
30
|
||||
),
|
||||
30
|
||||
);
|
||||
}
|
||||
|
||||
fn rect() -> Rect {
|
||||
Rect {
|
||||
x: 0,
|
||||
y: 0,
|
||||
width: 120,
|
||||
height: 180,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,409 @@
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use pretty_assertions::assert_eq;
|
||||
use tui::layout::{Alignment, Constraint, Direction, Layout, Rect};
|
||||
use tui::style::{Color, Modifier, Style};
|
||||
use tui::text::{Span, Spans};
|
||||
use tui::widgets::{Block, BorderType, Borders};
|
||||
|
||||
use crate::ui::utils::{
|
||||
borderless_block, centered_rect, get_width_from_percentage, horizontal_chunks,
|
||||
horizontal_chunks_with_margin, layout_block, layout_block_bottom_border,
|
||||
layout_block_top_border, layout_block_top_border_with_title, layout_block_with_title,
|
||||
layout_with_constraints, logo_block, spans_info_default, spans_info_primary,
|
||||
spans_info_with_style, style_block_highlight, style_bold, style_default, style_default_bold,
|
||||
style_failure, style_help, style_highlight, style_primary, style_secondary, style_success,
|
||||
style_system_function, style_unmonitored, style_warning, title_block, title_block_centered,
|
||||
title_style, vertical_chunks, vertical_chunks_with_margin,
|
||||
};
|
||||
|
||||
#[test]
|
||||
fn test_horizontal_chunks() {
|
||||
let constraints = [
|
||||
Constraint::Percentage(10),
|
||||
Constraint::Max(20),
|
||||
Constraint::Min(10),
|
||||
Constraint::Length(30),
|
||||
Constraint::Ratio(3, 4),
|
||||
];
|
||||
let area = rect();
|
||||
let expected_layout = Layout::default()
|
||||
.constraints(constraints)
|
||||
.direction(Direction::Horizontal)
|
||||
.split(area);
|
||||
|
||||
assert_eq!(horizontal_chunks(constraints.into(), area), expected_layout);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_horizontal_chunks_with_margin() {
|
||||
let constraints = [
|
||||
Constraint::Percentage(10),
|
||||
Constraint::Max(20),
|
||||
Constraint::Min(10),
|
||||
Constraint::Length(30),
|
||||
Constraint::Ratio(3, 4),
|
||||
];
|
||||
let area = rect();
|
||||
let expected_layout = Layout::default()
|
||||
.constraints(constraints)
|
||||
.direction(Direction::Horizontal)
|
||||
.margin(1)
|
||||
.split(area);
|
||||
|
||||
assert_eq!(
|
||||
horizontal_chunks_with_margin(constraints.into(), area, 1),
|
||||
expected_layout
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_vertical_chunks() {
|
||||
let constraints = [
|
||||
Constraint::Percentage(10),
|
||||
Constraint::Max(20),
|
||||
Constraint::Min(10),
|
||||
Constraint::Length(30),
|
||||
Constraint::Ratio(3, 4),
|
||||
];
|
||||
let area = rect();
|
||||
let expected_layout = Layout::default()
|
||||
.constraints(constraints)
|
||||
.direction(Direction::Vertical)
|
||||
.split(area);
|
||||
|
||||
assert_eq!(vertical_chunks(constraints.into(), area), expected_layout);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_vertical_chunks_with_margin() {
|
||||
let constraints = [
|
||||
Constraint::Percentage(10),
|
||||
Constraint::Max(20),
|
||||
Constraint::Min(10),
|
||||
Constraint::Length(30),
|
||||
Constraint::Ratio(3, 4),
|
||||
];
|
||||
let area = rect();
|
||||
let expected_layout = Layout::default()
|
||||
.constraints(constraints)
|
||||
.direction(Direction::Vertical)
|
||||
.margin(1)
|
||||
.split(area);
|
||||
|
||||
assert_eq!(
|
||||
vertical_chunks_with_margin(constraints.into(), area, 1),
|
||||
expected_layout
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_layout_with_constraints() {
|
||||
let constraints = [
|
||||
Constraint::Percentage(10),
|
||||
Constraint::Max(20),
|
||||
Constraint::Min(10),
|
||||
Constraint::Length(30),
|
||||
Constraint::Ratio(3, 4),
|
||||
];
|
||||
let expected_layout = Layout::default().constraints(constraints);
|
||||
|
||||
assert_eq!(layout_with_constraints(constraints.into()), expected_layout);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_layout_block() {
|
||||
assert_eq!(
|
||||
layout_block(),
|
||||
Block::default()
|
||||
.borders(Borders::ALL)
|
||||
.border_type(BorderType::Rounded)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_layout_block_with_title() {
|
||||
let title_span = Span::styled(
|
||||
"title",
|
||||
Style::default()
|
||||
.fg(Color::DarkGray)
|
||||
.add_modifier(Modifier::BOLD),
|
||||
);
|
||||
let expected_block = Block::default()
|
||||
.borders(Borders::ALL)
|
||||
.border_type(BorderType::Rounded)
|
||||
.title(title_span.clone());
|
||||
|
||||
assert_eq!(layout_block_with_title(title_span), expected_block);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_layout_block_top_border_with_title() {
|
||||
let title_span = Span::styled(
|
||||
"title",
|
||||
Style::default()
|
||||
.fg(Color::DarkGray)
|
||||
.add_modifier(Modifier::BOLD),
|
||||
);
|
||||
let expected_block = Block::default()
|
||||
.borders(Borders::TOP)
|
||||
.title(title_span.clone());
|
||||
|
||||
assert_eq!(
|
||||
layout_block_top_border_with_title(title_span),
|
||||
expected_block
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_layout_block_top_border() {
|
||||
assert_eq!(
|
||||
layout_block_top_border(),
|
||||
Block::default().borders(Borders::TOP)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_layout_block_bottom_border() {
|
||||
assert_eq!(
|
||||
layout_block_bottom_border(),
|
||||
Block::default().borders(Borders::BOTTOM)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_borderless_block() {
|
||||
assert_eq!(borderless_block(), Block::default());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_spans_info_with_style() {
|
||||
let first_style = Style::default()
|
||||
.fg(Color::DarkGray)
|
||||
.add_modifier(Modifier::BOLD);
|
||||
let second_style = Style::default()
|
||||
.fg(Color::LightYellow)
|
||||
.add_modifier(Modifier::ITALIC);
|
||||
let expected_spans = Spans::from(vec![
|
||||
Span::styled("title".to_owned(), first_style),
|
||||
Span::styled("content".to_owned(), second_style),
|
||||
]);
|
||||
|
||||
assert_eq!(
|
||||
spans_info_with_style(
|
||||
"title".to_owned(),
|
||||
"content".to_owned(),
|
||||
first_style,
|
||||
second_style
|
||||
),
|
||||
expected_spans
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_spans_info_default() {
|
||||
let expected_spans = Spans::from(vec![
|
||||
Span::styled(
|
||||
"title".to_owned(),
|
||||
Style::default().add_modifier(Modifier::BOLD),
|
||||
),
|
||||
Span::styled("content".to_owned(), Style::default().fg(Color::White)),
|
||||
]);
|
||||
|
||||
assert_eq!(
|
||||
spans_info_default("title".to_owned(), "content".to_owned()),
|
||||
expected_spans
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_spans_info_primary() {
|
||||
let expected_spans = Spans::from(vec![
|
||||
Span::styled(
|
||||
"title".to_owned(),
|
||||
Style::default()
|
||||
.fg(Color::Cyan)
|
||||
.add_modifier(Modifier::BOLD),
|
||||
),
|
||||
Span::styled("content".to_owned(), Style::default().fg(Color::White)),
|
||||
]);
|
||||
|
||||
assert_eq!(
|
||||
spans_info_primary("title".to_owned(), "content".to_owned()),
|
||||
expected_spans
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_style_bold() {
|
||||
assert_eq!(style_bold(), Style::default().add_modifier(Modifier::BOLD));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_style_highlight() {
|
||||
assert_eq!(
|
||||
style_highlight(),
|
||||
Style::default().add_modifier(Modifier::REVERSED)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_style_unmonitored() {
|
||||
assert_eq!(style_unmonitored(), Style::default().fg(Color::White));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_style_default() {
|
||||
assert_eq!(style_default(), Style::default().fg(Color::White));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_style_default_bold() {
|
||||
assert_eq!(
|
||||
style_default_bold(),
|
||||
Style::default()
|
||||
.fg(Color::White)
|
||||
.add_modifier(Modifier::BOLD)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_style_primary() {
|
||||
assert_eq!(style_primary(), Style::default().fg(Color::Cyan));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_style_secondary() {
|
||||
assert_eq!(style_secondary(), Style::default().fg(Color::Yellow));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_style_system_function() {
|
||||
assert_eq!(style_system_function(), Style::default().fg(Color::Yellow));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_style_success() {
|
||||
assert_eq!(style_success(), Style::default().fg(Color::Green));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_style_warning() {
|
||||
assert_eq!(style_warning(), Style::default().fg(Color::Magenta));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_style_failure() {
|
||||
assert_eq!(style_failure(), Style::default().fg(Color::Red));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_style_help() {
|
||||
assert_eq!(style_help(), Style::default().fg(Color::LightBlue));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_style_button_highlight_selected() {
|
||||
let expected_style = Style::default()
|
||||
.fg(Color::Yellow)
|
||||
.add_modifier(Modifier::BOLD);
|
||||
|
||||
assert_eq!(style_block_highlight(true), expected_style);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_style_button_highlight_unselected() {
|
||||
let expected_style = Style::default()
|
||||
.fg(Color::White)
|
||||
.add_modifier(Modifier::BOLD);
|
||||
|
||||
assert_eq!(style_block_highlight(false), expected_style);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_title_style() {
|
||||
let expected_span = Span::styled(" test ", Style::default().add_modifier(Modifier::BOLD));
|
||||
|
||||
assert_eq!(title_style("test"), expected_span);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_title_block() {
|
||||
let expected_block = Block::default()
|
||||
.borders(Borders::ALL)
|
||||
.border_type(BorderType::Rounded)
|
||||
.title(Span::styled(
|
||||
" test ",
|
||||
Style::default().add_modifier(Modifier::BOLD),
|
||||
));
|
||||
|
||||
assert_eq!(title_block("test"), expected_block);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_title_block_centered() {
|
||||
let expected_block = Block::default()
|
||||
.borders(Borders::ALL)
|
||||
.border_type(BorderType::Rounded)
|
||||
.title(Span::styled(
|
||||
" test ",
|
||||
Style::default().add_modifier(Modifier::BOLD),
|
||||
))
|
||||
.title_alignment(Alignment::Center);
|
||||
|
||||
assert_eq!(title_block_centered("test"), expected_block);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_logo_block() {
|
||||
let expected_block = Block::default()
|
||||
.borders(Borders::ALL)
|
||||
.border_type(BorderType::Rounded)
|
||||
.title(Span::styled(
|
||||
" Managarr - A Servarr management TUI ",
|
||||
Style::default()
|
||||
.fg(Color::Magenta)
|
||||
.add_modifier(Modifier::BOLD)
|
||||
.add_modifier(Modifier::ITALIC),
|
||||
));
|
||||
|
||||
assert_eq!(logo_block(), expected_block);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_centered_rect() {
|
||||
let expected_rect = Rect {
|
||||
x: 30,
|
||||
y: 45,
|
||||
width: 60,
|
||||
height: 90,
|
||||
};
|
||||
|
||||
assert_eq!(centered_rect(50, 50, rect()), expected_rect);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_get_width_from_percentage() {
|
||||
assert_eq!(
|
||||
get_width_from_percentage(
|
||||
Rect {
|
||||
x: 0,
|
||||
y: 0,
|
||||
width: 100,
|
||||
height: 10
|
||||
},
|
||||
30
|
||||
),
|
||||
30
|
||||
);
|
||||
}
|
||||
|
||||
fn rect() -> Rect {
|
||||
Rect {
|
||||
x: 0,
|
||||
y: 0,
|
||||
width: 120,
|
||||
height: 180,
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user