mirror of
https://github.com/pimalaya/himalaya.git
synced 2026-06-17 13:17:55 +08:00
use cargo workspace feature (#340)
For now, everything has been moved as it is in the "cli" workspace. The next step is to separate the "lib" from the "cli".
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
//! Module related to completion CLI.
|
||||
//!
|
||||
//! This module provides subcommands and a command matcher related to completion.
|
||||
|
||||
use anyhow::Result;
|
||||
use clap::{self, App, Arg, ArgMatches, Shell, SubCommand};
|
||||
use log::{debug, info};
|
||||
|
||||
type OptionShell<'a> = Option<&'a str>;
|
||||
|
||||
/// Completion commands.
|
||||
pub enum Command<'a> {
|
||||
/// Generate completion script for the given shell slice.
|
||||
Generate(OptionShell<'a>),
|
||||
}
|
||||
|
||||
/// Completion command matcher.
|
||||
pub fn matches<'a>(m: &'a ArgMatches) -> Result<Option<Command<'a>>> {
|
||||
info!("entering completion command matcher");
|
||||
|
||||
if let Some(m) = m.subcommand_matches("completion") {
|
||||
info!("completion command matched");
|
||||
let shell = m.value_of("shell");
|
||||
debug!("shell: {:?}", shell);
|
||||
return Ok(Some(Command::Generate(shell)));
|
||||
};
|
||||
|
||||
Ok(None)
|
||||
}
|
||||
|
||||
/// Completion subcommands.
|
||||
pub fn subcmds<'a>() -> Vec<App<'a, 'a>> {
|
||||
vec![SubCommand::with_name("completion")
|
||||
.aliases(&["completions", "compl", "compe", "comp"])
|
||||
.about("Generates the completion script for the given shell")
|
||||
.args(&[Arg::with_name("shell")
|
||||
.possible_values(&Shell::variants()[..])
|
||||
.required(true)])]
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
//! Module related to completion handling.
|
||||
//!
|
||||
//! This module gathers all completion commands.
|
||||
|
||||
use anyhow::{anyhow, Context, Result};
|
||||
use clap::{App, Shell};
|
||||
use log::{debug, info};
|
||||
use std::{io, str::FromStr};
|
||||
|
||||
/// Generates completion script from the given [`clap::App`] for the given shell slice.
|
||||
pub fn generate<'a>(mut app: App<'a, 'a>, shell: Option<&'a str>) -> Result<()> {
|
||||
info!("entering generate completion handler");
|
||||
|
||||
let shell = Shell::from_str(shell.unwrap_or_default())
|
||||
.map_err(|err| anyhow!(err))
|
||||
.context("cannot parse shell")?;
|
||||
debug!("shell: {}", shell);
|
||||
|
||||
app.gen_completions_to("himalaya", shell, &mut io::stdout());
|
||||
Ok(())
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
//! Module related to shell completion.
|
||||
//!
|
||||
//! This module allows users to generate autocompletion scripts for their shells. You can see the
|
||||
//! list of available shells directly on the [clap's docs.rs website].
|
||||
//!
|
||||
//! [clap's docs.rs website]: https://docs.rs/clap/2.33.3/clap/enum.Shell.html
|
||||
|
||||
pub mod compl_args;
|
||||
pub mod compl_handlers;
|
||||
Reference in New Issue
Block a user