-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
141 lines (120 loc) · 2.67 KB
/
main.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
package main
import (
"bufio"
"compress/gzip"
"encoding/json"
"fmt"
"io"
"os"
"path/filepath"
"strings"
tea "github.com/charmbracelet/bubbletea"
)
func findDocInManSection(sectionDir, target string) string {
section := strings.TrimPrefix(filepath.Base(sectionDir), "man")
fullTarget := fmt.Sprintf("%s.%s", target, section)
fullTargetGz := fmt.Sprintf("%s.%s.gz", target, section)
files, err := os.ReadDir(sectionDir)
if err != nil {
panic(err)
}
for _, file := range files {
if file.Name() == fullTarget || file.Name() == fullTargetGz {
return sectionDir + "/" + file.Name()
}
}
return ""
}
func findDocInManDir(mandir, target string) string {
dirs, err := os.ReadDir(mandir)
if err != nil {
panic(err)
}
for _, dir := range dirs {
if strings.HasPrefix(dir.Name(), "man") {
path := findDocInManSection(mandir+"/"+dir.Name(), target)
if path != "" {
return path
}
}
}
return ""
}
func findDoc(target string) string {
manPath := os.Getenv("MANPATH")
if len(manPath) > 0 {
for _, dir := range strings.Split(manPath, ":") {
if len(dir) == 0 {
continue
}
path := findDocInManDir(dir, target)
if path != "" {
return path
}
}
}
// TODO: locale support
return findDocInManDir("/usr/share/man", target)
}
func readManPage(path string) (string, error) {
file, err := os.Open(path)
if err != nil {
return "", nil
}
defer file.Close()
var reader io.Reader = bufio.NewReader(file)
if strings.HasSuffix(path, ".gz") {
gzipReader, err := gzip.NewReader(reader)
if err != nil {
return "", err
}
reader = gzipReader
}
data, err := io.ReadAll(reader)
if err != nil {
return "", err
}
return string(data), nil
}
func dumpAst(page manPage) {
bytes, err := json.Marshal(page)
if err != nil {
panic(err)
}
os.WriteFile("ast.json", bytes, 0666)
}
func main() {
if len(os.Args) != 2 {
fmt.Fprintf(os.Stderr, "Usage: %s <command>\n", os.Args[0])
os.Exit(1)
}
target := os.Args[1]
var manFile string
if _, err := os.Stat(target); err == nil {
manFile = target
} else {
manFile = findDoc(target)
if manFile == "" {
fmt.Fprintf(os.Stderr, "cannot find man page for \"%s\"\n", target)
os.Exit(1)
}
}
fmt.Println(manFile)
data, err := readManPage(manFile)
if err != nil {
panic(err)
}
parser := parser{}
page := parser.parseMdoc(data)
page.mergeSpans()
dumpAst(page)
p := tea.NewProgram(
NewModel(page),
tea.WithAltScreen(), // use the full size of the terminal in its "alternate screen buffer"
tea.WithMouseCellMotion(), // turn on mouse support so we can track the mouse wheel
)
if _, err := p.Run(); err != nil {
fmt.Println("could not run program:", err)
os.Exit(1)
}
}