You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
dotbare/scripts/fgrep

77 lines
1.7 KiB
Bash

#!/usr/bin/env bash
#
# Grep for words within all dotfiles
#
# @params
# Globals
# mydir: path to this current script
# selected_lines: selected lines to edit
# fzf_search_delimiter: the col to start searching in fzf
# Arguments
# -h|--help: show help message and exit
# -c COL| --col COL: use a different delimiter in fzf search
# -f|--full: include file name in fzf searching, as if using --col 1
set -e
set -f
mydir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
source "${mydir}"/../helper/set_variable.sh
source "${mydir}"/../helper/git_query.sh
function usage() {
echo -e "Usage: dotbare fgrep [-h] [-c] [-f] ...
Grep words within tracked files and select to edit matches.
Default: start searching from 3rd column (excluding the file name during search).
Optional arguments:
-h, --help\t\tshow this help message and exit.
-c COL, --col COL\tspecify the column number to start searching.
-f, --full\t\tinclude all column during search, as if using '--col 1'."
}
selected_lines=()
fzf_search_delimiter=3
while [[ "$#" -gt 0 ]]; do
case "$1" in
-c|--col)
fzf_search_delimiter="$2"
shift
shift
;;
-f|--full)
fzf_search_delimiter=1
shift
;;
-h|--help)
usage
exit 0
;;
*)
echo "Invalid option: $1" >&2
usage
exit 1
;;
esac
done
while IFS= read -r line; do
case "${EDITOR}" in
vim|nvim|nano)
# line number = "${line##*:}"
# file name = "${line%%:*}"
selected_lines+=(+"${line##*:}" "${line%%:*}")
;;
*)
selected_lines+=("${line}")
;;
esac
done < <(grep_words "select matches to edit" "${fzf_search_delimiter}")
[[ "${#selected_lines[@]}" -eq 0 ]] && exit 1
"${EDITOR}" "${selected_lines[@]}"