diff --git a/main.go b/main.go index ebca4af4..0b9dcffe 100644 --- a/main.go +++ b/main.go @@ -28,14 +28,15 @@ var ( // CommitSHA as provided by goreleaser. CommitSHA = "" - readmeNames = []string{"README.md", "README", "Readme.md", "Readme", "readme.md", "readme"} - readmeBranches = []string{"main", "master"} - configFile string - pager bool - style string - width uint - showAllFiles bool - mouse bool + readmeNames = []string{"README.md", "README", "Readme.md", "Readme", "readme.md", "readme"} + readmeBranches = []string{"main", "master"} + configFile string + pager bool + style string + width uint + showAllFiles bool + preserveNewLines bool + mouse bool rootCmd = &cobra.Command{ Use: "glow [SOURCE|DIR]", @@ -151,6 +152,7 @@ func validateOptions(cmd *cobra.Command) error { width = viper.GetUint("width") mouse = viper.GetBool("mouse") pager = viper.GetBool("pager") + preserveNewLines = viper.GetBool("preserveNewLines") // validate the glamour style style = viper.GetString("style") @@ -332,6 +334,7 @@ func runTUI(workingDirectory string) error { cfg.GlamourMaxWidth = width cfg.GlamourStyle = style cfg.EnableMouse = mouse + cfg.PreserveNewLines = preserveNewLines // Run Bubble Tea program if _, err := ui.NewProgram(cfg).Run(); err != nil { @@ -372,7 +375,7 @@ func init() { rootCmd.Flags().StringVarP(&style, "style", "s", glamour.AutoStyle, "style name or JSON path") rootCmd.Flags().UintVarP(&width, "width", "w", 0, "word-wrap at width") rootCmd.Flags().BoolVarP(&showAllFiles, "all", "a", false, "show system files and directories (TUI-mode only)") - + rootCmd.Flags().BoolVarP(&preserveNewLines, "preserve-new-lines", "n", false, "preserve newlines in the output") rootCmd.Flags().BoolVarP(&mouse, "mouse", "m", false, "enable mouse wheel (TUI-mode only)") _ = rootCmd.Flags().MarkHidden("mouse") @@ -381,6 +384,8 @@ func init() { _ = viper.BindPFlag("width", rootCmd.Flags().Lookup("width")) _ = viper.BindPFlag("debug", rootCmd.Flags().Lookup("debug")) _ = viper.BindPFlag("mouse", rootCmd.Flags().Lookup("mouse")) + _ = viper.BindPFlag("preserveNewLines", rootCmd.Flags().Lookup("preserve-new-lines")) + viper.SetDefault("style", glamour.AutoStyle) viper.SetDefault("width", 0) diff --git a/ui/config.go b/ui/config.go index 2365528c..44ddfece 100644 --- a/ui/config.go +++ b/ui/config.go @@ -2,12 +2,13 @@ package ui // Config contains TUI-specific configuration. type Config struct { - ShowAllFiles bool - Gopath string `env:"GOPATH"` - HomeDir string `env:"HOME"` - GlamourMaxWidth uint - GlamourStyle string - EnableMouse bool + ShowAllFiles bool + Gopath string `env:"GOPATH"` + HomeDir string `env:"HOME"` + GlamourMaxWidth uint + GlamourStyle string + EnableMouse bool + PreserveNewLines bool // Which directory should we start from? WorkingDirectory string diff --git a/ui/pager.go b/ui/pager.go index ade9b499..95b70c39 100644 --- a/ui/pager.go +++ b/ui/pager.go @@ -399,10 +399,15 @@ func glamourRender(m pagerModel, markdown string) (string, error) { width = 0 } - r, err := glamour.NewTermRenderer( + options := []glamour.TermRendererOption{ utils.GlamourStyle(m.common.cfg.GlamourStyle, isCode), glamour.WithWordWrap(width), - ) + } + + if m.common.cfg.PreserveNewLines { + options = append(options, glamour.WithPreservedNewLines()) + } + r, err := glamour.NewTermRenderer(options...) if err != nil { return "", err }