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

fish: performance improvments, add piping, fixes #218

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
22 changes: 18 additions & 4 deletions conf.d/enhancd.fish
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand All @@ -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
5 changes: 2 additions & 3 deletions functions/_enhancd_cd_after.fish
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
6 changes: 4 additions & 2 deletions functions/_enhancd_cd_ready.fish
Original file line number Diff line number Diff line change
@@ -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
14 changes: 3 additions & 11 deletions functions/_enhancd_command_awk.fish
Original file line number Diff line number Diff line change
@@ -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
end
4 changes: 3 additions & 1 deletion functions/_enhancd_complete.fish
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion functions/_enhancd_filepath_abs.fish
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion functions/_enhancd_filepath_get_parent_dirs.fish
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion functions/_enhancd_filepath_split.fish
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion functions/_enhancd_filter_fuzzy.fish
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
61 changes: 22 additions & 39 deletions functions/_enhancd_filter_interactive.fish
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion functions/_enhancd_filter_replace.fish
Original file line number Diff line number Diff line change
Expand Up @@ -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}'
Expand Down
12 changes: 7 additions & 5 deletions functions/_enhancd_filter_reverse.fish
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion functions/_enhancd_filter_unique.fish
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 2 additions & 1 deletion functions/_enhancd_helper_parse_filter_string.fish
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions functions/_enhancd_history_exists.fish
Original file line number Diff line number Diff line change
Expand Up @@ -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
7 changes: 2 additions & 5 deletions functions/_enhancd_history_open.fish
Original file line number Diff line number Diff line change
@@ -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
10 changes: 4 additions & 6 deletions functions/_enhancd_history_update.fish
Original file line number Diff line number Diff line change
@@ -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
4 changes: 2 additions & 2 deletions functions/_enhancd_ltsv_open.fish
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 7 additions & 7 deletions functions/_enhancd_ltsv_parse.fish
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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
21 changes: 13 additions & 8 deletions functions/enhancd.fish
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -13,22 +14,26 @@ 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,
# searchs from the last 10 directory items in the log
set -a args (_enhancd_source_mru | _enhancd_filter_interactive)
set code $status

case '-'
case -
set -a args "$OLDPWD"

case "$ENHANCD_ARG_DOUBLE_DOT"
Expand All @@ -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
Expand Down Expand Up @@ -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 '*'
Expand Down