From 6705c283aff30b4b982cd130b43428843b98f975 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Moritz=20B=C3=B6hme?= Date: Sat, 26 Aug 2023 09:35:02 +0200 Subject: [PATCH 1/5] feat: add man page generation --- Cargo.lock | 17 +++++++++++++++++ Cargo.toml | 1 + flake.nix | 3 +++ src/cli.rs | 6 ++++++ src/main.rs | 8 ++++++++ 5 files changed, 35 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index 78b6613..1d674b1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -343,6 +343,16 @@ version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2da6da31387c7e4ef160ffab6d5e7f00c42626fe39aea70a7b0f1773f7dd6c1b" +[[package]] +name = "clap_mangen" +version = "0.2.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf8e5f34d85d9e0bbe2491d100a7a7c1007bb2467b518080bfe311e8947197a9" +dependencies = [ + "clap", + "roff", +] + [[package]] name = "colorchoice" version = "1.0.0" @@ -948,6 +958,12 @@ version = "0.7.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e5ea92a5b6195c6ef2a0295ea818b312502c6fc94dde986c5553242e18fd4ce2" +[[package]] +name = "roff" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b833d8d034ea094b1ea68aa6d5c740e0d04bad9d16568d08ba6f76823a114316" + [[package]] name = "rustix" version = "0.37.20" @@ -1165,6 +1181,7 @@ dependencies = [ "anyhow", "clap", "clap_complete_command", + "clap_mangen", "humantime", "libc", "notify-rust", diff --git a/Cargo.toml b/Cargo.toml index 8c2f0e2..9d57901 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,6 +9,7 @@ edition = "2021" anyhow = "1.0.71" clap = { version = "4.3.4", features = ["derive"] } clap_complete_command = "0.5.1" +clap_mangen = "0.2.13" humantime = "2.1.0" libc = "0.2.147" notify-rust = "4.8.0" diff --git a/flake.nix b/flake.nix index 5be9bfb..c399655 100644 --- a/flake.nix +++ b/flake.nix @@ -17,6 +17,9 @@ nativeBuildInputs = with pkgs; [ installShellFiles ]; meta.mainProgram = "timers"; postInstall = '' + $out/bin/timers manpage timers.1 + installManPage timers.1 + installShellCompletion --cmd timers \ --bash <($out/bin/timers completions bash) \ --fish <($out/bin/timers completions fish) \ diff --git a/src/cli.rs b/src/cli.rs index 08daac2..f71c642 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -61,6 +61,12 @@ pub enum Command { Completions { #[arg(value_enum)] shell: clap_complete_command::Shell, + }, + + /// Generate man page (section 1) + Manpage { + /// File to save the man page to + file_path: String } } diff --git a/src/main.rs b/src/main.rs index 2249f05..7c7d841 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,4 @@ +use std::io::Write; mod cli; mod daemon; mod helper; @@ -46,6 +47,13 @@ fn main() -> Result<(), anyhow::Error> { shell.generate(&mut Cli::command(), &mut std::io::stdout()); return Ok(()); } + CliCommand::Manpage { file_path } => { + let man = clap_mangen::Man::new(Cli::command()); + let mut buffer: Vec = Default::default(); + man.render(&mut buffer)?; + std::fs::write(file_path, buffer)?; + return Ok(()); + } }; let answer = send_command(&args.socket, daemon_command)?; print!("{}", answer); From 3c6cc462f394b38f68d6f8f72fd188e5c336934d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Moritz=20B=C3=B6hme?= Date: Sat, 26 Aug 2023 09:40:12 +0200 Subject: [PATCH 2/5] build: update inputs --- flake.lock | 37 +++++++++++++------------------------ flake.nix | 3 ++- 2 files changed, 15 insertions(+), 25 deletions(-) diff --git a/flake.lock b/flake.lock index dea1ff1..1f49d10 100644 --- a/flake.lock +++ b/flake.lock @@ -2,14 +2,16 @@ "nodes": { "naersk": { "inputs": { - "nixpkgs": "nixpkgs" + "nixpkgs": [ + "nixpkgs" + ] }, "locked": { - "lastModified": 1686572087, - "narHash": "sha256-jXTut7ZSYqLEgm/nTk7TuVL2ExahTip605bLINklAnQ=", + "lastModified": 1692351612, + "narHash": "sha256-KTGonidcdaLadRnv9KFgwSMh1ZbXoR/OBmPjeNMhFwU=", "owner": "nix-community", "repo": "naersk", - "rev": "8507af04eb40c5520bd35d9ce6f9d2342cea5ad1", + "rev": "78789c30d64dea2396c9da516bbcc8db3a475207", "type": "github" }, "original": { @@ -21,24 +23,11 @@ }, "nixpkgs": { "locked": { - "lastModified": 1685677062, - "narHash": "sha256-zoHF7+HNwNwne2XEomphbdc4Y8tdWT16EUxUTXpOKpQ=", - "path": "/nix/store/dnqwkazyg92hzya7400klxlk072g3zsk-source", - "rev": "95be94370d09f97f6af6a1df1eb9649b5260724e", - "type": "path" - }, - "original": { - "id": "nixpkgs", - "type": "indirect" - } - }, - "nixpkgs_2": { - "locked": { - "lastModified": 1687103638, - "narHash": "sha256-dwy/TK6Db5W7ivcgmcxUykhFwodIg0jrRzOFt7H5NUc=", + "lastModified": 1692934111, + "narHash": "sha256-9EEE59v/esKNMR5zKbLRV9NoRPYvERw5jHQOnfr47bk=", "owner": "NixOS", "repo": "nixpkgs", - "rev": "91430887645a0953568da2f3e9a3a3bb0a0378ac", + "rev": "1e44a037bbf4fcaba041436e65e87be88f3f495b", "type": "github" }, "original": { @@ -51,7 +40,7 @@ "root": { "inputs": { "naersk": "naersk", - "nixpkgs": "nixpkgs_2", + "nixpkgs": "nixpkgs", "utils": "utils" } }, @@ -75,11 +64,11 @@ "systems": "systems" }, "locked": { - "lastModified": 1687171271, - "narHash": "sha256-BJlq+ozK2B1sJDQXS3tzJM5a+oVZmi1q0FlBK/Xqv7M=", + "lastModified": 1692799911, + "narHash": "sha256-3eihraek4qL744EvQXsK1Ha6C3CR7nnT8X2qWap4RNk=", "owner": "numtide", "repo": "flake-utils", - "rev": "abfb11bd1aec8ced1c9bb9adfe68018230f4fb3c", + "rev": "f9e7cf818399d17d347f847525c5a5a8032e4e44", "type": "github" }, "original": { diff --git a/flake.nix b/flake.nix index 5be9bfb..1e4f204 100644 --- a/flake.nix +++ b/flake.nix @@ -1,8 +1,9 @@ { inputs = { - naersk.url = "github:nix-community/naersk/master"; nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable"; utils.url = "github:numtide/flake-utils"; + naersk.url = "github:nix-community/naersk/master"; + naersk.inputs.nixpkgs.follows = "nixpkgs"; }; outputs = { self, nixpkgs, utils, naersk }: From 323c2fba18bc0d46f9582b53b99d87f68afddc2d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Moritz=20B=C3=B6hme?= Date: Sat, 26 Aug 2023 09:43:59 +0200 Subject: [PATCH 3/5] fix: add missing line break --- src/pomodoro.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/pomodoro.rs b/src/pomodoro.rs index 8cd5103..0c051cd 100644 --- a/src/pomodoro.rs +++ b/src/pomodoro.rs @@ -19,7 +19,7 @@ impl Display for Pomodoro { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!( f, - "Pomodoro ({}, {}, {}) currently {} with {} remaining.", + "Pomodoro ({}, {}, {}) currently {} with {} remaining.\n", humantime::format_duration(self.work), humantime::format_duration(self.pause), humantime::format_duration(self.long_pause), From 1a10f620b448b2a503aed405b9782f43d51e1d94 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Moritz=20B=C3=B6hme?= Date: Sat, 26 Aug 2023 11:06:51 +0200 Subject: [PATCH 4/5] feat: add argument to output answers as json --- Cargo.lock | 34 +++++++++++++++++++++++++++++----- Cargo.toml | 1 + src/cli.rs | 4 ++++ src/main.rs | 6 +++++- 4 files changed, 39 insertions(+), 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 78b6613..0335743 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -651,6 +651,12 @@ dependencies = [ "windows-sys", ] +[[package]] +name = "itoa" +version = "1.0.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "af150ab688ff2122fcef229be89cb50dd66af9e01a4ff320cc137eecc9bacc38" + [[package]] name = "libc" version = "0.2.147" @@ -976,10 +982,16 @@ dependencies = [ ] [[package]] -name = "serde" -version = "1.0.164" +name = "ryu" +version = "1.0.15" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9e8c8cf938e98f769bc164923b06dce91cea1751522f46f8466461af04c9027d" +checksum = "1ad4cc8da4ef723ed60bced201181d83791ad433213d8c24efffda1eec85d741" + +[[package]] +name = "serde" +version = "1.0.179" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0a5bf42b8d227d4abf38a1ddb08602e229108a517cd4e5bb28f9c7eaafdce5c0" dependencies = [ "serde_derive", ] @@ -996,15 +1008,26 @@ dependencies = [ [[package]] name = "serde_derive" -version = "1.0.164" +version = "1.0.179" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d9735b638ccc51c28bf6914d90a2e9725b377144fc612c49a611fddd1b631d68" +checksum = "741e124f5485c7e60c03b043f79f320bff3527f4bbf12cf3831750dc46a0ec2c" dependencies = [ "proc-macro2", "quote", "syn 2.0.27", ] +[[package]] +name = "serde_json" +version = "1.0.105" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "693151e1ac27563d6dbcec9dee9fbd5da8539b20fa14ad3752b2e6d363ace360" +dependencies = [ + "itoa", + "ryu", + "serde", +] + [[package]] name = "serde_repr" version = "0.1.16" @@ -1170,6 +1193,7 @@ dependencies = [ "notify-rust", "serde", "serde_cbor", + "serde_json", "signal-hook", "thiserror", ] diff --git a/Cargo.toml b/Cargo.toml index 8c2f0e2..e8603cb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,5 +14,6 @@ libc = "0.2.147" notify-rust = "4.8.0" serde = { version = "1.0.164", features = ["derive"] } serde_cbor = "0.11.2" +serde_json = "1.0.105" signal-hook = "0.3.17" thiserror = "1.0.44" diff --git a/src/cli.rs b/src/cli.rs index 08daac2..549024c 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -12,9 +12,13 @@ use std::time::Duration; pub struct Cli { #[command(subcommand)] pub command: Command, + #[arg(short, long)] #[clap(default_value_t = format!("{}/timers.socket", run_path()))] pub socket: String, + + #[arg(long)] + pub json: bool } #[derive(Debug, Subcommand)] diff --git a/src/main.rs b/src/main.rs index 2249f05..07a5b27 100644 --- a/src/main.rs +++ b/src/main.rs @@ -48,6 +48,10 @@ fn main() -> Result<(), anyhow::Error> { } }; let answer = send_command(&args.socket, daemon_command)?; - print!("{}", answer); + if args.json { + println!("{}", serde_json::to_string(&answer)?) + } else { + print!("{}", answer); + }; Ok(()) } From 739f76fa90271860e2c1d683e3a53cfb93e6eb89 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Moritz=20B=C3=B6hme?= Date: Sat, 26 Aug 2023 11:08:45 +0200 Subject: [PATCH 5/5] feat: better listing of timers --- src/pomodoro.rs | 3 ++- src/timer.rs | 21 ++++++++++++++++++--- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/src/pomodoro.rs b/src/pomodoro.rs index 0c051cd..7831dda 100644 --- a/src/pomodoro.rs +++ b/src/pomodoro.rs @@ -19,10 +19,11 @@ impl Display for Pomodoro { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!( f, - "Pomodoro ({}, {}, {}) currently {} with {} remaining.\n", + "Pomodoro ({}, {}, {}) currently {} {} with {} remaining.\n", humantime::format_duration(self.work), humantime::format_duration(self.pause), humantime::format_duration(self.long_pause), + self.timer.state, self.status, humantime::format_duration(self.timer.remaining()) ) diff --git a/src/timer.rs b/src/timer.rs index af2ca46..4326023 100644 --- a/src/timer.rs +++ b/src/timer.rs @@ -11,7 +11,7 @@ pub struct Timer { #[serde(with = "approx_instant")] start: Instant, duration: Duration, - state: State, + pub state: State, } #[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone)] @@ -20,6 +20,16 @@ pub enum State { Paused, } +impl Display for State { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + let string = match self { + State::Running => "running", + State::Paused => "paused", + }; + write!(f, "{}", string) + } +} + impl Timer { /// Create a new [`Timer`] with the supplied name and duration /// The timer is instantly started. @@ -72,10 +82,16 @@ impl Timer { impl Display for Timer { fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), std::fmt::Error> { + let state_message = match self.state { + State::Running => "has", + State::Paused => "is paused and has", + }; + write!( f, - "{} has {} remaining.", + "'{}' {} {} remaining.", self.name, + state_message, humantime::format_duration(self.remaining()) ) } @@ -107,4 +123,3 @@ mod approx_instant { Ok(instant) } } -