feat(tui): add main layout

This commit is contained in:
72-S committed 2025-01-29 18:54:10 +01:00
1 parent 9e21548009
commit 15abd84501
3 files changed
+85 -54

No files matched your search

+43 -54
View File
@@ -1,21 +1,16 @@
use color_eyre::eyre::Context;
use crossterm::event::{self, Event};
use ratatui::{
buffer::Buffer,
crossterm::event::{KeyCode, KeyEvent, KeyEventKind},
layout::Rect,
style::Stylize,
symbols::border,
text::{Line, Text},
widgets::{Block, Borders, Paragraph, Widget},
layout::{Constraint, Direction, Layout},
Frame,
};
use crate::tui::widgets;
use super::Tui;
#[derive(Debug, Default)]
pub struct App {
counter: u8,
exit: bool,
}
@@ -28,9 +23,47 @@ impl App {
}
Ok(())
}
fn render_frame(&self, frame: &mut Frame) {
frame.render_widget(self, frame.area());
// Divide the screen into horizontal chunks
let chunks = Layout::default()
.direction(Direction::Horizontal)
.constraints([
Constraint::Percentage(20), // Instances
Constraint::Percentage(80), // Main content
])
.split(frame.area());
// Render Instances
widgets::render_instances(frame, chunks[0]);
// 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
])
.split(chunks[1]);
// Render widgets in the main content
widgets::render_title(frame, main_chunks[0]);
widgets::render_content(frame, main_chunks[1]);
// Bottom panel split
let bottom_chunks = Layout::default()
.direction(Direction::Horizontal)
.constraints([
Constraint::Percentage(30), // Account
Constraint::Percentage(40), // Info
Constraint::Percentage(30), // Status
])
.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]);
}
/// updates the application's state based on user input
@@ -46,8 +79,6 @@ impl App {
fn handle_key_event(&mut self, key_event: KeyEvent) -> color_eyre::Result<()> {
match key_event.code {
KeyCode::Char('q') => self.exit(),
KeyCode::Left => self.decrement_counter()?,
KeyCode::Right => self.increment_counter()?,
_ => {}
}
Ok(())
@@ -56,46 +87,4 @@ impl App {
fn exit(&mut self) {
self.exit = true;
}
fn decrement_counter(&mut self) -> color_eyre::Result<()> {
if self.counter > 0 {
self.counter -= 1;
}
Ok(())
}
fn increment_counter(&mut self) -> color_eyre::Result<()> {
self.counter += 1;
Ok(())
}
}
impl Widget for &App {
fn render(self, area: Rect, buf: &mut Buffer) {
let title = Line::from(" Test ".bold());
let instructions = Line::from(vec![
" Decrement ".into(),
"<Left>".blue().bold(),
" Increment ".into(),
"<Right>".blue().bold(),
" Quit ".into(),
"<Q> ".blue().bold(),
]);
let block = Block::default()
.title(title.centered())
.title_bottom(instructions.centered())
.borders(Borders::ALL)
.border_set(border::THICK);
let counter_text = Text::from(vec![Line::from(vec![
"Value: ".into(),
self.counter.to_string().yellow(),
])]);
Paragraph::new(counter_text)
.centered()
.block(block)
.render(area, buf);
}
}
+1
View File
@@ -1,4 +1,5 @@
pub mod layout;
pub mod widgets;
use std::io::{stdout, Result, Stdout};
+41
View File
@@ -0,0 +1,41 @@
use ratatui::{
layout::Rect,
widgets::{Block, Borders, Paragraph},
Frame,
};
pub fn render_instances(frame: &mut Frame, area: Rect) {
let sidebar = Block::default()
.title(" Instances ")
.borders(Borders::ALL);
frame.render_widget(sidebar, area);
}
pub fn render_title(frame: &mut Frame, area: Rect) {
let top_bar = Paragraph::new(" TEST INSTANCE NAME").block(Block::default().borders(Borders::ALL)).centered();
frame.render_widget(top_bar, 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));
frame.render_widget(file_list, area);
}
pub fn render_account(frame: &mut Frame, area: Rect) {
let metadata = Paragraph::new(" ")
.block(Block::default().borders(Borders::ALL).title(" Account "));
frame.render_widget(metadata, area);
}
pub fn render_info(frame: &mut Frame, area: Rect) {
let clipboard = Paragraph::new(" ")
.block(Block::default().borders(Borders::ALL).title(" Info "));
frame.render_widget(clipboard, area);
}
pub fn render_status(frame: &mut Frame, area: Rect) {
let processes = Paragraph::new(" ")
.block(Block::default().borders(Borders::ALL).title(" Status "));
frame.render_widget(processes, area);
}