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

Add 'format' label to format custom input before passing cd #192

Merged
merged 4 commits into from
Apr 3, 2023
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
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