diff --git a/README.md b/README.md index 8afd93d..d4d1a29 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/functions/enhancd.fish b/functions/enhancd.fish index e89a41d..056d7b3 100644 --- a/functions/enhancd.fish +++ b/functions/enhancd.fish @@ -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 '*' diff --git a/functions/enhancd/lib/help.awk b/functions/enhancd/lib/help.awk index 645ff49..82d7d05 100644 --- a/functions/enhancd/lib/help.awk +++ b/functions/enhancd/lib/help.awk @@ -10,6 +10,8 @@ BEGIN { # Skip commented line starting with # or // /^(#|\/\/)/ { next } +# Skip empty line +/^ *$/ { next } { condition = ltsv("condition") diff --git a/src/cd.sh b/src/cd.sh index d1521c0..3832523 100644 --- a/src/cd.sh +++ b/src/cd.sh @@ -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 ;; *)