From a86c42deae376d0d99f688c0ac242a61b090bfaa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Silas=20Sch=C3=B6ffel?= Date: Wed, 8 May 2024 22:06:36 +0200 Subject: [PATCH] feat(ui): make tui compile again This also refactors the tui codebase, mainly making module locations more sane --- src/main.rs | 9 +++- src/ui/mod.rs | 1 + src/ui/{old/central => tui}/mod.rs | 20 +++---- .../central/update => tui/trinitrixui}/mod.rs | 53 ++++++++++++++++--- .../trinitrixui}/widgets/command_monitor.rs | 4 +- .../update => tui/trinitrixui}/widgets/mod.rs | 0 src/ui/{old/mod.rs => tui/utils.rs} | 6 +-- 7 files changed, 68 insertions(+), 25 deletions(-) rename src/ui/{old/central => tui}/mod.rs (97%) rename src/ui/{old/central/update => tui/trinitrixui}/mod.rs (68%) rename src/ui/{old/central/update => tui/trinitrixui}/widgets/command_monitor.rs (95%) rename src/ui/{old/central/update => tui/trinitrixui}/widgets/mod.rs (100%) rename src/ui/{old/mod.rs => tui/utils.rs} (96%) diff --git a/src/main.rs b/src/main.rs index 38431b0..4f2ee91 100644 --- a/src/main.rs +++ b/src/main.rs @@ -28,7 +28,7 @@ use clap::Parser; use crate::{ cli::{Args, Command}, - ui::repl::Repl, + ui::{repl::Repl, tui::Tui}, }; #[tokio::main] @@ -37,7 +37,12 @@ async fn main() -> anyhow::Result<()> { let args = Args::parse(); match args.subcommand { - Command::Tui {} => {} + Command::Tui {} => { + let mut app = app::App::new(Tui::new().context("Failed to setup tui")?)?; + + // NOTE(@soispha): The `None` here is temporary <2024-05-08> + app.run(None, args.plugin_path).await?; + } Command::Repl {} => { let mut app = app::App::new(Repl::new().context("Failed to setup repl")?)?; diff --git a/src/ui/mod.rs b/src/ui/mod.rs index aa2e404..99a1fd3 100644 --- a/src/ui/mod.rs +++ b/src/ui/mod.rs @@ -20,4 +20,5 @@ */ pub mod repl; +pub mod tui; pub mod ui_trait; diff --git a/src/ui/old/central/mod.rs b/src/ui/tui/mod.rs similarity index 97% rename from src/ui/old/central/mod.rs rename to src/ui/tui/mod.rs index a64b3d1..78cdc39 100644 --- a/src/ui/old/central/mod.rs +++ b/src/ui/tui/mod.rs @@ -19,7 +19,8 @@ * along with this program. If not, see . */ -pub mod update; +pub mod trinitrixui; +pub mod utils; use std::io::Stdout; @@ -30,7 +31,7 @@ use crossterm::{ execute, terminal::{disable_raw_mode, LeaveAlternateScreen}, }; -use tui::{ +use ratatui::{ backend::CrosstermBackend, style::Color, widgets::{Block, Borders, ListState}, @@ -38,9 +39,10 @@ use tui::{ }; use tui_textarea::TextArea; -use crate::ui::{terminal_prepare, textarea_activate, textarea_inactivate}; - -use super::ui_trait::TirinitrixUi; +use crate::ui::{ + tui::utils::{terminal_prepare, textarea_activate, textarea_inactivate}, + ui_trait::TrinitrixUi, +}; #[derive(Clone, Copy, PartialEq)] pub enum InputPosition { @@ -163,7 +165,7 @@ impl InputPosition { } } -pub struct UI<'a> { +pub struct Tui<'a> { terminal: Terminal>, input_position: InputPosition, pub rooms_state: ListState, @@ -171,9 +173,7 @@ pub struct UI<'a> { pub cli: Option>, } -impl<'r> TirinitrixUi for UI<'r> {} - -impl Drop for UI<'_> { +impl Drop for Tui<'_> { fn drop(&mut self) { info!("Destructing UI"); disable_raw_mode().expect("While destructing UI -> Failed to disable raw mode"); @@ -188,7 +188,7 @@ impl Drop for UI<'_> { } } -impl UI<'_> { +impl Tui<'_> { pub fn new() -> Result { let stdout = terminal_prepare().context("Falied to prepare terminal")?; let backend = CrosstermBackend::new(stdout); diff --git a/src/ui/old/central/update/mod.rs b/src/ui/tui/trinitrixui/mod.rs similarity index 68% rename from src/ui/old/central/update/mod.rs rename to src/ui/tui/trinitrixui/mod.rs index 613a8a4..2280cb2 100644 --- a/src/ui/old/central/update/mod.rs +++ b/src/ui/tui/trinitrixui/mod.rs @@ -21,21 +21,26 @@ use std::cmp; +use crate::{ + app::status::Status, + ui::{ + tui::{trinitrixui::widgets::command_monitor, Tui}, + ui_trait::TrinitrixUi, + }, +}; use anyhow::Result; -use tui::{ +use cli_log::{debug, info}; +use keymaps::key_repr::{Key, KeyValue}; +use ratatui::{ layout::{Constraint, Direction, Layout}, style::{Color, Style}, widgets::{Block, Borders, Paragraph}, }; -use self::widgets::command_monitor; -use super::UI; -use crate::app::status::Status; - pub mod widgets; -impl UI<'_> { - pub async fn update(&mut self, status: &Status) -> Result<()> { +impl<'r> TrinitrixUi for Tui<'r> { + async fn update(&mut self, status: &Status) -> Result<()> { let chunks = Layout::default() .direction(Direction::Vertical) .constraints([Constraint::Min(10), Constraint::Length(3)].as_ref()) @@ -112,4 +117,38 @@ impl UI<'_> { Ok(()) } + fn input(&mut self, input: Key) { + debug!("Input received in tui: {}", input); + if let Some(value) = input.value() { + match value { + KeyValue::Backspace => todo!(), + KeyValue::Enter => todo!(), + KeyValue::Left => todo!(), + KeyValue::Right => todo!(), + KeyValue::Up => todo!(), + KeyValue::Down => todo!(), + KeyValue::Home => todo!(), + KeyValue::End => todo!(), + KeyValue::PageUp => todo!(), + KeyValue::PageDown => todo!(), + KeyValue::Tab => todo!(), + KeyValue::BackTab => todo!(), + KeyValue::Delete => todo!(), + KeyValue::Insert => todo!(), + KeyValue::F(_) => todo!(), + KeyValue::Char(_) => todo!(), + KeyValue::Null => todo!(), + KeyValue::Esc => todo!(), + KeyValue::CapsLock => todo!(), + KeyValue::ScrollLock => todo!(), + KeyValue::NumLock => todo!(), + KeyValue::PrintScreen => todo!(), + KeyValue::Pause => todo!(), + KeyValue::Menu => todo!(), + KeyValue::KeypadBegin => todo!(), + } + } else { + info!("User wrote: '{}'", input.to_string_repr()); + } + } } diff --git a/src/ui/old/central/update/widgets/command_monitor.rs b/src/ui/tui/trinitrixui/widgets/command_monitor.rs similarity index 95% rename from src/ui/old/central/update/widgets/command_monitor.rs rename to src/ui/tui/trinitrixui/widgets/command_monitor.rs index eab16f4..c9ee5d0 100644 --- a/src/ui/old/central/update/widgets/command_monitor.rs +++ b/src/ui/tui/trinitrixui/widgets/command_monitor.rs @@ -19,14 +19,14 @@ * along with this program. If not, see . */ -use tui::{ +use ratatui::{ layout::Alignment, style::{Color, Style}, text::Text, widgets::{Block, Borders, Paragraph}, }; -use crate::{app::status::StatusMessage, ui::central::InputPosition}; +use crate::{app::status::StatusMessage, ui::tui::InputPosition}; pub fn init<'a>(status_events: &Vec, colors: &Vec) -> Paragraph<'a> { let mut command_monitor = Text::default(); diff --git a/src/ui/old/central/update/widgets/mod.rs b/src/ui/tui/trinitrixui/widgets/mod.rs similarity index 100% rename from src/ui/old/central/update/widgets/mod.rs rename to src/ui/tui/trinitrixui/widgets/mod.rs diff --git a/src/ui/old/mod.rs b/src/ui/tui/utils.rs similarity index 96% rename from src/ui/old/mod.rs rename to src/ui/tui/utils.rs index 27fceaf..c5de95d 100644 --- a/src/ui/old/mod.rs +++ b/src/ui/tui/utils.rs @@ -19,8 +19,6 @@ * along with this program. If not, see . */ -pub mod central; - use std::{io, io::Stdout}; use anyhow::{Context, Result}; @@ -30,13 +28,13 @@ use crossterm::{ execute, terminal::{enable_raw_mode, EnterAlternateScreen}, }; -use tui::{ +use ratatui::{ style::{Color, Modifier, Style}, widgets::{Block, Borders}, }; use tui_textarea::TextArea; -fn terminal_prepare() -> Result { +pub(super) fn terminal_prepare() -> Result { enable_raw_mode().context("Failed to enable raw mode")?; let mut stdout = io::stdout(); execute!(stdout, EnterAlternateScreen, EnableMouseCapture)?;