Files
MujicaUI/code/command_history.go
T
ZacharyZhang-NYandClaude Opus 5.5 bcce34cda1 Upgrade MyGo to v0.5.5
MyGo v0.5.5 returns ui.Element as a checked value handle instead of a
pointer. Migrated with `mygo migrate-ui`, then fixed what the codemod
leaves to review:

- Element nil checks become .Valid(); absent elements are ui.Element{}.
- Input handlers and paint callbacks run outside the build pass, where
  elements expire and Context methods are inert: read IsDisabled into a
  local during the build, keep a ui.Handle for Focus, use c.Services()
  for clipboard access, create ui.Local state during the build.
- Add core.AutoFocus: v0.5.5 records an overlay's opener after its
  content builds, so same-frame autofocus lost focus restore on close.
  It focuses one frame later; cascader rows defer their focus likewise.
- core.OverlayLayer identifies build passes by the root element, not
  c.Now(), which repeats across passes and under the Tester clock.
- Context menus survive the opening press counted as PressedOutside;
  split button tooltip no longer reorders its open menu; disabled
  menubar rows pass presses to the panel; Form defers blur-started
  async checks so the growing message no longer moves Submit away
  from the release; Combobox drops its disabled copy now that MyGo
  blocks typing under a disabled wrapper (canary flipped to a guard).
- The Tester clock is virtual: tests use Advance/Now instead of
  time.Sleep/time.Now and read element state inside the view. The web
  mirror advances the clock to wall time before each frame and input.
- AGENTS.md and docs describe the value Element API and the new pin.

Snapshot goldens are darwin-only and were not re-verified here.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-17 01:37:54 -04:00

122 lines
3.4 KiB
Go

package code
import (
"fmt"
"log/slog"
"strings"
"time"
"github.com/egoist/mygo/ui"
"git.zacharyzhang.com/ZacharyZhang-NY/MujicaUI/core"
"git.zacharyzhang.com/ZacharyZhang-NY/MujicaUI/icons"
"git.zacharyzhang.com/ZacharyZhang-NY/MujicaUI/input"
)
// HistoryEntry is one command a shell ran.
type HistoryEntry struct {
Command string
Time time.Time
ExitCode int
}
// CommandHistoryState is the filter and place of a CommandHistory.
type CommandHistoryState struct {
Query string
rows ui.ListState
selected int
shown []int
query string
n int
ran bool
}
// CommandHistoryOptions configure a CommandHistory.
type CommandHistoryOptions struct {
Disabled bool
}
// CommandHistoryResult carries the entry to replay.
type CommandHistoryResult struct {
Element ui.Element
// Replayed is the entry to run again, or nil.
Replayed *HistoryEntry
}
// CommandHistory lists commands newest first with search and replay.
func CommandHistory(c *ui.Context, entries []HistoryEntry, s *CommandHistoryState, opts CommandHistoryOptions) CommandHistoryResult {
if s == nil {
panic("mujica: CommandHistory needs a state")
}
var res CommandHistoryResult
res.Element = CodePanel(c).Disabled(opts.Disabled)
disabled := res.Element.IsDisabled()
replay := func(i int) {
if disabled {
return
}
e := entries[i]
res.Replayed = &e
slog.Debug("mujica: command_history replay", "index", i)
}
res.Element.Children(func() {
CodeHeader(c, func() {
input.SearchInput(c, &s.Query, input.SearchInputOptions{Disabled: opts.Disabled, Placeholder: core.Msg(c, "command_history.search")}).Element.Grow(1)
})
commandHistoryFilter(entries, s)
if len(s.shown) == 0 {
text := core.Msg(c, "command_history.empty")
if len(entries) > 0 {
text = core.Msg(c, "command_history.none")
}
CodeCaption(c, text).Padding(core.Density(c).Unit() * 4)
return
}
s.rows.Selected = &s.selected
s.rows.Key = func(i int) any { return s.shown[i] }
s.rows.Label = func(i int) string { return entries[s.shown[i]].Command }
list := CodeRows(c, &s.rows, len(s.shown), func(i int) {
e := entries[s.shown[i]]
commandHistoryRow(c, e)
if core.IconAction(c, icons.Must("refresh-cw"), fmt.Sprintf(core.Msg(c, "command_history.replay"), e.Command)).Clicked() {
replay(s.shown[i])
}
})
if list.Submitted() && s.selected >= 0 && s.selected < len(s.shown) {
replay(s.shown[s.selected])
}
})
return res
}
// commandHistoryFilter refilters when entries or query change.
func commandHistoryFilter(entries []HistoryEntry, s *CommandHistoryState) {
if s.ran && s.n == len(entries) && s.query == s.Query {
return
}
s.ran, s.n, s.query = true, len(entries), s.Query
s.shown = s.shown[:0]
q := strings.ToLower(strings.TrimSpace(s.Query))
for i := len(entries) - 1; i >= 0; i-- {
if q == "" || strings.Contains(strings.ToLower(entries[i].Command), q) {
s.shown = append(s.shown, i)
}
}
s.selected = min(s.selected, len(s.shown)-1)
}
func commandHistoryRow(c *ui.Context, e HistoryEntry) {
k := core.Tokens(c)
ui.Column(c).Grow(1).MinWidth(0).Children(func() {
CodeMono(c, e.Command)
ui.Row(c).Gap(core.Density(c).Unit() * 2).Children(func() {
if !e.Time.IsZero() {
CodeCaption(c, e.Time.Format("15:04:05"))
}
if e.ExitCode != 0 {
CodeCaption(c, fmt.Sprintf(core.Msg(c, "command_history.exit"), e.ExitCode)).TextColor(k.Danger)
}
})
})
}