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 (
keyword = NewStyle().
Foreground(AdaptiveColor{Light: "#04B575", Dark: "#04B575"}).
Foreground(Color("#04B575")).
Render
paragraph = NewStyle().

View File

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

View File

@ -34,9 +34,9 @@ var (
)
var (
dividerDot = darkGrayFg(" • ")
dividerBar = darkGrayFg(" │ ")
offlineHeaderNote = darkGrayFg("(Offline)")
dividerDot = darkGrayFg.SetString(" • ")
dividerBar = darkGrayFg.SetString(" │ ")
offlineHeaderNote = darkGrayFg.SetString("(Offline)")
logoStyle = lipgloss.NewStyle().
Foreground(lipgloss.Color("#ECFD65")).
@ -98,28 +98,7 @@ type section struct {
}
// map sections to their associated types.
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(),
},
}
var sections = map[sectionKey]section{}
// filterState is the current filtering state in the file listing.
type filterState int
@ -155,6 +134,31 @@ type statusMessage struct {
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
// given context.
func (s statusMessage) String() string {
@ -563,7 +567,7 @@ func newStashPaginator() paginator.Model {
p := paginator.New()
p.Type = paginator.Dots
p.ActiveDot = brightGrayFg("•")
p.InactiveDot = darkGrayFg("•")
p.InactiveDot = darkGrayFg.Render("•")
return p
}
@ -1255,13 +1259,13 @@ func (m stashModel) headerView() string {
sections[i] = grayFg(sections[i])
}
return strings.Join(sections, dividerDot)
return strings.Join(sections, dividerDot.String())
}
if m.loadingDone() && len(m.markdowns) == 0 {
var maybeOffline string
if m.common.authStatus == authFailed {
maybeOffline = " " + offlineHeaderNote
maybeOffline = " " + offlineHeaderNote.String()
}
if m.stashedOnly() {
@ -1302,9 +1306,9 @@ func (m stashModel) headerView() string {
sections = append(sections, s)
}
s := strings.Join(sections, dividerBar)
s := strings.Join(sections, dividerBar.String())
if m.common.authStatus == authFailed {
s += dividerDot + offlineHeaderNote
s += dividerDot.String() + offlineHeaderNote.String()
}
return s

View File

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

View File

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

View File

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