Archived
Offer "quit and update" in the footer, check for updates on every start
The update check ran at most once a day and only printed a line after the TUI exited. Now the registry is asked on every start (in the background, 3 s timeout, no cache) and the result reaches the TUI as a message. With a newer release, the footer shows `q quit and update to vX` right of help and quit, on a highlighted background (Theme.Highlight). `q` quits and `main` installs the release once the terminal is restored, sharing the code of `submarine update`; plain quit moves to `Q`, ctrl+c never updates. Drop LatestCached and CheckInterval. Add D27 and F58. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -19,13 +19,15 @@ const flashDuration = 2 * time.Second
|
||||
type clearFlashMsg struct{ id int }
|
||||
|
||||
// Keys provides the key groups. The short bar shows Navigation (and
|
||||
// Submodule, if requested) on the left and Global on the right; full help
|
||||
// Submodule, if requested) on the left, Global and then Highlight (on a
|
||||
// highlighted background) on the right; full help
|
||||
// shows FullHelp.
|
||||
type Keys interface {
|
||||
help.KeyMap
|
||||
Navigation() []key.Binding
|
||||
Submodule() []key.Binding
|
||||
Global() []key.Binding
|
||||
Highlight() []key.Binding
|
||||
}
|
||||
|
||||
type Model struct {
|
||||
@@ -42,6 +44,9 @@ func New(t theme.Theme, keys Keys) Model {
|
||||
return Model{theme: t, help: theme.NewHelp(t), keys: keys}
|
||||
}
|
||||
|
||||
// SetKeys replaces the key groups, e.g. if bindings changed.
|
||||
func (m *Model) SetKeys(k Keys) { m.keys = k }
|
||||
|
||||
// SetActions shows the submodule bindings in the short bar. Set it if the
|
||||
// detail pane doesn't show them.
|
||||
func (m *Model) SetActions(on bool) { m.actions = on }
|
||||
@@ -85,6 +90,13 @@ func (m Model) shortBar(w int) string {
|
||||
h := m.help
|
||||
h.Width = 0
|
||||
right := h.ShortHelpView(m.keys.Global())
|
||||
if hl := m.keys.Highlight(); len(hl) > 0 {
|
||||
var parts []string
|
||||
for _, b := range hl {
|
||||
parts = append(parts, b.Help().Key+" "+b.Help().Desc)
|
||||
}
|
||||
right += " " + m.theme.Highlight().Render(" "+strings.Join(parts, " ")+" ")
|
||||
}
|
||||
left := m.keys.Navigation()
|
||||
if m.actions {
|
||||
left = append(left[:len(left):len(left)], m.keys.Submodule()...)
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
package footer
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/charmbracelet/bubbles/key"
|
||||
"github.com/charmbracelet/lipgloss"
|
||||
"github.com/charmbracelet/x/ansi"
|
||||
|
||||
"submarine/internal/tui/theme"
|
||||
)
|
||||
|
||||
type testKeys struct{ highlight []key.Binding }
|
||||
|
||||
func bind(k, desc string) key.Binding { return key.NewBinding(key.WithKeys(k), key.WithHelp(k, desc)) }
|
||||
|
||||
func (testKeys) ShortHelp() []key.Binding { return nil }
|
||||
func (testKeys) FullHelp() [][]key.Binding { return nil }
|
||||
func (testKeys) Navigation() []key.Binding {
|
||||
return []key.Binding{bind("j", "move"), bind("h", "fold")}
|
||||
}
|
||||
func (testKeys) Submodule() []key.Binding { return nil }
|
||||
func (testKeys) Global() []key.Binding { return []key.Binding{bind("?", "help"), bind("Q", "quit")} }
|
||||
func (k testKeys) Highlight() []key.Binding { return k.highlight }
|
||||
|
||||
func bar(w int, hl ...key.Binding) string {
|
||||
m := New(theme.Default(), testKeys{hl})
|
||||
m.SetWidth(w)
|
||||
return ansi.Strip(m.View())
|
||||
}
|
||||
|
||||
func TestUpdateIsRightmost(t *testing.T) {
|
||||
got := strings.TrimRight(bar(80, bind("q", "quit and update to v1.2.3")), " ")
|
||||
if !strings.HasSuffix(got, "? help • Q quit q quit and update to v1.2.3") {
|
||||
t.Errorf("order must be help, quit, quit and update:\n%q", got)
|
||||
}
|
||||
if w := lipgloss.Width(bar(80, bind("q", "quit and update to v1.2.3"))); w != 80 {
|
||||
t.Errorf("bar is %d wide, want 80", w)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNoUpdate(t *testing.T) {
|
||||
if got := bar(80); strings.Contains(got, "update") {
|
||||
t.Errorf("unexpected update hint:\n%q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNavigationGivesWayToUpdate(t *testing.T) {
|
||||
got := bar(50, bind("q", "quit and update to v1.2.3"))
|
||||
if !strings.Contains(got, "q quit and update to v1.2.3") || !strings.Contains(got, "Q quit") {
|
||||
t.Errorf("the right group must be kept:\n%q", got)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user