Skip to content

Commit

Permalink
fix: completions for bins with dashes
Browse files Browse the repository at this point in the history
Fixes #163
  • Loading branch information
jdx committed Nov 10, 2024
1 parent a9e4482 commit adbb347
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 13 deletions.
7 changes: 4 additions & 3 deletions lib/src/complete/bash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ use heck::ToSnakeCase;

pub fn complete_bash(opts: &CompleteOptions) -> String {
let bin = &opts.bin;
let bin_snake = bin.to_snake_case();
let spec_variable = if let Some(cache_key) = &opts.cache_key {
format!("_usage_spec_{bin}_{}", cache_key.to_snake_case())
format!("_usage_spec_{bin_snake}_{}", cache_key.to_snake_case())
} else {
format!("_usage_spec_{bin}")
format!("_usage_spec_{bin_snake}")
};
let mut out = vec![format!(
r#"_{bin}() {{
Expand Down Expand Up @@ -46,7 +47,7 @@ __USAGE_EOF__"#,
return 0
}}
shopt -u hostcomplete && complete -o nospace -o bashdefault -o nosort -F _{bin} {bin}
shopt -u hostcomplete && complete -o nospace -o bashdefault -o nosort -F _{bin_snake} {bin}

This comment has been minimized.

Copy link
@liskin

liskin Nov 10, 2024

This isn't necessary. But if you want to keep it, then line 13 needs to become

r#"_{bin_snake}() {{

otherwise we get

bash: completion: function `_a_b_c' not found

This comment has been minimized.

Copy link
@jdx

jdx Nov 10, 2024

Author Owner

can you use dashes in bash? even if so I think it would look cleaner debugging if we escape to snake

This comment has been minimized.

Copy link
@liskin

liskin Nov 10, 2024

Yeah functions in bash can have dashes, but not in posix sh (like dash). Agreed that it looks nicer in snake case.

# vim: noet ci pi sts=0 sw=4 ts=4 ft=sh
"#
));
Expand Down
5 changes: 3 additions & 2 deletions lib/src/complete/fish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ use heck::ToSnakeCase;

pub fn complete_fish(opts: &CompleteOptions) -> String {
let bin = &opts.bin;
let bin_snake = bin.to_snake_case();
let spec_variable = if let Some(cache_key) = &opts.cache_key {
format!("_usage_spec_{bin}_{}", cache_key.to_snake_case())
format!("_usage_spec_{bin_snake}_{}", cache_key.to_snake_case())
} else {
format!("_usage_spec_{bin}")
format!("_usage_spec_{bin_snake}")
};
let mut out = vec![format!(
r#"
Expand Down
17 changes: 9 additions & 8 deletions lib/src/complete/zsh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ use heck::ToSnakeCase;

pub fn complete_zsh(opts: &CompleteOptions) -> String {
let bin = &opts.bin;
let bin_snake = bin.to_snake_case();
let spec_variable = if let Some(cache_key) = &opts.cache_key {
format!("_usage_spec_{bin}_{}", cache_key.to_snake_case())
format!("_usage_spec_{bin_snake}_{}", cache_key.to_snake_case())
} else {
format!("_usage_spec_{bin}")
format!("_usage_spec_{bin_snake}")
};
// let bin_snake = bin.to_snake_case();
let mut out = vec![format!(
Expand All @@ -19,7 +20,7 @@ local curcontext="$curcontext""#
out.push(format!(
r#"
# caching config
_usage_{bin}_cache_policy() {{
_usage_{bin_snake}_cache_policy() {{
if [[ -z "${{lifetime}}" ]]; then
lifetime=$((60*60*4)) # 4 hours
fi
Expand All @@ -32,7 +33,7 @@ _usage_{bin}_cache_policy() {{

out.push(format!(
r#"
_{bin}() {{
_{bin_snake}() {{
typeset -A opt_args
local curcontext="$curcontext" spec cache_policy
Expand All @@ -49,7 +50,7 @@ _{bin}() {{
r#"
zstyle -s ":completion:${{curcontext}}:" cache-policy cache_policy
if [[ -z $cache_policy ]]; then
zstyle ":completion:${{curcontext}}:" cache-policy _usage_{bin}_cache_policy
zstyle ":completion:${{curcontext}}:" cache-policy _usage_{bin_snake}_cache_policy
fi
if ( [[ -z "${{{spec_variable}:-}}" ]] || _cache_invalid {spec_variable} ) \
Expand All @@ -76,10 +77,10 @@ __USAGE_EOF__"#,
return 0
}}
if [ "$funcstack[1]" = "_{bin}" ]; then
_{bin} "$@"
if [ "$funcstack[1]" = "_{bin_snake}" ]; then
_{bin_snake} "$@"
else
compdef _{bin} {bin}
compdef _{bin_snake} {bin}
fi
# vim: noet ci pi sts=0 sw=4 ts=4"#,
Expand Down

0 comments on commit adbb347

Please sign in to comment.