From 96fe87fc695651ce4c952a6ca8e26aa915808439 Mon Sep 17 00:00:00 2001 From: 72-S Date: Sun, 16 Feb 2025 21:22:45 +0100 Subject: [PATCH] feat(tui): add per-widget config, keybinds, and scrollbar --- src/cli.rs | 5 +- src/config/mod.rs | 4 +- src/tui/layout.rs | 31 ++++---- src/tui/mod.rs | 1 - src/tui/widget/mod.rs | 0 src/tui/widgets.rs | 120 ----------------------------- src/tui/widgets/account.rs | 27 +++++++ src/tui/widgets/content.rs | 44 +++++++++++ src/tui/widgets/details.rs | 27 +++++++ src/tui/widgets/instances.rs | 142 +++++++++++++++++++++++++++++++++++ src/tui/widgets/mod.rs | 29 +++++++ src/tui/widgets/status.rs | 27 +++++++ 12 files changed, 318 insertions(+), 139 deletions(-) delete mode 100644 src/tui/widget/mod.rs delete mode 100644 src/tui/widgets.rs create mode 100644 src/tui/widgets/account.rs create mode 100644 src/tui/widgets/content.rs create mode 100644 src/tui/widgets/details.rs create mode 100644 src/tui/widgets/instances.rs create mode 100644 src/tui/widgets/mod.rs create mode 100644 src/tui/widgets/status.rs diff --git a/src/cli.rs b/src/cli.rs index 0c75321..b50170e 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -79,7 +79,7 @@ pub fn init() { .get_matches(); if matches.subcommand().is_none() { - open_tui(); + tui::show().unwrap() } match matches.subcommand() { @@ -127,7 +127,4 @@ pub fn init() { _ => {}, } - fn open_tui() { - tui::show().unwrap(); - } } diff --git a/src/config/mod.rs b/src/config/mod.rs index a14f23d..33a597b 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -4,6 +4,8 @@ use std::fs; use std::path::PathBuf; use obj::Config; +use crate::debug; + pub mod obj; fn get_config_path() -> PathBuf { @@ -20,7 +22,7 @@ pub fn ensure_config_exists(default_path: &str) -> PathBuf { } fs::copy(default_path, &config_path).expect("Failed to copy default configuration file"); - println!("Default configuration copied to '{}'", config_path.display()); + debug!("Default configuration copied to '{}'", config_path.display()); } config_path diff --git a/src/tui/layout.rs b/src/tui/layout.rs index 8ca386b..45fe4a6 100644 --- a/src/tui/layout.rs +++ b/src/tui/layout.rs @@ -1,5 +1,4 @@ -use super::widgets; -use super::Tui; +use super::{widgets::{self, instances, WidgetKey}, Tui}; use color_eyre::eyre::Context; use crossterm::event::{self, Event}; use ratatui::{ @@ -12,6 +11,7 @@ use ratatui::{ pub struct App { exit: bool, focused: FocusedArea, + instances_state: instances::State, } #[derive(Debug, Clone, Copy, PartialEq)] @@ -38,18 +38,21 @@ impl App { } Ok(()) } - fn render_frame(&self, frame: &mut Frame) { + fn render_frame(&mut self, frame: &mut Frame) { // Divide the screen into horizontal chunks let chunks = Layout::default() .direction(Direction::Horizontal) .constraints([ Constraint::Percentage(20), // Instances Constraint::Percentage(80), // Main content + Constraint::Min(1), ]) .split(frame.area()); + // Render Instances - widgets::render_instances(frame, chunks[0], self.focused); + widgets::instances::render(frame, chunks[0], self.focused, &mut self.instances_state); + // Divide the main content into vertical chunks let main_chunks = Layout::default() @@ -62,8 +65,8 @@ impl App { .split(chunks[1]); // Render widgets in the main content - widgets::render_title(frame, main_chunks[0], self.focused); - widgets::render_content(frame, main_chunks[1], self.focused); + widgets::content::title(frame, main_chunks[0], self.focused); + widgets::content::render(frame, main_chunks[1], self.focused); // Bottom panel split let bottom_chunks = Layout::default() @@ -76,9 +79,9 @@ impl App { .split(main_chunks[2]); // Render bottom widgets - widgets::render_account(frame, bottom_chunks[0], self.focused); - widgets::render_details(frame, bottom_chunks[1], self.focused); - widgets::render_status(frame, bottom_chunks[2], self.focused); + widgets::account::render(frame, bottom_chunks[0], self.focused); + widgets::details::render(frame, bottom_chunks[1], self.focused); + widgets::status::render(frame, bottom_chunks[2], self.focused); } /// updates the applications state based on user input @@ -93,7 +96,7 @@ impl App { fn handle_key_event(&mut self, key_event: KeyEvent) -> color_eyre::Result<()> { match key_event.code { - KeyCode::Char('q') => self.exit(), + KeyCode::Char('q') => self.exit = true, KeyCode::Char('I') => self.focused = FocusedArea::Instances, KeyCode::Char('C') => self.focused = FocusedArea::Content, KeyCode::Char('A') => self.focused = FocusedArea::Account, @@ -101,10 +104,12 @@ impl App { KeyCode::Char('S') => self.focused = FocusedArea::Status, _ => {} } + + match self.focused { + FocusedArea::Instances => self.instances_state.handle_key(&key_event), + _ => {} + } Ok(()) } - fn exit(&mut self) { - self.exit = true; - } } diff --git a/src/tui/mod.rs b/src/tui/mod.rs index 7bb89e3..0ed58ec 100644 --- a/src/tui/mod.rs +++ b/src/tui/mod.rs @@ -1,5 +1,4 @@ pub mod layout; -pub mod widget; pub mod widgets; use std::io::{stdout, Result, Stdout}; diff --git a/src/tui/widget/mod.rs b/src/tui/widget/mod.rs deleted file mode 100644 index e69de29..0000000 diff --git a/src/tui/widgets.rs b/src/tui/widgets.rs deleted file mode 100644 index 5c88551..0000000 --- a/src/tui/widgets.rs +++ /dev/null @@ -1,120 +0,0 @@ -use ratatui::{ - layout::Rect, - style::{Color, Style}, - text::{Line, Span}, - widgets::{Block, BorderType, Borders, Paragraph}, - Frame, -}; - -use super::layout::FocusedArea; - -fn styled_title(title: &str, highlight: bool) -> Line { - if !highlight || title.is_empty() { - Line::from(Span::raw(title)) - } else { - let mut chars = title.chars(); - let first = chars.next().unwrap_or_default().to_string(); - let rest: String = chars.collect(); - Line::from(vec![ - Span::styled(first, Style::default().fg(Color::Yellow)), - Span::raw(rest), - ]) - } -} - -fn render_section( - frame: &mut Frame, - area: Rect, - focused: FocusedArea, - title: &str, - content: &str, - focus_area: FocusedArea, - highlight: bool, -) { - let color = if focused == focus_area { - Color::White - } else { - Color::DarkGray - }; - - let block = Block::default() - .title(styled_title(title, highlight)) - .borders(Borders::ALL) - .border_type(BorderType::Rounded) - .border_style(Style::default().fg(color)); - - let widget = Paragraph::new(content).block(block); - frame.render_widget(widget, area); -} - -pub fn render_instances(frame: &mut Frame, area: Rect, focused: FocusedArea) { - render_section( - frame, - area, - focused, - "Instances", - "", - FocusedArea::Instances, - true, - ); -} - -pub fn render_title(frame: &mut Frame, area: Rect, focused: FocusedArea) { - render_section( - frame, - area, - focused, - "Title", - " TEST INSTANCE NAME", - FocusedArea::Content, - false, - ); -} - -pub fn render_content(frame: &mut Frame, area: Rect, focused: FocusedArea) { - render_section( - frame, - area, - focused, - "Content", - " TEST CONTENT LIBRARY PAGE", - FocusedArea::Content, - true, - ); -} - -pub fn render_account(frame: &mut Frame, area: Rect, focused: FocusedArea) { - render_section( - frame, - area, - focused, - "Account", - "", - FocusedArea::Account, - true, - ); -} - -pub fn render_details(frame: &mut Frame, area: Rect, focused: FocusedArea) { - render_section( - frame, - area, - focused, - "Details", - "", - FocusedArea::Details, - true, - ); -} - -pub fn render_status(frame: &mut Frame, area: Rect, focused: FocusedArea) { - render_section( - frame, - area, - focused, - "Status", - "", - FocusedArea::Status, - true, - ); -} diff --git a/src/tui/widgets/account.rs b/src/tui/widgets/account.rs new file mode 100644 index 0000000..f471590 --- /dev/null +++ b/src/tui/widgets/account.rs @@ -0,0 +1,27 @@ +use ratatui::{ + layout::Rect, + style::{Color, Style}, + widgets::{Block, BorderType, Borders, Paragraph}, + Frame, +}; + +use crate::tui::layout::FocusedArea; + +use super::styled_title; + +pub fn render(frame: &mut Frame, area: Rect, focused: FocusedArea) { + let color = if focused == FocusedArea::Account { + Color::White + } else { + Color::DarkGray + }; + + let block = Block::default() + .title(styled_title("Account", true)) + .borders(Borders::ALL) + .border_type(BorderType::Rounded) + .border_style(Style::default().fg(color)); + + let widget = Paragraph::new("TEST CONTENT").block(block); + frame.render_widget(widget, area); +} diff --git a/src/tui/widgets/content.rs b/src/tui/widgets/content.rs new file mode 100644 index 0000000..9fb9e76 --- /dev/null +++ b/src/tui/widgets/content.rs @@ -0,0 +1,44 @@ +use ratatui::{ + layout::Rect, + style::{Color, Style}, + widgets::{Block, BorderType, Borders, Paragraph}, + Frame, +}; + +use crate::tui::layout::FocusedArea; + +use super::styled_title; + +pub fn render(frame: &mut Frame, area: Rect, focused: FocusedArea) { + let color = if focused == FocusedArea::Content { + Color::White + } else { + Color::DarkGray + }; + + let block = Block::default() + .title(styled_title("Content", true)) + .borders(Borders::ALL) + .border_type(BorderType::Rounded) + .border_style(Style::default().fg(color)); + + let widget = Paragraph::new("TEST CONTENT").block(block); + frame.render_widget(widget, area); +} + +pub fn title(frame: &mut Frame, area: Rect, focused: FocusedArea) { + let color = if focused == FocusedArea::Content { + Color::White + } else { + Color::DarkGray + }; + + let block = Block::default() + .title(styled_title("Title", false)) + .borders(Borders::ALL) + .border_type(BorderType::Rounded) + .border_style(Style::default().fg(color)); + + let widget = Paragraph::new("TEST CONTENT").block(block); + frame.render_widget(widget, area); +} diff --git a/src/tui/widgets/details.rs b/src/tui/widgets/details.rs new file mode 100644 index 0000000..d9468ac --- /dev/null +++ b/src/tui/widgets/details.rs @@ -0,0 +1,27 @@ +use ratatui::{ + layout::Rect, + style::{Color, Style}, + widgets::{Block, BorderType, Borders, Paragraph}, + Frame, +}; + +use crate::tui::layout::FocusedArea; + +use super::styled_title; + +pub fn render(frame: &mut Frame, area: Rect, focused: FocusedArea) { + let color = if focused == FocusedArea::Details { + Color::White + } else { + Color::DarkGray + }; + + let block = Block::default() + .title(styled_title("Details", true)) + .borders(Borders::ALL) + .border_type(BorderType::Rounded) + .border_style(Style::default().fg(color)); + + let widget = Paragraph::new("TEST CONTENT").block(block); + frame.render_widget(widget, area); +} diff --git a/src/tui/widgets/instances.rs b/src/tui/widgets/instances.rs new file mode 100644 index 0000000..9dcc84f --- /dev/null +++ b/src/tui/widgets/instances.rs @@ -0,0 +1,142 @@ +use crossterm::event::KeyCode; +use ratatui::{ + layout::{Constraint, Rect}, + style::{Color, Modifier, Style}, + widgets::{ + Block, BorderType, Borders, Cell, Row, Scrollbar, ScrollbarOrientation, ScrollbarState, + Table, TableState, + }, + Frame, +}; + +use crate::tui::layout::FocusedArea; + +use super::{styled_title, WidgetKey}; + +#[derive(Debug, Default)] +pub struct State { + pub instances: Vec, + pub table_state: TableState, + pub scrollbar_state: ScrollbarState, +} + +impl State { + fn next(&mut self) { + let i = match self.table_state.selected() { + Some(i) => { + if i >= self.instances.len().saturating_sub(1) { + 0 + } else { + i + 1 + } + } + None => 0, + }; + self.table_state.select(Some(i)); + self.update_scrollbar(); + } + + fn previous(&mut self) { + let i = match self.table_state.selected() { + Some(i) => { + if i == 0 { + self.instances.len().saturating_sub(1) + } else { + i - 1 + } + } + None => 0, + }; + self.table_state.select(Some(i)); + self.update_scrollbar(); + } + + fn update_scrollbar(&mut self) { + let items = self.instances.len().saturating_sub(1); + let index = self.table_state.selected().unwrap_or(0); + + if self.instances.is_empty() { + self.table_state.select(None); + } else if self.table_state.selected().is_none() { + self.table_state.select(Some(0)); + } else if index > items { + self.table_state.select(Some(items)); + } + + self.scrollbar_state = ScrollbarState::new(items).position(index); + } +} + +impl WidgetKey for State { + fn handle_key(&mut self, key_event: &crossterm::event::KeyEvent) { + match key_event.code { + KeyCode::Char('a') => { + self.instances.push("Test".to_string()); + self.update_scrollbar(); + } + KeyCode::Char('d') => { + self.instances.clear(); + self.update_scrollbar(); + } + KeyCode::Char('j') | KeyCode::Down => self.next(), + KeyCode::Char('k') | KeyCode::Up => self.previous(), + _ => {} + } + } +} + +pub fn render(frame: &mut Frame, area: Rect, focused: FocusedArea, state: &mut State) { + let color = if focused == FocusedArea::Instances { + Color::White + } else { + Color::DarkGray + }; + + let block = Block::default() + .title(styled_title("Instances", true)) + .borders(Borders::ALL) + .border_type(BorderType::Rounded) + .border_style(Style::default().fg(color)); + + frame.render_widget(&block, area); + + let scrollbar_area = Rect { + x: area.x + area.width.saturating_sub(1), + y: area.y + 1, + width: 1, + height: area.height.saturating_sub(2), + }; + + let table_area = block.inner(area); + + let rows = state + .instances + .iter() + .map(|instance| Row::new(vec![Cell::from(instance.as_str())])); + + let widths = [Constraint::Percentage(100)]; + + let table = Table::new(rows, widths).row_highlight_style( + Style::default() + .add_modifier(Modifier::REVERSED) + .fg(Color::Yellow), + ); + + frame.render_stateful_widget(table, table_area, &mut state.table_state); + + frame.render_stateful_widget( + Scrollbar::default() + .orientation(ScrollbarOrientation::VerticalRight) + .begin_symbol(Some("▲")) + .style( + Style::default() + .fg(Color::White) + .add_modifier(Modifier::BOLD), + ) + .thumb_symbol("┃") + .track_symbol(Some("")) + .end_symbol(Some("▼")), + scrollbar_area, + &mut state.scrollbar_state, + ); +} diff --git a/src/tui/widgets/mod.rs b/src/tui/widgets/mod.rs new file mode 100644 index 0000000..fb06ba8 --- /dev/null +++ b/src/tui/widgets/mod.rs @@ -0,0 +1,29 @@ +use crossterm::event::KeyEvent; +use ratatui::{ + style::{Color, Style}, + text::{Line, Span}, +}; + +pub mod account; +pub mod content; +pub mod details; +pub mod instances; +pub mod status; + +pub fn styled_title(title: &str, highlight: bool) -> Line { + if !highlight || title.is_empty() { + Line::from(Span::raw(title)) + } else { + let mut chars = title.chars(); + let first = chars.next().unwrap_or_default().to_string(); + let rest: String = chars.collect(); + Line::from(vec![ + Span::styled(first, Style::default().fg(Color::Yellow)), + Span::raw(rest), + ]) + } +} + +pub trait WidgetKey { + fn handle_key(&mut self, key_event: &KeyEvent); +} diff --git a/src/tui/widgets/status.rs b/src/tui/widgets/status.rs new file mode 100644 index 0000000..83001ae --- /dev/null +++ b/src/tui/widgets/status.rs @@ -0,0 +1,27 @@ +use ratatui::{ + layout::Rect, + style::{Color, Style}, + widgets::{Block, BorderType, Borders, Paragraph}, + Frame, +}; + +use crate::tui::layout::FocusedArea; + +use super::styled_title; + +pub fn render(frame: &mut Frame, area: Rect, focused: FocusedArea) { + let color = if focused == FocusedArea::Status { + Color::White + } else { + Color::DarkGray + }; + + let block = Block::default() + .title(styled_title("Status", true)) + .borders(Borders::ALL) + .border_type(BorderType::Rounded) + .border_style(Style::default().fg(color)); + + let widget = Paragraph::new("TEST CONTENT").block(block); + frame.render_widget(widget, area); +}