diff options
author | EuAndreh <eu@euandre.org> | 2023-02-21 11:36:00 -0300 |
---|---|---|
committer | EuAndreh <eu@euandre.org> | 2023-02-21 20:34:46 -0300 |
commit | efa8809e7c2877e442a36f9540d171583f6fda1f (patch) | |
tree | 5394a11cf9c079a5a5e4131f3f0366448f6265f7 /etc | |
parent | etc/xmobar/xmobarrc: Show free disk over used (diff) | |
download | dotfiles-efa8809e7c2877e442a36f9540d171583f6fda1f.tar.gz dotfiles-efa8809e7c2877e442a36f9540d171583f6fda1f.tar.xz |
Fix f() and g() functions, with help of remembering(1) and highlight(1)
Diffstat (limited to 'etc')
-rw-r--r-- | etc/sh/rc | 108 |
1 files changed, 75 insertions, 33 deletions
@@ -256,43 +256,85 @@ PS1='`error_marker`'$(timestamp)' '$(path)' `shell_status``vcs ps1``guix_env``in -# g '^\w.*json_destroy(' g() { - # shellcheck disable=2086 - fn=$(git grep -n -- "$1" ${2:-} | \ - cut -d: -f -2 | \ - fzf --select-1 \ - --exit-0 \ - --preview "echo {} | \ - cut -d: -f1 | \ - xargs -I% awk -v bounds=25 -v pat=\"$1\" -v n=\$(echo {} | cut -d: -f2) ' - (n - bounds < NR) && (NR < n + bounds) && (NR != n) { print } - NR==n { gsub(pat, \"\033[1;33m&\033[1;000m\"); print } - ' %") - if [ -n "$fn" ]; then - f="$(echo "$fn" | cut -d: -f1)" - n="$(echo "$fn" | cut -d: -f2)" - # shellcheck disable=2068 - # history -s g "$@" - # history -s vi "+$n" "$f" - # vi "+$n" "$f" - history -s g "$@" - history -s e "+$n" "$f" - e "+$n" "$f" - fi + if [ -z "${1:-}" ]; then + cat <<-'EOF' + Missing PATTERN. + + Usage: + g PATTERN [PATH] + EOF + return 2 + fi + PAT="$(mkstemp)-g-shell-function" + printf '%s' "$1" > "$PAT" + PREVIEW_CMD="highlight -n{2} -f{1} -e \"\$(cat '$PAT')\"" + + LINE="$( + # shellcheck disable=2086 + git grep --color=always -n -- "$(cat "$PAT")" ${2:-} | + fzf \ + --exit-0 \ + --select-1 \ + --ansi \ + --delimiter=: \ + --preview-window '+{2}+3/3' \ + --preview "$PREVIEW_CMD" + )" + rm -f "$(dirname "$PAT")"/*-g-shell-function + if [ -n "$LINE" ]; then + FILE="$( echo "$LINE" | cut -d: -f1)" + NUMBER="$(echo "$LINE" | cut -d: -f2)" + case "$1" in + "'"*"'") + # Best effort handling of string escaping, since + # one can't recover what was typed at this level + H="\"$1\"" + ;; + *) + H="'$1'" + ;; + esac + if [ -z "${2:-}" ]; then + history -s g "$H" + else + history -s g "$H" "'$2'" + fi + history -s "$VISUAL" +"$NUMBER" "'$FILE'" + "$VISUAL" +"$NUMBER" "$FILE" + fi } f() { - # profile="f-shell-function$(pwd | sed -e 's_/_-_g')" - # file="$(git ls-files | grep ${2:-.} | remembering -p "$profile" -c "fzf --select-1 --exit-0 --preview 'cat {}'")" - # shellcheck disable=2086 - file="$(git ls-files | grep ${2:-.} | fzf --select-1 --exit-0 --preview 'cat {}')" - if [ -n "$file" ]; then - # shellcheck disable=2068 - history -s f "$@" - history -s "${1:-$EDITOR}" "$file" - "${1:-$EDITOR}" "$file" - fi + P="f-shell-function$(pwd | sed 's|/|-|g')" + F="$( + git ls-files | + grep -E -e "${2:-.}" | + remembering -p "$P" -- \ + fzf --select-1 --exit-0 --preview 'cat {}' + )" + CMD="${1:-$VISUAL}" + if [ -n "$F" ]; then + if [ -z "${1:-}" ]; then + history -s f + elif [ -z "${2:-}" ]; then + history -s f "$1" + else + history -s f "$1" "'$2'" + fi + + case "$F" in + -*) + DELIM='--' + ;; + *) + DELIM='' + ;; + esac + history -s "$CMD" $DELIM "'$F'" + + "$CMD" -- "$F" + fi } |