feat: --preserve-new-lines (#623)

closes #502
This commit is contained in:
Carlos Alexandro Becker 2024-07-09 09:50:10 -03:00 committed by GitHub
parent fe066f2140
commit d89d79a00c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 28 additions and 17 deletions

23
main.go
View file

@ -28,14 +28,15 @@ var (
// CommitSHA as provided by goreleaser. // CommitSHA as provided by goreleaser.
CommitSHA = "" CommitSHA = ""
readmeNames = []string{"README.md", "README", "Readme.md", "Readme", "readme.md", "readme"} readmeNames = []string{"README.md", "README", "Readme.md", "Readme", "readme.md", "readme"}
readmeBranches = []string{"main", "master"} readmeBranches = []string{"main", "master"}
configFile string configFile string
pager bool pager bool
style string style string
width uint width uint
showAllFiles bool showAllFiles bool
mouse bool preserveNewLines bool
mouse bool
rootCmd = &cobra.Command{ rootCmd = &cobra.Command{
Use: "glow [SOURCE|DIR]", Use: "glow [SOURCE|DIR]",
@ -151,6 +152,7 @@ func validateOptions(cmd *cobra.Command) error {
width = viper.GetUint("width") width = viper.GetUint("width")
mouse = viper.GetBool("mouse") mouse = viper.GetBool("mouse")
pager = viper.GetBool("pager") pager = viper.GetBool("pager")
preserveNewLines = viper.GetBool("preserveNewLines")
// validate the glamour style // validate the glamour style
style = viper.GetString("style") style = viper.GetString("style")
@ -332,6 +334,7 @@ func runTUI(workingDirectory string) error {
cfg.GlamourMaxWidth = width cfg.GlamourMaxWidth = width
cfg.GlamourStyle = style cfg.GlamourStyle = style
cfg.EnableMouse = mouse cfg.EnableMouse = mouse
cfg.PreserveNewLines = preserveNewLines
// Run Bubble Tea program // Run Bubble Tea program
if _, err := ui.NewProgram(cfg).Run(); err != nil { 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().StringVarP(&style, "style", "s", glamour.AutoStyle, "style name or JSON path")
rootCmd.Flags().UintVarP(&width, "width", "w", 0, "word-wrap at width") 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(&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().BoolVarP(&mouse, "mouse", "m", false, "enable mouse wheel (TUI-mode only)")
_ = rootCmd.Flags().MarkHidden("mouse") _ = rootCmd.Flags().MarkHidden("mouse")
@ -381,6 +384,8 @@ func init() {
_ = viper.BindPFlag("width", rootCmd.Flags().Lookup("width")) _ = viper.BindPFlag("width", rootCmd.Flags().Lookup("width"))
_ = viper.BindPFlag("debug", rootCmd.Flags().Lookup("debug")) _ = viper.BindPFlag("debug", rootCmd.Flags().Lookup("debug"))
_ = viper.BindPFlag("mouse", rootCmd.Flags().Lookup("mouse")) _ = viper.BindPFlag("mouse", rootCmd.Flags().Lookup("mouse"))
_ = viper.BindPFlag("preserveNewLines", rootCmd.Flags().Lookup("preserve-new-lines"))
viper.SetDefault("style", glamour.AutoStyle) viper.SetDefault("style", glamour.AutoStyle)
viper.SetDefault("width", 0) viper.SetDefault("width", 0)

View file

@ -2,12 +2,13 @@ package ui
// Config contains TUI-specific configuration. // Config contains TUI-specific configuration.
type Config struct { type Config struct {
ShowAllFiles bool ShowAllFiles bool
Gopath string `env:"GOPATH"` Gopath string `env:"GOPATH"`
HomeDir string `env:"HOME"` HomeDir string `env:"HOME"`
GlamourMaxWidth uint GlamourMaxWidth uint
GlamourStyle string GlamourStyle string
EnableMouse bool EnableMouse bool
PreserveNewLines bool
// Which directory should we start from? // Which directory should we start from?
WorkingDirectory string WorkingDirectory string

View file

@ -399,10 +399,15 @@ func glamourRender(m pagerModel, markdown string) (string, error) {
width = 0 width = 0
} }
r, err := glamour.NewTermRenderer( options := []glamour.TermRendererOption{
utils.GlamourStyle(m.common.cfg.GlamourStyle, isCode), utils.GlamourStyle(m.common.cfg.GlamourStyle, isCode),
glamour.WithWordWrap(width), glamour.WithWordWrap(width),
) }
if m.common.cfg.PreserveNewLines {
options = append(options, glamour.WithPreservedNewLines())
}
r, err := glamour.NewTermRenderer(options...)
if err != nil { if err != nil {
return "", err return "", err
} }