Files
managarr/src/ui/utils.rs

119 lines
3.1 KiB
Rust

use crate::ui::styles::ManagarrStyle;
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};
pub const COLOR_TEAL: Color = Color::Rgb(35, 50, 55);
#[cfg(test)]
#[path = "utils_tests.rs"]
mod utils_tests;
pub fn background_block<'a>() -> Block<'a> {
Block::new().white().bg(COLOR_TEAL)
}
pub fn layout_block<'a>() -> Block<'a> {
Block::new()
.borders(Borders::ALL)
.border_type(BorderType::Rounded)
}
pub fn layout_block_with_title(title_span: Span<'_>) -> Block<'_> {
layout_block().title(title_span)
}
pub fn layout_block_top_border_with_title(title_span: Span<'_>) -> Block<'_> {
layout_block_top_border().title(title_span)
}
pub fn layout_block_top_border<'a>() -> Block<'a> {
Block::new().borders(Borders::TOP)
}
pub fn layout_block_bottom_border<'a>() -> Block<'a> {
Block::new().borders(Borders::BOTTOM)
}
pub fn layout_paragraph_borderless(string: &str) -> Paragraph<'_> {
Paragraph::new(Text::from(string))
.block(borderless_block())
.primary()
.bold()
.wrap(Wrap { trim: false })
.centered()
}
pub fn borderless_block<'a>() -> Block<'a> {
Block::new()
}
pub fn style_block_highlight(is_selected: bool) -> Style {
if is_selected {
Style::new().system_function().bold()
} else {
Style::new().default().bold()
}
}
pub fn title_style(title: &str) -> Span<'_> {
format!(" {title} ").bold()
}
pub fn title_block(title: &str) -> Block<'_> {
layout_block_with_title(title_style(title))
}
pub fn title_block_centered(title: &str) -> Block<'_> {
title_block(title).title_alignment(Alignment::Center)
}
pub fn logo_block<'a>() -> Block<'a> {
layout_block().title(Span::styled(
" Managarr - A Servarr management TUI ",
Style::new().magenta().bold().italic(),
))
}
pub fn centered_rect(percent_x: u16, percent_y: u16, area: Rect) -> Rect {
let [_, vertical_area, _] = Layout::vertical([
Constraint::Percentage((100 - percent_y) / 2),
Constraint::Percentage(percent_y),
Constraint::Percentage((100 - percent_y) / 2),
])
.areas(area);
let [_, horizontal_layout, _] = Layout::horizontal([
Constraint::Percentage((100 - percent_x) / 2),
Constraint::Percentage(percent_x),
Constraint::Percentage((100 - percent_x) / 2),
])
.areas(vertical_area);
horizontal_layout
}
pub fn line_gauge_with_title(title: &str, ratio: f64) -> LineGauge<'_> {
LineGauge::new()
.block(Block::new().title(title))
.filled_style(Style::new().cyan())
.line_set(symbols::line::THICK)
.ratio(ratio)
.label(Line::from(format!("{:.0}%", ratio * 100.0)))
}
pub fn line_gauge_with_label(title: &str, ratio: f64) -> LineGauge<'_> {
LineGauge::new()
.block(Block::new())
.filled_style(Style::new().cyan())
.line_set(symbols::line::THICK)
.ratio(ratio)
.label(Line::from(format!("{title}: {:.0}%", ratio * 100.0)))
}
pub fn get_width_from_percentage(area: Rect, percentage: u16) -> usize {
(area.width as f64 * (percentage as f64 / 100.0)) as usize
}