Skip to content

Commit 432d5c9

Browse files
authored
Add 'format' label to format custom input before passing cd (#192)
* Add format label to format custom input before passing cd * Support format in fish * Check only format is set * Readme
1 parent 3b19e7c commit 432d5c9

File tree

4 files changed

+48
-13
lines changed

4 files changed

+48
-13
lines changed

README.md

+6-3
Original file line numberDiff line numberDiff line change
@@ -202,11 +202,14 @@ short:-G long:--ghq desc:Show ghq path func:ghq list --full-path condition:which
202202

203203
Label | Description
204204
---|---
205-
short | a short option (e.g. `-G`)
206-
long | a long option (e.g. `--ghq`)
205+
short (`*`) | a short option (e.g. `-G`)
206+
long (`*`) | a long option (e.g. `--ghq`)
207207
desc | a description for the option
208-
func | a command which returns directory list (e.g. `ghq list --full-path`)
208+
func (`*`) | a command which returns directory list (e.g. `ghq list --full-path`)
209209
condition | a command which determine that the option should be implemented or not (e.g. `which ghq`)
210+
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.
211+
212+
> **Note**: `*`: A required key. But either `short` or `long` is good enough.
210213
211214
<!-- <img width="600" alt="" src="https://user-images.githubusercontent.com/4442708/229298741-236f2920-cde2-4184-9fd3-72849af7a223.png"> -->
212215

functions/enhancd.fish

+19-4
Original file line numberDiff line numberDiff line change
@@ -59,19 +59,34 @@ function enhancd
5959
set -a opts "$argv[1]"
6060
else
6161
set -l opt "$argv[1]"
62-
set -l arg "$argv[2]"
63-
set -l func
64-
set func (_enhancd_ltsv_get "$opt" "func")
62+
set -l func cond format
6563
set cond (_enhancd_ltsv_get "$opt" "condition")
64+
set func (_enhancd_ltsv_get "$opt" "func")
65+
set format (_enhancd_ltsv_get "$opt" "format")
6666
if not _enhancd_command_run "$cond"
6767
echo "$opt: defined but require '$cond'" >&2
6868
return 1
6969
end
7070
if test -z $func
71-
echo "$opt: no such option" >&2
71+
echo "$opt: 'func' label is required" >&2
7272
return 1
7373
end
74+
if test -n $format; and not string match --quiet '*%*' $format
75+
echo "$opt: 'format' label needs to include '%' (selected line)" >&2
76+
return 1
77+
fi
7478
_enhancd_command_run "$func" "$arg" | _enhancd_filter_interactive
79+
set -l seleted
80+
if test -z $format
81+
set selected (_enhancd_command_run "$func" | _enhancd_filter_interactive)
82+
else
83+
# format is maybe including $HOME etc. need magic line of 'eval printf' to expand that.
84+
set selected (_enhancd_command_run "$func" | _enhancd_filter_interactive | xargs -I% echo (eval printf "%s" "$format"))
85+
end
86+
set code $status
87+
set -a args $selected
88+
break
89+
7590
end
7691

7792
case '*'

functions/enhancd/lib/help.awk

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ BEGIN {
1010

1111
# Skip commented line starting with # or //
1212
/^(#|\/\/)/ { next }
13+
# Skip empty line
14+
/^ *$/ { next }
1315

1416
{
1517
condition = ltsv("condition")

src/cd.sh

+21-6
Original file line numberDiff line numberDiff line change
@@ -76,19 +76,34 @@ __enhancd::cd()
7676
if __enhancd::helper::is_default_flag "${1}"; then
7777
opts+=( "${1}" )
7878
else
79-
local opt="${1}" arg="${2}" func
80-
func="$(__enhancd::ltsv::get "${opt}" "func")"
79+
local opt="${1}"
80+
local func cond format
8181
cond="$(__enhancd::ltsv::get "${opt}" "condition")"
82+
func="$(__enhancd::ltsv::get "${opt}" "func")"
83+
format="$(__enhancd::ltsv::get "${opt}" "format")"
8284
if ! __enhancd::command::run "${cond}" &>/dev/null; then
83-
echo "${opt}: defined but require '${cond}'" >&2
85+
echo "${opt}: does not meet '${cond}'" >&2
8486
return 1
8587
fi
8688
if [[ -z ${func} ]]; then
87-
echo "${opt}: no such option" >&2
89+
echo "${opt}: 'func' label is required" >&2
8890
return 1
8991
fi
90-
args+=( "$(__enhancd::command::run "${func}" "${arg}" | __enhancd::filter::interactive)" )
91-
code=${?}
92+
if [[ -n ${format} ]] && [[ ${format//\%/} == "${format}" ]]; then
93+
echo "${opt}: 'format' label needs to include '%' (selected line)" >&2
94+
return 1
95+
fi
96+
local selected
97+
if [[ -z ${format} ]]; then
98+
selected="$(__enhancd::command::run "${func}" | __enhancd::filter::interactive)"
99+
code=${?}
100+
else
101+
# format is maybe including $HOME etc. need magic line of 'eval printf' to expand that.
102+
selected="$(__enhancd::command::run "${func}" | __enhancd::filter::interactive | xargs -I% echo $(eval printf "%s" "${format}"))"
103+
code=${?}
104+
fi
105+
args+=( "${selected}" )
106+
break
92107
fi
93108
;;
94109
*)

0 commit comments

Comments
 (0)