forked from trinitrix/core
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.
This commit is contained in:
parent
189ae509f8
commit
c7a4d5a8ab
|
@ -7,10 +7,7 @@ license = "MIT"
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
default = ["cli"]
|
default = ["tui"]
|
||||||
full = ["cli", "tui"]
|
|
||||||
|
|
||||||
cli = ["tokio/io-std"]
|
|
||||||
tui = ["dep:tui", "dep:tui-textarea", "dep:crossterm", "dep:tokio-util", "dep:serde", "dep:indexmap"]
|
tui = ["dep:tui", "dep:tui-textarea", "dep:crossterm", "dep:tokio-util", "dep:serde", "dep:indexmap"]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
@ -18,7 +15,7 @@ clap = { version = "4.3.19", features = ["derive"] }
|
||||||
cli-log = "2.0"
|
cli-log = "2.0"
|
||||||
anyhow = "1.0"
|
anyhow = "1.0"
|
||||||
matrix-sdk = "0.6"
|
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 stuff
|
||||||
lua_macros = { path = "./lua_macros" }
|
lua_macros = { path = "./lua_macros" }
|
||||||
|
|
|
@ -11,13 +11,7 @@ pub struct Args {
|
||||||
}
|
}
|
||||||
#[derive(Subcommand, Debug)]
|
#[derive(Subcommand, Debug)]
|
||||||
pub enum Command {
|
pub enum Command {
|
||||||
/// Starts a repl, for the lua interface
|
|
||||||
#[cfg(feature = "cli")]
|
|
||||||
#[clap(value_parser)]
|
|
||||||
Repl {},
|
|
||||||
|
|
||||||
/// Starts the main tui client
|
/// Starts the main tui client
|
||||||
#[cfg(feature = "tui")]
|
|
||||||
#[clap(value_parser)]
|
#[clap(value_parser)]
|
||||||
Start {},
|
Start {},
|
||||||
}
|
}
|
||||||
|
|
25
src/main.rs
25
src/main.rs
|
@ -1,17 +1,12 @@
|
||||||
|
//mod app;
|
||||||
|
//mod ui;
|
||||||
|
//mod accounts;
|
||||||
mod cli;
|
mod cli;
|
||||||
pub mod event_handler;
|
|
||||||
|
|
||||||
#[cfg(feature = "tui")]
|
|
||||||
mod tui_app;
|
mod tui_app;
|
||||||
|
|
||||||
#[cfg(feature = "cli")]
|
|
||||||
mod repl;
|
|
||||||
|
|
||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
|
|
||||||
use crate::cli::{Args, Command};
|
use crate::cli::{Args, Command};
|
||||||
|
|
||||||
#[cfg(feature = "tui")]
|
|
||||||
pub use tui_app::*;
|
pub use tui_app::*;
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
|
@ -19,20 +14,8 @@ async fn main() -> anyhow::Result<()> {
|
||||||
cli_log::init_cli_log!();
|
cli_log::init_cli_log!();
|
||||||
|
|
||||||
let args = Args::parse();
|
let args = Args::parse();
|
||||||
let command = args.subcommand.unwrap_or(
|
let command = args.subcommand.unwrap_or(Command::Start {});
|
||||||
#[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 {},
|
|
||||||
);
|
|
||||||
match command {
|
match command {
|
||||||
#[cfg(feature = "cli")]
|
|
||||||
Command::Repl {} => {
|
|
||||||
repl::run().await?;
|
|
||||||
}
|
|
||||||
#[cfg(feature = "tui")]
|
|
||||||
Command::Start {} => {
|
Command::Start {} => {
|
||||||
let mut app = app::App::new()?;
|
let mut app = app::App::new()?;
|
||||||
app.run().await?;
|
app.run().await?;
|
||||||
|
|
|
@ -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")?
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -2,6 +2,6 @@ pub mod app;
|
||||||
pub mod ui;
|
pub mod ui;
|
||||||
pub mod accounts;
|
pub mod accounts;
|
||||||
|
|
||||||
pub use app::*;
|
//pub use app::*;
|
||||||
pub use ui::*;
|
//pub use ui::*;
|
||||||
pub use accounts::*;
|
//pub use accounts::*;
|
||||||
|
|
Loading…
Reference in New Issue