fzgrep (1552B) - raw


      1 #!/usr/bin/env zsh
      2 
      3 0="${${ZERO:-${0:#$ZSH_ARGZERO}}:-${(%):-%N}}"
      4 0="${${(M)0:#/*}:-$PWD/$0}"
      5 
      6 # Grep for words within all dotfiles
      7 #
      8 # @params
      9 # Globals
     10 #   mydir: path to this current script
     11 #   selected_lines: selected lines to edit
     12 #   fzf_search_delimiter: the col to start searching in fzf
     13 
     14 emulate -LR zsh
     15 setopt warncreateglobal noshortloops
     16 
     17 local mydir; mydir=${0:h:A}
     18 builtin source ${mydir}/fgit_helper
     19 
     20 
     21 usage() {
     22   echo -e "Usage: fgrep [-h] [-c] [-f] ...
     23 
     24 Grep words within tracked files and select to edit matches.
     25 
     26 Default: start searching from 3rd column (excluding the file name during search).
     27 
     28 Optional arguments:
     29   -h, --help\t\tshow this help message and exit.
     30   -c COL, --col COL\tspecify the column number to start searching.
     31   -f, --full\t\tinclude all column during search, as if using '--col 1'."
     32 }
     33 
     34 typeset -a selected_lines=()
     35 typeset -i fzf_search_delimiter=3
     36 
     37 while [[ "$#" -gt 0 ]]; do
     38   case "$1" in
     39     -c|--col) fzf_search_delimiter="$2" shift 2 ;;
     40     -f|--full) fzf_search_delimiter=1 shift ;;
     41     -h|--help) usage && exit 0 ;;
     42     *) echo "Invalid option: $1" >&2 && usage && exit 1 ;;
     43   esac
     44 done
     45 
     46 while IFS= read -r line; do
     47   case "${EDITOR}" in
     48     vim|nvim|nano)
     49       # line number = "${line##*:}"
     50       # file name = "${line%%:*}"
     51       selected_lines+=(+"${line##*:}" "${line%%:*}")
     52       ;;
     53     *)
     54       selected_lines+=("${line}")
     55       ;;
     56   esac
     57 done < <(grep_words "select matches to edit" "${fzf_search_delimiter}")
     58 
     59 [[ "${#selected_lines[@]}" -eq 0 ]] && exit 1
     60 
     61 "${EDITOR}" "${selected_lines[@]}"