diff --git a/.gitignore b/.gitignore index ca8e2a1..06e51c9 100644 --- a/.gitignore +++ b/.gitignore @@ -10,3 +10,4 @@ scripts/* !scripts/fstat !scripts/funtrack !scripts/fupgrade +!scripts/fgrep diff --git a/helper/git_query.sh b/helper/git_query.sh index 3fb92f9..f25ca82 100644 --- a/helper/git_query.sh +++ b/helper/git_query.sh @@ -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 + }' +} diff --git a/scripts/fgrep b/scripts/fgrep new file mode 100755 index 0000000..8e910f1 --- /dev/null +++ b/scripts/fgrep @@ -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[@]}"