-
Notifications
You must be signed in to change notification settings - Fork 48
/
vgrep.go
1289 lines (1113 loc) · 30.9 KB
/
vgrep.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package main
// (c) 2015-2024 Valentin Rothberg <[email protected]>
//
// Licensed under the terms of the GNU GPL License version 3.
import (
"bufio"
"bytes"
"fmt"
"os"
"os/exec"
"os/signal"
"path"
"path/filepath"
"regexp"
"runtime"
"runtime/pprof"
"sort"
"strconv"
"strings"
"sync"
"time"
"github.com/google/shlex"
"github.com/jessevdk/go-flags"
jsoniter "github.com/json-iterator/go"
"github.com/mattn/go-shellwords"
"github.com/nightlyone/lockfile"
"github.com/peterh/liner"
"github.com/sirupsen/logrus"
"github.com/vrothberg/vgrep/internal/ansi"
"github.com/vrothberg/vgrep/internal/colwriter"
"golang.org/x/term"
)
// Noticeably faster than the standard lib and battle tested.
var json = jsoniter.ConfigCompatibleWithStandardLibrary
// cliArgs passed to go-flags
type cliArgs struct {
Debug bool `short:"d" long:"debug" description:"Verbose debug logging"`
FilesOnly bool `short:"l" long:"files-with-matches" description:"Print matching files only"`
Interactive bool `long:"interactive" description:"Enter interactive shell"`
MemoryProfile string `long:"memory-profile" description:"Write a memory profile to the specified path"`
NoGit bool `long:"no-git" description:"Use grep instead of git-grep"`
NoRipgrep bool `long:"no-ripgrep" description:"Do not use ripgrep"`
NoHeader bool `long:"no-header" description:"Do not print pretty headers"`
NoLess bool `long:"no-less" description:"Use stdout instead of less"`
Show string `short:"s" long:"show" description:"Show specified matches or open shell" value-name:"SELECTORS"`
Version bool `short:"v" long:"version" description:"Print version number"`
}
// vgrep stores state and the user-specified command-line arguments.
type vgrep struct {
cliArgs
exitCode int
matches [][]string
workDir string
lock lockfile.Lockfile
waiter sync.WaitGroup
}
// the type of underlying grep program
const (
BSDGrep = "BSD"
GNUGrep = "GNU"
GITGrep = "GIT"
RIPGrep = "RIP"
)
var (
// set in the Makefile
version string
commands = [...]string{"print", "show", "context", "tree", "delete",
"keep", "refine", "files", "grep", "quit", "?"}
)
func main() {
var (
err error
v vgrep
)
// vgrep must not be terminated with SIGINT since less pager must be
// terminated before vgrep. Ignore SIGINT.
sc := make(chan os.Signal, 1)
signal.Notify(sc, os.Interrupt)
go func() {
for range sc {
}
}()
// Unknown flags will be ignored and stored in args to further pass them
// to (git) grep.
parser := flags.NewParser(&v, flags.Default|flags.IgnoreUnknown)
args, err := parser.ParseArgs(os.Args[1:])
if err != nil {
// Don't print the error to make sure the help message is printed once.
// In other words, let's rely on the parser to print errors.
os.Exit(1)
}
if v.MemoryProfile != "" {
// Same value as the default in github.com/pkg/profile.
runtime.MemProfileRate = 4096
if rate := os.Getenv("MemProfileRate"); rate != "" {
r, err := strconv.Atoi(rate)
if err != nil {
logrus.Errorf("%v", err)
os.Exit(1)
}
runtime.MemProfileRate = r
}
defer func() { // Write the profile at the end
f, err := os.Create(v.MemoryProfile)
if err != nil {
logrus.Errorf("Creating memory profile: %v", err)
return
}
defer f.Close()
runtime.GC() // get up-to-date GC statistics
if err := pprof.WriteHeapProfile(f); err != nil {
logrus.Errorf("Writing memory profile: %v", err)
return
}
}()
}
if v.Version {
fmt.Println(version)
os.Exit(0)
}
if v.Debug {
logrus.SetLevel(logrus.DebugLevel)
logrus.Debug("log level set to debug")
}
logrus.Debugf("passed args: %s", args)
// Load the cache if there's no new query, otherwise execute a new one.
err = v.makeLockFile()
if err != nil {
fmt.Fprintf(os.Stderr, "error creating lock file: %v\n", err)
os.Exit(1)
}
v.workDir, err = resolvedWorkdir()
if err != nil {
fmt.Fprintf(os.Stderr, "error resolving working directory: %v\n", err)
os.Exit(1)
}
haveToRunCommand := v.Show != "" || v.Interactive
// append additional args to the show command
if v.Show != "" && len(args) > 0 {
v.Show = fmt.Sprintf("%s %s", v.Show, strings.Join(args, ""))
}
if haveToRunCommand || len(args) == 0 {
err = v.loadCache()
if err != nil {
if os.IsNotExist(err) {
os.Exit(0)
}
fmt.Fprintf(os.Stderr, "error loading cache: %v\n", err)
os.Exit(1)
}
if len(v.matches) == 0 {
if v.exitCode != 0 {
os.Exit(v.exitCode)
}
os.Exit(1)
}
if haveToRunCommand {
v.commandParse()
} else {
v.commandPrintMatches([]int{})
}
v.waiter.Wait()
os.Exit(v.exitCode)
}
v.waiter.Add(1)
v.grep(args)
v.cacheWrite() // this runs in the background
if len(v.matches) == 0 {
v.waiter.Wait()
if v.exitCode != 0 {
os.Exit(v.exitCode)
}
os.Exit(1)
}
// Last resort, print all matches.
v.commandPrintMatches([]int{})
v.waiter.Wait()
os.Exit(v.exitCode)
}
// runCommand executes the program specified in args and returns the stdout as
// a line-separated []string.
func (v *vgrep) runCommand(args []string, env string) ([]string, error) {
var cmd *exec.Cmd
var sout, serr bytes.Buffer
logrus.Debugf("runCommand(args=%s, env=%s)", args, env)
cmd = exec.Command(args[0], args[1:]...)
cmd.Stdout = &sout
cmd.Stderr = &serr
cmd.Env = []string{env}
err := cmd.Run()
if err != nil {
logrus.Debugf("error running command: %v", err)
if exitError, ok := err.(*exec.ExitError); ok {
exitCode := exitError.ExitCode()
switch exitCode {
case 1:
logrus.Debug("ignoring error (no matches found)")
default:
logrus.Errorf("%s", strings.TrimSuffix(serr.String(), "\n"))
v.exitCode = exitCode
}
err = nil
}
}
slice := strings.Split(sout.String(), "\n")
return slice[:len(slice)-1], err
}
// insideGitTree returns true if the current working directory is inside a git
// tree.
func (v *vgrep) insideGitTree() bool {
cmd := []string{"git", "rev-parse", "--is-inside-work-tree"}
out, _ := v.runCommand(cmd, "")
inside := false
if len(out) > 0 && out[0] == "true" {
inside = true
}
logrus.Debugf("insideGitTree() -> %v", inside)
return inside
}
func (v *vgrep) isOpenBSD() bool {
return runtime.GOOS == "openbsd"
}
// ripgrepInstalled returns true if ripgrep is installed
func (v *vgrep) ripgrepInstalled() bool {
out, err := exec.LookPath("rg")
if err != nil {
logrus.Debug("error checking if ripgrep is installed")
}
installed := false
if len(out) > 0 {
installed = true
}
logrus.Debugf("ripgrepInstalled() -> %v", installed)
return installed
}
func (v *vgrep) getGrepType() string {
out, _ := v.runCommand([]string{"grep", "--version"}, "")
if len(out) == 0 {
return ""
}
versionString := out[0]
// versionString = "grep (BSD grep) 2.5.1-FreeBSD"
// versionString = "grep (BSD grep, GNU compatible) 2.6.0-FreeBSD"
versionRegex := regexp.MustCompile(`\(([[:alpha:]]+) grep`)
// versionRegex matches to ["(BSD grep)", "BSD"], return "BSD"
submatch := versionRegex.FindStringSubmatch(versionString)
if len(submatch) < 2 {
return ""
}
return submatch[1]
}
// isVscode checks if the terminal is running inside of vscode.
func isVscode() bool {
return os.Getenv("TERM_PROGRAM") == "vscode"
}
// isGoland checks if the terminal is running inside of goland or possible other JetBrains IDEs.
func isGoland() bool {
return strings.Contains(os.Getenv("TERMINAL_EMULATOR"), "JetBrains")
}
// grep (git) greps with the specified args and stores the results in v.matches.
func (v *vgrep) grep(args []string) {
var cmd []string
var env string
var greptype string // can have values , GIT, RIP, GNU, BSD
if v.ripgrepInstalled() && !v.NoRipgrep {
cmd = []string{
"rg", "-0", "--colors=path:none", "--colors=line:none",
"--color=always", "--no-heading", "--line-number",
"--with-filename",
}
cmd = append(cmd, args...)
greptype = RIPGrep
config := os.Getenv("RIPGREP_CONFIG_PATH")
if len(config) != 0 {
env = "RIPGREP_CONFIG_PATH=" + config
}
} else if v.insideGitTree() && !v.NoGit {
env = "HOME="
cmd = []string{
"git", "-c", "color.grep.match=red bold",
"grep", "-z", "-In", "--color=auto",
}
cmd = append(cmd, args...)
greptype = GITGrep
} else if v.isOpenBSD() && v.getGrepType() == "" {
// grep --version = "grep version 0.9"
cmd = []string{"grep", "-ZHInr"}
cmd = append(cmd, args...)
greptype = BSDGrep
} else {
env = "GREP_COLORS='ms=01;31:mc=:sl=:cx=:fn=:ln=:se=:bn='"
cmd = []string{"grep", "-ZHInr", "--color=always"}
cmd = append(cmd, args...)
greptype = v.getGrepType()
}
output, err := v.runCommand(cmd, env)
if err != nil {
fmt.Fprintf(os.Stderr, "searching symbols failed: %v\n", err)
os.Exit(1)
}
v.matches = make([][]string, len(output))
i := 0
for _, m := range output {
file, line, content, err := v.splitMatch(m, greptype)
if err != nil {
logrus.Debugf("skipping line %q (parse error: %v)", m, err)
continue
}
v.matches[i] = make([]string, 4)
v.matches[i][0] = strconv.Itoa(i)
v.matches[i][1] = file
v.matches[i][2] = line
v.matches[i][3] = content
i++
}
logrus.Debugf("found %d matches", len(v.matches))
}
// splitMatch splits match into its file, line and content. The format of
// match varies depending if it has been produced by grep or git-grep.
func (v *vgrep) splitMatch(match string, greptype string) (file, line, content string, err error) {
if greptype == RIPGrep {
// remove default color ansi escape codes from ripgrep's output
match = strings.Replace(match, "\x1b[0m", "", 4)
}
var separator []byte
switch greptype {
case BSDGrep:
separator = []byte(":")
case GITGrep, GNUGrep, RIPGrep:
separator = []byte{0}
}
switch greptype {
case BSDGrep, GITGrep:
spl := bytes.SplitN([]byte(match), separator, 3)
if len(spl) < 3 {
err = fmt.Errorf("expected %d but split into %d items (%v)", 3, len(spl), separator)
return
}
file, line, content = string(spl[0]), string(spl[1]), string(spl[2])
case GNUGrep, RIPGrep:
spl := bytes.SplitN([]byte(match), separator, 2)
if len(spl) < 2 {
err = fmt.Errorf("expected %d but split into %d items (%v)", 2, len(spl), separator)
return
}
splline := bytes.SplitN(spl[1], []byte(":"), 2)
if len(splline) != 2 {
// Fall back to "-" which is used when displaying
// context lines.
splline = bytes.SplitN(spl[1], []byte("-"), 2)
}
if len(splline) == 2 {
file, line, content = string(spl[0]), string(splline[0]), string(splline[1])
return
}
err = fmt.Errorf("unexpected input")
return
default:
err = fmt.Errorf("unknown grep type %q", greptype)
return
}
return
}
// getEditor returns the EDITOR environment variable (default="vim").
func (v *vgrep) getEditor() []string {
editor, err := shlex.Split(os.Getenv("EDITOR"))
if len(editor) == 0 {
editor = []string{"vim"}
}
if err != nil {
logrus.Info("Error parsing EDITOR, falling back to `vim'")
}
return editor
}
// getEditorLineFlag returns the EDITORLINEFLAG environment variable (default="+").
func (v *vgrep) getEditorLineFlag() string {
editor := os.Getenv("EDITORLINEFLAG")
if len(editor) == 0 {
editor = "+"
}
return editor
}
// getEditorLineFlagReversed returns the EDITORLINEFLAGREVERSED
// environment variable (default=false).
func (v *vgrep) getEditorLineFlagReversed() bool {
order, err := strconv.ParseBool(os.Getenv("EDITORLINEFLAGREVERSED"))
if err != nil {
order = false
}
return order
}
// Create the lock file to guard against concurrent processes
func (v *vgrep) makeLockFile() error {
var err error
var lockdir string
if runtime.GOOS == "windows" {
lockdir = filepath.Join(os.Getenv("LOCALAPPDATA"), "vgrep")
} else {
lockdir = filepath.Join(os.Getenv("HOME"), ".local/share/vgrep")
}
exists := true
if _, err := os.Stat(lockdir); err != nil {
if os.IsNotExist(err) {
exists = false
} else {
return err
}
}
if !exists {
if err := os.MkdirAll(lockdir, 0700); err != nil {
return err
}
}
v.lock, err = lockfile.New(filepath.Join(lockdir, "cache-lock"))
return err
}
// Try to acquire the lock file for the cache
func (v *vgrep) acquireLock() error {
for err := v.lock.TryLock(); err != nil; err = v.lock.TryLock() {
// If the lock is busy, wait for it, otherwise error out
if err != lockfile.ErrBusy {
return err
}
time.Sleep(10 * time.Millisecond)
}
return nil
}
// cachePath returns the path to the user-specific vgrep cache.
func (v *vgrep) cachePath() (string, error) {
var cache string
if runtime.GOOS == "windows" {
cache = filepath.Join(os.Getenv("LOCALAPPDATA"), "vgrep-cache/")
} else {
cache = filepath.Join(os.Getenv("HOME"), ".cache/")
}
exists := true
if _, err := os.Stat(cache); err != nil {
if os.IsNotExist(err) {
exists = false
} else {
return "", err
}
}
if !exists {
if err := os.Mkdir(cache, 0700); err != nil {
return "", err
}
}
return filepath.Join(cache, "vgrep-go"), nil
}
// cacheWrite uses cacheWriterHelper to write to the user-specific vgrep cache.
func (v *vgrep) cacheWrite() {
go func() {
defer v.waiter.Done()
if err := v.cacheWriterHelper(); err != nil {
logrus.Debugf("error writing cache: %v", err)
}
}()
}
// cacheWriterHelper writes to the user-specific vgrep cache.
func (v *vgrep) cacheWriterHelper() error {
logrus.Debug("cacheWriterHelper(): start")
defer logrus.Debug("cacheWriterHelper(): end")
workDir, err := resolvedWorkdir()
if err != nil {
return err
}
cache, err := v.cachePath()
if err != nil {
return fmt.Errorf("error getting cache path: %v", err)
}
if err := v.acquireLock(); err != nil {
return fmt.Errorf("error acquiring lock file: %v", err)
}
defer func() {
if err := v.lock.Unlock(); err != nil {
panic(fmt.Sprintf("Error releasing lock file: %v", err))
}
}()
file, err := os.OpenFile(cache, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
if err != nil {
return err
}
out := append(v.matches, []string{workDir})
b, err := json.Marshal(out)
if err != nil {
return err
}
if _, err := file.Write(b); err != nil {
return err
}
if err := file.Sync(); err != nil {
return err
}
return file.Close()
}
// resolvedWorkdir returns the path to current working directory (fully evaluated in case it's a symlink).
func resolvedWorkdir() (string, error) {
workDir, err := os.Getwd()
if err != nil {
return "", fmt.Errorf("error getting working dir: %v", err)
}
return filepath.EvalSymlinks(workDir)
}
// loadCache loads the user-specific vgrep cache.
func (v *vgrep) loadCache() error {
logrus.Debug("loadCache(): start")
defer logrus.Debug("loadCache(): end")
cache, err := v.cachePath()
if err != nil {
return fmt.Errorf("error getting cache path: %v", err)
}
if err := v.acquireLock(); err != nil {
return err
}
defer func() {
if err := v.lock.Unlock(); err != nil {
panic(fmt.Sprintf("Error releasing lock file: %v", err))
}
}()
file, err := os.ReadFile(cache)
if err != nil {
return err
}
if err := json.Unmarshal(file, &v.matches); err != nil {
// if there's an error unmarshalling it, remove the cache file
os.Remove(cache)
return err
}
if length := len(v.matches); length > 0 {
v.workDir = v.matches[length-1][0]
v.matches = v.matches[:len(v.matches)-1]
}
return nil
}
// sortKeys returns a sorted []string of m's keys.
func sortKeys(m map[string]int) []string {
keys := make([]string, 0, len(m))
for key := range m {
keys = append(keys, key)
}
sort.Strings(keys)
return keys
}
// shellCompleter is a completion function for the interactive shell's prompt.
func shellCompleter(line string) (c []string) {
args, err := shellwords.Parse(line)
if err != nil {
return
}
if len(args) < 2 {
for _, cmd := range commands {
if strings.HasPrefix(cmd, line) {
c = append(c, cmd)
}
}
return
}
arg := args[len(args)-1]
switch args[0] {
case "g", "grep":
if len(args) < 3 {
return
}
dir := filepath.Dir(arg)
base := filepath.Base(arg)
if arg[len(arg)-1] == '/' {
dir = arg
base = ""
}
files, err := os.ReadDir(dir)
if err != nil {
return
}
for _, f := range files {
if strings.HasPrefix(f.Name(), base) {
comp := line + f.Name()[len(base):]
c = append(c, comp)
}
}
default:
return
}
return
}
// commandParse starts and dispatches user-specific vgrep commands. If the
// user input matches a vgrep selector commandShow will be executed. It will
// prompt the user for commands if we're running in interactive mode.
func (v *vgrep) commandParse() {
line := liner.NewLiner()
defer line.Close()
line.SetCtrlCAborts(true)
line.SetCompleter(shellCompleter)
nextInput := func() string {
usrInp, err := line.Prompt("Enter a vgrep command: ")
if err != nil {
// Either we hit an error or EOF (ctrl+d)
line.Close()
fmt.Fprintf(os.Stderr, "error parsing user input: %v\n", err)
os.Exit(1)
}
line.AppendHistory(usrInp)
logrus.Debugf("user input: %q", usrInp)
return usrInp
}
input := v.Show
quit := false
if input != "" {
quit = v.dispatchCommand(input)
}
for !quit && v.Interactive {
input = nextInput()
quit = v.dispatchCommand(input)
}
}
// v.checkIndices is a helper function to fill indices in case it's an empty
// array and does some range checks otherwise.
func (v *vgrep) checkIndices(indices []int) ([]int, error) {
if len(indices) == 0 {
indices = make([]int, len(v.matches))
for i := range v.matches {
indices[i] = i
}
return indices, nil
}
for _, idx := range indices {
if idx < 0 || idx > len(v.matches)-1 {
return nil, fmt.Errorf("index %d out of range (%d, %d)", idx, 0, len(v.matches)-1)
}
}
return indices, nil
}
// dispatchCommand parses and dispatches the specified vgrep command in input.
// The return value indicates if dispatching of commands should be stopped.
func (v *vgrep) dispatchCommand(input string) bool {
logrus.Debugf("dispatchCommand(%s)", input)
if len(input) == 0 {
return v.commandPrintHelp()
}
// Most commands accept only a set of selectors on indices as arguments.
// Before we try to parse arguments as selectors, deal with the few
// commands that take random strings as arguments.
cmdArray := strings.SplitN(input, " ", 2)
if cmdArray[0] == "r" || cmdArray[0] == "refine" {
if len(cmdArray) != 2 {
fmt.Println("refine expects a regexp argument")
return false
}
return v.commandRefine(cmdArray[1])
}
if cmdArray[0] == "g" || cmdArray[0] == "grep" {
if len(cmdArray) < 2 {
fmt.Println("grep expects at least a pattern")
return false
}
return v.commandGrep(cmdArray[1])
}
// normalize selector-only inputs (e.g., "1,2,3,5-10") to the show cmd
selectorRegexp := `(\s*all|[\d , -]+){0,1}`
numRgx := regexp.MustCompile(`^` + selectorRegexp + `$`)
if numRgx.MatchString(input) {
input = "s " + input
}
cmdRgx := regexp.MustCompile(`^([a-z?]{1,})([\d]+){0,1}` + selectorRegexp + `$`)
if !cmdRgx.MatchString(input) {
fmt.Printf("%q doesn't match format %q\n", input, "command[context lines] [selectors]")
return false
}
var command, selectors string
var context int
cmdArray = cmdRgx.FindStringSubmatch(input)
command = cmdArray[1]
selectors = cmdArray[3]
context = -1
if len(cmdArray[2]) > 0 {
var err error
context, err = strconv.Atoi(cmdArray[2])
if err != nil {
fmt.Printf("cannot convert specified context lines %q: %v", cmdArray[2], err)
return false
}
}
indices, err := v.parseSelectors(selectors)
if err != nil {
fmt.Println(err)
return false
}
switch command {
case "?":
return v.commandPrintHelp()
case "c", "context":
if context == -1 {
context = 5
}
return v.commandPrintContextLines(indices, context)
case "d", "delete":
if len(indices) == 0 {
fmt.Println("delete requires specified selectors")
return false
}
return v.commandDelete(indices)
case "k", "keep":
if len(indices) == 0 {
fmt.Println("keep requires specified selectors")
return false
}
return v.commandKeep(indices)
case "f", "files":
return v.commandListFiles(indices)
case "p", "print":
return v.commandPrintMatches(indices)
case "q", "quit":
return true
case "s", "show":
if len(indices) == 0 {
fmt.Println("show requires specified selectors")
} else {
for _, idx := range indices {
v.commandShow(idx)
}
}
return false
case "t", "tree":
return v.commandListTree(indices)
default:
fmt.Printf("unsupported command %q\n", command)
return false
}
}
// commandPrintHelp prints the help/usage message for vgrep commands on stdout.
func (v *vgrep) commandPrintHelp() bool {
// Join command names, but write first letter in bold.
commandList := ansi.Bold(string(commands[0][0])) + commands[0][1:]
for _, c := range commands[1:] {
commandList += ", " + ansi.Bold(string(c[0])) + c[1:]
}
fmt.Printf("vgrep command help: command[context lines] [selectors]\n")
fmt.Printf(" selectors: '3' (single), '1,2,6' (multi), '1-8' (range), 'all'\n")
fmt.Printf(" commands: %s\n", commandList)
return false
}
// commandPrintMatches prints all matches specified in indices using less(1) or
// stdout in case v.NoLess is specified. If indices is empty all matches
// are printed.
func (v *vgrep) commandPrintMatches(indices []int) bool {
var toPrint [][]string
var err error
indices, err = v.checkIndices(indices)
if err != nil {
fmt.Printf("%v\n", err)
return false
}
if v.FilesOnly {
visited := make(map[string]bool)
for _, i := range indices {
file := v.matches[i][1]
if _, exists := visited[file]; exists {
continue
}
visited[file] = true
fmt.Println(file)
}
return false
}
if !v.NoHeader {
toPrint = append(toPrint, []string{"Index", "File", "Line", "Content"})
}
inIDE := isVscode() || isGoland()
for _, i := range indices {
switch {
case inIDE:
// If we're running inside an IDE's terminal, append
// the line to the file path, so we can quick jump to
// the specific location. Note that dancing around
// with the indexes below is intentional - ugly but
// fast.
toPrint = append(toPrint, []string{v.matches[i][0], v.matches[i][1] + ":" + v.matches[i][2], v.matches[i][2], v.matches[i][3]})
default:
toPrint = append(toPrint, v.matches[i])
}
}
useLess := !v.NoLess
if !term.IsTerminal(int(os.Stdout.Fd())) {
useLess = false
}
cw := colwriter.New(4)
cw.Headers = true && !v.NoHeader
cw.Colors = []ansi.COLOR{ansi.MAGENTA, ansi.BLUE, ansi.GREEN, ansi.DEFAULT}
cw.Padding = []colwriter.PaddingFunc{colwriter.PadLeft, colwriter.PadRight, colwriter.PadLeft, colwriter.PadNone}
cw.UseLess = useLess
cw.Trim = []bool{false, false, false, true}
cw.Open()
cw.Write(toPrint)
cw.Close()
return false
}
// fileLocation returns the path and line number of the matches at the
// specified index.
func (v *vgrep) fileLocation(index int) (string, int, error) {
p := v.matches[index][1]
// If it's not an absolute path, join it with the workDir.
// This allows for using vgrep from another working dir
// than where the initial query was done.
if !path.IsAbs(p) {
p = path.Join(v.workDir, p)
}
line, err := strconv.Atoi(v.matches[index][2])
if err != nil {
return "", 0, err
}
return p, line, nil
}
// getContextLines return numLines context lines before and after the match at
// the specified index including the matched line itself as []string.
func (v *vgrep) getContextLines(index int, numLines int) [][]string {
var contextLines [][]string
path, line, err := v.fileLocation(index)
if err != nil {
logrus.Warn(err.Error())
return nil
}
file, err := os.Open(path)
if err != nil {
logrus.Warnf("error opening file %q: %v", path, err)
return nil
}
defer file.Close()
scanner := bufio.NewScanner(file)
counter := 0
for scanner.Scan() {
counter++
if counter == line {
newContext := []string{strconv.Itoa(counter), v.matches[index][3]}
contextLines = append(contextLines, newContext)
} else if (counter >= line-numLines) && (counter <= line+numLines) {
newContext := []string{strconv.Itoa(counter), scanner.Text()}
contextLines = append(contextLines, newContext)
}
if counter > line+numLines {
break
}
}
return contextLines
}
// commandPrintContextLines prints at most numLines context lines before and
// after each match specified in indices.
func (v *vgrep) commandPrintContextLines(indices []int, numLines int) bool {
var err error
logrus.Debugf("commandPrintContextLines(indices=[..], numlines=%d)", numLines)
indices, err = v.checkIndices(indices)
if err != nil {
fmt.Printf("%v\n", err)
return false
}
cw := colwriter.New(2)
cw.Colors = []ansi.COLOR{ansi.MAGENTA, ansi.DEFAULT}
cw.Padding = []colwriter.PaddingFunc{colwriter.PadLeft, colwriter.PadNone}
cw.UseLess = !v.NoLess
cw.Open()
for _, idx := range indices {
toPrint := v.getContextLines(idx, numLines)
if toPrint == nil {
continue
}
sep := fmt.Sprintf("%s %s %s ",
ansi.Color("---", ansi.MAGENTA, false),
ansi.Color(strconv.Itoa(idx), ansi.MAGENTA, false),
ansi.Color(v.matches[idx][1], ansi.BLUE, false))