Commit 432d5c9 1 parent 3b19e7c commit 432d5c9 Copy full SHA for 432d5c9
File tree 4 files changed +48
-13
lines changed
4 files changed +48
-13
lines changed Original file line number Diff line number Diff line change @@ -202,11 +202,14 @@ short:-G long:--ghq desc:Show ghq path func:ghq list --full-path condition:which
202
202
203
203
Label | Description
204
204
---|---
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 ` )
207
207
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 ` )
209
209
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.
210
213
211
214
<!-- <img width="600" alt="" src="https://user-images.githubusercontent.com/4442708/229298741-236f2920-cde2-4184-9fd3-72849af7a223.png"> -->
212
215
Original file line number Diff line number Diff line change @@ -59,19 +59,34 @@ function enhancd
59
59
set -a opts " $argv [1]"
60
60
else
61
61
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
65
63
set cond (_enhancd_ltsv_get " $opt " " condition" )
64
+ set func (_enhancd_ltsv_get " $opt " " func" )
65
+ set format (_enhancd_ltsv_get " $opt " " format" )
66
66
if not _enhancd_command_run " $cond "
67
67
echo " $opt : defined but require '$cond '" >&2
68
68
return 1
69
69
end
70
70
if test -z $func
71
- echo " $opt : no such option " >&2
71
+ echo " $opt : 'func' label is required " >&2
72
72
return 1
73
73
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
74
78
_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
+
75
90
end
76
91
77
92
case ' *'
Original file line number Diff line number Diff line change @@ -10,6 +10,8 @@ BEGIN {
10
10
11
11
# Skip commented line starting with # or //
12
12
/^ (#| \/\/ ) / { next }
13
+ # Skip empty line
14
+ /^ * $ / { next }
13
15
14
16
{
15
17
condition = ltsv(" condition" )
Original file line number Diff line number Diff line change @@ -76,19 +76,34 @@ __enhancd::cd()
76
76
if __enhancd::helper::is_default_flag " ${1} " ; then
77
77
opts+=( " ${1} " )
78
78
else
79
- local opt=" ${1} " arg= " ${2} " func
80
- func= " $( __enhancd::ltsv::get " ${opt} " " func " ) "
79
+ local opt=" ${1} "
80
+ local func cond format
81
81
cond=" $( __enhancd::ltsv::get " ${opt} " " condition" ) "
82
+ func=" $( __enhancd::ltsv::get " ${opt} " " func" ) "
83
+ format=" $( __enhancd::ltsv::get " ${opt} " " format" ) "
82
84
if ! __enhancd::command::run " ${cond} " & > /dev/null; then
83
- echo " ${opt} : defined but require '${cond} '" >&2
85
+ echo " ${opt} : does not meet '${cond} '" >&2
84
86
return 1
85
87
fi
86
88
if [[ -z ${func} ]]; then
87
- echo " ${opt} : no such option " >&2
89
+ echo " ${opt} : 'func' label is required " >&2
88
90
return 1
89
91
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
92
107
fi
93
108
;;
94
109
* )
You can’t perform that action at this time.
0 commit comments