vcom/internal/tui/notify/notify.go
vrubelroman 9f29935356 feat(tui): notification center and per-theme styles (6.1.2)
Notes with a TTL by level (errors stay until a key after 3 s), timers
that only hide their own note (U-P1-1, crutch #13) and a 200-entry log.
All lipgloss styles are built once per theme with a generation counter
for render caches.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-01 08:56:53 +03:00

142 lines
3.1 KiB
Go

// Package notify is the notification center of the new UI (design §7.6):
// short messages shown in the status line with a time-to-live by level and
// a log of the last 200 for the "L" list. Notifications are not modals, so
// a timer can only ever hide its own note (U-P1-1, crutch #13).
//
// The center does no timing itself: Push returns the note and its TTL; the
// App schedules tea.Tick(ttl, expiredMsg{ID}) and calls Expire(ID) — only
// that note is affected. Errors stay until a key is pressed after the TTL.
package notify
import "time"
// Level of a note.
type Level uint8
const (
Info Level = iota
Success
Warn
Error
)
func (l Level) String() string {
switch l {
case Info:
return "info"
case Success:
return "ok"
case Warn:
return "warn"
case Error:
return "error"
}
return "?"
}
// TTL is how long a note of level l stays: Info/Success 3 s, Warn 6 s,
// Error at least 3 s and then until the next key press.
func TTL(l Level) time.Duration {
if l == Warn {
return 6 * time.Second
}
return 3 * time.Second
}
// LogSize is how many notes Log keeps.
const LogSize = 200
// Note is one notification.
type Note struct {
ID uint64
Level Level
Text string
At time.Time
ttlDone bool // the TTL timer fired
keyHit bool // (Error) a key was pressed after Push
hidden bool
}
// Center holds notes. Not safe for concurrent use (UI goroutine only).
type Center struct {
nextID uint64
live []*Note // not yet hidden, oldest first
log []Note // ring, oldest first
}
// Push adds a note and returns it; schedule Expire(n.ID) after TTL(n.Level).
func (c *Center) Push(l Level, text string, now time.Time) Note {
c.nextID++
n := &Note{ID: c.nextID, Level: l, Text: text, At: now}
c.live = append(c.live, n)
c.log = append(c.log, *n)
if len(c.log) > LogSize {
c.log = c.log[len(c.log)-LogSize:]
}
return *n
}
// Expire is called when the TTL timer of note id fires. It hides that note
// only — an Error note stays until a key was pressed.
func (c *Center) Expire(id uint64) {
for _, n := range c.live {
if n.ID != id {
continue
}
n.ttlDone = true
if n.Level != Error || n.keyHit {
n.hidden = true
}
}
c.compact()
}
// KeyPressed dismisses Error notes whose minimum time has passed and marks
// younger ones to go when their timer fires.
func (c *Center) KeyPressed() {
for _, n := range c.live {
if n.Level != Error {
continue
}
n.keyHit = true
if n.ttlDone {
n.hidden = true
}
}
c.compact()
}
// Dismiss hides note id at once.
func (c *Center) Dismiss(id uint64) {
for _, n := range c.live {
if n.ID == id {
n.hidden = true
}
}
c.compact()
}
func (c *Center) compact() {
out := c.live[:0]
for _, n := range c.live {
if !n.hidden {
out = append(out, n)
}
}
for i := len(out); i < len(c.live); i++ {
c.live[i] = nil
}
c.live = out
}
// Current is the note to show: the newest one still alive.
func (c *Center) Current() (Note, bool) {
if len(c.live) == 0 {
return Note{}, false
}
return *c.live[len(c.live)-1], true
}
// Log returns the last LogSize notes, oldest first.
func (c *Center) Log() []Note { return append([]Note(nil), c.log...) }