From 7ea7cc59e393b6d1d1606ad33d33ae6ddfef1080 Mon Sep 17 00:00:00 2001 From: Christian Rocha Date: Fri, 23 Oct 2020 23:30:09 -0400 Subject: [PATCH] Respect width flag/config setting in the TUI; closes #168 --- main.go | 4 +++- ui/pager.go | 57 ++++++++++++++++++++++++++--------------------------- ui/ui.go | 22 +++++++++++---------- 3 files changed, 43 insertions(+), 40 deletions(-) diff --git a/main.go b/main.go index fc6dd8d..5c286df 100644 --- a/main.go +++ b/main.go @@ -271,6 +271,8 @@ func runTUI(stashedOnly bool) error { } cfg.ShowAllFiles = showAllFiles + cfg.GlamourMaxWidth = width + cfg.GlamourStyle = style if stashedOnly { cfg.DocumentTypes = ui.StashedDocuments | ui.NewsDocuments @@ -279,7 +281,7 @@ func runTUI(stashedOnly bool) error { } // Run Bubble Tea program - p := ui.NewProgram(style, cfg) + p := ui.NewProgram(cfg) p.EnterAltScreen() if err := p.Start(); err != nil { return err diff --git a/ui/pager.go b/ui/pager.go index 89d65b1..d050989 100644 --- a/ui/pager.go +++ b/ui/pager.go @@ -20,15 +20,14 @@ import ( ) const ( - maxDocumentWidth = 120 - statusBarHeight = 1 - gray = "#333333" - yellowGreen = "#ECFD65" - fuschia = "#EE6FF8" - mintGreen = "#89F0CB" - darkGreen = "#1C8760" - noteHeadingText = " Set Memo " - notePromptText = " > " + statusBarHeight = 1 + gray = "#333333" + yellowGreen = "#ECFD65" + fuschia = "#EE6FF8" + mintGreen = "#89F0CB" + darkGreen = "#1C8760" + noteHeadingText = " Set Memo " + notePromptText = " > " ) var ( @@ -81,16 +80,16 @@ const ( ) type pagerModel struct { - cc *charm.Client - authStatus authStatus - viewport viewport.Model - state pagerState - glamourStyle string - width int - height int - showHelp bool - textInput textinput.Model - spinner spinner.Model + cfg *Config + cc *charm.Client + authStatus authStatus + viewport viewport.Model + state pagerState + width int + height int + showHelp bool + textInput textinput.Model + spinner spinner.Model statusMessage string statusMessageTimer *time.Timer @@ -104,7 +103,7 @@ type pagerModel struct { stashedDocument *markdown } -func newPagerModel(as authStatus, glamourStyle string) pagerModel { +func newPagerModel(cfg *Config, as authStatus) pagerModel { // Init viewport vp := viewport.Model{} vp.YPosition = 0 @@ -129,12 +128,12 @@ func newPagerModel(as authStatus, glamourStyle string) pagerModel { sp.MinimumLifetime = time.Millisecond * 180 return pagerModel{ - state: pagerStateBrowse, - authStatus: as, - glamourStyle: glamourStyle, - textInput: ti, - viewport: vp, - spinner: sp, + cfg: cfg, + state: pagerStateBrowse, + authStatus: as, + textInput: ti, + viewport: vp, + spinner: sp, } } @@ -529,13 +528,13 @@ func glamourRender(m pagerModel, markdown string) (string, error) { // initialize glamour var gs glamour.TermRendererOption - if m.glamourStyle == "auto" { + if m.cfg.GlamourStyle == "auto" { gs = glamour.WithAutoStyle() } else { - gs = glamour.WithStylePath(m.glamourStyle) + gs = glamour.WithStylePath(m.cfg.GlamourStyle) } - width := max(0, min(maxDocumentWidth, m.viewport.Width)) + width := max(0, min(int(m.cfg.GlamourMaxWidth), m.viewport.Width)) r, err := glamour.NewTermRenderer( gs, glamour.WithWordWrap(width), diff --git a/ui/ui.go b/ui/ui.go index 384aa69..0e82980 100644 --- a/ui/ui.go +++ b/ui/ui.go @@ -32,9 +32,11 @@ var ( // Config contains TUI-specific configuration. type Config struct { - ShowAllFiles bool - Gopath string `env:"GOPATH"` - HomeDir string `env:"HOME"` + ShowAllFiles bool + Gopath string `env:"GOPATH"` + HomeDir string `env:"HOME"` + GlamourMaxWidth uint + GlamourStyle string // Which document types shall we show? We work though this with bitmasking. DocumentTypes DocumentType @@ -46,7 +48,7 @@ type Config struct { } // NewProgram returns a new Tea program. -func NewProgram(style string, cfg Config) *tea.Program { +func NewProgram(cfg Config) *tea.Program { if cfg.Logfile != "" { log.Println("-- Starting Glow ----------------") log.Printf("High performance pager: %v", cfg.HighPerformancePager) @@ -55,7 +57,7 @@ func NewProgram(style string, cfg Config) *tea.Program { debug = true } config = cfg - return tea.NewProgram(newModel(cfg, style)) + return tea.NewProgram(newModel(cfg)) } type errMsg struct{ err error } @@ -174,13 +176,13 @@ func (m *model) setAuthStatus(as authStatus) { m.pager.authStatus = as } -func newModel(cfg Config, style string) tea.Model { - if style == "auto" { +func newModel(cfg Config) tea.Model { + if cfg.GlamourStyle == "auto" { dbg := te.HasDarkBackground() if dbg { - style = "dark" + cfg.GlamourStyle = "dark" } else { - style = "light" + cfg.GlamourStyle = "light" } } @@ -194,7 +196,7 @@ func newModel(cfg Config, style string) tea.Model { state: stateShowStash, authStatus: as, keygenState: keygenUnstarted, - pager: newPagerModel(as, style), + pager: newPagerModel(&cfg, as), stash: newStashModel(&cfg, as), } }