From c7a4d5a8ab51b60e7add730c110ec70f9da62f27 Mon Sep 17 00:00:00 2001 From: Soispha Date: Mon, 24 Jul 2023 23:38:16 +0200 Subject: [PATCH] Refactor(treewide): Remove the repl, reuse of e. handling is hard The event handling is deeply ingrained in the ui code, the commands are focused around the ui code, in short splitting of the event handling and command system from the ui is intentionally hard and in my opinion not really worth it right now. --- Cargo.toml | 7 ++----- src/cli.rs | 6 ------ src/main.rs | 25 ++++--------------------- src/repl/mod.rs | 41 ----------------------------------------- src/tui_app/mod.rs | 6 +++--- 5 files changed, 9 insertions(+), 76 deletions(-) delete mode 100644 src/repl/mod.rs diff --git a/Cargo.toml b/Cargo.toml index c28dc41..a60ace5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,10 +7,7 @@ license = "MIT" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [features] -default = ["cli"] -full = ["cli", "tui"] - -cli = ["tokio/io-std"] +default = ["tui"] tui = ["dep:tui", "dep:tui-textarea", "dep:crossterm", "dep:tokio-util", "dep:serde", "dep:indexmap"] [dependencies] @@ -18,7 +15,7 @@ clap = { version = "4.3.19", features = ["derive"] } cli-log = "2.0" anyhow = "1.0" matrix-sdk = "0.6" -tokio = { version = "1.29", features = ["macros", "rt-multi-thread"] } +tokio = { version = "1.29", features = ["macros", "rt-multi-thread", "io-std"] } # lua stuff lua_macros = { path = "./lua_macros" } diff --git a/src/cli.rs b/src/cli.rs index 9717a22..e23f01e 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -11,13 +11,7 @@ pub struct Args { } #[derive(Subcommand, Debug)] pub enum Command { - /// Starts a repl, for the lua interface - #[cfg(feature = "cli")] - #[clap(value_parser)] - Repl {}, - /// Starts the main tui client - #[cfg(feature = "tui")] #[clap(value_parser)] Start {}, } diff --git a/src/main.rs b/src/main.rs index f613497..b14a469 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,17 +1,12 @@ +//mod app; +//mod ui; +//mod accounts; mod cli; -pub mod event_handler; - -#[cfg(feature = "tui")] mod tui_app; -#[cfg(feature = "cli")] -mod repl; - use clap::Parser; use crate::cli::{Args, Command}; - -#[cfg(feature = "tui")] pub use tui_app::*; #[tokio::main] @@ -19,20 +14,8 @@ async fn main() -> anyhow::Result<()> { cli_log::init_cli_log!(); let args = Args::parse(); - let command = args.subcommand.unwrap_or( - #[cfg(all(feature = "tui", not(feature = "cli")))] - Command::Start {}, - #[cfg(all(feature = "cli", not(feature = "tui")))] - Command::Repl {}, - #[cfg(all(feature = "cli", feature = "tui"))] - Command::Start {}, - ); + let command = args.subcommand.unwrap_or(Command::Start {}); match command { - #[cfg(feature = "cli")] - Command::Repl {} => { - repl::run().await?; - } - #[cfg(feature = "tui")] Command::Start {} => { let mut app = app::App::new()?; app.run().await?; diff --git a/src/repl/mod.rs b/src/repl/mod.rs deleted file mode 100644 index 9dacdce..0000000 --- a/src/repl/mod.rs +++ /dev/null @@ -1,41 +0,0 @@ -use std::io::ErrorKind; - -use anyhow::{Context, Result}; -use cli_log::{info, warn}; -use tokio::io::{stdin, stdout, AsyncReadExt, AsyncWriteExt}; - -pub async fn run() -> Result<()> { - let mut stdin = stdin(); - let mut stdout = stdout(); - let mut buffer = [0; 1]; - let mut new_command = vec![]; - - loop { - stdout - .write("trinitrix:> ".as_bytes()) - .await - .context("Failed to write prompt")?; - stdout.flush().await.context("Failed to flush prompt")?; - new_command.clear(); - loop { - if let Err(err) = stdin.read_exact(&mut buffer).await { - if err.kind() == ErrorKind::UnexpectedEof { - warn!("Unexpected EOF, we assume the user quit."); - return Ok(()); - } else { - Err(err).context("Failed to read next character")?; - } - } - if buffer == "\n".as_bytes() { - break; - } else { - new_command.append(&mut buffer.to_vec()); - } - } - info!( - "Got user repl input: {}", - String::from_utf8(new_command.clone()) - .context("Failed to convert user input to utf8 string")? - ) - } -} diff --git a/src/tui_app/mod.rs b/src/tui_app/mod.rs index c0d91db..8da28d6 100644 --- a/src/tui_app/mod.rs +++ b/src/tui_app/mod.rs @@ -2,6 +2,6 @@ pub mod app; pub mod ui; pub mod accounts; -pub use app::*; -pub use ui::*; -pub use accounts::*; +//pub use app::*; +//pub use ui::*; +//pub use accounts::*;