Files
sshtermy/internal/ui/form.go
T
db9979andClaude Fable 5.2 e1db1395c1 Bedienung 13–22: Sammeländerung, Notiz, Startbefehl, Wake-on-LAN, Suche in allen Tabs, Update-Kanal (#134)
* Code-Review 1–12: Emulator-Hänger, zweites sshTermy, Update-Selbsttest mit Rückfall, Tests

1  Terminal-Emulator: Zählparameter begrenzt (REP höchstens ein Bildschirm,
   ECH/CHT/CBT eine Zeile). "CSI 2147483647 b" hielt den Emulator minutenlang
   fest; das war der zeitweise hängende Fuzz-Test.
2  Zwei sshTermy auf demselben Tresor: Store.Fresh lädt neu, wenn das andere
   Programm gespeichert hat (vor jeder Änderung und im Sekundentakt), statt
   dessen Stand zu überschreiben. Auch ein geändertes Master-Passwort bleibt.
3  Schreibweg zum Server entkoppelt (termemu/hostqueue.go): ein hängendes
   Schreiben friert die Oberfläche nicht mehr ein.
4  Tresor wird mit fsync und eigener temporärer Datei geschrieben.
5  Sync: payloadFormat 2; TestPayloadFields verlangt bei neuen Feldern eine
   höhere Zahl, damit ältere Versionen den Abgleich verweigern statt Felder
   zu verlieren.
6  Update: Selbsttest der neuen Programmdatei vor dem Tausch (-version muss
   die angekündigte Version nennen), alte Datei bleibt bis zur Bestätigung,
   nach zwei unbestätigten Starts automatischer Rückfall (update/guard.go).
   Release-Workflow prüft die gebaute Datei genauso.
7  Actions in allen Workflows auf Commit-SHA festgelegt.
8  Tests: update (Apply, Signatur, Rückfall), cmdset, vault, store, theme.
9  Fuzz-Tests mit Zeitlimit je Eingabe (internal/fuzzguard); Tests und Fuzz
   von third_party/ansi laufen in CI und make test.
10 Farbschemata als eigenes Paket internal/ui/theme.
11 Fehler beim Speichern werden zentral gemeldet (Store.OnSaveError).
12 shellWriter liest die Shell über atomic.Pointer (Wettlauf mit Reconnect).

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018i4GUiL1D3QV4KrFiJxU8R

* Bedienung 13–22: Sammeländerung, Notiz, Startbefehl, Wake-on-LAN, Suche in allen Tabs, Update-Kanal

- Manager: Mehrfachauswahl (Leertaste, *) mit Sammeländerung, Verschieben
  und Löschen für alle markierten (bulk.go, Store.ChangeConns,
  DeleteConnectionsTrash)
- Verbindung: Notiz (nur für den Nutzer), Startbefehl nach dem Anmelden,
  MAC-Adresse und Wake-on-LAN (internal/wol, connextra.go)
- Schnellbefehle: Platzhalter {{name}} und {{name:vorschlag}} (quickvars.go)
- Alt+S sucht im Verlauf aller offenen Tabs (searchall.go)
- Tabs lassen sich in der Tab-Leiste mit der Maus ziehen
- Store: kleine Änderungen (zuletzt benutzt, Ordner auf/zu, Runbook-Zähler)
  werden gebündelt gespeichert (later.go)
- Update-Kanal „stabil“: Releases erst nach 1 Tag oder 7 Tagen anbieten
  (update.CheckAged, Einstellung UpdateWaitH, je Rechner)
- Formulare, die nicht ins Fenster passen, blättern um das aktive Feld;
  die Hilfezeile bleibt sichtbar
- paletteItems, App.update und viewManager in kleinere Funktionen geteilt
- Sync: payloadFormat 3 (neue Felder note, start_cmd, mac)
- README, Webseite, Handbuch, Kurzreferenz, englische Texte, Tests

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018i4GUiL1D3QV4KrFiJxU8R

---------

Co-authored-by: Claude <noreply@anthropic.com>
2026-11-11 12:38:52 +01:00

491 lines
13 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package ui
import (
"fmt"
"image/color"
"strconv"
"strings"
"charm.land/bubbles/v2/textinput"
tea "charm.land/bubbletea/v2"
"charm.land/lipgloss/v2"
"github.com/charmbracelet/x/ansi"
"github.com/db9979/sshtermy/internal/i18n"
)
// fieldKind: Text, Geheimnis, Zahl, Auswahl oder Schalter.
type fieldKind int
const (
fText fieldKind = iota
fSecret
fNumber
fChoice
fBool
fPick // Auswahl über eine durchsuchbare Liste (picker.go)
fHeader // Abschnittsüberschrift; mit fold einklappbar und fokussierbar
)
type field struct {
key, label, help string
kind fieldKind
in textinput.Model
options []string // fChoice
opt int
on bool // fBool
hidden func(f *form) bool
// fPick: gewählter Wert, Anzeige und Öffnen der Auswahlliste
value string
display func(value string) string
open func() tea.Cmd
// fHeader: fold = einklappbar (dann fokussierbar), on = aufgeklappt,
// summary zeigt im zugeklappten Zustand, was darin eingestellt ist.
fold bool
summary func(f *form) string
tint func(t Theme) color.Color // Abschnittsfarbe (fHeader)
// swatch (fChoice, optional): Farbpunkt vor der Option, z. B. bei „Farbe“
swatch func(t Theme, opt string) color.Color
}
// form ist ein kleines, tastaturgesteuertes Formular (Tab/↑↓ wechseln,
// ←/→ ändern Auswahl und Schalter, Strg+S oder Enter im letzten Feld speichert).
type form struct {
title string
fields []*field
focus int
err string
minH int // höchste bisher gezeigte Höhe: das Formular schrumpft nicht (kein Springen)
maxH int // so viele Zeilen passen ins Fenster (0 = unbegrenzt); darüber blättert das Formular
top int // erste gezeigte Feldzeile, wenn geblättert wird
}
func newInput(val string, secret bool) textinput.Model {
ti := textinput.New()
ti.Prompt = ""
ti.SetValue(val)
ti.CharLimit = 4096
ti.SetWidth(40)
ti.SetStyles(inputStyles(activeTheme))
if secret {
ti.EchoMode = textinput.EchoPassword
ti.EchoCharacter = '•'
}
return ti
}
func (f *form) add(fl *field) *field { f.fields = append(f.fields, fl); return fl }
func (f *form) text(key, label, val, help string) *field {
return f.add(&field{key: key, label: label, help: help, kind: fText, in: newInput(val, false)})
}
func (f *form) secret(key, label, val, help string) *field {
return f.add(&field{key: key, label: label, help: help, kind: fSecret, in: newInput(val, true)})
}
func (f *form) number(key, label string, val int, help string) *field {
s := ""
if val != 0 {
s = strconv.Itoa(val)
}
return f.add(&field{key: key, label: label, help: help, kind: fNumber, in: newInput(s, false)})
}
func (f *form) choice(key, label string, opts []string, cur string, help string) *field {
fl := &field{key: key, label: label, help: help, kind: fChoice, options: opts}
for i, o := range opts {
if o == cur {
fl.opt = i
}
}
return f.add(fl)
}
func (f *form) pick(key, label, val string, display func(string) string, open func() tea.Cmd, help string) *field {
return f.add(&field{key: key, label: label, help: help, kind: fPick, value: val, display: display, open: open})
}
func (f *form) toggle(key, label string, on bool, help string) *field {
return f.add(&field{key: key, label: label, help: help, kind: fBool, on: on})
}
// section beginnt einen Abschnitt mit Überschrift. Felder darunter gehören
// dazu, bis der nächste Abschnitt beginnt.
func (f *form) section(key, label string, tint func(Theme) color.Color) *field {
return f.add(&field{key: key, label: label, kind: fHeader, tint: tint})
}
// folded beginnt einen einklappbaren Abschnitt (zu, solange open false).
func (f *form) folded(key, label string, open bool, summary func(f *form) string, help string) *field {
return f.add(&field{key: key, label: label, kind: fHeader, fold: true, on: open, summary: summary, help: help})
}
func (fl *field) focusable() bool { return fl.kind != fHeader || fl.fold }
func (f *form) get(key string) *field {
for _, fl := range f.fields {
if fl.key == key {
return fl
}
}
return &field{}
}
func (f *form) str(key string) string { return strings.TrimSpace(f.get(key).in.Value()) }
func (f *form) raw(key string) string { return f.get(key).in.Value() }
func (f *form) num(key string) int { n, _ := strconv.Atoi(f.str(key)); return n }
func (f *form) sel(key string) string {
fl := f.get(key)
if len(fl.options) == 0 {
return ""
}
return fl.options[fl.opt]
}
func (f *form) flag(key string) bool { return f.get(key).on }
func (f *form) val(key string) string { return f.get(key).value }
// visible: sichtbare Felder in Reihenfolge. Felder in einem zugeklappten
// Abschnitt sind unsichtbar; eine Überschrift erscheint nur, wenn ihr
// Abschnitt (aufgeklappt) mindestens ein sichtbares Feld hätte.
func (f *form) visible() []int {
var out []int
head, headUsed := -1, false
for i, fl := range f.fields {
if fl.kind == fHeader {
head, headUsed = i, false
continue
}
if fl.hidden != nil && fl.hidden(f) {
continue
}
if head >= 0 && !headUsed {
out = append(out, head)
headUsed = true
}
if head >= 0 && f.fields[head].fold && !f.fields[head].on {
continue
}
out = append(out, i)
}
return out
}
func (f *form) focusables() []int {
var out []int
for _, i := range f.visible() {
if f.fields[i].focusable() {
out = append(out, i)
}
}
return out
}
func (f *form) move(d int) tea.Cmd {
vis := f.focusables()
pos := 0
for i, v := range vis {
if v == f.focus {
pos = i
}
}
pos = (pos + d + len(vis)) % len(vis)
return f.setFocus(vis[pos])
}
func (f *form) setFocus(i int) tea.Cmd {
// Feste Überschriften bekommen keinen Fokus: zum nächsten Feld weiter.
for i < len(f.fields)-1 && !f.fields[i].focusable() {
i++
}
for j, fl := range f.fields {
if j != i {
fl.in.Blur()
}
}
f.focus = i
fl := f.fields[i]
if fl.kind == fText || fl.kind == fSecret || fl.kind == fNumber {
return fl.in.Focus()
}
return nil
}
func (f *form) isLast() bool {
vis := f.focusables()
return len(vis) > 0 && vis[len(vis)-1] == f.focus
}
// update: submit = true, wenn gespeichert werden soll.
func (f *form) update(msg tea.Msg) (submit bool, cmd tea.Cmd) {
k, ok := msg.(tea.KeyPressMsg)
if ok {
fl := f.fields[f.focus]
switch k.String() {
case "tab", "down":
return false, f.move(1)
case "shift+tab", "up":
return false, f.move(-1)
case "ctrl+s":
return true, nil
case "enter", "space", "right", "left":
if fl.kind == fPick && fl.open != nil {
return false, fl.open()
}
if fl.kind == fHeader {
fl.on = k.String() == "right" || (k.String() != "left" && !fl.on)
return false, nil
}
}
switch k.String() {
case "enter":
if f.isLast() {
return true, nil
}
return false, f.move(1)
case "left", "right", "space":
if fl.kind == fChoice {
d := 1
if k.String() == "left" {
d = -1
}
fl.opt = (fl.opt + d + len(fl.options)) % len(fl.options)
return false, nil
}
if fl.kind == fBool {
fl.on = !fl.on
return false, nil
}
}
if fl.kind == fNumber && len(k.Text) == 1 && (k.Text[0] < '0' || k.Text[0] > '9') {
return false, nil
}
}
fl := f.fields[f.focus]
if fl.kind == fText || fl.kind == fSecret || fl.kind == fNumber {
fl.in, cmd = fl.in.Update(msg)
}
return false, cmd
}
// z (optional) macht Felder anklickbar: Klick setzt den Fokus, wählt eine
// Option oder schaltet einen Schalter um.
//
// Ruhig gehalten: Auswahlfelder zeigen ohne Fokus nur den gewählten Wert,
// die Hilfe steht in einer festen Zeile unten (statt sich unter das Feld
// zu schieben) und das Formular wird nie wieder kleiner, solange es offen ist.
func (f *form) view(s Styles, width int, z *zones) string {
var b strings.Builder
lines := 0
out := func(str string) { b.WriteString(str + "\n"); lines++ }
if f.title != "" {
out(s.Accent.Render(f.title))
}
// Felder erst sammeln: passt das Formular nicht ins Fenster (maxH),
// zeigt es einen Ausschnitt um das aktive Feld; Titel und Hilfezone
// bleiben stehen.
var body []string
focusLine := 0
put := func(str string) { body = append(body, str) }
const labelW = 22
inW := max(20, width-labelW-4)
first := true
var tint color.Color // Farbe des aktuellen Abschnitts (nil = ohne)
for _, i := range f.visible() {
fl := f.fields[i]
focused := i == f.focus
if fl.kind == fHeader {
if !first || f.title != "" {
put("")
}
first = false
tint = s.T.Muted
if fl.tint != nil {
tint = fl.tint(s.T)
}
if focused {
focusLine = len(body)
}
put(f.headerRow(s, i, fl, focused, tint, width, z))
continue
}
first = false
label := s.Label.Width(labelW).Render(fl.label)
var val string
switch fl.kind {
case fChoice:
val = f.choiceView(s, i, fl, focused, tint, inW, z)
case fPick:
v := fl.value
if fl.display != nil {
v = fl.display(fl.value)
}
val = " " + ansi.Truncate(v, max(10, inW-3), "…") + s.Muted.Render(" ▾")
case fBool:
if fl.on {
val = " " + s.OK.Render(i18n.T("● an"))
} else {
val = " " + s.Muted.Render(i18n.T("○ aus"))
}
default:
fl.in.SetWidth(inW - 1)
val = " " + fl.in.View()
}
marker := " "
if tint != nil {
marker = lipgloss.NewStyle().Foreground(tint).Faint(true).Render("│ ")
}
if focused {
fc := s.T.Accent
if tint != nil {
fc = tint
}
marker = lipgloss.NewStyle().Foreground(fc).Render("▌ ")
label = lipgloss.NewStyle().Foreground(s.T.Text).Bold(true).Width(labelW).Render(fl.label)
}
row := marker + label + val
if z != nil {
i, fl := i, fl
row = z.click(row, func(_ tea.Mouse, _ bool) tea.Cmd {
if fl.kind == fBool {
fl.on = !fl.on
}
if fl.kind == fPick && fl.open != nil {
f.setFocus(i)
return fl.open()
}
return f.setFocus(i)
})
}
if focused {
focusLine = len(body)
}
put(row)
}
// Feste Hilfezone: immer zwei Zeilen, damit nichts nachrutscht.
help := ""
if f.focus < len(f.fields) {
help = f.fields[f.focus].help
}
hl := strings.Split(lipgloss.NewStyle().Width(max(20, width-2)).Render(help), "\n")
for len(hl) < 2 {
hl = append(hl, "")
}
tail := 4
if f.err != "" {
tail += 2
}
focusLine = min(focusLine, len(body)-1)
if room := f.maxH - lines - tail; f.maxH > 0 && room >= 3 && len(body) > room {
if focusLine < f.top+1 {
f.top = focusLine - 1
}
if focusLine > f.top+room-2 {
f.top = focusLine - room + 2
}
f.top = clamp(f.top, 0, len(body)-room)
above, below := f.top, len(body)-f.top-room
body = append([]string(nil), body[f.top:f.top+room]...)
if above > 0 {
body[0] = s.Muted.Render(" ↑ …")
}
if below > 0 {
body[len(body)-1] = s.Muted.Render(" ↓ …")
}
} else {
f.top = 0
}
for _, l := range body {
out(l)
}
out("")
out(s.Muted.Render(strings.Repeat("─", max(10, width))))
for _, l := range hl[:2] {
out(s.Muted.Render(strings.TrimRight(l, " ")))
}
if f.err != "" {
out("")
out(s.Danger.Render("× " + f.err))
}
for lines < f.minH && (f.maxH <= 0 || lines < f.maxH) {
out("")
}
f.minH = lines
return b.String()
}
// choiceView: mit Fokus alle Optionen (passt es nicht, ‹ Wert › zum
// Durchschalten), ohne Fokus nur der gewählte Wert.
func (f *form) choiceView(s Styles, i int, fl *field, focused bool, tint color.Color, inW int, z *zones) string {
if fl.opt >= len(fl.options) {
return ""
}
cur := i18n.T(fl.options[fl.opt])
if !focused {
if fl.swatch != nil {
if c := fl.swatch(s.T, fl.options[fl.opt]); c != nil {
return " " + lipgloss.NewStyle().Foreground(c).Render("● "+cur)
}
}
if tint == nil {
tint = s.T.Text
}
return " " + lipgloss.NewStyle().Foreground(tint).Render(cur)
}
parts := make([]string, len(fl.options))
for j, o := range fl.options {
var sw color.Color
if fl.swatch != nil {
sw = fl.swatch(s.T, o)
}
switch {
case j == fl.opt:
parts[j] = s.Selected.Render(" " + i18n.T(o) + " ")
if sw != nil {
parts[j] = s.Selected.Foreground(sw).Render(" " + i18n.T(o) + " ")
}
case sw != nil:
parts[j] = lipgloss.NewStyle().Foreground(sw).Faint(true).Render(" " + i18n.T(o) + " ")
default:
parts[j] = s.Muted.Render(" " + i18n.T(o) + " ")
}
if z != nil {
j := j
parts[j] = z.click(parts[j], func(tea.Mouse, bool) tea.Cmd { fl.opt = j; return f.setFocus(i) })
}
}
val := strings.Join(parts, "")
if lipgloss.Width(val) > inW {
val = s.Muted.Render("‹ ") + s.Selected.Render(" "+cur+" ") + s.Muted.Render(" ›") +
s.Muted.Render(fmt.Sprintf(" %d/%d", fl.opt+1, len(fl.options)))
}
return val
}
// headerRow: „── Anmeldung ──────“; einklappbar mit ▸/▾ und Zusammenfassung.
func (f *form) headerRow(s Styles, i int, fl *field, focused bool, tint color.Color, width int, z *zones) string {
tc := lipgloss.NewStyle().Foreground(tint)
title := tc.Bold(true).Render(fl.label)
lead := tc.Render("━━ ")
extra := ""
if fl.fold {
arrow := "▸ "
if fl.on {
arrow = "▾ "
} else if fl.summary != nil {
if sm := fl.summary(f); sm != "" {
extra = " " + s.Muted.Render(sm)
}
}
lead = tc.Render(" " + arrow)
if focused {
lead = s.Accent.Render("▌") + tc.Render(arrow)
}
}
row := lead + title + extra + " "
if rest := width - lipgloss.Width(row); rest > 0 {
row += tc.Faint(true).Render(strings.Repeat("─", rest))
}
if fl.fold && z != nil {
row = z.click(row, func(tea.Mouse, bool) tea.Cmd { fl.on = !fl.on; return f.setFocus(i) })
}
return row
}