From 0b5fa70957c1c73ad9e08874b72e5adea5670a62 Mon Sep 17 00:00:00 2001 From: Dheepak Krishnamurthy Date: Thu, 30 Jul 2020 19:41:11 -0600 Subject: [PATCH] Add clap for parsing command line arguments --- Cargo.lock | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 1 + src/app.rs | 12 +++++----- src/main.rs | 23 +++++++++++++++++-- 4 files changed, 94 insertions(+), 8 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index da78b74..2ee32fa 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -15,12 +15,32 @@ version = "0.2.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ee2a4ec343196209d6594e19543ae87a39f96d5534d7174822a3ad825dd6ed7e" +[[package]] +name = "ansi_term" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ee49baf6cb617b853aa8d93bf420db2383fab46d314482ca2803b40d5fde979b" +dependencies = [ + "winapi", +] + [[package]] name = "arc-swap" version = "0.4.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4d25d88fd6b8041580a654f9d0c581a047baee2b3efee13275f2fc392fc75034" +[[package]] +name = "atty" +version = "0.2.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" +dependencies = [ + "hermit-abi", + "libc", + "winapi", +] + [[package]] name = "autocfg" version = "0.1.7" @@ -76,6 +96,21 @@ dependencies = [ "time", ] +[[package]] +name = "clap" +version = "2.33.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bdfa80d47f954d53a35a64987ca1422f495b8d6483c0fe9f7117b36c2a792129" +dependencies = [ + "ansi_term", + "atty", + "bitflags", + "strsim", + "textwrap", + "unicode-width", + "vec_map", +] + [[package]] name = "cloudabi" version = "0.0.3" @@ -220,6 +255,15 @@ version = "0.22.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "aaf91faf136cb47367fa430cd46e37a788775e7fa104f8b4bcb3861dc389b724" +[[package]] +name = "hermit-abi" +version = "0.1.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3deed196b6e7f9e44a2ae8d94225d80302d81208b1bb673fd21fe634645c85a9" +dependencies = [ + "libc", +] + [[package]] name = "ident_case" version = "1.0.1" @@ -651,6 +695,12 @@ dependencies = [ "winapi", ] +[[package]] +name = "strsim" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a" + [[package]] name = "syn" version = "0.13.11" @@ -706,6 +756,7 @@ name = "taskwarrior-tui" version = "0.5.1" dependencies = [ "chrono", + "clap", "crossterm", "rand 0.7.3", "serde", @@ -716,6 +767,15 @@ dependencies = [ "unicode-width", ] +[[package]] +name = "textwrap" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060" +dependencies = [ + "unicode-width", +] + [[package]] name = "time" version = "0.1.43" @@ -773,6 +833,12 @@ dependencies = [ "serde", ] +[[package]] +name = "vec_map" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191" + [[package]] name = "wasi" version = "0.9.0+wasi-snapshot-preview1" diff --git a/Cargo.toml b/Cargo.toml index 6538b99..2f4164b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,6 +20,7 @@ crossterm-backend = ["tui/crossterm", "crossterm"] [dependencies] serde = { version = "1", features = ["derive"] } serde_json = "1" +clap = "2.33" task-hookrs = "0.7" rand = "0.7" shlex = "0.1" diff --git a/src/app.rs b/src/app.rs index 3367c12..78ff9f3 100644 --- a/src/app.rs +++ b/src/app.rs @@ -97,7 +97,7 @@ pub enum AppMode { TaskError, } -pub struct App { +pub struct TaskwarriorTUIApp { pub should_quit: bool, pub state: TableState, pub cursor_location: usize, @@ -111,9 +111,9 @@ pub struct App { pub mode: AppMode, } -impl App { - pub fn new() -> App { - let mut app = App { +impl TaskwarriorTUIApp { + pub fn new() -> TaskwarriorTUIApp { + let mut app = TaskwarriorTUIApp { should_quit: false, state: TableState::default(), tasks: vec![], @@ -724,7 +724,7 @@ impl App { #[cfg(test)] mod tests { - use crate::app::App; + use crate::app::TaskwarriorTUIApp; use crate::util::setup_terminal; use std::io::stdin; @@ -733,7 +733,7 @@ mod tests { #[test] fn test_app() { - let mut app = App::new(); + let mut app = TaskwarriorTUIApp::new(); app.update(); //println!("{:?}", app.tasks); diff --git a/src/main.rs b/src/main.rs index 828b6a4..7d18911 100644 --- a/src/main.rs +++ b/src/main.rs @@ -16,8 +16,9 @@ use std::process::Command; use std::time::{Duration, Instant}; use tui::backend::Backend; use unicode_width::UnicodeWidthStr; +use clap::{Arg, App, SubCommand}; -use app::{App, AppMode}; +use app::{TaskwarriorTUIApp, AppMode}; use crate::util::Key; use std::sync::{ @@ -30,6 +31,24 @@ const APP_NAME: &'static str = env!("CARGO_PKG_NAME"); fn main() -> Result<(), Box> { + let matches = App::new(APP_NAME) + .version(APP_VERSION) + .author("Dheepak Krishnamurthy <@kdheepak>") + .about("A taskwarrior terminal user interface") + .arg(Arg::with_name("config") + .short("c") + .long("config") + .value_name("FILE") + .help("Sets a custom config file") + .takes_value(true)) + .get_matches(); + + let config = matches.value_of("config").unwrap_or("default.conf"); + tui_main(config)?; + Ok(()) +} + +fn tui_main(config: &str) -> Result<(), Box> { // Terminal initialization let mut terminal = setup_terminal(); terminal.clear()?; @@ -39,7 +58,7 @@ fn main() -> Result<(), Box> { tick_rate: Duration::from_millis(250), }); - let mut app = App::new(); + let mut app = TaskwarriorTUIApp::new(); app.next(); loop {