Skip to content

Commit

Permalink
kamp-files: utilize getopts
Browse files Browse the repository at this point in the history
  • Loading branch information
vbauerster committed Nov 15, 2024
1 parent 0367833 commit e0128bb
Showing 1 changed file with 31 additions and 19 deletions.
50 changes: 31 additions & 19 deletions scripts/kamp-files
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,51 @@
#
# pick files
#
# Usage:
#
# kamp-files [<backend=fd|rg|find|git>] [backend_options]

# requires:
# - fd (https://github.com/sharkdp/fd)
# - fzf (https://github.com/junegunn/fzf)
# - bat (https://github.com/sharkdp/bat)

set -euf

preview_cmd='bat --color=always --line-range=:500 {}'
usage() {
printf "Usage: %s: [-b <fd|rg|find|git>] [backend_options]\n" "$(basename "$0")" >&2
exit 2
}

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

backend_cmd=""
case "$backend" in
fd) backend_cmd="fd --strip-cwd-prefix --color never --type file" ;;
rg) backend_cmd="rg --color never --files" ;;
find) backend_cmd="find . -type f" ;;
git) backend_cmd="git ls-files" ;;
*) backend_cmd="$backend" ;;
fd)
backend_cmd="fd --strip-cwd-prefix --type file"
;;
rg)
backend_cmd="rg --color never --files"
;;
find)
backend_cmd="find . -type f"
;;
git)
backend_cmd="git ls-files"
;;
*)
usage
;;
esac

backend_cmd="$backend_cmd $*"
preview_cmd='bat --color=always --line-range=:500 {}'

eval "$backend_cmd" |
fzf --multi --prompt "${backend}> " --preview "$preview_cmd" --bind 'enter:become(kamp edit {+})'

0 comments on commit e0128bb

Please sign in to comment.