diff --git a/conf.d/enhancd.fish b/conf.d/enhancd.fish index 2b90142..7b9a9cc 100644 --- a/conf.d/enhancd.fish +++ b/conf.d/enhancd.fish @@ -24,18 +24,18 @@ function __enhancd_install --on-event enhancd_install set -Ux _ENHANCD_VERSION (command cat "$root/VERSION") + set -Ux ENHANCD_DIRECTORIES + set -Ux ENHANCD_CURRENT_FILTER # Set the filters if empty set -Ux ENHANCD_FILTER "fzy:fzf:peco:sk:zf" + set -Ux ENHANCD_AWK_CMD "awk" + # make a log file and a root directory if not test -d "$ENHANCD_DIR" mkdir -p "$ENHANCD_DIR" end - if not test -f "$ENHANCD_DIR/enhancd.log" - touch "$ENHANCD_DIR/enhancd.log" - end - _enhancd_alias end @@ -58,7 +58,11 @@ function __enhancd_uninstall --on-event enhancd_uninstall set --erase ENHANCD_COMPLETION_BEHAVIOR set --erase ENHANCD_COMPLETION_KEYBIND set --erase ENHANCD_FILTER + set --erase ENHANCD_CURRENT_FILTER + set --erase ENHANCD_DIRECTORIES + set --erase ENHANCD_AWK_CMD set --erase _ENHANCD_VERSION + set --erase _ENHANCD_READY end # alias to enhancd @@ -68,3 +72,13 @@ end # bindings bind \ef '_enhancd_complete' + +set -Ux ENHANCD_CURRENT_FILTER (_enhancd_helper_parse_filter_string "$ENHANCD_FILTER") +set -Ux ENHANCD_AWK_CMD (_enhancd_command_awk) + +# Migrate from file based to universal var +if test -z "$ENHANCD_DIRECTORIES"; and test -f "$ENHANCD_DIR/enhancd.log" + while read -l line + set -a ENHANCD_DIRECTORIES "$line" + end < "$ENHANCD_DIR/enhancd.log" +end diff --git a/functions/_enhancd_cd_after.fish b/functions/_enhancd_cd_after.fish index e0d4e7e..c8ab575 100644 --- a/functions/_enhancd_cd_after.fish +++ b/functions/_enhancd_cd_after.fish @@ -2,10 +2,9 @@ function _enhancd_cd_after # Don't split on newlines throughout this function: set -l IFS "" - set -l list (_enhancd_history_update) - + set -l list (string split \n -- (_enhancd_history_update)) if test -n "$list" - echo "$list" >"$ENHANCD_DIR/enhancd.log" + set -Ux ENHANCD_DIRECTORIES $list end if test -n "$ENHANCD_HOOK_AFTER_CD" diff --git a/functions/_enhancd_cd_ready.fish b/functions/_enhancd_cd_ready.fish index d3731d6..b6f31ca 100644 --- a/functions/_enhancd_cd_ready.fish +++ b/functions/_enhancd_cd_ready.fish @@ -1,5 +1,7 @@ function _enhancd_cd_ready - _enhancd_helper_parse_filter_string "$ENHANCD_FILTER" 2 >/dev/null - ; and test -s $ENHANCD_DIR/enhancd.log + test -n "$ENHANCD_AWK_CMD" + ; and test -n "$ENHANCD_CURRENT_FILTER" + ; and test -n "$ENHANCD_DIRECTORIES" + ; and set -Ux _ENHANCD_READY 1 return $status end diff --git a/functions/_enhancd_command_awk.fish b/functions/_enhancd_command_awk.fish index 44523d8..9b6b2ca 100644 --- a/functions/_enhancd_command_awk.fish +++ b/functions/_enhancd_command_awk.fish @@ -1,16 +1,8 @@ # Returns gawk if found, else awk function _enhancd_command_awk - set -l awk_cmd - if type -q gawk - set awk_cmd "gawk" + echo "gawk" else - set awk_cmd "awk" + echo "awk" end - - if set -q argv[1] - $awk_cmd $argv - else - $awk_cmd "" - end -end \ No newline at end of file +end diff --git a/functions/_enhancd_complete.fish b/functions/_enhancd_complete.fish index 32030ba..31115f4 100644 --- a/functions/_enhancd_complete.fish +++ b/functions/_enhancd_complete.fish @@ -1,7 +1,9 @@ function _enhancd_complete - set -l query (_enhancd_source_home)/ + set -l query (_enhancd_source_home | _enhancd_filter_interactive) + if test -n "$query" commandline -t "$query" end + commandline -f repaint end diff --git a/functions/_enhancd_filepath_abs.fish b/functions/_enhancd_filepath_abs.fish index 12fd5a1..2361793 100644 --- a/functions/_enhancd_filepath_abs.fish +++ b/functions/_enhancd_filepath_abs.fish @@ -13,7 +13,7 @@ function _enhancd_filepath_abs return 1 end - _enhancd_command_awk \ + "$ENHANCD_AWK_CMD" \ -f "$ENHANCD_ROOT/lib/to_abspath.awk" \ -v cwd="$cwd" \ -v dir="$dir" diff --git a/functions/_enhancd_filepath_get_parent_dirs.fish b/functions/_enhancd_filepath_get_parent_dirs.fish index c46b6c1..948cd03 100644 --- a/functions/_enhancd_filepath_get_parent_dirs.fish +++ b/functions/_enhancd_filepath_get_parent_dirs.fish @@ -4,7 +4,7 @@ function _enhancd_filepath_get_parent_dirs set argv[1] "$PWD" end - _enhancd_command_awk \ + "$ENHANCD_AWK_CMD" \ -f "$ENHANCD_ROOT/lib/step_by_step.awk" \ -v dir="$argv[1]" end diff --git a/functions/_enhancd_filepath_split.fish b/functions/_enhancd_filepath_split.fish index 7292f04..67d28f2 100644 --- a/functions/_enhancd_filepath_split.fish +++ b/functions/_enhancd_filepath_split.fish @@ -4,7 +4,7 @@ function _enhancd_filepath_split set $argv[1] $PWD end - _enhancd_command_awk \ + "$ENHANCD_AWK_CMD" \ -f "$ENHANCD_ROOT/lib/split.awk" \ -v arg=$argv[1] end diff --git a/functions/_enhancd_filter_fuzzy.fish b/functions/_enhancd_filter_fuzzy.fish index d80d152..e946331 100644 --- a/functions/_enhancd_filter_fuzzy.fish +++ b/functions/_enhancd_filter_fuzzy.fish @@ -2,7 +2,7 @@ function _enhancd_filter_fuzzy if test -z "$argv[1]" command cat <&0 else - _enhancd_command_awk \ + "$ENHANCD_AWK_CMD" \ -f "$ENHANCD_ROOT/lib/fuzzy.awk" \ -v search_string="$argv[1]" end diff --git a/functions/_enhancd_filter_interactive.fish b/functions/_enhancd_filter_interactive.fish index a4063f1..c3cbb9c 100644 --- a/functions/_enhancd_filter_interactive.fish +++ b/functions/_enhancd_filter_interactive.fish @@ -1,47 +1,30 @@ function _enhancd_filter_interactive - set -l stdin "$argv[1]" + set -l filter "$ENHANCD_CURRENT_FILTER" + set -l before_cmd + set -l after_cmd - if test -z "$stdin"; or test -p /dev/stdin - read -z stdin + if test "$ENHANCD_USE_ABBREV" = true + # Escape '/' for sed processing + set -l home_escaped (string replace -a '/' '\/' "$HOME") + set before_cmd sed 's/^'$home_escaped'/~/g' + set after_cmd sed 's/^~/'$home_escaped'/g' + else + set before_cmd cat + set after_cmd cat end - if test -z "$stdin" + read --line line_1 line_2 + if test -z "$line_1" -a -z "$line_2" echo "no entry" >&2 return 1 - end - - set -l filter (_enhancd_helper_parse_filter_string "$ENHANCD_FILTER") - set -l count (echo "$stdin" | _enhancd_command_grep -c "") - - if test "$ENHANCD_USE_ABBREV" = true - function _enhancd_filter_interactive_abbrev - while read -l line - string replace --regex "^$HOME" "~" "$line" - end - end - - function _enhancd_filter_interactive_expand - while read -l line - string replace --regex "^~" "$HOME" "$line" - end - end - - set filter "_enhancd_filter_interactive_abbrev | $filter | _enhancd_filter_interactive_expand" - end - - switch "$count" - case '1' - if test -n "$stdin" - echo "$stdin" - else - return 1 - end - - case '*' - set -l selected (echo "$stdin" | eval "$filter") - if test -z "$selected" - return 1 - end - echo "$selected" + else if test -n "$line_1" -a -z "$line_2" + echo "$line_1" + else + # Prepend the first two lines we read to stdin + begin + printf "$line_1\n$line_2\n" + cat + end | $before_cmd | $filter | $after_cmd + or return 1 end end diff --git a/functions/_enhancd_filter_replace.fish b/functions/_enhancd_filter_replace.fish index 6be44be..2018275 100644 --- a/functions/_enhancd_filter_replace.fish +++ b/functions/_enhancd_filter_replace.fish @@ -3,7 +3,7 @@ function _enhancd_filter_replace set -l old $argv[1] set -q argv[2] && set -l new $argv[2] - _enhancd_command_awk \ + "$ENHANCD_AWK_CMD" \ -v old="$old" \ -v new="$new" \ 'sub(old, new, $0) {print $0}' diff --git a/functions/_enhancd_filter_reverse.fish b/functions/_enhancd_filter_reverse.fish index fcd976e..758858f 100644 --- a/functions/_enhancd_filter_reverse.fish +++ b/functions/_enhancd_filter_reverse.fish @@ -1,10 +1,12 @@ # _enhancd_filter_reverse reverses a stdin contents function _enhancd_filter_reverse + set -l lines if test -n "$argv[1]"; and test -f "$argv[1]" - command cat "$argv[1]" + set lines "$argv[1]" else - command cat <&0 - end \ - | _enhancd_command_awk -f "$ENHANCD_ROOT/lib/reverse.awk" \ - 2>/dev/null + read -z --list lines + end + for line in $lines[-1..1] + echo $line + end end diff --git a/functions/_enhancd_filter_unique.fish b/functions/_enhancd_filter_unique.fish index 145f10d..7a67381 100644 --- a/functions/_enhancd_filter_unique.fish +++ b/functions/_enhancd_filter_unique.fish @@ -4,5 +4,5 @@ function _enhancd_filter_unique command cat "$argv[1]" else command cat <&0 - end | _enhancd_command_awk '!a[$0]++' 2>/dev/null + end | "$ENHANCD_AWK_CMD" '!a[$0]++' 2>/dev/null end diff --git a/functions/_enhancd_helper_parse_filter_string.fish b/functions/_enhancd_helper_parse_filter_string.fish index 1850ff4..fe555d6 100644 --- a/functions/_enhancd_helper_parse_filter_string.fish +++ b/functions/_enhancd_helper_parse_filter_string.fish @@ -11,7 +11,8 @@ function _enhancd_helper_parse_filter_string while test -n "$str" for item in (string split ':' $str) - if _enhancd_command_which "$item" + set -l cmd (string split ' ' -- "$item") + if _enhancd_command_which (string trim -- "$cmd[1]") echo $item return 0 else diff --git a/functions/_enhancd_history_exists.fish b/functions/_enhancd_history_exists.fish index b090237..a4d5a47 100644 --- a/functions/_enhancd_history_exists.fish +++ b/functions/_enhancd_history_exists.fish @@ -4,6 +4,5 @@ function _enhancd_history_exists else set dir $argv[1] end - - _enhancd_history_open | _enhancd_command_grep "$dir" ^/dev/null >/dev/null + _enhancd_history_open | string match -r -q "$dir" end diff --git a/functions/_enhancd_history_open.fish b/functions/_enhancd_history_open.fish index 7d371e5..afe8592 100644 --- a/functions/_enhancd_history_open.fish +++ b/functions/_enhancd_history_open.fish @@ -1,7 +1,4 @@ function _enhancd_history_open - if test -f $ENHANCD_DIR/enhancd.log - command cat "$ENHANCD_DIR/enhancd.log" - return $status - end - return 1 + string join \n -- $ENHANCD_DIRECTORIES + return $status end diff --git a/functions/_enhancd_history_update.fish b/functions/_enhancd_history_update.fish index f1cba4e..055ca1e 100644 --- a/functions/_enhancd_history_update.fish +++ b/functions/_enhancd_history_update.fish @@ -1,12 +1,10 @@ function _enhancd_history_update - function sub + begin _enhancd_history_exists; or _enhancd_filepath_walk _enhancd_history_open echo "$HOME" - end - sub \ - | _enhancd_filter_reverse \ - | _enhancd_filter_unique \ - | _enhancd_filter_reverse + end | _enhancd_filter_reverse \ + | _enhancd_filter_unique \ + | _enhancd_filter_reverse _enhancd_filepath_current_dir end diff --git a/functions/_enhancd_ltsv_open.fish b/functions/_enhancd_ltsv_open.fish index 2d22c3f..c0ba043 100644 --- a/functions/_enhancd_ltsv_open.fish +++ b/functions/_enhancd_ltsv_open.fish @@ -4,8 +4,8 @@ function _enhancd_ltsv_open set -a configs "$ENHANCD_DIR/config.ltsv" set -a configs "$HOME/.config/enhancd/config.ltsv" - for config in "$configs" - if test -f "$config" + for config in $configs + if test -f $config command cat "$config" end end diff --git a/functions/_enhancd_ltsv_parse.fish b/functions/_enhancd_ltsv_parse.fish index 5c21158..ddd7065 100644 --- a/functions/_enhancd_ltsv_parse.fish +++ b/functions/_enhancd_ltsv_parse.fish @@ -5,14 +5,14 @@ function _enhancd_ltsv_parse set -l i 1 while [ $i -le (count argv) ] switch "$argv[$i]" - case "-q" + case -q set -l query $argv[(math "$i + 1")] shift - case "-v" - set -a args "-v" $argv[(math "$i + 1")] - case "-f" - set -a args "-f" "$ENHANCD_ROOT/lib/ltsv.awk" - set -a args "-f" $argv[(math "$i + 1")] + case -v + set -a args -v $argv[(math "$i + 1")] + case -f + set -a args -f "$ENHANCD_ROOT/lib/ltsv.awk" + set -a args -f $argv[(math "$i + 1")] set query "" end set i (math "$i + 1") @@ -26,5 +26,5 @@ function _enhancd_ltsv_parse end set -l awk_scripts "$ltsv_script $query" - _enhancd_command_awk $args "$awk_scripts" + "$ENHANCD_AWK_CMD" $args "$awk_scripts" end diff --git a/functions/enhancd.fish b/functions/enhancd.fish index b7e30fd..666dde6 100644 --- a/functions/enhancd.fish +++ b/functions/enhancd.fish @@ -4,7 +4,8 @@ function enhancd set -l opts set -l code 0 - if not _enhancd_cd_ready + if test -z "$_ENHANCD_READY" + _enhancd_cd_ready if not set -q argv[1] _enhancd_cd_builtin "$HOME" else @@ -13,14 +14,18 @@ function enhancd return $status end + if not isatty + _enhancd_filter_interactive | read --line args + end + # set indice to the first element set -l i 1 while [ $i -le (count $argv) ] switch $argv[$i] - case "--help" + case --help _enhancd_ltsv_open \ - | _enhancd_command_awk -f "$ENHANCD_ROOT/lib/help.awk" + | "$ENHANCD_AWK_CMD" -f "$ENHANCD_ROOT/lib/help.awk" case "$ENHANCD_ARG_HYPHEN" # If a hyphen is passed as the argument, @@ -28,7 +33,7 @@ function enhancd set -a args (_enhancd_source_mru | _enhancd_filter_interactive) set code $status - case '-' + case - set -a args "$OLDPWD" case "$ENHANCD_ARG_DOUBLE_DOT" @@ -49,7 +54,7 @@ function enhancd set -a args (_enhancd_source_home | _enhancd_filter_interactive) set code $status - case '--' + case -- set -a opts "$argv[1]" set -a args (_enhancd_source_history "$argv[2]" | _enhancd_filter_interactive) set code $status @@ -96,14 +101,14 @@ function enhancd set i (math "$i + 1") end - switch (count $argv) - case '0' + switch (count $args) + case 0 set -a args (_enhancd_source_home | _enhancd_filter_interactive) set code $status end switch "$code" - case '0' + case 0 _enhancd_cd_builtin $opts $args return $status case '*'