Skip to content

Commit

Permalink
Add 'format' label to format custom input before passing cd (#192)
Browse files Browse the repository at this point in the history
* Add format label to format custom input before passing cd

* Support format in fish

* Check only format is set

* Readme
  • Loading branch information
b4b4r07 committed Apr 3, 2023
1 parent 3b19e7c commit 432d5c9
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 13 deletions.
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -202,11 +202,14 @@ short:-G long:--ghq desc:Show ghq path func:ghq list --full-path condition:which

Label | Description
---|---
short | a short option (e.g. `-G`)
long | a long option (e.g. `--ghq`)
short (`*`) | a short option (e.g. `-G`)
long (`*`) | a long option (e.g. `--ghq`)
desc | a description for the option
func | a command which returns directory list (e.g. `ghq list --full-path`)
func (`*`) | a command which returns directory list (e.g. `ghq list --full-path`)
condition | a command which determine that the option should be implemented or not (e.g. `which ghq`)
format | a string which indicates how to format a line selected by the filter before passing cd command. `%` is replaced as a selected line and then passed to cd command (e.g. `$HOME/src/%`). This is useful for the case that input sources for the interactive filter are not a full-path.

> **Note**: `*`: A required key. But either `short` or `long` is good enough.
<!-- <img width="600" alt="" src="https://user-images.githubusercontent.com/4442708/229298741-236f2920-cde2-4184-9fd3-72849af7a223.png"> -->

Expand Down
23 changes: 19 additions & 4 deletions functions/enhancd.fish
Original file line number Diff line number Diff line change
Expand Up @@ -59,19 +59,34 @@ function enhancd
set -a opts "$argv[1]"
else
set -l opt "$argv[1]"
set -l arg "$argv[2]"
set -l func
set func (_enhancd_ltsv_get "$opt" "func")
set -l func cond format
set cond (_enhancd_ltsv_get "$opt" "condition")
set func (_enhancd_ltsv_get "$opt" "func")
set format (_enhancd_ltsv_get "$opt" "format")
if not _enhancd_command_run "$cond"
echo "$opt: defined but require '$cond'" >&2
return 1
end
if test -z $func
echo "$opt: no such option" >&2
echo "$opt: 'func' label is required" >&2
return 1
end
if test -n $format; and not string match --quiet '*%*' $format
echo "$opt: 'format' label needs to include '%' (selected line)" >&2
return 1
fi
_enhancd_command_run "$func" "$arg" | _enhancd_filter_interactive
set -l seleted
if test -z $format
set selected (_enhancd_command_run "$func" | _enhancd_filter_interactive)
else
# format is maybe including $HOME etc. need magic line of 'eval printf' to expand that.
set selected (_enhancd_command_run "$func" | _enhancd_filter_interactive | xargs -I% echo (eval printf "%s" "$format"))
end
set code $status
set -a args $selected
break

end

case '*'
Expand Down
2 changes: 2 additions & 0 deletions functions/enhancd/lib/help.awk
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ BEGIN {

# Skip commented line starting with # or //
/^(#|\/\/)/ { next }
# Skip empty line
/^ *$/ { next }

{
condition = ltsv("condition")
Expand Down
27 changes: 21 additions & 6 deletions src/cd.sh
Original file line number Diff line number Diff line change
Expand Up @@ -76,19 +76,34 @@ __enhancd::cd()
if __enhancd::helper::is_default_flag "${1}"; then
opts+=( "${1}" )
else
local opt="${1}" arg="${2}" func
func="$(__enhancd::ltsv::get "${opt}" "func")"
local opt="${1}"
local func cond format
cond="$(__enhancd::ltsv::get "${opt}" "condition")"
func="$(__enhancd::ltsv::get "${opt}" "func")"
format="$(__enhancd::ltsv::get "${opt}" "format")"
if ! __enhancd::command::run "${cond}" &>/dev/null; then
echo "${opt}: defined but require '${cond}'" >&2
echo "${opt}: does not meet '${cond}'" >&2
return 1
fi
if [[ -z ${func} ]]; then
echo "${opt}: no such option" >&2
echo "${opt}: 'func' label is required" >&2
return 1
fi
args+=( "$(__enhancd::command::run "${func}" "${arg}" | __enhancd::filter::interactive)" )
code=${?}
if [[ -n ${format} ]] && [[ ${format//\%/} == "${format}" ]]; then
echo "${opt}: 'format' label needs to include '%' (selected line)" >&2
return 1
fi
local selected
if [[ -z ${format} ]]; then
selected="$(__enhancd::command::run "${func}" | __enhancd::filter::interactive)"
code=${?}
else
# format is maybe including $HOME etc. need magic line of 'eval printf' to expand that.
selected="$(__enhancd::command::run "${func}" | __enhancd::filter::interactive | xargs -I% echo $(eval printf "%s" "${format}"))"
code=${?}
fi
args+=( "${selected}" )
break
fi
;;
*)
Expand Down

0 comments on commit 432d5c9

Please sign in to comment.