Skip to content

Commit

Permalink
feat(cli): add completion (#86)
Browse files Browse the repository at this point in the history
Co-authored-by: sgoudham <sgoudham@gmail.com>
  • Loading branch information
nekowinston and sgoudham committed Nov 5, 2023
1 parent 9aef1af commit a9a79c6
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 11 deletions.
5 changes: 3 additions & 2 deletions Cargo.lock

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

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "mdbook-catppuccin"
authors = ["Goudham Suresh <sgoudham@gmail.com>", "winston <hey@winston.sh>"]
description = "A mdbook preprocessor that exposes the catppuccin flavours as available themes!"
description = "🎊 Soothing pastel theme for mdBook"
version = "2.0.1"
edition = "2021"
license = "MIT"
Expand All @@ -20,6 +20,7 @@ path = "src/lib.rs"

[dependencies]
clap = { version = "4.4.6", features = ["cargo"] }
clap_complete = "4.4.4"
mdbook = { version = "0.4.35", default_features = false }
env_logger = "0.10.0"
log = "0.4.20"
Expand Down
48 changes: 40 additions & 8 deletions src/bin/mdbook-catppuccin.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
use std::{io, process};

use clap::{command, crate_version, Arg, ArgAction, ArgMatches, Command};
use clap::{
command, crate_authors, crate_description, crate_version, value_parser, Arg, ArgAction,
ArgMatches, Command,
};
use clap_complete::{generate, Generator, Shell};
use mdbook::errors::Error;
use mdbook::preprocess::{CmdPreprocessor, Preprocessor};
use semver::{Version, VersionReq};
Expand All @@ -11,6 +15,16 @@ fn main() {
env_logger::init_from_env(env_logger::Env::default().default_filter_or("info"));
let matches = make_app().get_matches();

if let Some(generator) = matches.subcommand_matches("completion") {
let generator = generator
.get_one::<Shell>("shell")
.expect("Shell argument is required");
let mut cmd = make_app();
eprintln!("Generating completion file for {generator}...");
print_completions(*generator, &mut cmd);
return;
}

let preprocessor = Catppuccin::new();

if let Some(sub_args) = matches.subcommand_matches("supports") {
Expand All @@ -23,22 +37,30 @@ fn main() {
}
}

fn print_completions<G: Generator>(gen: G, cmd: &mut Command) {
generate(gen, cmd, cmd.get_name().to_string(), &mut io::stdout());
}

pub fn make_app() -> Command {
command!()
.name("mdbook-catppuccin")
.about("A mdbook preprocessor that implements catppuccin flavours as default themes")
.about(crate_description!())
.author(crate_authors!())
.version(crate_version!())
.subcommand_required(true)
.subcommand(
Command::new("supports")
.arg(Arg::new("renderer").required(true))
command!("supports")
.arg(
Arg::new("renderer").required(true)
)
.about("Check whether a renderer is supported by this preprocessor"),
)
.subcommand(
Command::new("install")
command!("install")
.arg(
Arg::new("dir")
.default_value(".")
.help("Root directory for the book, this should contain the configuration file `book.toml`")
.default_value(".")
.help("Root directory for the book, this should contain the configuration file `book.toml`")
)
.arg(
Arg::new("force")
Expand All @@ -49,6 +71,16 @@ pub fn make_app() -> Command {
)
.about("Install the necessary files needed and update the config to include them"),
)
.subcommand(
command!("completion")
.arg(
Arg::new("shell")
.action(ArgAction::Set)
.required(true)
.value_parser(value_parser!(Shell))
)
.about("Generate shell completion scripts")
)
}

fn handle_preprocessing(pre: &Catppuccin) -> Result<(), Error> {
Expand Down Expand Up @@ -145,7 +177,7 @@ mod install {

fn read_configuration_file(toml_config: &PathBuf) -> (String, Document) {
info!("Reading configuration file '{}'", toml_config.display());
let toml = fs::read_to_string(&toml_config).expect("Can't read configuration file");
let toml = fs::read_to_string(toml_config).expect("Can't read configuration file");
let document = toml
.parse::<Document>()
.expect("Configuration is not valid TOML");
Expand Down

0 comments on commit a9a79c6

Please sign in to comment.