fix: lazily init UI

This commit is contained in:
Christian Muehlhaeuser 2023-05-05 11:40:36 +02:00
parent 4a3d98697b
commit 8c80ea5f67
6 changed files with 41 additions and 35 deletions

View file

@ -4,7 +4,7 @@ import . "github.com/charmbracelet/lipgloss" //nolint:revive
var ( var (
keyword = NewStyle(). keyword = NewStyle().
Foreground(AdaptiveColor{Light: "#04B575", Dark: "#04B575"}). Foreground(Color("#04B575")).
Render Render
paragraph = NewStyle(). paragraph = NewStyle().

View file

@ -32,7 +32,7 @@ var (
Foreground(cream). Foreground(cream).
Background(green). Background(green).
Padding(0, 1). Padding(0, 1).
Render("Set Memo") SetString("Set Memo")
statusBarNoteFg = lipgloss.AdaptiveColor{Light: "#656565", Dark: "#7D7D7D"} statusBarNoteFg = lipgloss.AdaptiveColor{Light: "#656565", Dark: "#7D7D7D"}
statusBarBg = lipgloss.AdaptiveColor{Light: "#E6E6E6", Dark: "#242424"} statusBarBg = lipgloss.AdaptiveColor{Light: "#E6E6E6", Dark: "#242424"}
@ -168,7 +168,7 @@ func (m *pagerModel) setSize(w, h int) {
m.viewport.Width = w m.viewport.Width = w
m.viewport.Height = h - statusBarHeight m.viewport.Height = h - statusBarHeight
m.textInput.Width = w - m.textInput.Width = w -
ansi.PrintableRuneWidth(noteHeading) - ansi.PrintableRuneWidth(noteHeading.String()) -
ansi.PrintableRuneWidth(m.textInput.Prompt) - 1 ansi.PrintableRuneWidth(m.textInput.Prompt) - 1
if m.showHelp { if m.showHelp {

View file

@ -34,9 +34,9 @@ var (
) )
var ( var (
dividerDot = darkGrayFg(" • ") dividerDot = darkGrayFg.SetString(" • ")
dividerBar = darkGrayFg(" │ ") dividerBar = darkGrayFg.SetString(" │ ")
offlineHeaderNote = darkGrayFg("(Offline)") offlineHeaderNote = darkGrayFg.SetString("(Offline)")
logoStyle = lipgloss.NewStyle(). logoStyle = lipgloss.NewStyle().
Foreground(lipgloss.Color("#ECFD65")). Foreground(lipgloss.Color("#ECFD65")).
@ -98,28 +98,7 @@ type section struct {
} }
// map sections to their associated types. // map sections to their associated types.
var sections = map[sectionKey]section{ var sections = map[sectionKey]section{}
localSection: {
key: localSection,
docTypes: NewDocTypeSet(LocalDoc),
paginator: newStashPaginator(),
},
stashedSection: {
key: stashedSection,
docTypes: NewDocTypeSet(StashedDoc, ConvertedDoc),
paginator: newStashPaginator(),
},
newsSection: {
key: newsSection,
docTypes: NewDocTypeSet(NewsDoc),
paginator: newStashPaginator(),
},
filterSection: {
key: filterSection,
docTypes: DocTypeSet{},
paginator: newStashPaginator(),
},
}
// filterState is the current filtering state in the file listing. // filterState is the current filtering state in the file listing.
type filterState int type filterState int
@ -155,6 +134,31 @@ type statusMessage struct {
message string message string
} }
func initSections() {
sections = map[sectionKey]section{
localSection: {
key: localSection,
docTypes: NewDocTypeSet(LocalDoc),
paginator: newStashPaginator(),
},
stashedSection: {
key: stashedSection,
docTypes: NewDocTypeSet(StashedDoc, ConvertedDoc),
paginator: newStashPaginator(),
},
newsSection: {
key: newsSection,
docTypes: NewDocTypeSet(NewsDoc),
paginator: newStashPaginator(),
},
filterSection: {
key: filterSection,
docTypes: DocTypeSet{},
paginator: newStashPaginator(),
},
}
}
// String returns a styled version of the status message appropriate for the // String returns a styled version of the status message appropriate for the
// given context. // given context.
func (s statusMessage) String() string { func (s statusMessage) String() string {
@ -563,7 +567,7 @@ func newStashPaginator() paginator.Model {
p := paginator.New() p := paginator.New()
p.Type = paginator.Dots p.Type = paginator.Dots
p.ActiveDot = brightGrayFg("•") p.ActiveDot = brightGrayFg("•")
p.InactiveDot = darkGrayFg("•") p.InactiveDot = darkGrayFg.Render("•")
return p return p
} }
@ -1255,13 +1259,13 @@ func (m stashModel) headerView() string {
sections[i] = grayFg(sections[i]) sections[i] = grayFg(sections[i])
} }
return strings.Join(sections, dividerDot) return strings.Join(sections, dividerDot.String())
} }
if m.loadingDone() && len(m.markdowns) == 0 { if m.loadingDone() && len(m.markdowns) == 0 {
var maybeOffline string var maybeOffline string
if m.common.authStatus == authFailed { if m.common.authStatus == authFailed {
maybeOffline = " " + offlineHeaderNote maybeOffline = " " + offlineHeaderNote.String()
} }
if m.stashedOnly() { if m.stashedOnly() {
@ -1302,9 +1306,9 @@ func (m stashModel) headerView() string {
sections = append(sections, s) sections = append(sections, s)
} }
s := strings.Join(sections, dividerBar) s := strings.Join(sections, dividerBar.String())
if m.common.authStatus == authFailed { if m.common.authStatus == authFailed {
s += dividerDot + offlineHeaderNote s += dividerDot.String() + offlineHeaderNote.String()
} }
return s return s

View file

@ -231,7 +231,7 @@ func (m stashModel) miniHelpView(entries ...string) string {
next = fmt.Sprintf("%s %s", k, v) next = fmt.Sprintf("%s %s", k, v)
if i < len(entries)-2 { if i < len(entries)-2 {
next += dividerDot next += dividerDot.String()
} }
// Only this (and the following) help text items if we have the // Only this (and the following) help text items if we have the

View file

@ -41,7 +41,7 @@ var (
grayFg = NewStyle().Foreground(gray).Render grayFg = NewStyle().Foreground(gray).Render
midGrayFg = NewStyle().Foreground(midGray).Render midGrayFg = NewStyle().Foreground(midGray).Render
darkGrayFg = NewStyle().Foreground(darkGray).Render darkGrayFg = NewStyle().Foreground(darkGray)
greenFg = NewStyle().Foreground(green).Render greenFg = NewStyle().Foreground(green).Render
semiDimGreenFg = NewStyle().Foreground(semiDimGreen).Render semiDimGreenFg = NewStyle().Foreground(semiDimGreen).Render

View file

@ -199,6 +199,8 @@ func (m *model) unloadDocument() []tea.Cmd {
} }
func newModel(cfg Config) tea.Model { func newModel(cfg Config) tea.Model {
initSections()
if cfg.GlamourStyle == "auto" { if cfg.GlamourStyle == "auto" {
if te.HasDarkBackground() { if te.HasDarkBackground() {
cfg.GlamourStyle = "dark" cfg.GlamourStyle = "dark"