From 845fe79b5e14a23b2f473ef04bfd4308f83288db Mon Sep 17 00:00:00 2001 From: Ajeet D'Souza <98ajeet@gmail.com> Date: Thu, 13 Feb 2025 13:00:58 +0530 Subject: [PATCH] Lazy initialize help template --- build.rs | 2 +- src/cmd/cmd.rs | 25 ++++++++++++++++--------- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/build.rs b/build.rs index 5831b2d..bf6dbbf 100644 --- a/build.rs +++ b/build.rs @@ -3,7 +3,7 @@ mod cmd; use std::{env, io}; -use clap::CommandFactory; +use clap::CommandFactory as _; use clap_complete::shells::{Bash, Elvish, Fish, PowerShell, Zsh}; use clap_complete_fig::Fig; use cmd::Cmd; diff --git a/src/cmd/cmd.rs b/src/cmd/cmd.rs index 61ecbaa..cff7e79 100644 --- a/src/cmd/cmd.rs +++ b/src/cmd/cmd.rs @@ -2,9 +2,14 @@ use std::path::PathBuf; +use clap::builder::{IntoResettable, Resettable, StyledStr}; use clap::{Parser, Subcommand, ValueEnum, ValueHint}; -const HELP_TEMPLATE: &str = color_print::cstr!("\ +struct HelpTemplate; + +impl IntoResettable for HelpTemplate { + fn into_resettable(self) -> Resettable { + color_print::cstr!("\ {before-help}{name} {version} {author} https://github.com/ajeetdsouza/zoxide @@ -22,13 +27,15 @@ https://github.com/ajeetdsouza/zoxide {tab}_ZO_EXCLUDE_DIRS {tab}List of directory globs to be excluded {tab}_ZO_FZF_OPTS {tab}Custom flags to pass to fzf {tab}_ZO_MAXAGE {tab}Maximum total age after which entries start getting deleted -{tab}_ZO_RESOLVE_SYMLINKS{tab}Resolve symlinks when storing paths"); +{tab}_ZO_RESOLVE_SYMLINKS{tab}Resolve symlinks when storing paths").into_resettable() + } +} #[derive(Debug, Parser)] #[clap( about, author, - help_template = HELP_TEMPLATE, + help_template = HelpTemplate, disable_help_subcommand = true, propagate_version = true, version, @@ -46,7 +53,7 @@ pub enum Cmd { #[derive(Debug, Parser)] #[clap( author, - help_template = HELP_TEMPLATE, + help_template = HelpTemplate, )] pub struct Add { #[clap(num_args = 1.., required = true, value_hint = ValueHint::DirPath)] @@ -57,7 +64,7 @@ pub struct Add { #[derive(Debug, Parser)] #[clap( author, - help_template = HELP_TEMPLATE, + help_template = HelpTemplate, )] pub struct Edit { #[clap(subcommand)] @@ -80,7 +87,7 @@ pub enum EditCommand { #[derive(Debug, Parser)] #[clap( author, - help_template = HELP_TEMPLATE, + help_template = HelpTemplate, )] pub struct Import { #[clap(value_hint = ValueHint::FilePath)] @@ -106,7 +113,7 @@ pub enum ImportFrom { #[derive(Debug, Parser)] #[clap( author, - help_template = HELP_TEMPLATE, + help_template = HelpTemplate, )] pub struct Init { #[clap(value_enum)] @@ -149,7 +156,7 @@ pub enum InitShell { #[derive(Debug, Parser)] #[clap( author, - help_template = HELP_TEMPLATE, + help_template = HelpTemplate, )] pub struct Query { pub keywords: Vec, @@ -179,7 +186,7 @@ pub struct Query { #[derive(Debug, Parser)] #[clap( author, - help_template = HELP_TEMPLATE, + help_template = HelpTemplate, )] pub struct Remove { #[clap(value_hint = ValueHint::DirPath)]