feat(tui): render instances as multi-line rows
This commit is contained in:
1 parent
96fe87fc69
commit
3bc5432a20
5 files changed
+860
-55
No files matched your search
Generated
+797
-40
File diff suppressed because it is too large.
Load diff
@@ -16,4 +16,7 @@ serde_derive = "1.0.217"
|
|||||||
lazy_static = "1.5.0"
|
lazy_static = "1.5.0"
|
||||||
colored = "3.0.0"
|
colored = "3.0.0"
|
||||||
chrono = "0.4.39"
|
chrono = "0.4.39"
|
||||||
|
ratatui-image = { version = "5.0.0", features = ["crossterm", "serde"]}
|
||||||
|
image = "0.25.5"
|
||||||
|
fmt = "0.1.0"
|
||||||
|
|
||||||
+2
-2
@@ -1,7 +1,7 @@
|
|||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
|
|
||||||
#[derive(Debug, Deserialize)]
|
#[derive(Debug, Deserialize)]
|
||||||
pub struct Variables {
|
pub struct General {
|
||||||
pub debug: bool,
|
pub debug: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -21,7 +21,7 @@ pub struct Colors {
|
|||||||
|
|
||||||
#[derive(Debug, Deserialize)]
|
#[derive(Debug, Deserialize)]
|
||||||
pub struct Config {
|
pub struct Config {
|
||||||
pub variables: Variables,
|
pub general: General,
|
||||||
pub paths: Paths,
|
pub paths: Paths,
|
||||||
pub colors: Colors,
|
pub colors: Colors,
|
||||||
}
|
}
|
||||||
+1
-1
@@ -15,7 +15,7 @@ fn main() {
|
|||||||
|
|
||||||
match load_config(&path) {
|
match load_config(&path) {
|
||||||
Ok(config) => {
|
Ok(config) => {
|
||||||
Logger::init(config.variables.debug);
|
Logger::init(config.general.debug);
|
||||||
cli::init()
|
cli::init()
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
|
|||||||
@@ -2,12 +2,14 @@ use crossterm::event::KeyCode;
|
|||||||
use ratatui::{
|
use ratatui::{
|
||||||
layout::{Constraint, Rect},
|
layout::{Constraint, Rect},
|
||||||
style::{Color, Modifier, Style},
|
style::{Color, Modifier, Style},
|
||||||
|
text::Text,
|
||||||
widgets::{
|
widgets::{
|
||||||
Block, BorderType, Borders, Cell, Row, Scrollbar, ScrollbarOrientation, ScrollbarState,
|
Block, BorderType, Borders, Cell, Row, Scrollbar, ScrollbarOrientation, ScrollbarState,
|
||||||
Table, TableState,
|
Table, TableState,
|
||||||
},
|
},
|
||||||
Frame,
|
Frame,
|
||||||
};
|
};
|
||||||
|
use ratatui_image::StatefulImage;
|
||||||
|
|
||||||
use crate::tui::layout::FocusedArea;
|
use crate::tui::layout::FocusedArea;
|
||||||
|
|
||||||
@@ -15,11 +17,30 @@ use super::{styled_title, WidgetKey};
|
|||||||
|
|
||||||
#[derive(Debug, Default)]
|
#[derive(Debug, Default)]
|
||||||
pub struct State {
|
pub struct State {
|
||||||
pub instances: Vec<String>,
|
pub instances: Vec<Data>,
|
||||||
pub table_state: TableState,
|
pub table_state: TableState,
|
||||||
pub scrollbar_state: ScrollbarState,
|
pub scrollbar_state: ScrollbarState,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Default)]
|
||||||
|
pub struct Data {
|
||||||
|
pub title: String,
|
||||||
|
pub id: String,
|
||||||
|
pub running: bool,
|
||||||
|
pub img: Option<StatefulImage>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl std::fmt::Debug for Data {
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
f.debug_struct("Data")
|
||||||
|
.field("title", &self.title)
|
||||||
|
.field("id", &self.id)
|
||||||
|
.field("running", &self.running)
|
||||||
|
.field("img", &"<StatefulImage>")
|
||||||
|
.finish()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl State {
|
impl State {
|
||||||
fn next(&mut self) {
|
fn next(&mut self) {
|
||||||
let i = match self.table_state.selected() {
|
let i = match self.table_state.selected() {
|
||||||
@@ -71,7 +92,12 @@ impl WidgetKey for State {
|
|||||||
fn handle_key(&mut self, key_event: &crossterm::event::KeyEvent) {
|
fn handle_key(&mut self, key_event: &crossterm::event::KeyEvent) {
|
||||||
match key_event.code {
|
match key_event.code {
|
||||||
KeyCode::Char('a') => {
|
KeyCode::Char('a') => {
|
||||||
self.instances.push("Test".to_string());
|
self.instances.push(Data {
|
||||||
|
title: "Test".to_string(),
|
||||||
|
id: "123".to_string(),
|
||||||
|
running: false,
|
||||||
|
img: Some(StatefulImage::default()),
|
||||||
|
});
|
||||||
self.update_scrollbar();
|
self.update_scrollbar();
|
||||||
}
|
}
|
||||||
KeyCode::Char('d') => {
|
KeyCode::Char('d') => {
|
||||||
@@ -109,18 +135,37 @@ pub fn render(frame: &mut Frame, area: Rect, focused: FocusedArea, state: &mut S
|
|||||||
|
|
||||||
let table_area = block.inner(area);
|
let table_area = block.inner(area);
|
||||||
|
|
||||||
let rows = state
|
let rows = state.instances.iter().enumerate().map(|(i, data)| {
|
||||||
.instances
|
let status = if data.running { "Running" } else { "Stopped" };
|
||||||
.iter()
|
|
||||||
.map(|instance| Row::new(vec![Cell::from(instance.as_str())]));
|
|
||||||
|
|
||||||
let widths = [Constraint::Percentage(100)];
|
let background_color = if i % 2 == 0 {
|
||||||
|
Color::Reset
|
||||||
|
} else {
|
||||||
|
Color::Rgb(40, 40, 40)
|
||||||
|
};
|
||||||
|
|
||||||
let table = Table::new(rows, widths).row_highlight_style(
|
Row::new(vec![
|
||||||
Style::default()
|
Cell::from(Text::from(format!("\n{}\n", data.title))),
|
||||||
.add_modifier(Modifier::REVERSED)
|
Cell::from(Text::from(format!("\n{}\n", data.id))),
|
||||||
.fg(Color::Yellow),
|
Cell::from(Text::from(format!("\n{}\n", status))),
|
||||||
);
|
])
|
||||||
|
.height(4)
|
||||||
|
.style(Style::default().bg(background_color))
|
||||||
|
});
|
||||||
|
|
||||||
|
let widths = [
|
||||||
|
Constraint::Percentage(40),
|
||||||
|
Constraint::Percentage(30),
|
||||||
|
Constraint::Percentage(30),
|
||||||
|
];
|
||||||
|
|
||||||
|
let table = Table::new(rows, widths)
|
||||||
|
.row_highlight_style(
|
||||||
|
Style::default()
|
||||||
|
.add_modifier(Modifier::REVERSED)
|
||||||
|
.fg(Color::Yellow),
|
||||||
|
)
|
||||||
|
.highlight_spacing(ratatui::widgets::HighlightSpacing::Always);
|
||||||
|
|
||||||
frame.render_stateful_widget(table, table_area, &mut state.table_state);
|
frame.render_stateful_widget(table, table_area, &mut state.table_state);
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user