Respect width flag/config setting in the TUI; closes #168

This commit is contained in:
Christian Rocha 2020-10-23 23:30:09 -04:00
parent 2091f03235
commit 7ea7cc59e3
No known key found for this signature in database
GPG key ID: D6CC7A16E5878018
3 changed files with 43 additions and 40 deletions

View file

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

View file

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

View file

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