cat / 012
clone

a spreadsheet for the terminal: lotus 1-2-3 looks, google sheets keys and formulas, one go binary

cat/module-path
012 / internal / ui / termbg.go
termbg.go 1.9K
1package ui23import (4 "time"56 tea "charm.land/bubbletea/v2"7)89// The theme follows the terminal's background: dark on a dark one,10// light on a light one (prefs.go). Until the terminal has said which,11// the screen stays blank rather than draw a frame in the dark theme a12// light terminal would flash, for as long as a question takes to cross13// an SSH connection. The wait ends with the background's answer, with14// the answer to the device attributes asked after it (probes), which a15// terminal that doesn't say its background sends instead, or after16// bgPatience for one that answers neither.1718// bgPatience is how long the screen waits for a terminal that answers19// nothing.20const bgPatience = 500 * time.Millisecond2122// bgWaitedMsg is bgPatience over.23type bgWaitedMsg struct{}2425// after is the clock the model's waits are timed on: a command that26// gives msg once d has passed; tea.Tick's when nil. Unit tests give one27// that holds notebooks' pauses for them to end (pauses_test.go).28var after func(d time.Duration, msg tea.Msg) tea.Cmd2930// awaitBackground holds the screen until the terminal says its31// background; probes asks. Its command gives bgWaitedMsg after32// bgPatience, or nothing once the wait is over.33func (t *terminal) awaitBackground() tea.Cmd {34 t.waitBG, t.bgKnown = true, make(chan struct{})35 if after != nil {36 return after(bgPatience, bgWaitedMsg{})37 }38 known := t.bgKnown39 return func() tea.Msg {40 timer := time.NewTimer(bgPatience)41 defer timer.Stop()42 select {43 case <-timer.C:44 return bgWaitedMsg{}45 case <-known:46 return nil47 }48 }49}5051// backgroundKnown ends the wait for the background.52func (t *terminal) backgroundKnown() {53 if t.waitBG {54 t.waitBG = false55 close(t.bgKnown)56 }57}5859// View implements tea.Model: the screen, once the theme is known.60func (m *Model) View() tea.View {61 if m.term.waitBG {62 v := tea.NewView(" ") // an empty view is drawn again every frame63 v.AltScreen = true64 return v65 }66 return m.frame()67}