Skip to content
Myr

tty.go

This file

Path

tty.go

176 lines · 3.9 KiB · Go

Last changed by

fix(tty): fall back to default dimensions when terminal size is 0x0 (#1836)adam maguedbefore adoption96d69d2

History of this file

Take it

The walk stopped after 200 commits: lines older than 3dfbcd9 point at it.

feat(render): skipping lines from previous frameKonstantin Antonovbefore adoption3dfbcd9

lines 1–4why, one step back
1package tea
23import (4 "fmt"

Merge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97

line 5why, one step back
5 "os"

fix(tty): fall back to default dimensions when terminal size is 0x0 (#1836)adam maguedbefore adoption96d69d2

line 6why, one step back
6 "strconv"

feat(render): skipping lines from previous frameKonstantin Antonovbefore adoption3dfbcd9

lines 7–8why, one step back
7 "time"
8

Merge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97

line 9why, one step back
9 uv "github.com/charmbracelet/ultraviolet"

feat(render): skipping lines from previous frameKonstantin Antonovbefore adoption3dfbcd9

lines 10–13why, one step back
10 "github.com/charmbracelet/x/term"
11)1213func (p *Program) suspend() {

Merge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97

line 14why, one step back
14 if err := p.releaseTerminal(true); err != nil {

feat(render): skipping lines from previous frameKonstantin Antonovbefore adoption3dfbcd9

lines 15–25why, one step back
15 // If we can't release input, abort.
16 return17 }1819 suspendProcess()2021 _ = p.RestoreTerminal()22 go p.Send(ResumeMsg{})23}2425func (p *Program) initTerminal() error {

Merge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97

line 26why, one step back
26 if p.disableRenderer {

feat(render): skipping lines from previous frameKonstantin Antonovbefore adoption3dfbcd9

lines 27–28why, one step back
27 return nil
28 }

Merge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97

line 29why, one step back
29 return p.initInput()

feat(render): skipping lines from previous frameKonstantin Antonovbefore adoption3dfbcd9

lines 30–34why, one step back
30}
3132// restoreTerminalState restores the terminal to the state prior to running the33// Bubble Tea program.34func (p *Program) restoreTerminalState() error {

Merge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97

lines 35–36why, one step back
35 // Flush queued commands.
36 _ = p.flush()

feat(render): skipping lines from previous frameKonstantin Antonovbefore adoption3dfbcd9

lines 37–44why, one step back
37
38 return p.restoreInput()39}4041// restoreInput restores the tty input to its original state.42func (p *Program) restoreInput() error {43 if p.ttyInput != nil && p.previousTtyInputState != nil {44 if err := term.Restore(p.ttyInput.Fd(), p.previousTtyInputState); err != nil {

Merge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97

line 45why, one step back
45 return fmt.Errorf("bubbletea: error restoring console: %w", err)

feat(render): skipping lines from previous frameKonstantin Antonovbefore adoption3dfbcd9

lines 46–49why, one step back
46 }
47 }48 if p.ttyOutput != nil && p.previousOutputState != nil {49 if err := term.Restore(p.ttyOutput.Fd(), p.previousOutputState); err != nil {

Merge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97

line 50why, one step back
50 return fmt.Errorf("bubbletea: error restoring console: %w", err)

feat(render): skipping lines from previous frameKonstantin Antonovbefore adoption3dfbcd9

lines 51–55why, one step back
51 }
52 }53 return nil54}55

Merge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97

lines 56–57why, one step back
56// initInputReader (re)commences reading inputs.
57func (p *Program) initInputReader(cancel bool) error {

fix: windows: enable mouse mode on demand (#1340)Ayman Bagabasbefore adoptionbf1216d

lines 58–62why, one step back
58 if cancel && p.cancelReader != nil {
59 p.cancelReader.Cancel()60 p.waitForReadLoop()61 }62

Merge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97

lines 63–68why, one step back
63 term := p.environ.Getenv("TERM")
6465 // Initialize the input reader.66 // This need to be done after the terminal has been initialized and set to67 // raw mode.68

feat(render): skipping lines from previous frameKonstantin Antonovbefore adoption3dfbcd9

line 69why, one step back
69 var err error

Merge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97

line 70why, one step back
70 p.cancelReader, err = uv.NewCancelReader(p.input)

feat(render): skipping lines from previous frameKonstantin Antonovbefore adoption3dfbcd9

line 71why, one step back
71 if err != nil {

Merge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97

line 72why, one step back
72 return fmt.Errorf("bubbletea: could not create cancelable reader: %w", err)

feat(render): skipping lines from previous frameKonstantin Antonovbefore adoption3dfbcd9

lines 73–74why, one step back
73 }
74

Merge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97

lines 75–77why, one step back
75 drv := uv.NewTerminalReader(p.cancelReader, term)
76 drv.SetLogger(p.logger)77 p.inputScanner = drv

feat(render): skipping lines from previous frameKonstantin Antonovbefore adoption3dfbcd9

line 78why, one step back
78 p.readLoopDone = make(chan struct{})

Merge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97

line 79why, one step back
79

feat(render): skipping lines from previous frameKonstantin Antonovbefore adoption3dfbcd9

lines 80–87why, one step back
80 go p.readLoop()
8182 return nil83}8485func (p *Program) readLoop() {86 defer close(p.readLoopDone)87

Merge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97

line 88why, one step back
88 if err := p.inputScanner.StreamEvents(p.ctx, p.msgs); err != nil {

feat(render): skipping lines from previous frameKonstantin Antonovbefore adoption3dfbcd9

lines 89–90why, one step back
89 select {
90 case <-p.ctx.Done():

Merge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97

line 91why, one step back
91 return

feat(render): skipping lines from previous frameKonstantin Antonovbefore adoption3dfbcd9

lines 92–100why, one step back
92 case p.errs <- err:
93 }94 }95}9697// waitForReadLoop waits for the cancelReader to finish its read loop.98func (p *Program) waitForReadLoop() {99 select {100 case <-p.readLoopDone:

ci(lint): fix all lint issues on `main` (#1342)Andrey Neringbefore adoptionb224818

line 101why, one step back
101 case <-time.After(500 * time.Millisecond): //nolint:mnd

feat(render): skipping lines from previous frameKonstantin Antonovbefore adoption3dfbcd9

lines 102–107why, one step back
102 // The read loop hangs, which means the input
103 // cancelReader's cancel function has returned true even104 // though it was not able to cancel the read.105 }106}107

fix(tty): fall back to default dimensions when terminal size is 0x0 (#1836)adam maguedbefore adoption96d69d2

lines 108–144why, one step back
108const (
109 defaultWidth = 80110 defaultHeight = 24111)112113// fallbackDimensions returns width and height, falling back to COLUMNS/LINES114// environment variables or 80x24 defaults when dimensions are non-positive.115func (p *Program) fallbackDimensions(width, height int) (int, int) {116 if width <= 0 {117 var cols string118 if p != nil && p.environ != nil {119 cols = p.environ.Getenv("COLUMNS")120 } else {121 cols = os.Getenv("COLUMNS")122 }123 if c, err := strconv.Atoi(cols); err == nil && c > 0 {124 width = c125 } else {126 width = defaultWidth127 }128 }129 if height <= 0 {130 var lines string131 if p != nil && p.environ != nil {132 lines = p.environ.Getenv("LINES")133 } else {134 lines = os.Getenv("LINES")135 }136 if l, err := strconv.Atoi(lines); err == nil && l > 0 {137 height = l138 } else {139 height = defaultHeight140 }141 }142 return width, height143}144

feat(render): skipping lines from previous frameKonstantin Antonovbefore adoption3dfbcd9

lines 145–161why, one step back
145// checkResize detects the current size of the output and informs the program
146// via a WindowSizeMsg.147func (p *Program) checkResize() {148 if p.ttyOutput == nil {149 // can't query window size150 return151 }152153 w, h, err := term.GetSize(p.ttyOutput.Fd())154 if err != nil {155 select {156 case <-p.ctx.Done():157 case p.errs <- err:158 }159160 return161 }

fix(tty): fall back to default dimensions when terminal size is 0x0 (#1836)adam maguedbefore adoption96d69d2

lines 162–163why, one step back
162
163 w, h = p.fallbackDimensions(w, h)

feat(render): skipping lines from previous frameKonstantin Antonovbefore adoption3dfbcd9

line 164why, one step back
164

Merge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97

lines 165–175why, one step back
165 p.width, p.height = w, h
166 p.Send(WindowSizeMsg{Width: w, Height: h})167}168169// OpenTTY opens the running terminal's TTY for reading and writing.170func OpenTTY() (*os.File, *os.File, error) {171 in, out, err := uv.OpenTTY()172 if err != nil {173 return nil, nil, fmt.Errorf("bubbletea: could not open TTY: %w", err)174 }175 return in, out, nil

feat(render): skipping lines from previous frameKonstantin Antonovbefore adoption3dfbcd9

line 176why, one step back
176}