Skip to content

Commit

Permalink
fix: lazily init UI
Browse files Browse the repository at this point in the history
  • Loading branch information
muesli committed May 9, 2023
1 parent 4a3d986 commit 8c80ea5
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 35 deletions.
2 changes: 1 addition & 1 deletion style.go
Original file line number Diff line number Diff line change
Expand Up @@ -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().
Expand Down
4 changes: 2 additions & 2 deletions ui/pager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"}
Expand Down Expand Up @@ -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 {
Expand Down
64 changes: 34 additions & 30 deletions ui/stash.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")).
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion ui/stashhelp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion ui/styles.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions ui/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 8c80ea5

Please sign in to comment.