feat(tui): add focus navigation

This commit is contained in:
72-S committed 2025-02-16 18:03:15 +01:00
1 parent 8f1977c10c
commit aee8ebb72b
3 files changed
+137 -39

No files matched your search

+33 -13
View File
@@ -1,3 +1,5 @@
use super::widgets;
use super::Tui;
use color_eyre::eyre::Context;
use crossterm::event::{self, Event};
use ratatui::{
@@ -5,13 +7,26 @@ use ratatui::{
layout::{Constraint, Direction, Layout},
Frame,
};
use super::widgets;
use super::Tui;
#[derive(Debug, Default)]
pub struct App {
exit: bool,
focused: FocusedArea,
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum FocusedArea {
Instances,
Content,
Account,
Details,
Status,
}
impl Default for FocusedArea {
fn default() -> Self {
FocusedArea::Instances
}
}
impl App {
@@ -34,23 +49,23 @@ impl App {
.split(frame.area());
// Render Instances
widgets::render_instances(frame, chunks[0]);
widgets::render_instances(frame, chunks[0], self.focused);
// Divide the main content into vertical chunks
let main_chunks = Layout::default()
.direction(Direction::Vertical)
.constraints([
Constraint::Length(3), // Instances
Constraint::Min(10), // Main content
Constraint::Length(5), // Bottom panel
Constraint::Length(3), // Instances
Constraint::Min(10), // Main content
Constraint::Length(5), // Bottom panel
])
.split(chunks[1]);
// Render widgets in the main content
widgets::render_title(frame, main_chunks[0]);
widgets::render_content(frame, main_chunks[1]);
widgets::render_title(frame, main_chunks[0], self.focused);
widgets::render_content(frame, main_chunks[1], self.focused);
// Bottom panel split
// Bottom panel split
let bottom_chunks = Layout::default()
.direction(Direction::Horizontal)
.constraints([
@@ -61,9 +76,9 @@ impl App {
.split(main_chunks[2]);
// Render bottom widgets
widgets::render_account(frame, bottom_chunks[0]);
widgets::render_info(frame, bottom_chunks[1]);
widgets::render_status(frame, bottom_chunks[2]);
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);
}
/// updates the applications state based on user input
@@ -79,6 +94,11 @@ 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('I') => self.focused = FocusedArea::Instances,
KeyCode::Char('C') => self.focused = FocusedArea::Content,
KeyCode::Char('A') => self.focused = FocusedArea::Account,
KeyCode::Char('D') => self.focused = FocusedArea::Details,
KeyCode::Char('S') => self.focused = FocusedArea::Status,
_ => {}
}
Ok(())
+1 -1
View File
@@ -1,6 +1,6 @@
pub mod layout;
pub mod widgets;
pub mod widget;
pub mod widgets;
use std::io::{stdout, Result, Stdout};
+103 -25
View File
@@ -1,42 +1,120 @@
use ratatui::{
layout::Rect,
widgets::{Block, Borders, Paragraph},
style::{Color, Style},
text::{Line, Span},
widgets::{Block, BorderType, Borders, Paragraph},
Frame,
};
use ratatui::widgets::BorderType;
pub fn render_instances(frame: &mut Frame, area: Rect) {
let sidebar = Block::default()
.title(" Instances ")
.borders(Borders::ALL).borders(Borders::ALL).border_type(BorderType::Rounded);
frame.render_widget(sidebar, area);
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),
])
}
}
pub fn render_title(frame: &mut Frame, area: Rect) {
let top_bar = Paragraph::new(" TEST INSTANCE NAME").block(Block::default().borders(Borders::ALL).border_type(BorderType::Rounded)).centered();
frame.render_widget(top_bar, area);
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_content(frame: &mut Frame, area: Rect) {
let file_list = Paragraph::new(" TEST CONTENT LIBRARY PAGE").block(Block::default().borders(Borders::ALL).border_type(BorderType::Rounded));
frame.render_widget(file_list, area);
pub fn render_instances(frame: &mut Frame, area: Rect, focused: FocusedArea) {
render_section(
frame,
area,
focused,
"Instances",
"",
FocusedArea::Instances,
true,
);
}
pub fn render_account(frame: &mut Frame, area: Rect) {
let metadata = Paragraph::new(" ")
.block(Block::default().borders(Borders::ALL).borders(Borders::ALL).border_type(BorderType::Rounded).title(" Account "));
frame.render_widget(metadata, area);
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_info(frame: &mut Frame, area: Rect) {
let clipboard = Paragraph::new(" ")
.block(Block::default().borders(Borders::ALL).borders(Borders::ALL).border_type(BorderType::Rounded).title(" Info "));
frame.render_widget(clipboard, area);
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_status(frame: &mut Frame, area: Rect) {
let processes = Paragraph::new(" ")
.block(Block::default().borders(Borders::ALL).borders(Borders::ALL).border_type(BorderType::Rounded).title(" Status "));
frame.render_widget(processes, area);
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,
);
}