mirror of
https://github.com/db9979/sshtermy.git
synced 2026-12-06 12:44:08 +01:00
* Leistungsanzeige verständlicher, Zeichnen etwa doppelt so schnell - Anzeige bewertet die Zeichenzeit in Worten (flüssig/gut/etwas träge/ruckelt), zwei Zeilen Balken je Bild statt Braille-Punkte, "möglich"-Bildrate, 1 Bild/s im Leerlauf wird erklärt; im Terminal links neben der F6-Leiste, Toasts weichen darunter aus - Overlays (Toasts, Tooltip, Ausblenden, Leistungsanzeige) ohne Compositor: stamp() fasst nur die überdeckten Zeilen an - Terminalbild wird gemerkt, solange keine neue Ausgabe kam (Term.Gen) - Seitenleiste ohne lipgloss-Umbruch anfügen (beside), Verläufe in einem Render - ViewHL misst bereits aufgefüllte Zeilen nicht noch einmal - Benchmarks: Terminal 2,9 → 0,9 ms, mit F6 + Anzeige 6,5 → 2,3 ms, mit laufender Ausgabe 6,9 → 3,5 ms je Bild Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_018i4GUk5KxaHH89KrFPon9n * Zeichnen noch einmal fast doppelt so schnell - x/ansi als Fork (third_party/ansi): ASCII-Schnellweg in FirstGraphemeCluster, lipgloss misst jede Zeile jedes Bildes - Terminal-Zeilen unter einer Sperre holen (vt LineCopy), Farbregeln aus denselben Zellen statt jede Zelle zweimal einzeln - Seitenleiste (F6, Ziel, …) gemerkt, solange ihr Inhalt gleich bleibt - Balken der Leistungsanzeige je Farbe in einem Render - Benchmark mit F6, Anzeige und laufender Ausgabe: 3,5 → 1,8 ms je Bild Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_018i4GUk5KxaHH89KrFPon9n --------- Co-authored-by: Claude <noreply@anthropic.com>
287 lines
7.6 KiB
Go
287 lines
7.6 KiB
Go
package termemu
|
|
|
|
import (
|
|
"image/color"
|
|
"strings"
|
|
|
|
uv "github.com/charmbracelet/ultraviolet"
|
|
"github.com/charmbracelet/x/ansi"
|
|
)
|
|
|
|
// Maus und Markieren
|
|
//
|
|
// Fordert das Programm auf dem Server die Maus an (htop, mc, vim mit
|
|
// mouse=a, tmux …), merkt sich das Terminal den Modus; die Oberfläche leitet
|
|
// Klicks dann weiter. Sonst markiert sshTermy selbst: Positionen zählen
|
|
// über Scrollback und Bildschirm hinweg (Zeile 0 = älteste Scrollback-Zeile),
|
|
// damit eine Markierung beim Blättern stehen bleibt.
|
|
|
|
// Pos ist eine Zelle im Gesamtpuffer (Scrollback + Bildschirm).
|
|
type Pos struct{ Line, Col int }
|
|
|
|
func (p Pos) Before(o Pos) bool { return p.Line < o.Line || (p.Line == o.Line && p.Col < o.Col) }
|
|
|
|
// Sel ist eine Markierung von A bis B (beide inklusive, Reihenfolge egal).
|
|
type Sel struct{ A, B Pos }
|
|
|
|
func (s Sel) ordered() (Pos, Pos) {
|
|
if s.B.Before(s.A) {
|
|
return s.B, s.A
|
|
}
|
|
return s.A, s.B
|
|
}
|
|
|
|
func (s Sel) Contains(p Pos) bool {
|
|
a, b := s.ordered()
|
|
return !p.Before(a) && !b.Before(p)
|
|
}
|
|
|
|
func (s Sel) Empty() bool { return s.A == s.B }
|
|
|
|
func (t *Term) onMode(m ansi.Mode, on bool) {
|
|
dm, ok := m.(ansi.DECMode)
|
|
if !ok {
|
|
return
|
|
}
|
|
switch dm {
|
|
case ansi.ModeMouseX10, ansi.ModeMouseNormal, ansi.ModeMouseHighlight, ansi.ModeMouseButtonEvent, ansi.ModeMouseAnyEvent:
|
|
default:
|
|
return
|
|
}
|
|
t.mu.Lock()
|
|
defer t.mu.Unlock()
|
|
if t.mouse == nil {
|
|
t.mouse = map[ansi.DECMode]bool{}
|
|
}
|
|
if on {
|
|
t.mouse[dm] = true
|
|
} else {
|
|
delete(t.mouse, dm)
|
|
}
|
|
}
|
|
|
|
// MouseLevel: wie viel Maus das Programm haben will.
|
|
type MouseLevel int
|
|
|
|
const (
|
|
MouseOff MouseLevel = iota
|
|
MousePress // ?9 nur Drücken (X10)
|
|
MouseButtons // ?1000 Drücken und Loslassen
|
|
MouseDrag // ?1002 dazu Bewegung mit gedrückter Taste
|
|
MouseAny // ?1003 jede Bewegung
|
|
)
|
|
|
|
// MouseTracking: höchster vom Programm eingeschalteter Maus-Modus.
|
|
func (t *Term) MouseTracking() MouseLevel {
|
|
t.mu.Lock()
|
|
defer t.mu.Unlock()
|
|
switch {
|
|
case t.mouse[ansi.ModeMouseAnyEvent]:
|
|
return MouseAny
|
|
case t.mouse[ansi.ModeMouseButtonEvent]:
|
|
return MouseDrag
|
|
case t.mouse[ansi.ModeMouseNormal], t.mouse[ansi.ModeMouseHighlight]:
|
|
return MouseButtons
|
|
case t.mouse[ansi.ModeMouseX10]:
|
|
return MousePress
|
|
}
|
|
return MouseOff
|
|
}
|
|
|
|
// SendMouse kodiert ein Mausereignis (Koordinaten im Terminal, 0-basiert)
|
|
// passend zum Modus des Programms (X10 oder SGR) und schickt es zum Server.
|
|
func (t *Term) SendMouse(m uv.MouseEvent) { t.emu.SendMouse(m) }
|
|
|
|
// ScrollbackLen: Anzahl Zeilen oberhalb des Bildschirms (0 im Alt-Screen).
|
|
func (t *Term) ScrollbackLen() int {
|
|
if t.AltScreen() {
|
|
return 0
|
|
}
|
|
return t.sb.Len()
|
|
}
|
|
|
|
// cellAt liefert die Zelle im Gesamtpuffer; ok = false, wenn dort keine
|
|
// ist (hinter dem Zeilenende im Verlauf, außerhalb des Bildschirms).
|
|
// lineCells füllt dst (Länge = Breite) mit Zeile l des Gesamtpuffers, unter
|
|
// einer Sperre statt einer je Zelle. Fehlendes wird zur leeren Zelle.
|
|
func (t *Term) lineCells(sb, l int, dst uv.Line) {
|
|
if l < sb {
|
|
src := t.sb.line(l)
|
|
for x := range dst {
|
|
if x < len(src) {
|
|
dst[x] = src[x]
|
|
} else {
|
|
dst[x] = uv.EmptyCell
|
|
}
|
|
}
|
|
return
|
|
}
|
|
if !t.emu.LineCopy(l-sb, dst) {
|
|
for x := range dst {
|
|
dst[x] = uv.EmptyCell
|
|
}
|
|
}
|
|
}
|
|
|
|
func (t *Term) cellAt(sb int, p Pos) (uv.Cell, bool) {
|
|
if p.Line < sb {
|
|
return t.sb.cell(p.Col, p.Line)
|
|
}
|
|
return t.emu.CellCopy(p.Col, p.Line-sb)
|
|
}
|
|
|
|
// Render zeichnet den Bildschirm, um offset Zeilen in den Scrollback
|
|
// zurückversetzt, und hebt sel (falls gesetzt) invertiert hervor.
|
|
func (t *Term) Render(offset int, sel *Sel) string { return t.render(offset, sel, nil, nil) }
|
|
|
|
// RenderHL: wie Render, zusätzlich mit Farbregeln (hl darf nil sein).
|
|
func (t *Term) RenderHL(offset int, sel *Sel, hl *HL) string { return t.render(offset, sel, nil, hl) }
|
|
|
|
// render: hook darf jede Zelle vor dem Zeichnen verändern (Suchtreffer),
|
|
// hl färbt nach Regeln (highlight.go).
|
|
func (t *Term) render(offset int, sel *Sel, hook func(line, x int, c *uv.Cell), hl *HL) string {
|
|
w, h := t.Size()
|
|
sb := t.ScrollbackLen()
|
|
offset = max(0, min(offset, sb))
|
|
first := sb - offset
|
|
rows := make([]string, h)
|
|
line := make(uv.Line, w)
|
|
if hl != nil && t.AltScreen() {
|
|
hl = nil // vim, htop, less: die Programme färben selbst
|
|
}
|
|
for y := 0; y < h; y++ {
|
|
abs := first + y
|
|
t.lineCells(sb, abs, line)
|
|
var colors []color.Color
|
|
if hl != nil {
|
|
colors = hl.columnsOf(line)
|
|
}
|
|
for x := 0; x < w; x++ {
|
|
if sel != nil && !line[x].IsZero() && sel.Contains(Pos{abs, x}) {
|
|
if line[x].Content == "" {
|
|
line[x].Content, line[x].Width = " ", 1
|
|
}
|
|
line[x].Style.Attrs ^= uv.AttrReverse
|
|
}
|
|
if colors != nil && colors[x] != nil && line[x].Style.Fg == nil && line[x].Style.Bg == nil && !line[x].IsZero() {
|
|
line[x].Style.Fg = colors[x]
|
|
}
|
|
if hook != nil && !line[x].IsZero() {
|
|
hook(abs, x, &line[x])
|
|
}
|
|
}
|
|
r := line.Render()
|
|
if rw := ansi.StringWidth(r); rw < w {
|
|
r += strings.Repeat(" ", w-rw)
|
|
}
|
|
rows[y] = r
|
|
}
|
|
return strings.Join(rows, "\n")
|
|
}
|
|
|
|
// ScreenPos rechnet eine Bildschirmzeile (bei offset Zeilen Rückblick) in
|
|
// eine Position im Gesamtpuffer um.
|
|
func (t *Term) ScreenPos(x, y, offset int) Pos {
|
|
sb := t.ScrollbackLen()
|
|
offset = max(0, min(offset, sb))
|
|
return Pos{sb - offset + y, x}
|
|
}
|
|
|
|
// Text liefert den markierten Text, Zeilen mit \n getrennt, ohne
|
|
// nachlaufende Leerzeichen.
|
|
func (t *Term) Text(s Sel) string {
|
|
a, b := s.ordered()
|
|
w, _ := t.Size()
|
|
sb := t.ScrollbackLen()
|
|
var out []string
|
|
for l := a.Line; l <= b.Line; l++ {
|
|
from, to := 0, w-1
|
|
if l == a.Line {
|
|
from = a.Col
|
|
}
|
|
if l == b.Line {
|
|
to = min(b.Col, w-1)
|
|
}
|
|
var sbuf strings.Builder
|
|
for x := from; x <= to; x++ {
|
|
c, ok := t.cellAt(sb, Pos{l, x})
|
|
switch {
|
|
case !ok:
|
|
sbuf.WriteByte(' ')
|
|
case c.IsZero():
|
|
// zweite Hälfte eines breiten Zeichens
|
|
case c.Content == "":
|
|
sbuf.WriteByte(' ')
|
|
default:
|
|
sbuf.WriteString(c.Content)
|
|
}
|
|
}
|
|
out = append(out, strings.TrimRight(sbuf.String(), " "))
|
|
}
|
|
return strings.Join(out, "\n")
|
|
}
|
|
|
|
// WordAt: Markierung des Wortes unter p (für Doppelklick). Wortzeichen sind
|
|
// alles außer Leerzeichen und typischen Trennern, damit Pfade, Hostnamen
|
|
// und IPs am Stück markiert werden.
|
|
func (t *Term) WordAt(p Pos) Sel {
|
|
w, _ := t.Size()
|
|
sb := t.ScrollbackLen()
|
|
isWord := func(x int) bool {
|
|
c, ok := t.cellAt(sb, Pos{p.Line, x})
|
|
if !ok {
|
|
return false
|
|
}
|
|
if c.IsZero() {
|
|
return true
|
|
}
|
|
return c.Content != "" && !strings.ContainsAny(c.Content, " \t\"'`()[]{}<>|;,")
|
|
}
|
|
if !isWord(p.Col) {
|
|
return Sel{p, p}
|
|
}
|
|
a, b := p.Col, p.Col
|
|
for a > 0 && isWord(a-1) {
|
|
a--
|
|
}
|
|
for b < w-1 && isWord(b+1) {
|
|
b++
|
|
}
|
|
return Sel{Pos{p.Line, a}, Pos{p.Line, b}}
|
|
}
|
|
|
|
// LineAt: ganze Zeile (Dreifachklick).
|
|
func (t *Term) LineAt(p Pos) Sel {
|
|
w, _ := t.Size()
|
|
return Sel{Pos{p.Line, 0}, Pos{p.Line, w - 1}}
|
|
}
|
|
|
|
// LineText liefert eine Zeile des Gesamtpuffers als Text und zu jeder Spalte
|
|
// den Byte-Versatz im Text (cols[x], len(cols) = Breite+1). Die zweite Hälfte
|
|
// eines breiten Zeichens zeigt auf dessen Anfang. So lassen sich Muster
|
|
// (Links, IPs) per Regex suchen und auf Mausspalten zurückrechnen.
|
|
func (t *Term) LineText(line int) (text string, cols []int) {
|
|
w, _ := t.Size()
|
|
sb := t.ScrollbackLen()
|
|
var b strings.Builder
|
|
cols = make([]int, w+1)
|
|
last := 0
|
|
for x := 0; x < w; x++ {
|
|
c, ok := t.cellAt(sb, Pos{line, x})
|
|
switch {
|
|
case !ok, c.Content == "" && !c.IsZero():
|
|
cols[x] = b.Len()
|
|
b.WriteByte(' ')
|
|
case c.IsZero():
|
|
cols[x] = last // zweite Hälfte eines breiten Zeichens
|
|
continue
|
|
default:
|
|
cols[x] = b.Len()
|
|
b.WriteString(c.Content)
|
|
}
|
|
last = cols[x]
|
|
}
|
|
cols[w] = b.Len()
|
|
return b.String(), cols
|
|
}
|