Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ serde = "1.0.144"
reqwest = { version = "0.11", features = ["json"] }
colored = "2"
bincode = { version = "2.0.0-rc.1", features = ["alloc"]}
clap_complete = "3.2.2"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Question]
Currently clap_complete's latest version is 4.1.4. (doc link) Is there a reason to use the version 3.2.2? Maybe rust-cli-pomodoro's clap version is 3.2.22 ?

Copy link
Contributor Author

@TheRustyPickle TheRustyPickle Mar 1, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct, it's related to the clap's version. If I use the latest version I get some kind of error with generate which I don't understand. Something to do with the clap's main command generation


[[bin]]
name = "pomodoro"
Expand Down
4 changes: 4 additions & 0 deletions src/command/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ pub const DEFAULT_BREAK_TIME: u16 = 5;
pub enum CommandType {
StartUp(Arc<Configuration>),
UdsClient(ArgMatches),
AutoComplete(ArgMatches),
}

pub fn get_start_and_uds_client_command() -> Command<'static> {
Expand Down Expand Up @@ -94,6 +95,9 @@ fn get_common_subcommands() -> Vec<Command<'static>> {
.about("list notifications"),
Command::new(ActionType::History).about("show archived notifications"),
Command::new(ActionType::Test).about("test notification"),
Command::new("completion")
.about("generate completions for shells")
.arg(Arg::new("shell").possible_values(["fish", "zsh", "bash"])),
]
}

Expand Down
18 changes: 16 additions & 2 deletions src/command/util.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use std::io::{self, BufRead, Write};
use std::str::FromStr;

use clap::ArgMatches;

use crate::command::{DEFAULT_BREAK_TIME, DEFAULT_WORK_TIME};
use crate::error::ParseError;
use clap::ArgMatches;
use clap_complete::Shell;

pub fn parse_work_and_break_time(matches: &ArgMatches) -> Result<(u16, u16), ParseError> {
let (work_time, break_time) = if matches.is_present("default") {
Expand All @@ -19,6 +19,20 @@ pub fn parse_work_and_break_time(matches: &ArgMatches) -> Result<(u16, u16), Par
Ok((work_time, break_time))
}

pub fn parse_shell(matches: &ArgMatches) -> Option<Shell> {
let shell = matches.value_of("shell");
if let Some(shell) = shell {
match shell {
"fish" => Some(Shell::Fish),
"zsh" => Some(Shell::Zsh),
"bash" => Some(Shell::Bash),
_ => None,
}
} else {
None
}
}

pub fn parse_arg<C>(arg_matches: &ArgMatches, arg_name: &str) -> Result<C, ParseError>
where
C: FromStr,
Expand Down
23 changes: 21 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use chrono::Utc;
use clap_complete::generate;
use gluesql::prelude::{Glue, MemoryStorage};
use std::collections::HashMap;
use std::error::Error;
Expand Down Expand Up @@ -152,6 +153,18 @@ async fn main() -> Result<(), Box<dyn Error>> {

handler::uds_client::handle(matches, socket).await?;
}
CommandType::AutoComplete(sub_matches) => {
if sub_matches.is_present("shell") {
if let Some(shell) = util::parse_shell(&sub_matches) {
let mut main_command = command::get_main_command();
let bin_name = main_command.get_name().to_string();
let mut stdout = std::io::stdout();
generate(shell, &mut main_command, bin_name, &mut stdout);
}
} else {
println!("no shell name was passed");
}
}
}

debug!("handle_uds_client_command called successfully");
Expand All @@ -163,9 +176,15 @@ async fn detect_command_type() -> Result<CommandType, ConfigurationError> {
let matches = command::get_start_and_uds_client_command().get_matches();
debug!("handle_uds_client_command, matches: {:?}", &matches);

let command_type = match (&matches).subcommand().is_none() {
let command_type = match matches.subcommand().is_none() {
true => CommandType::StartUp(get_configuration(&matches)?),
false => CommandType::UdsClient(matches),
false => {
if let Some(val) = matches.subcommand_matches("completion") {
CommandType::AutoComplete(val.to_owned())
} else {
CommandType::UdsClient(matches)
}
}
};

Ok(command_type)
Expand Down