feat: add shell completions

This commit is contained in:
Moritz Böhme 2023-08-25 18:18:45 +02:00
parent 00d3c0ce84
commit f7aa8942e0
Signed by: moritz
GPG key ID: 970C6E89EB0547A9
5 changed files with 66 additions and 3 deletions

42
Cargo.lock generated
View file

@ -284,6 +284,47 @@ dependencies = [
"strsim", "strsim",
] ]
[[package]]
name = "clap_complete"
version = "4.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "586a385f7ef2f8b4d86bddaa0c094794e7ccbfe5ffef1f434fe928143fc783a5"
dependencies = [
"clap",
]
[[package]]
name = "clap_complete_command"
version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "183495371ea78d4c9ff638bfc6497d46fed2396e4f9c50aebc1278a4a9919a3d"
dependencies = [
"clap",
"clap_complete",
"clap_complete_fig",
"clap_complete_nushell",
]
[[package]]
name = "clap_complete_fig"
version = "4.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9e9bae21b3f6eb417ad3054c8b1094aa0542116eba4979b1b271baefbfa6b965"
dependencies = [
"clap",
"clap_complete",
]
[[package]]
name = "clap_complete_nushell"
version = "0.1.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5d02bc8b1a18ee47c4d2eec3fb5ac034dc68ebea6125b1509e9ccdffcddce66e"
dependencies = [
"clap",
"clap_complete",
]
[[package]] [[package]]
name = "clap_derive" name = "clap_derive"
version = "4.3.2" version = "4.3.2"
@ -1123,6 +1164,7 @@ version = "0.1.0"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"clap", "clap",
"clap_complete_command",
"humantime", "humantime",
"libc", "libc",
"notify-rust", "notify-rust",

View file

@ -8,6 +8,7 @@ edition = "2021"
[dependencies] [dependencies]
anyhow = "1.0.71" anyhow = "1.0.71"
clap = { version = "4.3.4", features = ["derive"] } clap = { version = "4.3.4", features = ["derive"] }
clap_complete_command = "0.5.1"
humantime = "2.1.0" humantime = "2.1.0"
libc = "0.2.147" libc = "0.2.147"
notify-rust = "4.8.0" notify-rust = "4.8.0"

View file

@ -14,7 +14,14 @@
{ {
packages.default = naersk-lib.buildPackage { packages.default = naersk-lib.buildPackage {
src = ./.; src = ./.;
nativeBuildInputs = with pkgs; [ installShellFiles ];
meta.mainProgram = "timers"; meta.mainProgram = "timers";
postInstall = ''
installShellCompletion --cmd timers \
--bash <($out/bin/timers completions bash) \
--fish <($out/bin/timers completions fish) \
--zsh <($out/bin/timers completions zsh) \
'';
}; };
devShells.default = with pkgs; mkShell { devShells.default = with pkgs; mkShell {
buildInputs = [ cargo rustc rustfmt pre-commit rustPackages.clippy rust-analyzer ]; buildInputs = [ cargo rustc rustfmt pre-commit rustPackages.clippy rust-analyzer ];

View file

@ -1,5 +1,5 @@
use crate::daemon::{Answer, AnswerErr, Command as OtherCommand}; use crate::daemon::{Answer, AnswerErr, Command as OtherCommand};
use crate::run_path; use crate::helper::run_path;
use anyhow::{Context, Result}; use anyhow::{Context, Result};
use clap::{Parser, Subcommand}; use clap::{Parser, Subcommand};
use std::net::Shutdown; use std::net::Shutdown;
@ -56,6 +56,12 @@ pub enum Command {
#[command(subcommand)] #[command(subcommand)]
#[clap(visible_alias = "p")] #[clap(visible_alias = "p")]
Pomodoro(PomodoroCommand), Pomodoro(PomodoroCommand),
/// Shell completions
Completions {
#[arg(value_enum)]
shell: clap_complete_command::Shell,
}
} }
#[derive(Debug, Subcommand)] #[derive(Debug, Subcommand)]

View file

@ -7,14 +7,17 @@ mod timer;
use crate::cli::{send_command, Cli, Command as CliCommand}; use crate::cli::{send_command, Cli, Command as CliCommand};
use crate::daemon::{Command as DaemonCommand, Daemon}; use crate::daemon::{Command as DaemonCommand, Daemon};
use crate::helper::run_path; use clap::CommandFactory;
use clap::Parser; use clap::Parser;
use cli::PomodoroCommand; use cli::PomodoroCommand;
fn main() -> Result<(), anyhow::Error> { fn main() -> Result<(), anyhow::Error> {
let args = Cli::parse(); let args = Cli::parse();
let daemon_command = match args.command { let daemon_command = match args.command {
CliCommand::Daemon { no_notify, pid_file } => { CliCommand::Daemon {
no_notify,
pid_file,
} => {
return Daemon::new(args.socket, pid_file, no_notify)?.run(); return Daemon::new(args.socket, pid_file, no_notify)?.run();
} }
CliCommand::Add { name, duration } => { CliCommand::Add { name, duration } => {
@ -39,6 +42,10 @@ fn main() -> Result<(), anyhow::Error> {
PomodoroCommand::List => DaemonCommand::PomodoroList, PomodoroCommand::List => DaemonCommand::PomodoroList,
PomodoroCommand::Toggle => DaemonCommand::PomodoroToggle, PomodoroCommand::Toggle => DaemonCommand::PomodoroToggle,
}, },
CliCommand::Completions { shell } => {
shell.generate(&mut Cli::command(), &mut std::io::stdout());
return Ok(());
}
}; };
let answer = send_command(&args.socket, daemon_command)?; let answer = send_command(&args.socket, daemon_command)?;
print!("{}", answer); print!("{}", answer);