Skip to content

Commit

Permalink
kamp-grep: utilize getopts
Browse files Browse the repository at this point in the history
  • Loading branch information
vbauerster committed Nov 15, 2024
1 parent 90ca7a2 commit df5df5e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 21 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ alias global popup tmux-terminal-popup
map global normal -docstring 'files' <c-f> ':connect popup kamp-files<ret>'
map global normal -docstring 'git ls-files' <c-l> ':connect popup kamp-files -b git<ret>'
map global normal -docstring 'buffers' <c-b> ':connect popup kamp-buffers<ret>'
map global normal -docstring 'grep selection' <c-g> ':connect popup kamp-grep ''query=%val{selection}<a-!>''<ret>'
map global normal -docstring 'grep limit by filetype' <c-y> ':connect popup kamp-grep -t %opt{filetype}<ret>'
map global normal -docstring 'grep selection' <c-g> ':connect popup kamp-grep -q ''%val{selection}<a-!>''<ret>'
map global normal -docstring 'grep by filetype' <c-y> ':connect popup kamp-grep -- -t %opt{filetype}<ret>'
```

## Shell integration
Expand Down
47 changes: 28 additions & 19 deletions scripts/kamp-grep
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@
#
# Open files by content.
#
# Usage:
#
# kamp-grep [<query=...>] [rg_options]

# requires:
# – fzf (https://github.com/junegunn/fzf)
# https://github.com/junegunn/fzf/blob/master/CHANGELOG.md#0190
Expand All @@ -19,32 +15,45 @@ set -euf
# define SHELL so --preview arguments do not error if current SHELL is not POSIX
SHELL=/bin/sh

query="$(kamp get opt kamp_grep_query)"
usage() {
printf "Usage: %s: [-q query] [rg_options]\n" "$(basename "$0")" >&2
exit 2
}

if [ $# -ne 0 ]; then
case "$1" in
query=*)
query="${1#*=}"
shift
;;
esac
fi
qflag=
while getopts 'q:h' OPTION; do
case $OPTION in
q)
qflag=1
query="$OPTARG"
;;
h|?)
usage
;;
esac
done
shift $((OPTIND - 1))

rg_cmd="rg $*"
rg_cmd="rg --color always --column $*"

if [ ! "$qflag" ]; then
query="$(kamp get opt kamp_grep_query)"
fi

if [ -z "$query" ]; then
export FZF_DEFAULT_COMMAND="$rg_cmd --files"
export FZF_DEFAULT_COMMAND="rg --files $*"
else
export FZF_DEFAULT_COMMAND="$rg_cmd --color always --column -- '$query'"
pattern=$(printf %s "$query" | sed 's/"/\\"/')
export FZF_DEFAULT_COMMAND="$rg_cmd -- \"$pattern\""
fi

fzf \
--phony \
--query "${query:-}" \
--query "$query" \
--delimiter ':' \
--ansi \
--bind "change:reload($rg_cmd --color always --column -- {q} || true)" \
--bind 'enter:execute-silent(kamp send set global kamp_grep_query "{q}")+become(kamp edit {1} +{2}:{3})' \
--bind "change:reload($rg_cmd -- {q} || true)" \
--bind 'enter:execute-silent(kamp send set global kamp_grep_query {q})+become(kamp edit {1} +{2}:{3})' \
--preview '
highlight_line={2}
line_range_begin=$((highlight_line - FZF_PREVIEW_LINES / 2))
Expand Down

0 comments on commit df5df5e

Please sign in to comment.