mirror of
https://github.com/charmbracelet/glow
synced 2025-01-05 16:48:46 +00:00
Rework mini stash help view builder + stub out larger stash help
This commit is contained in:
parent
cffa611dfb
commit
c4230ca3d9
1 changed files with 64 additions and 27 deletions
91
ui/stash.go
91
ui/stash.go
|
@ -24,7 +24,7 @@ const (
|
||||||
stashIndent = 1
|
stashIndent = 1
|
||||||
stashViewItemHeight = 3
|
stashViewItemHeight = 3
|
||||||
stashViewTopPadding = 5
|
stashViewTopPadding = 5
|
||||||
stashViewBottomPadding = 4
|
stashViewBottomPadding = 3
|
||||||
stashViewHorizontalPadding = 6
|
stashViewHorizontalPadding = 6
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -93,6 +93,7 @@ type stashModel struct {
|
||||||
viewState stashViewState
|
viewState stashViewState
|
||||||
filterState filterState
|
filterState filterState
|
||||||
selectionState selectionState
|
selectionState selectionState
|
||||||
|
showFullHelp bool
|
||||||
|
|
||||||
// The types of documents we are showing
|
// The types of documents we are showing
|
||||||
docState stashDocState
|
docState stashDocState
|
||||||
|
@ -188,7 +189,13 @@ func (m stashModel) shouldUpdateFilter() bool {
|
||||||
// Sets the total paginator pages according to the amount of markdowns for the
|
// Sets the total paginator pages according to the amount of markdowns for the
|
||||||
// current state.
|
// current state.
|
||||||
func (m *stashModel) setTotalPages() {
|
func (m *stashModel) setTotalPages() {
|
||||||
m.paginator.PerPage = max(1, (m.general.height-stashViewTopPadding-stashViewBottomPadding)/stashViewItemHeight)
|
_, helpHeight := m.helpView()
|
||||||
|
|
||||||
|
availableHeight := m.general.height -
|
||||||
|
stashViewTopPadding -
|
||||||
|
stashViewBottomPadding -
|
||||||
|
helpHeight
|
||||||
|
m.paginator.PerPage = max(1, availableHeight/stashViewItemHeight)
|
||||||
|
|
||||||
if pages := len(m.getVisibleMarkdowns()); pages < 1 {
|
if pages := len(m.getVisibleMarkdowns()); pages < 1 {
|
||||||
m.paginator.SetTotalPages(1)
|
m.paginator.SetTotalPages(1)
|
||||||
|
@ -719,6 +726,10 @@ func (m *stashModel) handleDocumentBrowsing(msg tea.Msg) tea.Cmd {
|
||||||
m.selectionState = selectionPromptingDelete
|
m.selectionState = selectionPromptingDelete
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Toggle full help
|
||||||
|
case "?":
|
||||||
|
m.showFullHelp = !m.showFullHelp
|
||||||
|
|
||||||
// Show errors
|
// Show errors
|
||||||
case "!":
|
case "!":
|
||||||
if m.err != nil && m.viewState == stashStateReady {
|
if m.err != nil && m.viewState == stashStateReady {
|
||||||
|
@ -923,9 +934,11 @@ func (m stashModel) view() string {
|
||||||
loadingIndicator = m.spinner.View()
|
loadingIndicator = m.spinner.View()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
help, helpHeight := m.helpView()
|
||||||
|
|
||||||
// We need to fill any empty height with newlines so the footer reaches
|
// We need to fill any empty height with newlines so the footer reaches
|
||||||
// the bottom.
|
// the bottom.
|
||||||
numBlankLines := max(0, (m.general.height-stashViewTopPadding-stashViewBottomPadding)%stashViewItemHeight)
|
numBlankLines := max(0, (m.general.height-stashViewTopPadding-stashViewBottomPadding-helpHeight)%stashViewItemHeight)
|
||||||
blankLines := ""
|
blankLines := ""
|
||||||
if numBlankLines > 0 {
|
if numBlankLines > 0 {
|
||||||
blankLines = strings.Repeat("\n", numBlankLines)
|
blankLines = strings.Repeat("\n", numBlankLines)
|
||||||
|
@ -982,7 +995,7 @@ func (m stashModel) view() string {
|
||||||
m.populatedView(),
|
m.populatedView(),
|
||||||
blankLines,
|
blankLines,
|
||||||
pagination,
|
pagination,
|
||||||
m.miniHelpView(),
|
help,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
return "\n" + indent(s, stashIndent)
|
return "\n" + indent(s, stashIndent)
|
||||||
|
@ -1089,6 +1102,17 @@ func (m stashModel) populatedView() string {
|
||||||
return b.String()
|
return b.String()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (m stashModel) helpView() (string, int) {
|
||||||
|
var s string
|
||||||
|
if m.showFullHelp {
|
||||||
|
s = m.fullHelpView()
|
||||||
|
} else {
|
||||||
|
s = m.miniHelpView()
|
||||||
|
}
|
||||||
|
|
||||||
|
return s, strings.Count(s, "\n")
|
||||||
|
}
|
||||||
|
|
||||||
func (m stashModel) miniHelpView() string {
|
func (m stashModel) miniHelpView() string {
|
||||||
var (
|
var (
|
||||||
h []string
|
h []string
|
||||||
|
@ -1104,48 +1128,55 @@ func (m stashModel) miniHelpView() string {
|
||||||
}
|
}
|
||||||
|
|
||||||
if m.selectionState == selectionSettingNote {
|
if m.selectionState == selectionSettingNote {
|
||||||
h = append(h, "enter: confirm", "esc: cancel")
|
h = append(h, "enter", "confirm", "esc", "cancel")
|
||||||
} else if m.selectionState == selectionPromptingDelete {
|
} else if m.selectionState == selectionPromptingDelete {
|
||||||
h = append(h, "y: delete", "n: cancel")
|
h = append(h, "y", "delete", "n", "cancel")
|
||||||
} else if m.filterState == filtering && numDocs == 1 {
|
} else if m.filterState == filtering && numDocs == 1 {
|
||||||
h = append(h, "enter: open", "esc: cancel")
|
h = append(h, "enter", "open", "esc", "cancel")
|
||||||
} else if m.filterState == filtering && numDocs == 0 {
|
} else if m.filterState == filtering && numDocs == 0 {
|
||||||
h = append(h, "enter/esc: cancel")
|
h = append(h, "enter/esc: cancel")
|
||||||
} else if m.filterState == filtering {
|
} else if m.filterState == filtering {
|
||||||
h = append(h, "enter: confirm", "esc: cancel", "ctrl+j/ctrl+k, ↑/↓: choose")
|
h = append(h, "enter", "confirm", "esc", "cancel", "ctrl+j/ctrl+k, ↑/↓", "choose")
|
||||||
|
h = append(h, "?", "help")
|
||||||
} else if m.docState == stashShowNewsDocs {
|
} else if m.docState == stashShowNewsDocs {
|
||||||
h = append(h, "enter: open", "esc: return", "j/k, ↑/↓: choose", "q: quit")
|
h = append(h, "enter", "open", "esc", "return", "j/k, ↑/↓", "choose", "q", "quit")
|
||||||
|
h = append(h, "?", "help")
|
||||||
} else {
|
} else {
|
||||||
if len(m.markdowns) > 0 {
|
if len(m.markdowns) > 0 {
|
||||||
h = append(h, "enter: open")
|
h = append(h, "enter", "open")
|
||||||
}
|
}
|
||||||
if m.filterState == filterApplied {
|
if m.filterState == filterApplied {
|
||||||
h = append(h, "esc: clear filter")
|
h = append(h, "esc", "clear filter")
|
||||||
}
|
}
|
||||||
if len(m.markdowns) > 1 {
|
if len(m.markdowns) > 1 {
|
||||||
h = append(h, "j/k, ↑/↓: choose")
|
h = append(h, "j/k, ↑/↓", "choose")
|
||||||
}
|
}
|
||||||
if m.paginator.TotalPages > 1 {
|
if m.paginator.TotalPages > 1 {
|
||||||
h = append(h, "h/l, ←/→: page")
|
h = append(h, "h/l, ←/→", "page")
|
||||||
}
|
}
|
||||||
if isStashed {
|
if isStashed {
|
||||||
h = append(h, "x: delete", "m: set memo")
|
h = append(h, "x: delete", "m: set memo")
|
||||||
} else if isLocal && m.general.authStatus == authOK {
|
} else if isLocal && m.general.authStatus == authOK {
|
||||||
h = append(h, "s: stash")
|
h = append(h, "s", "stash")
|
||||||
}
|
}
|
||||||
if m.err != nil {
|
if m.err != nil {
|
||||||
h = append(h, "!: errors")
|
h = append(h, "!", "errors")
|
||||||
}
|
}
|
||||||
h = append(h, "/: filter")
|
h = append(h, "/", "filter")
|
||||||
h = append(h, "q: quit")
|
h = append(h, "q", "quit")
|
||||||
|
h = append(h, "?", "help")
|
||||||
}
|
}
|
||||||
return stashMiniHelpViewBuilder(m.general.width, h...)
|
return stashMiniHelpViewBuilder(m.general.width, h...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// builds the help view from various sections pieces, truncating it if the view
|
// Builds the help view from various sections pieces, truncating it if the view
|
||||||
// would otherwise wrap to two lines.
|
// would otherwise wrap to two lines. Help view entires should come in as pairs,
|
||||||
func stashMiniHelpViewBuilder(windowWidth int, sections ...string) string {
|
// with the first being the key and the second being the help text.
|
||||||
if len(sections) == 0 {
|
func stashMiniHelpViewBuilder(windowWidth int, entries ...string) string {
|
||||||
|
if len(entries)%2 != 0 {
|
||||||
|
panic("mini help view entires must be set in pairs")
|
||||||
|
}
|
||||||
|
if len(entries) == 0 {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1157,17 +1188,19 @@ func stashMiniHelpViewBuilder(windowWidth int, sections ...string) string {
|
||||||
maxWidth = windowWidth - stashViewHorizontalPadding - truncationWidth
|
maxWidth = windowWidth - stashViewHorizontalPadding - truncationWidth
|
||||||
)
|
)
|
||||||
|
|
||||||
for i := 0; i < len(sections); i++ {
|
for i := 0; i < len(entries); i = i + 2 {
|
||||||
|
next = fmt.Sprintf("%s: %s", entries[i], entries[i+1])
|
||||||
|
|
||||||
// If we need this more often we'll formalize something rather than
|
// If we need this more often we'll formalize something rather than
|
||||||
// use an if clause/switch here.
|
// use an if clause/switch here.
|
||||||
switch sections[i] {
|
switch entries[i+1] {
|
||||||
case "s: stash":
|
case "stash":
|
||||||
next = greenFg(sections[i])
|
next = greenFg(next)
|
||||||
default:
|
default:
|
||||||
next = stashHelpItemStyle(sections[i])
|
next = stashHelpItemStyle(next)
|
||||||
}
|
}
|
||||||
|
|
||||||
if i < len(sections)-1 {
|
if i < len(entries)-1 {
|
||||||
next += dividerDot
|
next += dividerDot
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1183,6 +1216,10 @@ func stashMiniHelpViewBuilder(windowWidth int, sections ...string) string {
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (m stashModel) fullHelpView() string {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
// COMMANDS
|
// COMMANDS
|
||||||
|
|
||||||
func loadRemoteMarkdown(cc *charm.Client, id int, t DocType) tea.Cmd {
|
func loadRemoteMarkdown(cc *charm.Client, id int, t DocType) tea.Cmd {
|
||||||
|
|
Loading…
Reference in a new issue