feat: add shell completions

This commit is contained in:
Moritz Böhme 2023-08-25 18:18:45 +02:00
parent 00d3c0ce84
commit f7aa8942e0
No known key found for this signature in database
GPG key ID: 970C6E89EB0547A9
5 changed files with 66 additions and 3 deletions

View file

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

View file

@ -7,14 +7,17 @@ mod timer;
use crate::cli::{send_command, Cli, Command as CliCommand};
use crate::daemon::{Command as DaemonCommand, Daemon};
use crate::helper::run_path;
use clap::CommandFactory;
use clap::Parser;
use cli::PomodoroCommand;
fn main() -> Result<(), anyhow::Error> {
let args = Cli::parse();
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();
}
CliCommand::Add { name, duration } => {
@ -39,6 +42,10 @@ fn main() -> Result<(), anyhow::Error> {
PomodoroCommand::List => DaemonCommand::PomodoroList,
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)?;
print!("{}", answer);