Skip to content

Commit

Permalink
auto man&completion: add dynamic + add cat example
Browse files Browse the repository at this point in the history
  • Loading branch information
Its-Just-Nans committed Jul 5, 2024
1 parent 724eca9 commit 28505c3
Show file tree
Hide file tree
Showing 25 changed files with 759 additions and 322 deletions.
328 changes: 164 additions & 164 deletions Cargo.toml

Large diffs are not rendered by default.

19 changes: 16 additions & 3 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,34 @@
// file that was distributed with this source code.

// spell-checker:ignore (vars) krate
use clap::Command;
use clap_complete::{generate_to, shells};
use clap_mangen::Man;
use std::collections::HashMap;
use std::env;
use std::fs::File;
use std::io::Write;
use std::path::Path;

include!("./src/args.rs");

macro_rules! collect_functions {
($($module:ident),*) => {{
let mut map: HashMap<&'static str, fn() -> Command> = HashMap::new();
$(
map.insert(stringify!($module), $module::uu_app);
)*
map
}};
}

pub fn generate_manpages(_crates: &Vec<String>) -> Result<(), std::io::Error> {
let crates = vec!["arch"];
for one_crate in crates {
let crates = collect_functions!(uu_arch, uu_cat);
println!("{:?}", crates);
for (one_crate, args_fn) in crates {
let app_name = one_crate;
let outdir = "completion";
let mut cmd = uu_arch::uu_app();
let mut cmd = args_fn();

generate_to(shells::Bash, &mut cmd, app_name, outdir)?;
generate_to(shells::Zsh, &mut cmd, app_name, outdir)?;
Expand Down
35 changes: 35 additions & 0 deletions completion/_uu_arch
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#compdef uu_arch

autoload -U is-at-least

_uu_arch() {
typeset -A opt_args
typeset -a _arguments_options
local ret=1

if is-at-least 5.2; then
_arguments_options=(-s -S -C)
else
_arguments_options=(-s -C)
fi

local context curcontext="$curcontext" state line
_arguments "${_arguments_options[@]}" \
'-h[Print help]' \
'--help[Print help]' \
'-V[Print version]' \
'--version[Print version]' \
&& ret=0
}

(( $+functions[_uu_arch_commands] )) ||
_uu_arch_commands() {
local commands; commands=()
_describe -t commands 'uu_arch commands' commands "$@"
}

if [ "$funcstack[1]" = "_uu_arch" ]; then
_uu_arch "$@"
else
compdef _uu_arch uu_arch
fi
34 changes: 34 additions & 0 deletions completion/_uu_arch.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@

using namespace System.Management.Automation
using namespace System.Management.Automation.Language

Register-ArgumentCompleter -Native -CommandName 'uu_arch' -ScriptBlock {
param($wordToComplete, $commandAst, $cursorPosition)

$commandElements = $commandAst.CommandElements
$command = @(
'uu_arch'
for ($i = 1; $i -lt $commandElements.Count; $i++) {
$element = $commandElements[$i]
if ($element -isnot [StringConstantExpressionAst] -or
$element.StringConstantType -ne [StringConstantType]::BareWord -or
$element.Value.StartsWith('-') -or
$element.Value -eq $wordToComplete) {
break
}
$element.Value
}) -join ';'

$completions = @(switch ($command) {
'uu_arch' {
[CompletionResult]::new('-h', 'h', [CompletionResultType]::ParameterName, 'Print help')
[CompletionResult]::new('--help', 'help', [CompletionResultType]::ParameterName, 'Print help')
[CompletionResult]::new('-V', 'V ', [CompletionResultType]::ParameterName, 'Print version')
[CompletionResult]::new('--version', 'version', [CompletionResultType]::ParameterName, 'Print version')
break
}
})

$completions.Where{ $_.CompletionText -like "$wordToComplete*" } |
Sort-Object -Property ListItemText
}
53 changes: 53 additions & 0 deletions completion/_uu_cat
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#compdef uu_cat

autoload -U is-at-least

_uu_cat() {
typeset -A opt_args
typeset -a _arguments_options
local ret=1

if is-at-least 5.2; then
_arguments_options=(-s -S -C)
else
_arguments_options=(-s -C)
fi

local context curcontext="$curcontext" state line
_arguments "${_arguments_options[@]}" \
'-A[equivalent to -vET]' \
'--show-all[equivalent to -vET]' \
'-b[number nonempty output lines, overrides -n]' \
'--number-nonblank[number nonempty output lines, overrides -n]' \
'-e[equivalent to -vE]' \
'-E[display \$ at end of each line]' \
'--show-ends[display \$ at end of each line]' \
'-n[number all output lines]' \
'--number[number all output lines]' \
'-s[suppress repeated empty output lines]' \
'--squeeze-blank[suppress repeated empty output lines]' \
'-t[equivalent to -vT]' \
'-T[display TAB characters at ^I]' \
'--show-tabs[display TAB characters at ^I]' \
'-v[use ^ and M- notation, except for LF (\\n) and TAB (\\t)]' \
'--show-nonprinting[use ^ and M- notation, except for LF (\\n) and TAB (\\t)]' \
'-u[(ignored)]' \
'-h[Print help]' \
'--help[Print help]' \
'-V[Print version]' \
'--version[Print version]' \
'::file:_files' \
&& ret=0
}

(( $+functions[_uu_cat_commands] )) ||
_uu_cat_commands() {
local commands; commands=()
_describe -t commands 'uu_cat commands' commands "$@"
}

if [ "$funcstack[1]" = "_uu_cat" ]; then
_uu_cat "$@"
else
compdef _uu_cat uu_cat
fi
51 changes: 51 additions & 0 deletions completion/_uu_cat.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@

using namespace System.Management.Automation
using namespace System.Management.Automation.Language

Register-ArgumentCompleter -Native -CommandName 'uu_cat' -ScriptBlock {
param($wordToComplete, $commandAst, $cursorPosition)

$commandElements = $commandAst.CommandElements
$command = @(
'uu_cat'
for ($i = 1; $i -lt $commandElements.Count; $i++) {
$element = $commandElements[$i]
if ($element -isnot [StringConstantExpressionAst] -or
$element.StringConstantType -ne [StringConstantType]::BareWord -or
$element.Value.StartsWith('-') -or
$element.Value -eq $wordToComplete) {
break
}
$element.Value
}) -join ';'

$completions = @(switch ($command) {
'uu_cat' {
[CompletionResult]::new('-A', 'A ', [CompletionResultType]::ParameterName, 'equivalent to -vET')
[CompletionResult]::new('--show-all', 'show-all', [CompletionResultType]::ParameterName, 'equivalent to -vET')
[CompletionResult]::new('-b', 'b', [CompletionResultType]::ParameterName, 'number nonempty output lines, overrides -n')
[CompletionResult]::new('--number-nonblank', 'number-nonblank', [CompletionResultType]::ParameterName, 'number nonempty output lines, overrides -n')
[CompletionResult]::new('-e', 'e', [CompletionResultType]::ParameterName, 'equivalent to -vE')
[CompletionResult]::new('-E', 'E ', [CompletionResultType]::ParameterName, 'display $ at end of each line')
[CompletionResult]::new('--show-ends', 'show-ends', [CompletionResultType]::ParameterName, 'display $ at end of each line')
[CompletionResult]::new('-n', 'n', [CompletionResultType]::ParameterName, 'number all output lines')
[CompletionResult]::new('--number', 'number', [CompletionResultType]::ParameterName, 'number all output lines')
[CompletionResult]::new('-s', 's', [CompletionResultType]::ParameterName, 'suppress repeated empty output lines')
[CompletionResult]::new('--squeeze-blank', 'squeeze-blank', [CompletionResultType]::ParameterName, 'suppress repeated empty output lines')
[CompletionResult]::new('-t', 't', [CompletionResultType]::ParameterName, 'equivalent to -vT')
[CompletionResult]::new('-T', 'T ', [CompletionResultType]::ParameterName, 'display TAB characters at ^I')
[CompletionResult]::new('--show-tabs', 'show-tabs', [CompletionResultType]::ParameterName, 'display TAB characters at ^I')
[CompletionResult]::new('-v', 'v', [CompletionResultType]::ParameterName, 'use ^ and M- notation, except for LF (\n) and TAB (\t)')
[CompletionResult]::new('--show-nonprinting', 'show-nonprinting', [CompletionResultType]::ParameterName, 'use ^ and M- notation, except for LF (\n) and TAB (\t)')
[CompletionResult]::new('-u', 'u', [CompletionResultType]::ParameterName, '(ignored)')
[CompletionResult]::new('-h', 'h', [CompletionResultType]::ParameterName, 'Print help')
[CompletionResult]::new('--help', 'help', [CompletionResultType]::ParameterName, 'Print help')
[CompletionResult]::new('-V', 'V ', [CompletionResultType]::ParameterName, 'Print version')
[CompletionResult]::new('--version', 'version', [CompletionResultType]::ParameterName, 'Print version')
break
}
})

$completions.Where{ $_.CompletionText -like "$wordToComplete*" } |
Sort-Object -Property ListItemText
}
38 changes: 38 additions & 0 deletions completion/uu_arch.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
_uu_arch() {
local i cur prev opts cmd
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
cmd=""
opts=""

for i in ${COMP_WORDS[@]}
do
case "${cmd},${i}" in
",$1")
cmd="uu_arch"
;;
*)
;;
esac
done

case "${cmd}" in
uu_arch)
opts="-h -V --help --version"
if [[ ${cur} == -* || ${COMP_CWORD} -eq 1 ]] ; then
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
return 0
fi
case "${prev}" in
*)
COMPREPLY=()
;;
esac
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
return 0
;;
esac
}

complete -F _uu_arch -o nosort -o bashdefault -o default uu_arch
28 changes: 28 additions & 0 deletions completion/uu_arch.elv
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@

use builtin;
use str;

set edit:completion:arg-completer[uu_arch] = {|@words|
fn spaces {|n|
builtin:repeat $n ' ' | str:join ''
}
fn cand {|text desc|
edit:complex-candidate $text &display=$text' '(spaces (- 14 (wcswidth $text)))$desc
}
var command = 'uu_arch'
for word $words[1..-1] {
if (str:has-prefix $word '-') {
break
}
set command = $command';'$word
}
var completions = [
&'uu_arch'= {
cand -h 'Print help'
cand --help 'Print help'
cand -V 'Print version'
cand --version 'Print version'
}
]
$completions[$command]
}
2 changes: 2 additions & 0 deletions completion/uu_arch.fish
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
complete -c uu_arch -s h -l help -d 'Print help'
complete -c uu_arch -s V -l version -d 'Print version'
38 changes: 38 additions & 0 deletions completion/uu_cat.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
_uu_cat() {
local i cur prev opts cmd
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
cmd=""
opts=""

for i in ${COMP_WORDS[@]}
do
case "${cmd},${i}" in
",$1")
cmd="uu_cat"
;;
*)
;;
esac
done

case "${cmd}" in
uu_cat)
opts="-A -b -e -E -n -s -t -T -v -u -h -V --show-all --number-nonblank --show-ends --number --squeeze-blank --show-tabs --show-nonprinting --help --version [file]..."
if [[ ${cur} == -* || ${COMP_CWORD} -eq 1 ]] ; then
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
return 0
fi
case "${prev}" in
*)
COMPREPLY=()
;;
esac
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
return 0
;;
esac
}

complete -F _uu_cat -o nosort -o bashdefault -o default uu_cat
45 changes: 45 additions & 0 deletions completion/uu_cat.elv
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@

use builtin;
use str;

set edit:completion:arg-completer[uu_cat] = {|@words|
fn spaces {|n|
builtin:repeat $n ' ' | str:join ''
}
fn cand {|text desc|
edit:complex-candidate $text &display=$text' '(spaces (- 14 (wcswidth $text)))$desc
}
var command = 'uu_cat'
for word $words[1..-1] {
if (str:has-prefix $word '-') {
break
}
set command = $command';'$word
}
var completions = [
&'uu_cat'= {
cand -A 'equivalent to -vET'
cand --show-all 'equivalent to -vET'
cand -b 'number nonempty output lines, overrides -n'
cand --number-nonblank 'number nonempty output lines, overrides -n'
cand -e 'equivalent to -vE'
cand -E 'display $ at end of each line'
cand --show-ends 'display $ at end of each line'
cand -n 'number all output lines'
cand --number 'number all output lines'
cand -s 'suppress repeated empty output lines'
cand --squeeze-blank 'suppress repeated empty output lines'
cand -t 'equivalent to -vT'
cand -T 'display TAB characters at ^I'
cand --show-tabs 'display TAB characters at ^I'
cand -v 'use ^ and M- notation, except for LF (\n) and TAB (\t)'
cand --show-nonprinting 'use ^ and M- notation, except for LF (\n) and TAB (\t)'
cand -u '(ignored)'
cand -h 'Print help'
cand --help 'Print help'
cand -V 'Print version'
cand --version 'Print version'
}
]
$completions[$command]
}
12 changes: 12 additions & 0 deletions completion/uu_cat.fish
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
complete -c uu_cat -s A -l show-all -d 'equivalent to -vET'
complete -c uu_cat -s b -l number-nonblank -d 'number nonempty output lines, overrides -n'
complete -c uu_cat -s e -d 'equivalent to -vE'
complete -c uu_cat -s E -l show-ends -d 'display $ at end of each line'
complete -c uu_cat -s n -l number -d 'number all output lines'
complete -c uu_cat -s s -l squeeze-blank -d 'suppress repeated empty output lines'
complete -c uu_cat -s t -d 'equivalent to -vT'
complete -c uu_cat -s T -l show-tabs -d 'display TAB characters at ^I'
complete -c uu_cat -s v -l show-nonprinting -d 'use ^ and M- notation, except for LF (\\n) and TAB (\\t)'
complete -c uu_cat -s u -d '(ignored)'
complete -c uu_cat -s h -l help -d 'Print help'
complete -c uu_cat -s V -l version -d 'Print version'
Loading

0 comments on commit 28505c3

Please sign in to comment.