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 = ""
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)

View file

@ -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

View file

@ -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
}