Added better support for contexts now and improved base Radarr UI

This commit is contained in:
2023-08-08 10:50:04 -06:00
parent 9276fb474d
commit d39acb0683
11 changed files with 407 additions and 153 deletions
+7 -111
View File
@@ -1,15 +1,9 @@
use std::ops::Sub;
use chrono::{Duration, Utc};
use tui::{Frame, symbols};
use tui::backend::Backend;
use tui::layout::{Alignment, Constraint, Rect};
use tui::style::{Color, Modifier, Style};
use tui::text::{Span, Spans, Text};
use tui::widgets::{Block, Borders, Cell, LineGauge, Paragraph, Row, Table};
use tui::Frame;
use tui::layout::{Constraint, Rect};
use tui::widgets::{Block, Borders};
use crate::app::App;
use crate::app::radarr::RadarrData;
use crate::logos::{
BAZARR_LOGO, LIDARR_LOGO, PROWLARR_LOGO, RADARR_LOGO, READARR_LOGO, SONARR_LOGO,
};
@@ -18,6 +12,7 @@ use crate::ui::utils::{
};
mod utils;
mod radarr_ui;
static HIGHLIGHT_SYMBOL: &str = "=> ";
@@ -29,7 +24,7 @@ pub fn ui<B: Backend>(f: &mut Frame<B>, app: &mut App) {
);
draw_context_row(f, app, main_chunks[0]);
draw_radarr_ui(f, app, main_chunks[1]);
radarr_ui::draw_radarr_ui(f, app, main_chunks[1]);
}
fn draw_context_row<B: Backend>(f: &mut Frame<'_, B>, app: &App, area: Rect) {
@@ -44,109 +39,10 @@ fn draw_context_row<B: Backend>(f: &mut Frame<'_, B>, app: &App, area: Rect) {
area,
);
draw_stats(f, app, chunks[0]);
radarr_ui::draw_stats(f, app, chunks[0]);
f.render_widget(Block::default().borders(Borders::ALL), chunks[1]);
f.render_widget(Block::default().borders(Borders::ALL), chunks[2]);
f.render_widget(Block::default().borders(Borders::ALL), chunks[3]);
draw_logo(f, chunks[4]);
}
fn draw_radarr_ui<B: Backend>(f: &mut Frame<'_, B>, app: &mut App, area: Rect) {
let block = Block::default().borders(Borders::ALL).title(Spans::from(vec![
Span::styled("Movies", Style::default().fg(Color::Yellow).add_modifier(Modifier::BOLD))
]));
let row_style = Style::default().fg(Color::Cyan);
let rows = app.data.radarr_data.movies.items
.iter()
.map(|movie| Row::new(vec![
Cell::from(movie.title.to_owned()),
Cell::from(movie.year.to_string()),
Cell::from(movie.monitored.to_string()),
Cell::from(movie.has_file.to_string())
]).style(row_style));
let header_row = Row::new(vec!["Title", "Year", "Monitored", "Downloaded"])
.style(Style::default().fg(Color::White))
.bottom_margin(0);
let constraints = vec![
Constraint::Percentage(25),
Constraint::Percentage(25),
Constraint::Percentage(25),
Constraint::Percentage(25),
];
let table = Table::new(rows)
.header(header_row)
.block(block)
.highlight_style(Style::default().add_modifier(Modifier::REVERSED))
.highlight_symbol(HIGHLIGHT_SYMBOL)
.widths(&constraints);
f.render_stateful_widget(table, area, &mut app.data.radarr_data.movies.state)
}
fn draw_stats<B: Backend>(f: &mut Frame<'_, B>, app: &App, area: Rect) {
let RadarrData {
free_space,
total_space,
start_time,
..
} = app.data.radarr_data;
let ratio = if total_space == 0 {
0f64
} else {
1f64 - (free_space as f64 / total_space as f64)
};
let base_block = Block::default().title("Stats").borders(Borders::ALL);
f.render_widget(base_block, area);
let chunks =
vertical_chunks_with_margin(vec![
Constraint::Length(1),
Constraint::Length(1),
Constraint::Min(2)], area, 1);
let version_paragraph = Paragraph::new(Text::from(format!(
"Radarr Version: {}",
app.data.radarr_data.version
)))
.block(Block::default());
let uptime = Utc::now().sub(start_time);
let days = uptime.num_days();
let day_difference = uptime.sub(Duration::days(days));
let hours = day_difference.num_hours();
let hour_difference = day_difference.sub(Duration::hours(hours));
let minutes = hour_difference.num_minutes();
let seconds = hour_difference.sub(Duration::minutes(minutes)).num_seconds();
let uptime_paragraph = Paragraph::new(Text::from(format!(
"Uptime: {}d {:0width$}:{:0width$}:{:0width$}",
days, hours, minutes, seconds, width = 2
))).block(Block::default());
let space_gauge = LineGauge::default()
.block(Block::default().title("Storage:"))
.gauge_style(Style::default().fg(Color::Cyan))
.line_set(symbols::line::THICK)
.ratio(ratio)
.label(Spans::from(format!("{:.0}%", ratio * 100.0)));
f.render_widget(version_paragraph, chunks[0]);
f.render_widget(uptime_paragraph, chunks[1]);
f.render_widget(space_gauge, chunks[2]);
}
fn draw_logo<B: Backend>(f: &mut Frame<'_, B>, area: Rect) {
let chunks = vertical_chunks(
vec![Constraint::Percentage(60), Constraint::Percentage(40)],
area,
);
let logo = Paragraph::new(Text::from(RADARR_LOGO))
.block(Block::default())
.alignment(Alignment::Center);
f.render_widget(logo, chunks[0]);
radarr_ui::draw_logo(f, chunks[4]);
}
+138
View File
@@ -0,0 +1,138 @@
use std::ops::Sub;
use chrono::{Duration, Utc};
use tui::{Frame, symbols};
use tui::backend::Backend;
use tui::layout::{Alignment, Constraint, Rect};
use tui::style::{Color, Modifier, Style};
use tui::text::{Span, Spans, Text};
use tui::widgets::{Block, Borders, Cell, LineGauge, Paragraph, Row, Table};
use crate::app::App;
use crate::app::radarr::RadarrData;
use crate::logos::RADARR_LOGO;
use crate::network::radarr_network::Movie;
use crate::ui::HIGHLIGHT_SYMBOL;
use crate::ui::utils::{style_default, style_highlight, style_primary, style_secondary, style_tertiary, title_block, vertical_chunks, vertical_chunks_with_margin};
pub(super) fn draw_radarr_ui<B: Backend>(f: &mut Frame<'_, B>, app: &mut App, area: Rect) {
let block = Block::default().borders(Borders::ALL).title(Spans::from(vec![
Span::styled("Movies", Style::default().fg(Color::Yellow).add_modifier(Modifier::BOLD))
]));
let rows = app.data.radarr_data.movies.items
.iter()
.map(|movie| {
let runtime = movie.runtime.as_u64().unwrap();
let hours = runtime / 60;
let minutes = runtime % 60;
let file_size: f64 = movie.size_on_disk.as_u64().unwrap() as f64 / 1024f64.powi(3);
Row::new(vec![
Cell::from(movie.title.to_owned()),
Cell::from(movie.year.to_string()),
Cell::from(format!("{:0width$}:{:0width$}", hours, minutes, width = 2)),
Cell::from(format!("{:.2} GB", file_size)),
Cell::from(app.data.radarr_data.quality_profile_map.get(&movie.quality_profile_id.as_u64().unwrap()).unwrap().to_owned())
]).style(determine_row_style(app, movie))
});
let header_row = Row::new(vec!["Title", "Year", "Runtime", "Size", "Quality Profile"])
.style(style_default())
.bottom_margin(0);
let constraints = vec![
Constraint::Percentage(20),
Constraint::Percentage(20),
Constraint::Percentage(20),
Constraint::Percentage(20),
Constraint::Percentage(20),
];
let table = Table::new(rows)
.header(header_row)
.block(block)
.highlight_style(style_highlight())
.highlight_symbol(HIGHLIGHT_SYMBOL)
.widths(&constraints);
f.render_stateful_widget(table, area, &mut app.data.radarr_data.movies.state)
}
pub(super) fn draw_stats<B: Backend>(f: &mut Frame<'_, B>, app: &App, area: Rect) {
let RadarrData {
free_space,
total_space,
start_time,
..
} = app.data.radarr_data;
let ratio = if total_space == 0 {
0f64
} else {
1f64 - (free_space as f64 / total_space as f64)
};
f.render_widget(title_block("Stats"), area);
let chunks =
vertical_chunks_with_margin(vec![
Constraint::Length(1),
Constraint::Length(1),
Constraint::Min(2)], area, 1);
let version_paragraph = Paragraph::new(Text::from(format!(
"Radarr Version: {}",
app.data.radarr_data.version
))).block(Block::default());
let uptime = Utc::now().sub(start_time);
let days = uptime.num_days();
let day_difference = uptime.sub(Duration::days(days));
let hours = day_difference.num_hours();
let hour_difference = day_difference.sub(Duration::hours(hours));
let minutes = hour_difference.num_minutes();
let seconds = hour_difference.sub(Duration::minutes(minutes)).num_seconds();
let uptime_paragraph = Paragraph::new(Text::from(format!(
"Uptime: {}d {:0width$}:{:0width$}:{:0width$}",
days, hours, minutes, seconds, width = 2
))).block(Block::default());
let space_gauge = LineGauge::default()
.block(Block::default().title("Storage:"))
.gauge_style(Style::default().fg(Color::Cyan))
.line_set(symbols::line::THICK)
.ratio(ratio)
.label(Spans::from(format!("{:.0}%", ratio * 100.0)));
f.render_widget(version_paragraph, chunks[0]);
f.render_widget(uptime_paragraph, chunks[1]);
f.render_widget(space_gauge, chunks[2]);
}
pub(super) fn draw_logo<B: Backend>(f: &mut Frame<'_, B>, area: Rect) {
let chunks = vertical_chunks(
vec![Constraint::Percentage(60), Constraint::Percentage(40)],
area,
);
let logo = Paragraph::new(Text::from(RADARR_LOGO))
.block(Block::default())
.alignment(Alignment::Center);
f.render_widget(logo, chunks[0]);
}
fn determine_row_style(app: &App, movie: &Movie) -> Style {
let downloads_vec = &app.data.radarr_data.downloads.items;
if !movie.has_file {
if let Some(download) = downloads_vec.iter()
.find(|&download | download.movie_id == movie.id) {
if download.status == "downloading" {
return style_secondary()
}
}
return style_tertiary()
}
style_primary()
}
+39
View File
@@ -1,4 +1,7 @@
use tui::layout::{Constraint, Direction, Layout, Rect};
use tui::style::{Color, Modifier, Style};
use tui::text::Span;
use tui::widgets::{Block, Borders};
pub fn horizontal_chunks(constraints: Vec<Constraint>, size: Rect) -> Vec<Rect> {
Layout::default()
@@ -45,3 +48,39 @@ pub fn vertical_chunks_with_margin(
.margin(margin)
.split(size)
}
pub fn layout_block(title_span: Span<'_>) -> Block<'_> {
Block::default().borders(Borders::ALL).title(title_span)
}
pub fn style_bold() -> Style {
Style::default().add_modifier(Modifier::BOLD)
}
pub fn style_highlight() -> Style {
Style::default().add_modifier(Modifier::REVERSED)
}
pub fn style_default() -> Style {
Style::default().fg(Color::White)
}
pub fn style_primary() -> Style {
Style::default().fg(Color::Green)
}
pub fn style_secondary() -> Style {
Style::default().fg(Color::Magenta)
}
pub fn style_tertiary() -> Style {
Style::default().fg(Color::Red)
}
pub fn title_style(title: &str) -> Span<'_> {
Span::styled(title, style_bold())
}
pub fn title_block(title: &str) -> Block<'_> {
layout_block(title_style(title))
}