forked from trinitrix/core
1
0
Fork 0

feat(ui): make tui compile again

This also refactors the tui codebase,
mainly making module locations more sane
This commit is contained in:
Silas Schöffel 2024-05-08 22:06:36 +02:00
parent e560790e00
commit a86c42deae
Signed by: sils
GPG Key ID: 6A587D46E3F44592
7 changed files with 68 additions and 25 deletions

View File

@ -28,7 +28,7 @@ use clap::Parser;
use crate::{ use crate::{
cli::{Args, Command}, cli::{Args, Command},
ui::repl::Repl, ui::{repl::Repl, tui::Tui},
}; };
#[tokio::main] #[tokio::main]
@ -37,7 +37,12 @@ async fn main() -> anyhow::Result<()> {
let args = Args::parse(); let args = Args::parse();
match args.subcommand { 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 {} => { Command::Repl {} => {
let mut app = app::App::new(Repl::new().context("Failed to setup repl")?)?; let mut app = app::App::new(Repl::new().context("Failed to setup repl")?)?;

View File

@ -20,4 +20,5 @@
*/ */
pub mod repl; pub mod repl;
pub mod tui;
pub mod ui_trait; pub mod ui_trait;

View File

@ -19,7 +19,8 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>. * along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
pub mod update; pub mod trinitrixui;
pub mod utils;
use std::io::Stdout; use std::io::Stdout;
@ -30,7 +31,7 @@ use crossterm::{
execute, execute,
terminal::{disable_raw_mode, LeaveAlternateScreen}, terminal::{disable_raw_mode, LeaveAlternateScreen},
}; };
use tui::{ use ratatui::{
backend::CrosstermBackend, backend::CrosstermBackend,
style::Color, style::Color,
widgets::{Block, Borders, ListState}, widgets::{Block, Borders, ListState},
@ -38,9 +39,10 @@ use tui::{
}; };
use tui_textarea::TextArea; use tui_textarea::TextArea;
use crate::ui::{terminal_prepare, textarea_activate, textarea_inactivate}; use crate::ui::{
tui::utils::{terminal_prepare, textarea_activate, textarea_inactivate},
use super::ui_trait::TirinitrixUi; ui_trait::TrinitrixUi,
};
#[derive(Clone, Copy, PartialEq)] #[derive(Clone, Copy, PartialEq)]
pub enum InputPosition { pub enum InputPosition {
@ -163,7 +165,7 @@ impl InputPosition {
} }
} }
pub struct UI<'a> { pub struct Tui<'a> {
terminal: Terminal<CrosstermBackend<Stdout>>, terminal: Terminal<CrosstermBackend<Stdout>>,
input_position: InputPosition, input_position: InputPosition,
pub rooms_state: ListState, pub rooms_state: ListState,
@ -171,9 +173,7 @@ pub struct UI<'a> {
pub cli: Option<TextArea<'a>>, pub cli: Option<TextArea<'a>>,
} }
impl<'r> TirinitrixUi for UI<'r> {} impl Drop for Tui<'_> {
impl Drop for UI<'_> {
fn drop(&mut self) { fn drop(&mut self) {
info!("Destructing UI"); info!("Destructing UI");
disable_raw_mode().expect("While destructing UI -> Failed to disable raw mode"); 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<Self> { pub fn new() -> Result<Self> {
let stdout = terminal_prepare().context("Falied to prepare terminal")?; let stdout = terminal_prepare().context("Falied to prepare terminal")?;
let backend = CrosstermBackend::new(stdout); let backend = CrosstermBackend::new(stdout);

View File

@ -21,21 +21,26 @@
use std::cmp; use std::cmp;
use crate::{
app::status::Status,
ui::{
tui::{trinitrixui::widgets::command_monitor, Tui},
ui_trait::TrinitrixUi,
},
};
use anyhow::Result; use anyhow::Result;
use tui::{ use cli_log::{debug, info};
use keymaps::key_repr::{Key, KeyValue};
use ratatui::{
layout::{Constraint, Direction, Layout}, layout::{Constraint, Direction, Layout},
style::{Color, Style}, style::{Color, Style},
widgets::{Block, Borders, Paragraph}, widgets::{Block, Borders, Paragraph},
}; };
use self::widgets::command_monitor;
use super::UI;
use crate::app::status::Status;
pub mod widgets; pub mod widgets;
impl UI<'_> { impl<'r> TrinitrixUi for Tui<'r> {
pub async fn update(&mut self, status: &Status) -> Result<()> { async fn update(&mut self, status: &Status) -> Result<()> {
let chunks = Layout::default() let chunks = Layout::default()
.direction(Direction::Vertical) .direction(Direction::Vertical)
.constraints([Constraint::Min(10), Constraint::Length(3)].as_ref()) .constraints([Constraint::Min(10), Constraint::Length(3)].as_ref())
@ -112,4 +117,38 @@ impl UI<'_> {
Ok(()) 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());
}
}
} }

View File

@ -19,14 +19,14 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>. * along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
use tui::{ use ratatui::{
layout::Alignment, layout::Alignment,
style::{Color, Style}, style::{Color, Style},
text::Text, text::Text,
widgets::{Block, Borders, Paragraph}, 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<StatusMessage>, colors: &Vec<Color>) -> Paragraph<'a> { pub fn init<'a>(status_events: &Vec<StatusMessage>, colors: &Vec<Color>) -> Paragraph<'a> {
let mut command_monitor = Text::default(); let mut command_monitor = Text::default();

View File

@ -19,8 +19,6 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>. * along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
pub mod central;
use std::{io, io::Stdout}; use std::{io, io::Stdout};
use anyhow::{Context, Result}; use anyhow::{Context, Result};
@ -30,13 +28,13 @@ use crossterm::{
execute, execute,
terminal::{enable_raw_mode, EnterAlternateScreen}, terminal::{enable_raw_mode, EnterAlternateScreen},
}; };
use tui::{ use ratatui::{
style::{Color, Modifier, Style}, style::{Color, Modifier, Style},
widgets::{Block, Borders}, widgets::{Block, Borders},
}; };
use tui_textarea::TextArea; use tui_textarea::TextArea;
fn terminal_prepare() -> Result<Stdout> { pub(super) fn terminal_prepare() -> Result<Stdout> {
enable_raw_mode().context("Failed to enable raw mode")?; enable_raw_mode().context("Failed to enable raw mode")?;
let mut stdout = io::stdout(); let mut stdout = io::stdout();
execute!(stdout, EnterAlternateScreen, EnableMouseCapture)?; execute!(stdout, EnterAlternateScreen, EnableMouseCapture)?;