-
Notifications
You must be signed in to change notification settings - Fork 20
/
usage.go
274 lines (230 loc) · 7.25 KB
/
usage.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
package main
import (
"flag"
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"sort"
"strings"
log "github.com/sirupsen/logrus"
"github.com/walles/moar/m"
"github.com/walles/moar/twin"
)
func renderLessTermcapEnvVar(envVarName string, description string, colors twin.ColorCount) string {
value := os.Getenv(envVarName)
if len(value) == 0 {
return ""
}
style, err := m.TermcapToStyle(value)
if err != nil {
bold := twin.StyleDefault.WithAttr(twin.AttrBold).RenderUpdateFrom(twin.StyleDefault, colors)
notBold := twin.StyleDefault.RenderUpdateFrom(twin.StyleDefault.WithAttr(twin.AttrBold), colors)
return fmt.Sprintf(" %s (%s): %s %s<- Error: %v%s\n",
envVarName,
description,
strings.ReplaceAll(value, "\x1b", "ESC"),
bold,
err,
notBold,
)
}
prefix := style.RenderUpdateFrom(twin.StyleDefault, colors)
suffix := twin.StyleDefault.RenderUpdateFrom(style, colors)
return fmt.Sprintf(" %s (%s): %s\n",
envVarName,
description,
prefix+strings.ReplaceAll(value, "\x1b", "ESC")+suffix,
)
}
func renderPagerEnvVar(name string, colors twin.ColorCount) string {
bold := twin.StyleDefault.WithAttr(twin.AttrBold).RenderUpdateFrom(twin.StyleDefault, colors)
notBold := twin.StyleDefault.RenderUpdateFrom(twin.StyleDefault.WithAttr(twin.AttrBold), colors)
value, isSet := os.LookupEnv(name)
if value == "" {
what := "unset"
if isSet {
what = "empty"
}
return fmt.Sprintf(" %s is %s %s<- Should be %s%s\n",
name,
what,
bold,
getMoarPath(),
notBold,
)
}
absMoarPath, err := absLookPath(os.Args[0])
if err != nil {
log.Warn("Unable to find absolute moar path: ", err)
return ""
}
absEnvValue, err := absLookPath(value)
if err != nil {
// This can happen if this is set to some outdated value
absEnvValue = value
}
if absEnvValue == absMoarPath {
return fmt.Sprintf(" %s=%s\n", name, value)
}
return fmt.Sprintf(" %s=%s %s<- Should be %s%s\n",
name,
value,
bold,
getMoarPath(),
notBold,
)
}
// If the environment variable is set, render it as APA=bepa indented two
// spaces, plus a newline at the end. Otherwise, return an empty string.
func renderPlainEnvVar(envVarName string) string {
value := os.Getenv(envVarName)
if value == "" {
return ""
}
return fmt.Sprintf(" %s=%s\n", envVarName, value)
}
func printCommandline(output io.Writer) {
fmt.Fprintln(output, "Commandline: moar", strings.Join(os.Args[1:], " "))
fmt.Fprintf(output, "Environment: MOAR=\"%v\"\n", os.Getenv("MOAR"))
fmt.Fprintln(output)
}
func heading(text string, colors twin.ColorCount) string {
style := twin.StyleDefault.WithAttr(twin.AttrItalic)
prefix := style.RenderUpdateFrom(twin.StyleDefault, colors)
suffix := twin.StyleDefault.RenderUpdateFrom(style, colors)
return prefix + text + suffix
}
func printUsage(flagSet *flag.FlagSet, colors twin.ColorCount) {
// This controls where PrintDefaults() prints, see below
flagSet.SetOutput(os.Stdout)
// FIXME: Log if any printouts fail?
fmt.Println(heading("Usage", colors))
fmt.Println(" moar [options] <file>")
fmt.Println(" ... | moar")
fmt.Println(" moar < file")
fmt.Println()
fmt.Println("Shows file contents. Compressed files will be transparently decompressed.")
fmt.Println("Input is expected to be (possibly compressed) UTF-8 encoded text. Invalid /")
fmt.Println("non-printable characters are by default rendered as '?'.")
fmt.Println()
fmt.Println("More information + source code:")
fmt.Println(" <https://github.com/walles/moar#readme>")
fmt.Println()
fmt.Println(heading("Environment", colors))
moarEnv := os.Getenv("MOAR")
if len(moarEnv) == 0 {
fmt.Println(" Additional options are read from the MOAR environment variable if set.")
fmt.Println(" But currently, the MOAR environment variable is not set.")
} else {
fmt.Println(" Additional options are read from the MOAR environment variable.")
fmt.Printf(" Current setting: MOAR=\"%s\"\n", moarEnv)
}
envSection := ""
envSection += renderLessTermcapEnvVar("LESS_TERMCAP_md", "man page bold style", colors)
envSection += renderLessTermcapEnvVar("LESS_TERMCAP_us", "man page underline style", colors)
envSection += renderLessTermcapEnvVar("LESS_TERMCAP_so", "search hits and footer style", colors)
envSection += renderPagerEnvVar("PAGER", colors)
envVars := os.Environ()
sort.Strings(envVars)
for _, env := range envVars {
split := strings.SplitN(env, "=", 2)
if len(split) != 2 {
continue
}
name := split[0]
if name == "PAGER" {
// Already done above
continue
}
if !strings.HasSuffix(name, "PAGER") {
continue
}
envSection += renderPagerEnvVar(name, colors)
}
envSection += renderPlainEnvVar("TERM")
envSection += renderPlainEnvVar("TERM_PROGRAM")
envSection += renderPlainEnvVar("COLORTERM")
// Requested here: https://github.com/walles/moar/issues/170#issuecomment-1891154661
envSection += renderPlainEnvVar("MANROFFOPT")
if envSection != "" {
fmt.Println()
// Not Println since the section already ends with a newline
fmt.Print(envSection)
}
printSetDefaultPagerHelp(colors)
fmt.Println()
fmt.Println(heading("Options", colors))
flagSet.PrintDefaults()
fmt.Println(" +1234")
fmt.Println(" \tImmediately scroll to line 1234")
}
// If $PAGER isn't pointing to us, print a help text on how to set it.
func printSetDefaultPagerHelp(colors twin.ColorCount) {
absMoarPath, err := absLookPath(os.Args[0])
if err != nil {
log.Warn("Unable to find moar binary ", err)
return
}
absPagerValue, err := absLookPath(os.Getenv("PAGER"))
if err != nil {
absPagerValue = ""
}
if absPagerValue == absMoarPath {
// We're already the default pager
return
}
fmt.Println()
fmt.Println(heading("Making moar Your Default Pager", colors))
shellIsFish := strings.HasSuffix(os.Getenv("SHELL"), "fish")
shellIsPowershell := len(os.Getenv("PSModulePath")) > 0
if shellIsFish {
fmt.Println(" Write this command at your prompt:")
fmt.Println()
fmt.Printf(" set -Ux PAGER %s\n", getMoarPath())
} else if shellIsPowershell {
fmt.Println(" Put the following line in your $PROFILE file (\"echo $PROFILE\" to find it)")
fmt.Println(" and moar will be used as the default pager in all new terminal windows:")
fmt.Println()
fmt.Printf(" $env:PAGER = \"%s\"\n", getMoarPath())
} else {
// I don't know how to identify bash / zsh, put generic instructions here
fmt.Println(" Put the following line in your ~/.bashrc, ~/.bash_profile or ~/.zshrc")
fmt.Println(" and moar will be used as the default pager in all new terminal windows:")
fmt.Println()
fmt.Printf(" export PAGER=%s\n", getMoarPath())
}
}
// "moar" if we're in the $PATH, otherwise an absolute path
func getMoarPath() string {
moarPath := os.Args[0]
if filepath.IsAbs(moarPath) {
return moarPath
}
if strings.Contains(moarPath, string(os.PathSeparator)) {
// Relative path
moarPath, err := filepath.Abs(moarPath)
if err != nil {
panic(err)
}
return moarPath
}
// Neither absolute nor relative, try PATH
_, err := exec.LookPath(moarPath)
if err != nil {
panic("Unable to find in $PATH: " + moarPath)
}
return moarPath
}
func absLookPath(path string) (string, error) {
lookedPath, err := exec.LookPath(path)
if err != nil {
return "", err
}
absLookedPath, err := filepath.Abs(lookedPath)
if err != nil {
return "", err
}
return absLookedPath, err
}