feat: show line numbers configuration option

closes #311
This commit is contained in:
Carlos Alexandro Becker 2024-07-11 12:08:57 -03:00
parent 247c707b25
commit 2d5bd3b799
No known key found for this signature in database
3 changed files with 7 additions and 1 deletions

View file

@ -36,6 +36,7 @@ var (
style string style string
width uint width uint
showAllFiles bool showAllFiles bool
showLineNumbers bool
preserveNewLines bool preserveNewLines bool
mouse bool mouse bool
@ -324,6 +325,7 @@ func runTUI(workingDirectory string) error {
cfg.WorkingDirectory = workingDirectory cfg.WorkingDirectory = workingDirectory
cfg.ShowAllFiles = showAllFiles cfg.ShowAllFiles = showAllFiles
cfg.ShowLineNumbers = showLineNumbers
cfg.GlamourMaxWidth = width cfg.GlamourMaxWidth = width
cfg.GlamourStyle = style cfg.GlamourStyle = style
cfg.EnableMouse = mouse cfg.EnableMouse = mouse
@ -368,6 +370,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(&showLineNumbers, "line-numbers", "l", false, "show line numbers (TUI-mode only)")
rootCmd.Flags().BoolVarP(&preserveNewLines, "preserve-new-lines", "n", false, "preserve newlines in the output") 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")
@ -378,6 +381,8 @@ func init() {
_ = 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.BindPFlag("preserveNewLines", rootCmd.Flags().Lookup("preserve-new-lines"))
_ = viper.BindPFlag("showLineNumbers", rootCmd.Flags().Lookup("line-numbers"))
_ = viper.BindPFlag("showAllFiles", rootCmd.Flags().Lookup("all"))
viper.SetDefault("style", glamour.AutoStyle) viper.SetDefault("style", glamour.AutoStyle)
viper.SetDefault("width", 0) viper.SetDefault("width", 0)

View file

@ -3,6 +3,7 @@ package ui
// Config contains TUI-specific configuration. // Config contains TUI-specific configuration.
type Config struct { type Config struct {
ShowAllFiles bool ShowAllFiles bool
ShowLineNumbers bool
Gopath string `env:"GOPATH"` Gopath string `env:"GOPATH"`
HomeDir string `env:"HOME"` HomeDir string `env:"HOME"`
GlamourMaxWidth uint GlamourMaxWidth uint

View file

@ -436,7 +436,7 @@ func glamourRender(m pagerModel, markdown string) (string, error) {
var content strings.Builder var content strings.Builder
for i, s := range lines { for i, s := range lines {
if isCode { if isCode || m.common.cfg.ShowLineNumbers {
content.WriteString(lineNumberStyle(fmt.Sprintf("%"+fmt.Sprint(lineNumberWidth)+"d", i+1))) content.WriteString(lineNumberStyle(fmt.Sprintf("%"+fmt.Sprint(lineNumberWidth)+"d", i+1)))
content.WriteString(trunc(s)) content.WriteString(trunc(s))
} else { } else {