Skip to content

Commit

Permalink
Merge pull request #85 from Anomalocaridid/generate-shell-completions
Browse files Browse the repository at this point in the history
feat: generate shell completions
  • Loading branch information
Anomalocaridid authored Oct 13, 2024
2 parents ad61b00 + 6e21328 commit 034d6e5
Show file tree
Hide file tree
Showing 21 changed files with 311 additions and 245 deletions.
1 change: 1 addition & 0 deletions .envrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
use flake
13 changes: 13 additions & 0 deletions .github/workflows/build_nix.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
name: "Build legacy Nix package on Ubuntu"

on:
push:

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: cachix/install-nix-action@v26
- name: Building package
run: nix build
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
/target
/mutants.out*
/release
/result
38 changes: 30 additions & 8 deletions Cargo.lock

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

4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ serde_ini = "0.2.0"
serde_with = "3.8.3"
wildmatch = "2.3.4"
mutants = "0.0.3"
clap_complete = { version = "4.5.33", features = ["unstable-dynamic"] }

[[bin]]
name = "handlr"
Expand All @@ -44,7 +45,10 @@ pretty_assertions = "1.4.0"

[build-dependencies]
clap = { version = "4.5.2", features = ["derive"] }
clap_complete = { version = "4.5.33", features = ["unstable-dynamic"] }
clap_mangen = "0.2.20"
mime-db = "1.3.0"
mutants = "0.0.3"

[profile.release]
opt-level = "z"
Expand Down
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,17 @@ This is off by default and will not automatically expand wildcards already prese

In addition, regardless of settings, literal wildcards are preferred when using `handlr remove` and `handlr unset`. (e.g. When using `handlr remove text/*`, if `text/*` is present, it will be removed, but `text/plain`, etc. will not be.)

## Completion scripts

To generate a shell completion script, run `COMPLETE=<shell> handlr`, where `<shell>` is the name of the target shell (e.g. bash, zsh, fish, elvish, powershell, etc.). Note that this will only print it to stdout rather than creating a file or installing the script automatically.

If you usually install `handlr-regex` from your distribution's repository, and you are not involved with packaging it, you probably do not need to worry about this.

See [`clap_complete`'s documentation](https://docs.rs/clap_complete/latest/clap_complete/aot/enum.Shell.html) for the list of currently supported shells.

> [!NOTE]
> This currently relies on unstable features of the `clap_complete` crate and may potentially change in the future.
## Screenshots

<table><tr><td>
Expand Down
77 changes: 0 additions & 77 deletions assets/completions/_handlr

This file was deleted.

34 changes: 0 additions & 34 deletions assets/completions/handlr

This file was deleted.

32 changes: 0 additions & 32 deletions assets/completions/handlr.fish

This file was deleted.

19 changes: 19 additions & 0 deletions build/apps.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// This file exists solely to trick build script into working
// These types are used by cli.rs, which cannot be transitively imported
// because they rely on their own dependencies and so on

use std::error::Error;

pub struct SystemApps;
pub struct DesktopEntry {
pub name: String,
}

impl SystemApps {
pub fn get_entries(
) -> Result<impl Iterator<Item = (String, DesktopEntry)>, Box<dyn Error>>
{
let name = "".to_string();
Ok(vec![(name.clone(), DesktopEntry { name })].into_iter())
}
}
4 changes: 4 additions & 0 deletions build/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,7 @@
pub type DesktopHandler = String;
pub type MimeOrExtension = String;
pub type UserPath = String;

pub fn mime_types() -> Vec<String> {
vec!["".to_string()]
}
33 changes: 11 additions & 22 deletions build/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
mod cli {
include!("../src/cli.rs");
}
mod common; // Trick the cli module into cooperating

// Trick the cli module into cooperating
mod apps;
mod common;

use cli::Cmd;

Expand All @@ -10,43 +13,29 @@ use std::{
env,
error::Error,
fs::{create_dir_all, remove_file},
path::{Path, PathBuf},
path::Path,
};

type DynResult = Result<(), Box<dyn Error>>;

fn main() -> DynResult {
println!("cargo:rerun-if-changed=build/");
mangen()
let out_dir = Path::new(&env::var("OUT_DIR")?).to_path_buf();
mangen(&out_dir)
}

/// Generate man page for binary and subcommands
fn mangen() -> DynResult {
fn mangen(out_dir: &Path) -> DynResult {
println!("cargo:rerun-if-env-changed=PROJECT_NAME");
println!("cargo:rerun-if-env-changed=PROJECT_EXECUTABLE");
println!("cargo:rerun-if-env-changed=CARGO_PKG_VERSION");

eprintln!("Generating man pages");

let out_dir = release_dir().join("manual/man1");
let cmd = Cmd::command().name("handlr");

create_dir_all(&out_dir)?;
let dest_dir = out_dir.join("manual/man1");
create_dir_all(&dest_dir)?;

clap_mangen::generate_to(cmd, &out_dir)?;

// Remove hidden subcommand's manpage
remove_file(out_dir.join("handlr-autocomplete.1"))?;
clap_mangen::generate_to(Cmd::command().name("handlr"), &dest_dir)?;

Ok(())
}

// Project root
fn project_root() -> PathBuf {
Path::new(&env!("CARGO_MANIFEST_DIR")).to_path_buf()
}

/// Output directory for generated assets
fn release_dir() -> PathBuf {
project_root().join("release")
}
7 changes: 7 additions & 0 deletions default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
(import (
fetchTarball {
url = "https://github.com/edolstra/flake-compat/archive/99f1c2157fba4bfe6211a321fd0ee43199025dbf.tar.gz";
sha256 = "0x2jn3vrawwv9xp15674wjz9pixwjyj3j771izayl962zziivbx2"; }
) {
src = ./.;
}).defaultNix
Loading

0 comments on commit 034d6e5

Please sign in to comment.