From 357c42332f37a1e91b63a6c07306cc0ca44d08ae Mon Sep 17 00:00:00 2001 From: Soispha Date: Sat, 9 Sep 2023 21:29:46 +0200 Subject: [PATCH] feat(app): Add support for a Lua based configuration file --- Cargo.lock | 39 ++++++++++++++++++++++++++++++++ Cargo.toml | 1 + src/app/config/lua/mod.rs | 14 ++++++++++++ src/app/config/mod.rs | 1 + src/app/mod.rs | 47 ++++++++++++++++++++++++++++++++++++--- src/cli.rs | 8 ++++++- src/main.rs | 2 +- 7 files changed, 107 insertions(+), 5 deletions(-) create mode 100644 src/app/config/lua/mod.rs create mode 100644 src/app/config/mod.rs diff --git a/Cargo.lock b/Cargo.lock index dfe5701..1085cd3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -732,6 +732,27 @@ dependencies = [ "subtle", ] +[[package]] +name = "directories" +version = "5.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a49173b84e034382284f27f1af4dcbbd231ffa358c0fe316541a7337f376a35" +dependencies = [ + "dirs-sys", +] + +[[package]] +name = "dirs-sys" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "520f05a5cbd335fae5a99ff7a6ab8627577660ee5cfd6a94a6a929b52ff0321c" +dependencies = [ + "libc", + "option-ext", + "redox_users", + "windows-sys", +] + [[package]] name = "discard" version = "1.0.4" @@ -1744,6 +1765,12 @@ dependencies = [ "vcpkg", ] +[[package]] +name = "option-ext" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "04744f49eae99ab78e0d5c0b603ab218f515ea8cfe5a456d7629ad883a3b6e7d" + [[package]] name = "ordered-float" version = "2.10.0" @@ -2044,6 +2071,17 @@ dependencies = [ "bitflags 1.3.2", ] +[[package]] +name = "redox_users" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b033d837a7cf162d7993aded9304e30a83213c648b6e389db233191f891e5c2b" +dependencies = [ + "getrandom 0.2.10", + "redox_syscall 0.2.16", + "thiserror", +] + [[package]] name = "regex" version = "1.9.5" @@ -2677,6 +2715,7 @@ dependencies = [ "clap", "cli-log", "crossterm", + "directories", "indexmap 2.0.0", "language_macros", "matrix-sdk", diff --git a/Cargo.toml b/Cargo.toml index 90b68f8..b0fd130 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -29,6 +29,7 @@ crossterm = { version = "0.25", optional = true } tokio-util = { version = "0.7", optional = true } serde = { version = "1.0", optional = true } indexmap = { version = "2.0.0", optional = true } +directories = "5.0.1" [profile.release] lto = true diff --git a/src/app/config/lua/mod.rs b/src/app/config/lua/mod.rs new file mode 100644 index 0000000..7daa225 --- /dev/null +++ b/src/app/config/lua/mod.rs @@ -0,0 +1,14 @@ +use std::path::PathBuf; + +use anyhow::Result; +use cli_log::info; +use tokio::{fs, sync::mpsc::Sender}; + +use crate::app::events::event_types::Event; + +pub async fn load(tx: Sender, path: PathBuf) -> Result<()> { + let lua_config_code = fs::read_to_string(&path).await?; + tx.send(Event::LuaCommand(lua_config_code)).await?; + info!("Loaded config file at `{}`", path.display()); + Ok(()) +} diff --git a/src/app/config/mod.rs b/src/app/config/mod.rs new file mode 100644 index 0000000..f97296a --- /dev/null +++ b/src/app/config/mod.rs @@ -0,0 +1 @@ +pub mod lua; diff --git a/src/app/mod.rs b/src/app/mod.rs index ad52a11..3fb1ff6 100644 --- a/src/app/mod.rs +++ b/src/app/mod.rs @@ -1,11 +1,13 @@ pub mod command_interface; +pub mod config; pub mod events; pub mod status; -use std::path::Path; +use std::path::{Path, PathBuf}; use anyhow::{Context, Error, Result}; -use cli_log::info; +use cli_log::{info, warn}; +use directories::ProjectDirs; use matrix_sdk::Client; use tokio::sync::mpsc; use tokio_util::sync::CancellationToken; @@ -33,6 +35,8 @@ pub struct App<'ui> { matrix_listener_killer: CancellationToken, lua: LuaCommandManager, + + project_dirs: ProjectDirs, } impl App<'_> { @@ -57,16 +61,53 @@ impl App<'_> { matrix_listener_killer: CancellationToken::new(), lua: LuaCommandManager::new(tx), + + // TODO: We probably want to populate the strings below a bit more <2023-09-09> + project_dirs: ProjectDirs::from("", "", "trinitrix").context( + "Failed to allocate project direcectory paths, \ + please ensure your $HOME is set correctly", + )?, }) } - pub async fn run(&mut self) -> Result<()> { + pub async fn run(&mut self, cli_lua_config_file: Option) -> Result<()> { // Spawn input event listener tokio::task::spawn(events::poll_input_events( self.tx.clone(), self.input_listener_killer.clone(), )); + let config_dir = self.project_dirs.config_dir(); + let mut lua_config_file = None; + if config_dir + .try_exists() + .context("Failed to search for a config file")? + { + lua_config_file = Some(config_dir.join("init.lua")); + info!( + "Found config dir: `{}`", + config_dir.display() + ); + } else { + warn!("No config dir found!"); + } + + if let Some(config_file) = cli_lua_config_file { + config::lua::load(self.tx.clone(), config_file).await?; + } else if let Some(config_file) = lua_config_file { + info!("Searching for config file at `{}`", config_file.display()); + if config_file.try_exists().with_context(|| { + format!( + "Failed to check, if the lua config file at `{}` exists", + config_file.display() + ) + })? { + config::lua::load(self.tx.clone(), config_file).await?; + } else { + warn!("No config file found!"); + } + } + if self.account().is_err() { info!("No saved sessions found -> jumping into setup"); self.setup().await?; diff --git a/src/cli.rs b/src/cli.rs index e23f01e..6680486 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -1,3 +1,5 @@ +use std::path::PathBuf; + use clap::{Parser, Subcommand}; // TODO: The description could be better @@ -8,10 +10,14 @@ pub struct Args { #[command(subcommand)] /// The subcommand to execute, default is start pub subcommand: Option, + + #[clap(value_parser, long, short)] + /// Path to the Lua config file, executed instead of the normal one + pub lua_config_file: Option, } #[derive(Subcommand, Debug)] pub enum Command { - /// Starts the main tui client #[clap(value_parser)] + /// Starts the main TUI client Start {}, } diff --git a/src/main.rs b/src/main.rs index 0c8e5b4..964f61f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -16,7 +16,7 @@ async fn main() -> anyhow::Result<()> { match command { Command::Start {} => { let mut app = app::App::new()?; - app.run().await?; + app.run(args.lua_config_file).await?; } };