Skip to content

Commit

Permalink
feat: --preserve-new-lines (#623)
Browse files Browse the repository at this point in the history
closes #502
  • Loading branch information
caarlos0 authored Jul 9, 2024
1 parent fe066f2 commit d89d79a
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 17 deletions.
23 changes: 14 additions & 9 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]",
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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")

Expand All @@ -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)

Expand Down
13 changes: 7 additions & 6 deletions ui/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 7 additions & 2 deletions ui/pager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit d89d79a

Please sign in to comment.