Skip to content

Commit d89d79a

Browse files
authored
feat: --preserve-new-lines (#623)
closes #502
1 parent fe066f2 commit d89d79a

File tree

3 files changed

+28
-17
lines changed

3 files changed

+28
-17
lines changed

main.go

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,15 @@ var (
2828
// CommitSHA as provided by goreleaser.
2929
CommitSHA = ""
3030

31-
readmeNames = []string{"README.md", "README", "Readme.md", "Readme", "readme.md", "readme"}
32-
readmeBranches = []string{"main", "master"}
33-
configFile string
34-
pager bool
35-
style string
36-
width uint
37-
showAllFiles bool
38-
mouse bool
31+
readmeNames = []string{"README.md", "README", "Readme.md", "Readme", "readme.md", "readme"}
32+
readmeBranches = []string{"main", "master"}
33+
configFile string
34+
pager bool
35+
style string
36+
width uint
37+
showAllFiles bool
38+
preserveNewLines bool
39+
mouse bool
3940

4041
rootCmd = &cobra.Command{
4142
Use: "glow [SOURCE|DIR]",
@@ -151,6 +152,7 @@ func validateOptions(cmd *cobra.Command) error {
151152
width = viper.GetUint("width")
152153
mouse = viper.GetBool("mouse")
153154
pager = viper.GetBool("pager")
155+
preserveNewLines = viper.GetBool("preserveNewLines")
154156

155157
// validate the glamour style
156158
style = viper.GetString("style")
@@ -332,6 +334,7 @@ func runTUI(workingDirectory string) error {
332334
cfg.GlamourMaxWidth = width
333335
cfg.GlamourStyle = style
334336
cfg.EnableMouse = mouse
337+
cfg.PreserveNewLines = preserveNewLines
335338

336339
// Run Bubble Tea program
337340
if _, err := ui.NewProgram(cfg).Run(); err != nil {
@@ -372,7 +375,7 @@ func init() {
372375
rootCmd.Flags().StringVarP(&style, "style", "s", glamour.AutoStyle, "style name or JSON path")
373376
rootCmd.Flags().UintVarP(&width, "width", "w", 0, "word-wrap at width")
374377
rootCmd.Flags().BoolVarP(&showAllFiles, "all", "a", false, "show system files and directories (TUI-mode only)")
375-
378+
rootCmd.Flags().BoolVarP(&preserveNewLines, "preserve-new-lines", "n", false, "preserve newlines in the output")
376379
rootCmd.Flags().BoolVarP(&mouse, "mouse", "m", false, "enable mouse wheel (TUI-mode only)")
377380
_ = rootCmd.Flags().MarkHidden("mouse")
378381

@@ -381,6 +384,8 @@ func init() {
381384
_ = viper.BindPFlag("width", rootCmd.Flags().Lookup("width"))
382385
_ = viper.BindPFlag("debug", rootCmd.Flags().Lookup("debug"))
383386
_ = viper.BindPFlag("mouse", rootCmd.Flags().Lookup("mouse"))
387+
_ = viper.BindPFlag("preserveNewLines", rootCmd.Flags().Lookup("preserve-new-lines"))
388+
384389
viper.SetDefault("style", glamour.AutoStyle)
385390
viper.SetDefault("width", 0)
386391

ui/config.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@ package ui
22

33
// Config contains TUI-specific configuration.
44
type Config struct {
5-
ShowAllFiles bool
6-
Gopath string `env:"GOPATH"`
7-
HomeDir string `env:"HOME"`
8-
GlamourMaxWidth uint
9-
GlamourStyle string
10-
EnableMouse bool
5+
ShowAllFiles bool
6+
Gopath string `env:"GOPATH"`
7+
HomeDir string `env:"HOME"`
8+
GlamourMaxWidth uint
9+
GlamourStyle string
10+
EnableMouse bool
11+
PreserveNewLines bool
1112

1213
// Which directory should we start from?
1314
WorkingDirectory string

ui/pager.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -399,10 +399,15 @@ func glamourRender(m pagerModel, markdown string) (string, error) {
399399
width = 0
400400
}
401401

402-
r, err := glamour.NewTermRenderer(
402+
options := []glamour.TermRendererOption{
403403
utils.GlamourStyle(m.common.cfg.GlamourStyle, isCode),
404404
glamour.WithWordWrap(width),
405-
)
405+
}
406+
407+
if m.common.cfg.PreserveNewLines {
408+
options = append(options, glamour.WithPreservedNewLines())
409+
}
410+
r, err := glamour.NewTermRenderer(options...)
406411
if err != nil {
407412
return "", err
408413
}

0 commit comments

Comments
 (0)