-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
104 lines (75 loc) · 2.28 KB
/
Copy pathmain.go
File metadata and controls
104 lines (75 loc) · 2.28 KB
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
package main
import (
"bufio"
"flag"
"fmt"
"os"
"strings"
"github.com/sftsrv/tri/tree"
"github.com/sftsrv/tri/ui"
)
var usage = `tri
An interactive tree-based search tool with file preview. Give it some path-looking things, and it'll make them readable
## Usage
'''
# files in a directory
find ./ | tri
# use alternate preview
find ./ | tri --preview glow
# files in a pr
git diff --name-only | tri
'''
### Using Patterns
Use Regexps to parse the input string to create more complex commands
> Escaping of regex special chars will depend on your shell
'''
# viewing a formatted git log and showing the changes for each hash
git log --pretty=format:"%h %f"
| tri --preview "git show $1" --pattern "^(\w+)"
# using a more complex regexp and command
git log --pretty=format:"%h %f"
| tri --preview "echo title: $title hash: $hash" --pattern "(?<hash>\w+) (?<title>.*)"
'''
### Using Regexps
The structure of the regexp provided should be compatible with Go's implementation,
with the following affordances made:
1. Named capture groups can be specified as '<name>' instead of '?P<name>'
2. Regexps are always matched against a single line - so multiline captures are not meaningful
### Using Patterns
References in pattern are indicated with a '$' in the pattern
- '$' refers to the entire match
- '$0' (entire match), '$1' (first capture group), etc. refer to capture groups in order of capture
- '$name' refers to named capture groups
`
func main() {
help := flag.Bool("help", false, "show help menu")
print := flag.Bool("print", false, "print tree (non interactive)")
flat := flag.Bool("flat", false, "flatten direct paths")
preview := flag.String("preview", "", "command to use for file preview")
pattern := flag.String("pattern", "", "pattern to use when parsing path")
flag.Parse()
if *help {
fmt.Println(usage)
flag.Usage()
return
}
paths := []string{}
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
paths = append(paths, line)
}
if len(paths) == 0 {
panic("Expected to be called with a list of paths from stdin")
}
t := tree.PathsToTree(paths)
if *print {
t.ExpandAll()
if *flat {
t.Flatten()
}
fmt.Println(tree.Render(t))
return
}
ui.Run(t, *preview, *pattern, *flat)
}