-
Notifications
You must be signed in to change notification settings - Fork 0
/
flan.go
120 lines (91 loc) · 2.23 KB
/
flan.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
package main
import (
"bufio"
"errors"
"fmt"
"os"
"strings"
)
/*
TODO:
- show historical invocations of command
- add $arg$ substitution to flannotations
- parse commands and allow jumping to relevant flags in manpage with tab?
*/
func flanpage(arg string) error {
manOut, manErr := manOutput(arg)
cmds, err := readFlanFile()
if err != nil {
return err
}
flannotations, prs := cmds[arg]
if prs {
err := combineFlanAndManOutput(flannotations, manOut, manErr)
if err != nil {
return err
}
} else {
return errors.New("no manpage or flannotations found :(")
}
return nil
}
func combineFlanAndManOutput(flannotations [][]string, manOut []byte, manErr error) error {
lessCmd := lessCommand(os.Environ(), os.Stdout, os.Stderr)
lessIn, err := lessCmd.StdinPipe()
if err != nil {
return err
}
if err := lessCmd.Start(); err != nil {
return err
}
lessIn.Write([]byte(color("FLANNOTATIONS:\n\n",
bold, yellow)))
for _, flanno := range flannotations {
lessIn.Write([]byte(" "))
lessIn.Write([]byte(color(flanno[1], yellow)))
lessIn.Write([]byte("\n"))
lessIn.Write([]byte(" "))
lessIn.Write([]byte(color("$ ", green)))
lessIn.Write([]byte(color(flanno[0], red)))
lessIn.Write([]byte("\n\n"))
}
if manErr != nil {
lessIn.Write([]byte(color("WARNING:\n\n", bold, red)))
}
lessIn.Write(manOut)
lessIn.Close()
if err := lessCmd.Wait(); err != nil {
return err
}
return nil
}
func flannotate() error {
leader := color("> ", bold, green)
cmds, err := readFlanFile()
if err != nil {
return err
}
prompt := fmt.Sprintf("Input a command example to flannotate:\n%s", leader)
fmt.Print(color(prompt, bold, blue))
reader := bufio.NewReader(os.Stdin)
input, err := reader.ReadString('\n')
if err != nil {
return err
}
cmdEx := strings.TrimSpace(input)
cmdName := strings.Split(cmdEx, " ")[0]
prompt = fmt.Sprintf("Input a new flannotation for %s:\n%s",
color(cmdEx, bold, red), leader)
fmt.Print(color(prompt, bold, blue))
input, err = reader.ReadString('\n')
if err != nil {
return err
}
cmdAnno := strings.TrimSpace(input)
store(cmdName, cmdEx, cmdAnno, cmds)
if err = writeFlanFile(cmds); err != nil {
return err
}
fmt.Println(color("🍮", magenta))
return nil
}