feat(fgrep): added fgrep

pull/19/head
Kevin Zhuang 4 years ago
parent 8f69044794
commit 4acc4963c9

1
.gitignore vendored

@ -10,3 +10,4 @@ scripts/*
!scripts/fstat
!scripts/funtrack
!scripts/fupgrade
!scripts/fgrep

@ -189,3 +189,26 @@ function get_stash() {
print $1
}'
}
#######################################
# Using git grep to find word within
# all tracked files in the bare repo.
# Arguments:
# $1: the help message to display in header
# $2: if exists, don't do multi select, only allow single selection
# Outputs:
# the selected file name with it's line number and line, seperated by ":"
# e.g. .bash_profile:1:echo hello
#######################################
function grep_lines() {
local header="${1:-select lines to edit}"
set_fzf_multi "$2"
cd "${DOTBARE_TREE}"
git --git-dir="${DOTBARE_DIR}" --work-tree="${DOTBARE_TREE}" \
grep --line-number -- . \
| fzf --delimiter : --nth 3.. --header="${header}" \
--preview "${mydir}/../helper/preview.sh ${DOTBARE_TREE}/{}" \
| awk -F ":" -v home="${DOTBARE_TREE}" '{
print home "/" $1 ":" $2
}'
}

@ -0,0 +1,59 @@
#!/usr/bin/env bash
#
# Grep for words within all dotfiles
#
# @params
# Globals
# mydir: path to this current script
# selected_lines: selected lines to edit
# Arguments
# -h|--help: show help message and exit
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 fedit [-h] ...
Grep words within tracked files and select to edit them through fzf.
Optional arguments:
-h, --help\t\tshow this help message and exit."
}
selected_lines=()
while [[ "$#" -gt 0 ]]; do
case "$1" in
-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_lines)
[[ "${#selected_lines[@]}" -eq 0 ]] && exit 1
"${EDITOR}" "${selected_lines[@]}"
Loading…
Cancel
Save