feat(tui): add per-widget config, keybinds, and scrollbar
This commit is contained in:
1 parent
aee8ebb72b
commit
96fe87fc69
12 files changed
+318
-139
No files matched your search
+1
-4
@@ -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();
|
||||
}
|
||||
}
|
||||
+3
-1
@@ -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
|
||||
|
||||
+18
-13
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,4 @@
|
||||
pub mod layout;
|
||||
pub mod widget;
|
||||
pub mod widgets;
|
||||
|
||||
use std::io::{stdout, Result, Stdout};
|
||||
|
||||
Whitespace-only changes.
@@ -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,
|
||||
);
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
@@ -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<String>,
|
||||
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,
|
||||
);
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
Reference in new issue
Block a user