Browse Source

update fv

DanielFGray 1 year ago
parent
commit
3a3436b130
1 changed files with 136 additions and 74 deletions
  1. 136
    74
      fv

+ 136
- 74
fv View File

@@ -1,11 +1,51 @@
1 1
 #!/usr/bin/env bash
2 2
 
3
-declare -r esc=$'\033'
4
-declare -r c_reset="${esc}[0m"
5
-declare -r c_red="${esc}[31m"
3
+usage() {
4
+  LESS=-FEXR less <<'HELP'
5
+fv [OPTIONS] [SEARCH]
6
+fuzzy file filtering and command executing
7
+
8
+a) allfiles=1 ;;
9
+c) cmd="$OPTARG" ;;
10
+d) dtach=1 ;;
11
+o) loop=1 ;;
12
+s) small=1 ;;
13
+
14
+-a    search all dirs and hidden files (possibly quirky)
15
+-c    command to execute [defaults to vim]
16
+-d    run in background (for ie non-terminal programs)
17
+-h    show this help
18
+-l    additional arguments to pass to filtering program
19
+-o    run continuously
20
+-s    run in a smaller window
21
+HELP
22
+}
23
+
24
+declare cmd=''
25
+declare cmdopts=()
26
+declare search_str=''
27
+declare search_cmd=''
28
+declare search_opts=()
29
+declare allfiles=''
30
+declare loop
31
+declare small
32
+declare -A colors
33
+colors[red]=$(tput setaf 1)
34
+colors[green]=$(tput setaf 2)
35
+colors[blue]=$(tput setaf 4)
36
+colors[reset]=$(tput sgr0)
37
+
38
+color() {
39
+  local c
40
+  c="$1"
41
+  shift
42
+  printf '%s' "${colors[$c]}"
43
+  printf '%s\n' "$@"
44
+  printf '%s' "${colors[reset]}"
45
+}
6 46
 
7 47
 err() {
8
-  printf "${c_red}%s${c_reset}\n" "$*" >&2
48
+  color red "$@" >&2
9 49
 }
10 50
 
11 51
 die() {
@@ -14,11 +54,14 @@ die() {
14 54
 }
15 55
 
16 56
 has() {
17
-  local verbose=0
18
-  if [[ $1 == '-v' ]]; then
19
-    verbose=1
20
-    shift
21
-  fi
57
+  local o c verbose
58
+  verbose=0
59
+  while getopts 'v' o; do
60
+    case "$o" in
61
+      v) v=1 ;;
62
+    esac
63
+  done
64
+  shift "$((OPTIND-1))"
22 65
   for c; do c="${c%% *}"
23 66
     if ! command -v "$c" &> /dev/null; then
24 67
       (( verbose > 0 )) && err "$c not found"
@@ -27,59 +70,59 @@ has() {
27 70
   done
28 71
 }
29 72
 
30
-usage() {
31
-  more <<'HELP'
32
-fv [OPTIONS] [SEARCH]
33
-fuzzy file filtering and command executing
34
-
35
--c    command to execute [defaults to vim]
36
--a    search all dirs and hidden files (still quirky)
37
--d    detach from terminal via nohup
38
-HELP
39
-}
40
-
41
-set_cmd() {
42
-   if has "$1"; then
43
-    cmd="$1"
44
-  else
45
-    die "$1 is not a valid command"
46
-  fi
73
+select_from() {
74
+  local o c cmd OPTARG OPTIND
75
+  cmd='command -v'
76
+  while getopts 'c:' o; do
77
+    case "$o" in
78
+      c) cmd="$OPTARG" ;;
79
+    esac
80
+  done
81
+  shift "$((OPTIND-1))"
82
+  for c; do
83
+    if $cmd "${c%% *}" &> /dev/null; then
84
+      echo "$c"
85
+      return 0
86
+    fi
87
+  done
88
+  return 1
47 89
 }
48 90
 
49
-declare cmd='vim'
50
-declare cmdopts=()
51
-declare search_str=''
52
-declare search_cmd=''
53
-declare search_opts=()
54
-declare allfiles=0
55
-
56
-while getopts "hadlc:" opt; do
91
+while getopts 'ac:dhlos' opt; do
57 92
   case "$opt" in
58
-    h) usage; exit 0        ;;
59
-    a) allfiles=1           ;;
60
-    c) set_cmd "$OPTARG"     ;;
61
-    d) unset detach         ;;
93
+    a) allfiles=1 ;;
94
+    c) cmd="$OPTARG" ;;
95
+    d) dtach=1 ;;
96
+    h) usage; exit 0 ;;
62 97
     l) search_opts+=( '-l' ) ;;
98
+    o) loop=1 ;;
99
+    s) small=1 ;;
63 100
   esac
64 101
 done
65 102
 shift "$((OPTIND-1))"
66 103
 
67 104
 has -v 'fzf' || die
68 105
 
69
-for c in 'ag' 'ack' 'grep'; do
70
-  if has "$c"; then
71
-    search_cmd="$c"
72
-    break
73
-  fi
74
-done
106
+if [[ -v FV_CMD ]]; then
107
+  cmd="$FV_CMD"
108
+elif [[ -z "$cmd" ]]; then
109
+  cmd=$(select_from 'v' 'vim')
110
+fi
111
+
112
+
113
+if [[ -v FV_SEARCH ]]; then
114
+  search_cmd="$FV_SEARCH"
115
+else
116
+  search_cmd=$(select_from 'ag' 'ack' 'grep')
117
+fi
75 118
 
76 119
 if [[ "$search_cmd"  == 'grep' ]]; then
77 120
   err 'grep is slow, you should strongly consider installing ag or ack'
78
-  sleep .5
121
+  sleep .75
79 122
 fi
80 123
 
81 124
 if [[ -n "$1" ]]; then
82
-  if [[ -d "$1" ]]; then
125
+  if [[ -e "$1" ]]; then
83 126
     search_opts+=( "$1" )
84 127
   else
85 128
     search_str="$1"
@@ -90,55 +133,74 @@ fi
90 133
 case "$search_cmd" in
91 134
   'ag')
92 135
     search_opts+=( '--color' )
93
-    if [[ "$allfiles" == 1 ]]; then
136
+    if [[ -n "$allfiles" ]]; then
94 137
       search_opts+=( '-u' '--hidden' )
95 138
     fi
96 139
     if [[ "$search_str" == '' ]]; then
97 140
       search_opts+=( '-l' )
98
-    fi
99
-    ;;
141
+    fi ;;
100 142
   'ack')
101 143
     if [[ "$search_str" == '' ]]; then
102
-      if [[ "$allfiles" == 0 ]]; then
103
-        search_opts+=( '-g' '^[^\.]' )
104
-      else
144
+      if [[ -z "$allfiles" ]]; then
105 145
         search_opts+=( '-f' )
146
+      else
147
+        search_opts+=( '-g' '^[^\.]' )
106 148
       fi
107 149
     else
108 150
       search_opts+=( '-l' )
109 151
     #   search_opts+=( '--match' )
110
-    fi
111
-    ;;
152
+    fi ;;
112 153
   'grep')
113 154
     search_opts+=( '-r' '-I' )
114
-    if [[ "$allfiles" == 0 ]]; then
115
-      search_opts+=( '--exclude-dir=bower_components' )
116
-      search_opts+=( '--exclude-dir=node_modules' )
117
-      search_opts+=( '--exclude-dir=jspm_packages' )
118
-      search_opts+=( '--exclude-dir=.cvs' )
119
-      search_opts+=( '--exclude-dir=.git' )
120
-      search_opts+=( '--exclude-dir=.hg' )
121
-      search_opts+=( '--exclude-dir=.svn' )
122
-    fi
123
-    if [[ "$search_str" == '' ]]; then
124
-      search_opts+=( '' )
155
+    if [[ -z "$allfiles" ]]; then
156
+      if [[ -r ~/.ignore ]]; then
157
+        while read -r line; do
158
+          search_opts+=( "--exclude-dir=$line" )
159
+        done < ~/.ignore
160
+      else
161
+        search_opts+=( '--exclude-dir=bower_components' )
162
+        search_opts+=( '--exclude-dir=node_modules' )
163
+        search_opts+=( '--exclude-dir=jspm_packages' )
164
+        search_opts+=( '--exclude-dir=.cvs' )
165
+        search_opts+=( '--exclude-dir=.git' )
166
+        search_opts+=( '--exclude-dir=.hg' )
167
+        search_opts+=( '--exclude-dir=.svn' )
168
+      fi
125 169
     fi
126
-    ;;
170
+    if [[ -z "$search_str" ]]; then
171
+      search_opts+=( -F '' )
172
+    else
173
+      search_opts+=( -P )
174
+    fi ;;
127 175
 esac
128 176
 
129 177
 if [[ "$search_str" != '' ]]; then
130 178
   search_opts+=( "$search_str" )
131 179
 fi
132 180
 
133
-choices=$($search_cmd "${search_opts[@]}" 2> /dev/null |
134
-  fzf --ansi --cycle --multi) || exit 1
181
+main() {
182
+  choices=$($search_cmd "${search_opts[@]}" 2> /dev/null |
183
+  fzf -e --ansi --cycle --multi --reverse ${small:+--reverse --height=50%}) || exit 1
135 184
 
136
-if [[ "$search_str" != '' ]]; then
137
-  if [[ $search_cmd == 'ag' ]]; then
138
-    choices=$(cut -d: -f1 <<< "$choices")
185
+  if [[ "$search_str" != '' ]]; then
186
+    if [[ $search_cmd == 'ag' ]]; then
187
+      choices=$(cut -d: -f1 <<< "$choices")
188
+    fi
139 189
   fi
140
-fi
141 190
 
142
-mapfile -t choices <<< "$choices"
191
+  mapfile -t choices <<< "$choices"
143 192
 
144
-$cmd ${cmdopts[*]} "${choices[@]}"
193
+  if [[ $dtach ]]; then
194
+    ($cmd "${cmdopts[@]}" "${choices[@]}" &> /dev/null &)
195
+  else
196
+    $cmd "${cmdopts[@]}" "${choices[@]}"
197
+  fi
198
+}
199
+
200
+if [[ -n "$loop" ]]; then
201
+  while main; do
202
+    true
203
+  done
204
+else
205
+  main
206
+fi

Loading…
Cancel
Save