Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

50 add shell completion #52

Merged
merged 2 commits into from
Apr 19, 2022
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 @@ -37,3 +37,4 @@ clap = { version = "3.1.9", features = ["derive", "env", "wrap_help"] }
clap-verbosity-flag = "1.0.0"

clap_mangen = "0.1.3"
clap_complete = "3.1.1"
34 changes: 29 additions & 5 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,12 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use crate::cli::Cli;
use clap::CommandFactory;
use std::{env, fs, io, path};
use clap_complete::{generate_to, Shell};
use clap_mangen::Man;
use std::path::Path;
use std::{env, fs, io, path::PathBuf};

#[path = "src/cli.rs"]
mod cli;
Expand All @@ -22,14 +26,34 @@ mod cli;
fn main() -> io::Result<()> {
println!("cargo:rerun-if-changed=src/cli.rs");

let out_dir = path::PathBuf::from(env::var_os("OUT_DIR").ok_or(io::ErrorKind::NotFound)?);
let cmd = cli::Cli::command();
let out_dir = PathBuf::from(env::var_os("OUT_DIR").ok_or(io::ErrorKind::NotFound)?);

let man = clap_mangen::Man::new(cmd);
generate_man(&out_dir)?;
generate_completion(&out_dir)?;

Ok(())
}

fn generate_completion(out_dir: &Path) -> io::Result<()> {
// We generate Elvish shell completion for anyone who wants to manually install it,
// but Homebrew is unable to install Elvish shell completion.
let shells = &[Shell::Bash, Shell::Elvish, Shell::Fish, Shell::Zsh];

for shell in shells {
let mut cmd = Cli::command();
generate_to(*shell, &mut cmd, "xshe", out_dir)?;
}
Ok(())
}

fn generate_man(out_dir: &Path) -> io::Result<()> {
let cmd = Cli::command();

// Generate Man File
let man = Man::new(cmd);
let mut buffer: Vec<u8> = Default::default();
man.render(&mut buffer)?;

fs::write(out_dir.join("xshe.1"), buffer)?;

Ok(())
}