A cross-cutting pass, each fix with a test that failed first. Veils: MyGo still hands a key to a base that turned off this frame, so every element key now goes through pixel.Shortcut, which an archtest enforces. Input handlers record and the build applies them only while enabled; the editor turns away a veil-frame letter or copy; value bases get a copy and keep only enabled turns. Popups give a held focus back to their opener when the veil lifts. Motion: S5 is enforced at the clock. Every Tick, Since, Step, Toward and Shot runs at 4-15 fps; blinks keep 1 Hz on 4 fps steps, fast animations step at 15, and drawn time is quantized to frames. Clocks stepping back: Since holds at zero without overflow, and k.Elapsed moves a stored start to now, so nothing goes negative, runs backward or indexes out of range. Also: logs count typed text instead of holding it, gallery names use glyphs the font has, removable rows are keyed, components take their own Options (archtest), and sweeps over all 508 demos check veiled keys, clicks, wheels, the clock stepping back and names.
105 lines
3.0 KiB
Go
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
|
|
}
|