Files
MujicaUI/git/command_block.go
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

150 lines
5.1 KiB
Go

package git
import (
"fmt"
"log/slog"
"time"
"github.com/egoist/mygo/ui"
"git.zacharyzhang.com/ZacharyZhang-NY/MujicaUI/agent"
"git.zacharyzhang.com/ZacharyZhang-NY/MujicaUI/core"
"git.zacharyzhang.com/ZacharyZhang-NY/MujicaUI/icons"
"git.zacharyzhang.com/ZacharyZhang-NY/MujicaUI/internal"
"git.zacharyzhang.com/ZacharyZhang-NY/MujicaUI/theme"
)
// CommandBlockOptions configure a CommandBlock.
type CommandBlockOptions struct {
// Prompt precedes the command; empty means "$".
Prompt string
// MaxOutputHeight scrolls longer output; zero means 320 DIPs.
MaxOutputHeight float32
}
// CommandBlockResult is a built CommandBlock and its events.
type CommandBlockResult struct {
Element ui.Element
toggled bool
copied string
}
// Toggled reports the output was shown or hidden this frame.
func (r CommandBlockResult) Toggled() bool { return r.toggled }
// Copied returns "command" or "output" when copied.
func (r CommandBlockResult) Copied() (string, bool) { return r.copied, r.copied != "" }
// CommandBlock shows a command run; *open shows its output.
func CommandBlock(c *ui.Context, run agent.CommandRun, open *bool, opts CommandBlockOptions) CommandBlockResult {
if run.Command == "" {
panic("mujica: CommandBlock needs a Command")
}
if run.Duration < 0 || opts.MaxOutputHeight < 0 {
panic("mujica: CommandBlock Duration and MaxOutputHeight must not be negative")
}
prompt := opts.Prompt
if prompt == "" {
prompt = "$"
}
maxH := opts.MaxOutputHeight
if maxH == 0 {
maxH = 320
}
k := core.Tokens(c)
d := core.Density(c)
u := d.Unit()
box := ui.Column(c).AlignItems(ui.Stretch).Background(k.Surface).Border(theme.BorderWidth, k.Border).Radius(theme.CardRadius).Clip()
res := CommandBlockResult{Element: box}
box.Children(func() {
ui.Row(c).AlignItems(ui.Center).Gap(u).Padding(0, u*2, 0, 0).MinHeight(d.ControlHeight()).Children(func() {
was := *open
tr := ui.CollapsibleBase(c, open).Trigger
if box.IsDisabled() {
*open = was
}
if *open != was {
res.toggled = true
slog.Debug("mujica: command block toggle", "open", *open)
}
tr.Grow(1).MinWidth(0).MinHeight(d.ControlHeight()).AlignItems(ui.Center).Gap(u*2).Padding(0, u*3).
Radius(theme.ControlRadius).Label(prompt + " " + run.Command).Cursor(ui.CursorPointer)
internal.Ring(tr, k.Focus, theme.ControlRadius)
if tr.Hovered() {
tr.Background(k.SurfaceHover)
}
tr.Transition(core.ColorFade)
turn := tr.Animate("mujica.turn", commandBlockTurn(*open), core.Motion(c, theme.StateDuration))
tr.Children(func() {
ui.Icon(c, icons.Must("chevron-right")).Size(14, 14).Shrink(0).Rotate(turn).TextColor(k.TextMuted)
ui.Text(c, prompt).Font(theme.MonoFont).TextColor(k.Ornament.Mix(k.Text, 0.35)).Shrink(0)
spans, _ := internal.CodeSpans(k, "shell", run.Command, false)
ui.RichText(c, spans...).Font(theme.MonoFont).SingleLine().MinWidth(0)
})
commandBlockStatus(c, run)
if core.IconAction(c, icons.Must("copy"), core.Msg(c, "command_block.copy_command")).Clicked() && !box.IsDisabled() {
c.WriteClipboard(run.Command)
res.copied = "command"
}
if core.IconAction(c, icons.Must("file-text"), core.Msg(c, "command_block.copy_output")).Disabled(run.Output == "").Clicked() && !box.IsDisabled() {
c.WriteClipboard(internal.StripAnsi(run.Output))
res.copied = "output"
}
})
if *open {
ui.Box(c).Height(1).Background(k.Border).Role(ui.RoleNone)
out := ui.ScrollBoth(c).MaxHeight(maxH).Padding(u*3, u*4).Background(k.Background).Label(core.Msg(c, "command_block.output"))
out.Children(func() {
if run.Output == "" {
ui.Text(c, core.Msg(c, "command_block.no_output")).TextColor(k.TextMuted).FontSize(core.FontSize(c, theme.CaptionSize))
return
}
AnsiText(c, run.Output, AnsiTextOptions{})
})
}
})
if res.copied != "" {
slog.Debug("mujica: command block copy", "what", res.copied)
}
return res
}
// commandBlockStatus shows running, success or failure with icon and words.
func commandBlockStatus(c *ui.Context, run agent.CommandRun) {
k := core.Tokens(c)
u := core.Density(c).Unit()
ui.Row(c).AlignItems(ui.Center).Gap(u * 1.5).Shrink(0).FontSize(core.FontSize(c, theme.CaptionSize)).Children(func() {
if run.Running {
core.Spinner(c, core.SpinnerOptions{Label: core.Msg(c, "command_block.running"), ShowLabel: true, Size: 12})
return
}
icon, col := "circle-check", k.Success
if run.ExitCode != 0 {
icon, col = "circle-x", k.Danger
}
ui.Row(c).Gap(u).AlignItems(ui.Center).TextColor(col).Children(func() {
ui.Icon(c, icons.Must(icon)).Size(14, 14)
ui.Text(c, fmt.Sprintf(core.Msg(c, "command_block.exit"), run.ExitCode)).FontFeatures("tnum").SingleLine()
})
ui.Text(c, commandBlockDuration(run.Duration)).TextColor(k.TextMuted).FontFeatures("tnum").SingleLine()
})
}
func commandBlockTurn(open bool) float32 {
if open {
return 90
}
return 0
}
// commandBlockDuration prints milliseconds, seconds or minutes.
func commandBlockDuration(d time.Duration) string {
if d < time.Second {
return fmt.Sprintf("%d ms", d.Milliseconds())
}
if d < time.Minute {
return fmt.Sprintf("%.1f s", d.Seconds())
}
return fmt.Sprintf("%dm %02ds", int(d.Minutes()), int(d.Seconds())%60)
}