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 | |
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)
-rwxr-xr-x | bin/highlight | 91 | ||||
-rw-r--r-- | etc/sh/rc | 108 |
2 files changed, 166 insertions, 33 deletions
diff --git a/bin/highlight b/bin/highlight new file mode 100755 index 0000000..70cf5ff --- /dev/null +++ b/bin/highlight @@ -0,0 +1,91 @@ +#!/bin/sh +set -eu + +usage() { + cat <<-'EOF' + Usage: + highlight -n LINE_NUMBER -e PATTERN -f FILENAME + highlight -h + EOF +} + +help() { + cat <<-'EOF' + + + Options: + -n LINE_NUMBER the specific line number to highligh + -e PATTERN the pattern to highlight all over the file + -f FILENAME the file to be processed + -h, --help show this message + + + Highlight FILENAME: + a) with an ANSI background color on LINE_NUMBER; + b) with an ANSI foreground color on every occurrence of PATTERN. + + Useful as a tool for development workflow, when one wants to + show a specific matched line, and at the same time visualize all + other ocurrences of PATTERN. + + + Examples: + + Highlight the line 10 of Makefile, and all occurences of "lisp": + + $ highlight -n10 -e 'lisp' -f Makefile + EOF +} + + +for flag in "$@"; do + case "$flag" in + --) + break + ;; + --help) + usage + help + exit + ;; + *) + ;; + esac +done + +while getopts 'n:e:f:h' flag; do + case "$flag" in + n) + LINE_NUMBER="$OPTARG" + ;; + e) + PATTERN="$OPTARG" + ;; + f) + FILENAME="$OPTARG" + ;; + h) + usage + help + exit + ;; + *) + usage >&2 + exit 2 + ;; + esac +done +shift $((OPTIND - 1)) + +eval "$(assert-arg -- "${LINE_NUMBER:-}" '-n LINE_NUMBER')" +eval "$(assert-arg -- "${PATTERN:-}" '-e PATTERN')" +eval "$(assert-arg -- "${FILENAME:-}" '-f FILENAME')" + + + +graybg="$(printf '\033[7;2;40m')" +end="$(printf '\033[0m')" + +grep -E --color=always -e ^ -e "$PATTERN" "$FILENAME" | + perl -pe "s/\x1b\[m/${end}${graybg}/g if $. == $LINE_NUMBER" | + sed "${LINE_NUMBER}s|^\(.*\)\$|${graybg}\\1${end}|" @@ -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 } |