Files
ZacharyZhang-NYandClaude Opus 5.5 03e1f91706 Upgrade MyGo to v0.5.5
Elements are now pass-scoped value handles: tests that read bounds
after a frame bind a ui.Handle. pixel tells frames apart by the build
pass's root, since the Tester clock no longer moves on its own.

Fix the regressions the upgrade brought:
- Clipboard reads in input handlers go through c.Services().
- MyGo notes an overlay's opener after building it, so AutoFocus
  inside lost it; pixel.AutoFocus focuses through a Handle, and
  archtest bans the raw call. Dialogs hand the focus back again.
- Elements around a press stay hovered: Tooltip tells a press-given
  focus by focus arriving under the pointer.
- The web host moves the Tester clock with real time, at most 500 ms
  a call, and gives components the real clock.

The gallery's source index also accepts CRLF checkouts.

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

105 lines
3.0 KiB
Go

package code
import (
"log/slog"
"unicode/utf8"
"github.com/egoist/mygo/ui"
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/layout"
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/pixel"
)
type TerminalOptions struct {
// Prompt starts the input line; zero means "$".
Prompt string
// Width and Height are dots; zeros mean 240 by 120.
Width, Height int
Disabled bool
}
type TerminalResult struct {
Element ui.Element
// Ran is the command Enter sent, else "".
Ran string
Changed bool
}
type termKey struct{}
// term is the scroll, history, how far back, the draft.
type term struct {
scroll ui.ScrollState
history []string
back int
draft string
}
// Terminal prints lines over a prompt; Enter runs, arrows recall.
func Terminal(c *ui.Context, text string, lines []string, input *string, o TerminalOptions) TerminalResult {
k := pixel.K(c)
w, h := pixel.Or("Terminal Width", o.Width, 240), pixel.Or("Terminal Height", o.Height, 120)
prompt := o.Prompt
if prompt == "" {
prompt = "$"
}
var res TerminalResult
res.Element = layout.Panel(c, layout.PanelOptions{Style: pixel.Sunken, Fill: pixel.Well, Padding: 2}, func() {
box := ui.Column(c).Size(k.D(w), k.D(h)).Gap(k.D(2))
tm := ui.Local(box, termKey{}, func() term { return term{} })
box.Children(func() {
layout.ScrollArea(c, &tm.scroll, layout.ScrollAreaOptions{Label: text, Follow: true, Disabled: o.Disabled}, func() {
ui.Column(c).FillWidth().Children(func() {
for _, l := range lines {
pixel.Text(c, l, pixel.TextOptions{Wrap: true})
}
})
}).Element.Grow(1).FillWidth()
row, off := ui.Row(c).FillWidth().Gap(k.D(4)), false
row.Children(func() {
pixel.Text(c, prompt, pixel.TextOptions{Slot: pixel.Good})
ed := pixel.Editor(c, input, pixel.EditorOptions{Label: k.Msg("command"), Disabled: o.Disabled})
ed.Element.Grow(1)
res.Changed, off = ed.Changed, ed.Input.IsDisabled()
if ed.Submitted && !off {
// The editor drew the old line; draw the empty one.
c.Invalidate()
res.Ran, *input, tm.back = *input, "", 0
if n := len(tm.history); res.Ran != "" && (n == 0 || tm.history[n-1] != res.Ran) {
tm.history = append(tm.history, res.Ran)
}
}
})
// Up and Down are history here, taken from the editor.
if up, down := pixel.Shortcut(row, 0, ui.KeyUp), pixel.Shortcut(row, 0, ui.KeyDown); (up || down) && !off {
res.Changed = recall(tm, input, up) || res.Changed
}
})
})
if res.Ran != "" || res.Changed {
slog.Debug("8bit: terminal", "label", text, "ran", res.Ran != "", "runes", utf8.RuneCountInString(*input))
}
return res
}
// recall steps through history; past the newest is the draft.
func recall(tm *term, input *string, up bool) bool {
n := len(tm.history)
switch {
case up && tm.back < n:
if tm.back == 0 {
tm.draft = *input
}
tm.back++
case !up && tm.back > 0:
tm.back--
default:
return false
}
*input = tm.draft
if tm.back > 0 {
*input = tm.history[n-tm.back]
}
return true
}