• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

charmbracelet / glow / 14244739488

03 Apr 2025 01:52PM UTC coverage: 3.722% (-0.007%) from 3.729%
14244739488

Pull #735

github

web-flow
chore: fix typo in error

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Pull Request #735: chore: do some basic repo maintenance (editorconfig, linting, etc)

0 of 85 new or added lines in 14 files covered. (0.0%)

1 existing line in 1 file now uncovered.

77 of 2069 relevant lines covered (3.72%)

0.05 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

18.87
/main.go
1
// Package main provides the entry point for the Glow CLI application.
2
package main
3

4
import (
5
        "errors"
6
        "fmt"
7
        "io"
8
        "io/fs"
9
        "net/http"
10
        "net/url"
11
        "os"
12
        "os/exec"
13
        "path/filepath"
14
        "strings"
15

16
        "github.com/caarlos0/env/v11"
17
        "github.com/charmbracelet/glamour"
18
        "github.com/charmbracelet/glamour/styles"
19
        "github.com/charmbracelet/glow/v2/ui"
20
        "github.com/charmbracelet/glow/v2/utils"
21
        "github.com/charmbracelet/lipgloss"
22
        "github.com/charmbracelet/log"
23
        gap "github.com/muesli/go-app-paths"
24
        "github.com/spf13/cobra"
25
        "github.com/spf13/viper"
26
        "golang.org/x/term"
27
)
28

29
var (
30
        // Version as provided by goreleaser.
31
        Version = ""
32
        // CommitSHA as provided by goreleaser.
33
        CommitSHA = ""
34

35
        readmeNames      = []string{"README.md", "README", "Readme.md", "Readme", "readme.md", "readme"}
36
        configFile       string
37
        pager            bool
38
        tui              bool
39
        style            string
40
        width            uint
41
        showAllFiles     bool
42
        showLineNumbers  bool
43
        preserveNewLines bool
44
        mouse            bool
45

46
        rootCmd = &cobra.Command{
47
                Use:   "glow [SOURCE|DIR]",
48
                Short: "Render markdown on the CLI, with pizzazz!",
49
                Long: paragraph(
50
                        fmt.Sprintf("\nRender markdown on the CLI, %s!", keyword("with pizzazz")),
51
                ),
52
                SilenceErrors:    false,
53
                SilenceUsage:     true,
54
                TraverseChildren: true,
55
                Args:             cobra.MaximumNArgs(1),
56
                ValidArgsFunction: func(*cobra.Command, []string, string) ([]string, cobra.ShellCompDirective) {
×
57
                        return nil, cobra.ShellCompDirectiveDefault
×
58
                },
×
59
                PersistentPreRunE: func(cmd *cobra.Command, _ []string) error {
×
60
                        return validateOptions(cmd)
×
61
                },
×
62
                RunE: execute,
63
        }
64
)
65

66
// source provides a readable markdown source.
67
type source struct {
68
        reader io.ReadCloser
69
        URL    string
70
}
71

72
// sourceFromArg parses an argument and creates a readable source for it.
73
func sourceFromArg(arg string) (*source, error) {
×
74
        // from stdin
×
75
        if arg == "-" {
×
76
                return &source{reader: os.Stdin}, nil
×
77
        }
×
78

79
        // a GitHub or GitLab URL (even without the protocol):
80
        src, err := readmeURL(arg)
×
81
        if src != nil && err == nil {
×
82
                // if there's an error, try next methods...
×
83
                return src, nil
×
84
        }
×
85

86
        // HTTP(S) URLs:
NEW
87
        if u, err := url.ParseRequestURI(arg); err == nil && strings.Contains(arg, "://") { //nolint:nestif
×
88
                if u.Scheme != "" {
×
89
                        if u.Scheme != "http" && u.Scheme != "https" {
×
90
                                return nil, fmt.Errorf("%s is not a supported protocol", u.Scheme)
×
91
                        }
×
92
                        // consumer of the source is responsible for closing the ReadCloser.
NEW
93
                        resp, err := http.Get(u.String()) //nolint: noctx,bodyclose
×
94
                        if err != nil {
×
NEW
95
                                return nil, fmt.Errorf("glow: unable to get url: %w", err)
×
96
                        }
×
97
                        if resp.StatusCode != http.StatusOK {
×
98
                                return nil, fmt.Errorf("HTTP status %d", resp.StatusCode)
×
99
                        }
×
100
                        return &source{resp.Body, u.String()}, nil
×
101
                }
102
        }
103

104
        // a directory:
105
        if len(arg) == 0 {
×
106
                // use the current working dir if no argument was supplied
×
107
                arg = "."
×
108
        }
×
109
        st, err := os.Stat(arg)
×
NEW
110
        if err == nil && st.IsDir() { //nolint:nestif
×
111
                var src *source
×
112
                _ = filepath.Walk(arg, func(path string, _ os.FileInfo, err error) error {
×
113
                        if err != nil {
×
114
                                return err
×
115
                        }
×
116
                        for _, v := range readmeNames {
×
117
                                if strings.EqualFold(filepath.Base(path), v) {
×
118
                                        r, err := os.Open(path)
×
119
                                        if err != nil {
×
120
                                                continue
×
121
                                        }
122

123
                                        u, _ := filepath.Abs(path)
×
124
                                        src = &source{r, u}
×
125

×
126
                                        // abort filepath.Walk
×
127
                                        return errors.New("source found")
×
128
                                }
129
                        }
130
                        return nil
×
131
                })
132

133
                if src != nil {
×
134
                        return src, nil
×
135
                }
×
136

137
                return nil, errors.New("missing markdown source")
×
138
        }
139

UNCOV
140
        r, err := os.Open(arg)
×
NEW
141
        if err != nil {
×
NEW
142
                return nil, fmt.Errorf("glow: unable to open file: %w", err)
×
NEW
143
        }
×
NEW
144
        u, err := filepath.Abs(arg)
×
NEW
145
        if err != nil {
×
NEW
146
                return nil, fmt.Errorf("glow: unable to get absolute path: %w", err)
×
NEW
147
        }
×
NEW
148
        return &source{r, u}, nil
×
149
}
150

151
// validateStyle checks if the style is a default style, if not, checks that
152
// the custom style exists.
153
func validateStyle(style string) error {
×
154
        if style != "auto" && styles.DefaultStyles[style] == nil {
×
155
                style = utils.ExpandPath(style)
×
156
                if _, err := os.Stat(style); errors.Is(err, fs.ErrNotExist) {
×
NEW
157
                        return fmt.Errorf("specified style does not exist: %s", style)
×
158
                } else if err != nil {
×
NEW
159
                        return fmt.Errorf("glow: unable to stat file: %w", err)
×
160
                }
×
161
        }
162
        return nil
×
163
}
164

165
func validateOptions(cmd *cobra.Command) error {
×
166
        // grab config values from Viper
×
167
        width = viper.GetUint("width")
×
168
        mouse = viper.GetBool("mouse")
×
169
        pager = viper.GetBool("pager")
×
170
        tui = viper.GetBool("tui")
×
171
        showAllFiles = viper.GetBool("all")
×
172
        preserveNewLines = viper.GetBool("preserveNewLines")
×
173

×
174
        if pager && tui {
×
175
                return errors.New("glow: cannot use both pager and tui")
×
176
        }
×
177

178
        // validate the glamour style
179
        style = viper.GetString("style")
×
180
        if err := validateStyle(style); err != nil {
×
181
                return err
×
182
        }
×
183

184
        isTerminal := term.IsTerminal(int(os.Stdout.Fd()))
×
185
        // We want to use a special no-TTY style, when stdout is not a terminal
×
186
        // and there was no specific style passed by arg
×
187
        if !isTerminal && !cmd.Flags().Changed("style") {
×
188
                style = "notty"
×
189
        }
×
190

191
        // Detect terminal width
NEW
192
        if !cmd.Flags().Changed("width") { //nolint:nestif
×
193
                if isTerminal && width == 0 {
×
194
                        w, _, err := term.GetSize(int(os.Stdout.Fd()))
×
195
                        if err == nil {
×
NEW
196
                                width = uint(w) //nolint:gosec
×
197
                        }
×
198

199
                        if width > 120 {
×
200
                                width = 120
×
201
                        }
×
202
                }
203
                if width == 0 {
×
204
                        width = 80
×
205
                }
×
206
        }
207
        return nil
×
208
}
209

210
func stdinIsPipe() (bool, error) {
×
211
        stat, err := os.Stdin.Stat()
×
212
        if err != nil {
×
NEW
213
                return false, fmt.Errorf("glow: unable to open file: %w", err)
×
214
        }
×
215
        if stat.Mode()&os.ModeCharDevice == 0 || stat.Size() > 0 {
×
216
                return true, nil
×
217
        }
×
218
        return false, nil
×
219
}
220

221
func execute(cmd *cobra.Command, args []string) error {
×
222
        // if stdin is a pipe then use stdin for input. note that you can also
×
223
        // explicitly use a - to read from stdin.
×
224
        if yes, err := stdinIsPipe(); err != nil {
×
225
                return err
×
226
        } else if yes {
×
227
                src := &source{reader: os.Stdin}
×
228
                defer src.reader.Close() //nolint:errcheck
×
229
                return executeCLI(cmd, src, os.Stdout)
×
230
        }
×
231

232
        switch len(args) {
×
233
        // TUI running on cwd
234
        case 0:
×
235
                return runTUI("", "")
×
236

237
        // TUI with possible dir argument
238
        case 1:
×
239
                // Validate that the argument is a directory. If it's not treat it as
×
240
                // an argument to the non-TUI version of Glow (via fallthrough).
×
241
                info, err := os.Stat(args[0])
×
242
                if err == nil && info.IsDir() {
×
243
                        p, err := filepath.Abs(args[0])
×
244
                        if err == nil {
×
245
                                return runTUI(p, "")
×
246
                        }
×
247
                }
248
                fallthrough
×
249

250
        // CLI
251
        default:
×
252
                for _, arg := range args {
×
253
                        if err := executeArg(cmd, arg, os.Stdout); err != nil {
×
254
                                return err
×
255
                        }
×
256
                }
257
        }
258

259
        return nil
×
260
}
261

262
func executeArg(cmd *cobra.Command, arg string, w io.Writer) error {
×
263
        // create an io.Reader from the markdown source in cli-args
×
264
        src, err := sourceFromArg(arg)
×
265
        if err != nil {
×
266
                return err
×
267
        }
×
268
        defer src.reader.Close() //nolint:errcheck
×
269
        return executeCLI(cmd, src, w)
×
270
}
271

272
func executeCLI(cmd *cobra.Command, src *source, w io.Writer) error {
×
273
        b, err := io.ReadAll(src.reader)
×
274
        if err != nil {
×
NEW
275
                return fmt.Errorf("glow: unable to read from reader: %w", err)
×
276
        }
×
277

278
        b = utils.RemoveFrontmatter(b)
×
279

×
280
        // render
×
281
        var baseURL string
×
282
        u, err := url.ParseRequestURI(src.URL)
×
283
        if err == nil {
×
284
                u.Path = filepath.Dir(u.Path)
×
285
                baseURL = u.String() + "/"
×
286
        }
×
287

288
        isCode := !utils.IsMarkdownFile(src.URL)
×
289

×
290
        // initialize glamour
×
291
        r, err := glamour.NewTermRenderer(
×
292
                glamour.WithColorProfile(lipgloss.ColorProfile()),
×
293
                utils.GlamourStyle(style, isCode),
×
NEW
294
                glamour.WithWordWrap(int(width)), //nolint:gosec
×
295
                glamour.WithBaseURL(baseURL),
×
296
                glamour.WithPreservedNewLines(),
×
297
        )
×
298
        if err != nil {
×
NEW
299
                return fmt.Errorf("glow: unable to create renderer: %w", err)
×
300
        }
×
301

302
        content := string(b)
×
303
        ext := filepath.Ext(src.URL)
×
304
        if isCode {
×
305
                content = utils.WrapCodeBlock(string(b), ext)
×
306
        }
×
307

308
        out, err := r.Render(content)
×
309
        if err != nil {
×
NEW
310
                return fmt.Errorf("glow: unable to render markdown: %w", err)
×
311
        }
×
312

313
        // display
314
        switch {
×
315
        case pager || cmd.Flags().Changed("pager"):
×
316
                pagerCmd := os.Getenv("PAGER")
×
317
                if pagerCmd == "" {
×
318
                        pagerCmd = "less -r"
×
319
                }
×
320

321
                pa := strings.Split(pagerCmd, " ")
×
NEW
322
                c := exec.Command(pa[0], pa[1:]...) //nolint:gosec
×
323
                c.Stdin = strings.NewReader(out)
×
324
                c.Stdout = os.Stdout
×
NEW
325
                if err := c.Run(); err != nil {
×
NEW
326
                        return fmt.Errorf("glow: unable to run command: %w", err)
×
NEW
327
                }
×
NEW
328
                return nil
×
329
        case tui || cmd.Flags().Changed("tui"):
×
330
                return runTUI(src.URL, content)
×
331
        default:
×
NEW
332
                if _, err = fmt.Fprint(w, out); err != nil {
×
NEW
333
                        return fmt.Errorf("glow: unable to write to writer: %w", err)
×
NEW
334
                }
×
NEW
335
                return nil
×
336
        }
337
}
338

339
func runTUI(path string, content string) error {
×
340
        // Read environment to get debugging stuff
×
341
        cfg, err := env.ParseAs[ui.Config]()
×
342
        if err != nil {
×
343
                return fmt.Errorf("error parsing config: %v", err)
×
344
        }
×
345

346
        // use style set in env, or auto if unset
347
        if err := validateStyle(cfg.GlamourStyle); err != nil {
×
348
                cfg.GlamourStyle = style
×
349
        }
×
350

351
        cfg.Path = path
×
352
        cfg.ShowAllFiles = showAllFiles
×
353
        cfg.ShowLineNumbers = showLineNumbers
×
354
        cfg.GlamourMaxWidth = width
×
355
        cfg.EnableMouse = mouse
×
356
        cfg.PreserveNewLines = preserveNewLines
×
357

×
358
        // Run Bubble Tea program
×
359
        if _, err := ui.NewProgram(cfg, content).Run(); err != nil {
×
NEW
360
                return fmt.Errorf("glow: unable to run tui program: %w", err)
×
361
        }
×
362

363
        return nil
×
364
}
365

366
func main() {
×
367
        closer, err := setupLog()
×
368
        if err != nil {
×
369
                fmt.Println(err)
×
370
                os.Exit(1)
×
371
        }
×
372
        if err := rootCmd.Execute(); err != nil {
×
373
                _ = closer()
×
374
                os.Exit(1)
×
375
        }
×
376
        _ = closer()
×
377
}
378

379
func init() {
1✔
380
        tryLoadConfigFromDefaultPlaces()
1✔
381
        if len(CommitSHA) >= 7 {
1✔
382
                vt := rootCmd.VersionTemplate()
×
383
                rootCmd.SetVersionTemplate(vt[:len(vt)-1] + " (" + CommitSHA[0:7] + ")\n")
×
384
        }
×
385
        if Version == "" {
2✔
386
                Version = "unknown (built from source)"
1✔
387
        }
1✔
388
        rootCmd.Version = Version
1✔
389
        rootCmd.InitDefaultCompletionCmd()
1✔
390

1✔
391
        // "Glow Classic" cli arguments
1✔
392
        rootCmd.PersistentFlags().StringVar(&configFile, "config", "", fmt.Sprintf("config file (default %s)", viper.GetViper().ConfigFileUsed()))
1✔
393
        rootCmd.Flags().BoolVarP(&pager, "pager", "p", false, "display with pager")
1✔
394
        rootCmd.Flags().BoolVarP(&tui, "tui", "t", false, "display with tui")
1✔
395
        rootCmd.Flags().StringVarP(&style, "style", "s", styles.AutoStyle, "style name or JSON path")
1✔
396
        rootCmd.Flags().UintVarP(&width, "width", "w", 0, "word-wrap at width (set to 0 to disable)")
1✔
397
        rootCmd.Flags().BoolVarP(&showAllFiles, "all", "a", false, "show system files and directories (TUI-mode only)")
1✔
398
        rootCmd.Flags().BoolVarP(&showLineNumbers, "line-numbers", "l", false, "show line numbers (TUI-mode only)")
1✔
399
        rootCmd.Flags().BoolVarP(&preserveNewLines, "preserve-new-lines", "n", false, "preserve newlines in the output")
1✔
400
        rootCmd.Flags().BoolVarP(&mouse, "mouse", "m", false, "enable mouse wheel (TUI-mode only)")
1✔
401
        _ = rootCmd.Flags().MarkHidden("mouse")
1✔
402

1✔
403
        // Config bindings
1✔
404
        _ = viper.BindPFlag("pager", rootCmd.Flags().Lookup("pager"))
1✔
405
        _ = viper.BindPFlag("tui", rootCmd.Flags().Lookup("tui"))
1✔
406
        _ = viper.BindPFlag("style", rootCmd.Flags().Lookup("style"))
1✔
407
        _ = viper.BindPFlag("width", rootCmd.Flags().Lookup("width"))
1✔
408
        _ = viper.BindPFlag("debug", rootCmd.Flags().Lookup("debug"))
1✔
409
        _ = viper.BindPFlag("mouse", rootCmd.Flags().Lookup("mouse"))
1✔
410
        _ = viper.BindPFlag("preserveNewLines", rootCmd.Flags().Lookup("preserve-new-lines"))
1✔
411
        _ = viper.BindPFlag("showLineNumbers", rootCmd.Flags().Lookup("line-numbers"))
1✔
412
        _ = viper.BindPFlag("all", rootCmd.Flags().Lookup("all"))
1✔
413

1✔
414
        viper.SetDefault("style", styles.AutoStyle)
1✔
415
        viper.SetDefault("width", 0)
1✔
416
        viper.SetDefault("all", true)
1✔
417

1✔
418
        rootCmd.AddCommand(configCmd, manCmd)
1✔
419
}
420

421
func tryLoadConfigFromDefaultPlaces() {
1✔
422
        scope := gap.NewScope(gap.User, "glow")
1✔
423
        dirs, err := scope.ConfigDirs()
1✔
424
        if err != nil {
1✔
425
                fmt.Println("Could not load find configuration directory.")
×
426
                os.Exit(1)
×
427
        }
×
428

429
        if c := os.Getenv("XDG_CONFIG_HOME"); c != "" {
2✔
430
                dirs = append([]string{filepath.Join(c, "glow")}, dirs...)
1✔
431
        }
1✔
432

433
        if c := os.Getenv("GLOW_CONFIG_HOME"); c != "" {
1✔
434
                dirs = append([]string{c}, dirs...)
×
435
        }
×
436

437
        for _, v := range dirs {
5✔
438
                viper.AddConfigPath(v)
4✔
439
        }
4✔
440

441
        viper.SetConfigName("glow")
1✔
442
        viper.SetConfigType("yaml")
1✔
443
        viper.SetEnvPrefix("glow")
1✔
444
        viper.AutomaticEnv()
1✔
445

1✔
446
        if err := viper.ReadInConfig(); err != nil {
2✔
447
                if _, ok := err.(viper.ConfigFileNotFoundError); !ok {
1✔
448
                        log.Warn("Could not parse configuration file", "err", err)
×
449
                }
×
450
        }
451

452
        if used := viper.ConfigFileUsed(); used != "" {
1✔
453
                log.Debug("Using configuration file", "path", viper.ConfigFileUsed())
×
454
                return
×
455
        }
×
456

457
        if viper.ConfigFileUsed() == "" {
2✔
458
                configFile = filepath.Join(dirs[0], "glow.yml")
1✔
459
        }
1✔
460
        if err := ensureConfigFile(); err != nil {
1✔
461
                log.Error("Could not create default configuration", "error", err)
×
462
        }
×
463
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc