diff --git a/Cargo.lock b/Cargo.lock index 37a1a63..209d7ad 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -191,6 +191,26 @@ dependencies = [ "winapi", ] +[[package]] +name = "dirs" +version = "4.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ca3aa72a6f96ea37bbc5aa912f6788242832f75369bdfdadcb0e38423f100059" +dependencies = [ + "dirs-sys", +] + +[[package]] +name = "dirs-sys" +version = "0.3.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1b1d1d91c932ef41c0f2663aa8b0ca0342d444d842c06914aa0a7e352d0bada6" +dependencies = [ + "libc", + "redox_users", + "winapi", +] + [[package]] name = "dotenv" version = "0.15.0" @@ -579,6 +599,16 @@ version = "0.2.159" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "561d97a539a36e26a9a5fad1ea11a3039a67714694aaa379433e580854bc3dc5" +[[package]] +name = "libredox" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c0ff37bd590ca25063e35af745c343cb7a0271906fb7b37e4813e8f79f00268d" +dependencies = [ + "bitflags 2.6.0", + "libc", +] + [[package]] name = "linux-raw-sys" version = "0.4.14" @@ -1093,6 +1123,17 @@ dependencies = [ "bitflags 2.6.0", ] +[[package]] +name = "redox_users" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ba009ff324d1fc1b900bd1fdb31564febe58a8ccc8a6fdbb93b543d33b13ca43" +dependencies = [ + "getrandom 0.2.15", + "libredox", + "thiserror", +] + [[package]] name = "reqwest" version = "0.11.27" @@ -1456,6 +1497,26 @@ dependencies = [ "utf-8", ] +[[package]] +name = "thiserror" +version = "1.0.65" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5d11abd9594d9b38965ef50805c5e469ca9cc6f197f883f717e0269a3057b3d5" +dependencies = [ + "thiserror-impl", +] + +[[package]] +name = "thiserror-impl" +version = "1.0.65" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ae71770322cbd277e69d762a16c444af02aa0575ac0d174f0b9562d3b37f8602" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.79", +] + [[package]] name = "time" version = "0.1.45" @@ -1534,6 +1595,15 @@ dependencies = [ "tokio", ] +[[package]] +name = "toml" +version = "0.5.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f4f7f0dd8d50a853a531c426359045b1998f04219d88799810762cd4ad314234" +dependencies = [ + "serde", +] + [[package]] name = "tower-service" version = "0.3.3" @@ -1638,6 +1708,7 @@ version = "0.1.0" dependencies = [ "ansi-parser", "crossterm 0.22.1", + "dirs", "dotenv", "html2text", "ratatui", @@ -1645,6 +1716,7 @@ dependencies = [ "serde", "serde_json", "tokio", + "toml", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 3686b84..2c17a1e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,6 +4,8 @@ version = "0.1.0" edition = "2021" [dependencies] +toml = "0.5" +dirs = "4.0" tokio = { version = "1", features = ["full"] } reqwest = { version = "0.11", features = ["json"] } serde = { version = "1.0", features = ["derive"] } diff --git a/src/main.rs b/src/main.rs index fbe5810..e2acfbd 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,42 +8,62 @@ use crossterm::{ execute, terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen}, }; -use dotenv::dotenv; +use dirs::config_dir; use ratatui::{backend::CrosstermBackend, Terminal}; -use std::env; +use serde::Deserialize; +use std::fs; use std::io; +use std::path::PathBuf; use ui::run_app; +#[derive(Deserialize)] +struct VikunjaConfig { + instance_url: String, + api_key: String, +} + +#[derive(Deserialize)] +struct Config { + vikunja: VikunjaConfig, +} + +fn load_config() -> Result> { + let mut config_path: PathBuf = config_dir().expect("Could not determine config directory"); + config_path.push("vikunja-tui/config.toml"); + + // Read the config file + let config_content = fs::read_to_string(config_path)?; + + // Parse the TOML content + let config: Config = toml::from_str(&config_content)?; + + Ok(config) +} + #[tokio::main] async fn main() -> Result<(), Box> { - // Load environment variables from .env file - dotenv().ok(); + let config = load_config().expect("Failed to load config file"); - // Read API key and instance URL from environment variables - let instance_url = env::var("INSTANCE_URL").expect("INSTANCE_URL not set"); - let api_key = env::var("API_KEY").expect("API_KEY not set"); + let instance_url = config.vikunja.instance_url; + let api_key = config.vikunja.api_key; - // Fetch all tasks from the API let all_tasks = api::fetch_tasks(&instance_url, &api_key).await?; - // Filter out completed tasks let incomplete_tasks: Vec = all_tasks.into_iter().filter(|task| !task.done).collect(); - // Setup terminal UI enable_raw_mode()?; let mut stdout = io::stdout(); execute!(stdout, EnterAlternateScreen)?; let backend = CrosstermBackend::new(stdout); let mut terminal = Terminal::new(backend)?; - terminal.hide_cursor()?; // Hide the cursor + terminal.hide_cursor()?; - let app = App::new(incomplete_tasks); // Initialize app with incomplete tasks + let app = App::new(incomplete_tasks); let res = run_app(&mut terminal, app, &instance_url, &api_key).await; - // Restore terminal disable_raw_mode()?; execute!(terminal.backend_mut(), LeaveAlternateScreen)?; terminal.show_cursor()?;