Skip to content
Myr

feat: use Termenv.Output to write to tty

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Christian Muehlhaeuser · committed by Christian Muehlhaeuser · parent 5c4218e · Browse files · 13 files+111 added−219 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

13 files+111 added−219 removed

  • other:+111 added−219 removed

13 files+111 added−219 removed

modified:examples/go.mod+1 added-1 removed
9 unmodified lines
10 github.com/fogleman/ease v0.0.0-20170301025033-8da417bf177611 github.com/lucasb-eyer/go-colorful v1.2.012 github.com/mattn/go-isatty v0.0.1613 github.com/muesli/reflow v0.3.014 github.com/muesli/termenv v0.12.1-0.20220615005108-4e9068de989814 github.com/muesli/termenv v0.13.015)1617replace github.com/charmbracelet/bubbletea => ../
modified:examples/go.sum+4 added-5 removed
37 unmodified lines
38github.com/mattn/go-localereader v0.0.1/go.mod h1:8fBrzywKY7BI3czFoHkuzRoWE9C+EiG4R1k4Cjx5p88=39github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=40github.com/mattn/go-runewidth v0.0.10/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk=41github.com/mattn/go-runewidth v0.0.12/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk=42github.com/mattn/go-runewidth v0.0.13 h1:lTGmDsbAYt5DmK6OnoV7EuIF1wEIFAcxld6ypU4OSgU=42github.com/mattn/go-runewidth v0.0.13/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=43github.com/mattn/go-runewidth v0.0.14 h1:+xnbZSEeDbOIg5/mE6JF0w6n9duR1l3/WmbinWVwUuU=44github.com/mattn/go-runewidth v0.0.14/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=45github.com/microcosm-cc/bluemonday v1.0.17 h1:Z1a//hgsQ4yjC+8zEkV8IWySkXnsxmdSY642CTFQb5Y=46github.com/microcosm-cc/bluemonday v1.0.17/go.mod h1:Z0r70sCuXHig8YpBzCc5eGHAap2K7e/u082ZUpDRRqM=47github.com/muesli/ansi v0.0.0-20211018074035-2e021307bc4b h1:1XF24mVaiu7u+CFywTdcDo2ie1pzzhwjt6RHqzpMU34=48github.com/muesli/ansi v0.0.0-20211018074035-2e021307bc4b/go.mod h1:fQuZ0gauxyBcmsdE3ZT4NasjaRdxmbCS0jRHsrWu3Ho=
4 unmodified lines
53github.com/muesli/reflow v0.3.0/go.mod h1:pbwTDkVPibjO2kyvBQRBxTWEEGDGq0FlB1BIKtnHY/8=54github.com/muesli/termenv v0.9.0/go.mod h1:R/LzAKf+suGs4IsO95y7+7DpFHO0KABgnZqtlyx2mBw=55github.com/muesli/termenv v0.11.1-0.20220204035834-5ac8409525e0/go.mod h1:Bd5NYQ7pd+SrtBSrSNoBBmXlcY8+Xj4BMJgh8qcZrvs=56github.com/muesli/termenv v0.11.1-0.20220212125758-44cd13922739/go.mod h1:Bd5NYQ7pd+SrtBSrSNoBBmXlcY8+Xj4BMJgh8qcZrvs=56github.com/muesli/termenv v0.12.1-0.20220615005108-4e9068de9898 h1:0j+cbZdhLgpNxjg0nWCasHUA82fgWOXxxGgWNVOLS1I=57github.com/muesli/termenv v0.12.1-0.20220615005108-4e9068de9898/go.mod h1:bN6sPNtkiahdhHv2Xm6RGU16LSCxfbIZvMfqjOCfrR4=57github.com/muesli/termenv v0.13.0 h1:wK20DRpJdDX8b7Ek2QfhvqhRQFZ237RGRO0RQ/Iqdy0=58github.com/muesli/termenv v0.13.0/go.mod h1:sP1+uffeLaEYpyOTb8pLCUctGcGLnoFjSn4YJK5e2bc=59github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec=60github.com/olekukonko/tablewriter v0.0.5/go.mod h1:hPp6KlRPjbx+hW8ykQs1w3UBbZlj6HuIJcUGPhkA7kY=61github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=62github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
18 unmodified lines
81golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=82golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=83golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=84golang.org/x/sys v0.0.0-20220204135822-1c1b9b1eba6a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=84golang.org/x/sys v0.0.0-20220209214540-3681064d5158/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=85golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab h1:2QkjZIsXupsJbJIdSjjUOgWK3aEtzyuh2mPt3l/CkeU=86golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=87golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=88golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 h1:JGgROgKl9N8DuW20oFS5gxc+lE67/N3FcwmBPMe7ArY=
1 unmodified line
90golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=91golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk=92golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=93golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=94gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=94gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=95gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=96gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
modified:tutorials/go.sum+6 added-5 removed
1github.com/aymanbagabas/go-osc52 v1.0.3 h1:DTwqENW7X9arYimJrPeGZcV0ln14sGMt3pHZspWD+Mg=2github.com/aymanbagabas/go-osc52 v1.0.3/go.mod h1:zT8H+Rk4VSabYN90pWyugflM3ZhpTZNC7cASDfUCdT4=3github.com/containerd/console v1.0.3 h1:lIr7SlA5PxZyMV30bDW0MGbiOPXwc63yRuCP0ARubLw=4github.com/containerd/console v1.0.3/go.mod h1:7LqA/THxQ86k76b8c/EMSiaJ3h1eZkMkXar0TQ1gf3U=5github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=6github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=5github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=7github.com/mattn/go-isatty v0.0.16 h1:bq3VjFmv/sOjHtdEhmkEV4x1AJtvUvOJ2PFAZ5+peKQ=8github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=9github.com/mattn/go-localereader v0.0.1 h1:ygSAOl7ZXTx4RdPYinUpg6W99U8jWvWi9Ye2JC/oIi4=10github.com/mattn/go-localereader v0.0.1/go.mod h1:8fBrzywKY7BI3czFoHkuzRoWE9C+EiG4R1k4Cjx5p88=11github.com/mattn/go-runewidth v0.0.12/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk=11github.com/mattn/go-runewidth v0.0.13 h1:lTGmDsbAYt5DmK6OnoV7EuIF1wEIFAcxld6ypU4OSgU=12github.com/mattn/go-runewidth v0.0.13/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=13github.com/mattn/go-runewidth v0.0.14 h1:+xnbZSEeDbOIg5/mE6JF0w6n9duR1l3/WmbinWVwUuU=14github.com/mattn/go-runewidth v0.0.14/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=15github.com/muesli/ansi v0.0.0-20211018074035-2e021307bc4b h1:1XF24mVaiu7u+CFywTdcDo2ie1pzzhwjt6RHqzpMU34=16github.com/muesli/ansi v0.0.0-20211018074035-2e021307bc4b/go.mod h1:fQuZ0gauxyBcmsdE3ZT4NasjaRdxmbCS0jRHsrWu3Ho=17github.com/muesli/cancelreader v0.2.2 h1:3I4Kt4BQjOR54NavqnDogx/MIoWBFa0StPA8ELUXHmA=18github.com/muesli/cancelreader v0.2.2/go.mod h1:3XuTXfFS2VjM+HTLZY9Ak0l6eUKfijIfMUZ4EgX0QYo=19github.com/muesli/reflow v0.3.0 h1:IFsN6K9NfGtjeggFP+68I4chLZV2yIKsXJFNZ+eWh6s=20github.com/muesli/reflow v0.3.0/go.mod h1:pbwTDkVPibjO2kyvBQRBxTWEEGDGq0FlB1BIKtnHY/8=19github.com/muesli/termenv v0.11.1-0.20220212125758-44cd13922739 h1:QANkGiGr39l1EESqrE0gZw0/AJNYzIvoGLhIoVYtluI=20github.com/muesli/termenv v0.11.1-0.20220212125758-44cd13922739/go.mod h1:Bd5NYQ7pd+SrtBSrSNoBBmXlcY8+Xj4BMJgh8qcZrvs=21github.com/muesli/termenv v0.13.0 h1:wK20DRpJdDX8b7Ek2QfhvqhRQFZ237RGRO0RQ/Iqdy0=22github.com/muesli/termenv v0.13.0/go.mod h1:sP1+uffeLaEYpyOTb8pLCUctGcGLnoFjSn4YJK5e2bc=23github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=24github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=25github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=26golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=27golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=26golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=28golang.org/x/sys v0.0.0-20220204135822-1c1b9b1eba6a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=29golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab h1:2QkjZIsXupsJbJIdSjjUOgWK3aEtzyuh2mPt3l/CkeU=30golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=31golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 h1:JGgROgKl9N8DuW20oFS5gxc+lE67/N3FcwmBPMe7ArY=
4 unmodified lines
modified:go.mod+1 added-2 removed
7 unmodified lines
8 github.com/mattn/go-localereader v0.0.19 github.com/muesli/ansi v0.0.0-20211018074035-2e021307bc4b10 github.com/muesli/cancelreader v0.2.211 github.com/muesli/reflow v0.3.012 github.com/muesli/termenv v0.11.1-0.20220212125758-44cd1392273913 golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab12 github.com/muesli/termenv v0.13.013 golang.org/x/term v0.0.0-20210927222741-03fcf44c221114 golang.org/x/text v0.3.7 // indirect15)
modified:go.sum+6 added-5 removed
1github.com/aymanbagabas/go-osc52 v1.0.3 h1:DTwqENW7X9arYimJrPeGZcV0ln14sGMt3pHZspWD+Mg=2github.com/aymanbagabas/go-osc52 v1.0.3/go.mod h1:zT8H+Rk4VSabYN90pWyugflM3ZhpTZNC7cASDfUCdT4=3github.com/containerd/console v1.0.3 h1:lIr7SlA5PxZyMV30bDW0MGbiOPXwc63yRuCP0ARubLw=4github.com/containerd/console v1.0.3/go.mod h1:7LqA/THxQ86k76b8c/EMSiaJ3h1eZkMkXar0TQ1gf3U=5github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=6github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=5github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=7github.com/mattn/go-isatty v0.0.16 h1:bq3VjFmv/sOjHtdEhmkEV4x1AJtvUvOJ2PFAZ5+peKQ=8github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=9github.com/mattn/go-localereader v0.0.1 h1:ygSAOl7ZXTx4RdPYinUpg6W99U8jWvWi9Ye2JC/oIi4=10github.com/mattn/go-localereader v0.0.1/go.mod h1:8fBrzywKY7BI3czFoHkuzRoWE9C+EiG4R1k4Cjx5p88=11github.com/mattn/go-runewidth v0.0.12/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk=11github.com/mattn/go-runewidth v0.0.13 h1:lTGmDsbAYt5DmK6OnoV7EuIF1wEIFAcxld6ypU4OSgU=12github.com/mattn/go-runewidth v0.0.13/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=13github.com/mattn/go-runewidth v0.0.14 h1:+xnbZSEeDbOIg5/mE6JF0w6n9duR1l3/WmbinWVwUuU=14github.com/mattn/go-runewidth v0.0.14/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=15github.com/muesli/ansi v0.0.0-20211018074035-2e021307bc4b h1:1XF24mVaiu7u+CFywTdcDo2ie1pzzhwjt6RHqzpMU34=16github.com/muesli/ansi v0.0.0-20211018074035-2e021307bc4b/go.mod h1:fQuZ0gauxyBcmsdE3ZT4NasjaRdxmbCS0jRHsrWu3Ho=17github.com/muesli/cancelreader v0.2.2 h1:3I4Kt4BQjOR54NavqnDogx/MIoWBFa0StPA8ELUXHmA=18github.com/muesli/cancelreader v0.2.2/go.mod h1:3XuTXfFS2VjM+HTLZY9Ak0l6eUKfijIfMUZ4EgX0QYo=19github.com/muesli/reflow v0.3.0 h1:IFsN6K9NfGtjeggFP+68I4chLZV2yIKsXJFNZ+eWh6s=20github.com/muesli/reflow v0.3.0/go.mod h1:pbwTDkVPibjO2kyvBQRBxTWEEGDGq0FlB1BIKtnHY/8=19github.com/muesli/termenv v0.11.1-0.20220212125758-44cd13922739 h1:QANkGiGr39l1EESqrE0gZw0/AJNYzIvoGLhIoVYtluI=20github.com/muesli/termenv v0.11.1-0.20220212125758-44cd13922739/go.mod h1:Bd5NYQ7pd+SrtBSrSNoBBmXlcY8+Xj4BMJgh8qcZrvs=21github.com/muesli/termenv v0.13.0 h1:wK20DRpJdDX8b7Ek2QfhvqhRQFZ237RGRO0RQ/Iqdy0=22github.com/muesli/termenv v0.13.0/go.mod h1:sP1+uffeLaEYpyOTb8pLCUctGcGLnoFjSn4YJK5e2bc=23github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=24github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=25github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=26golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=27golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=26golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=28golang.org/x/sys v0.0.0-20220204135822-1c1b9b1eba6a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=29golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab h1:2QkjZIsXupsJbJIdSjjUOgWK3aEtzyuh2mPt3l/CkeU=30golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=31golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 h1:JGgROgKl9N8DuW20oFS5gxc+lE67/N3FcwmBPMe7ArY=
4 unmodified lines
modified:options_test.go+2 added-2 removed
7 unmodified lines
8func TestOptions(t *testing.T) {9 t.Run("output", func(t *testing.T) {10 var b bytes.Buffer11 p := NewProgram(nil, WithOutput(&b))12 if p.output != &b {13 t.Errorf("expected output to custom, got %v", p.output)12 if p.output.TTY() != nil {13 t.Errorf("expected output to custom, got %v", p.output.TTY().Fd())14 }15 })1617 t.Run("input", func(t *testing.T) {
77 unmodified lines
modified:options.go+14 added-10 removed
1package tea23import "io"3import (4 "io"56 "github.com/muesli/termenv"7)89// ProgramOption is used to set options when initializing a Program. Program can10// accept a variable number of options.11//12// Example usage:
3 unmodified lines
1617// WithOutput sets the output which, by default, is stdout. In most cases you18// won't need to use this.19func WithOutput(output io.Writer) ProgramOption {16 return func(m *Program) {17 m.output = output20 return func(p *Program) {21 p.output = termenv.NewOutput(output, termenv.WithColorCache(true))22 }23}2425// WithInput sets the input which, by default, is stdin. In most cases you26// won't need to use this.27func WithInput(input io.Reader) ProgramOption {24 return func(m *Program) {25 m.input = input26 m.startupOptions |= withCustomInput28 return func(p *Program) {29 p.input = input30 p.startupOptions |= withCustomInput31 }32}3334// WithInputTTY open a new TTY for input (or console input device on Windows).
7 unmodified lines
42// default. If panic catching is disabled the terminal will be in a fairly43// unusable state after a panic because Bubble Tea will not perform its usual44// cleanup on exit.45func WithoutCatchPanics() ProgramOption {42 return func(m *Program) {43 m.CatchPanics = false46 return func(p *Program) {47 p.CatchPanics = false48 }49}5051// WithAltScreen starts the program with the alternate screen buffer enabled
65 unmodified lines
117// application, or to provide an additional non-TUI mode to your Bubble Tea118// programs. For example, your program could behave like a daemon if output is119// not a TTY.120func WithoutRenderer() ProgramOption {117 return func(m *Program) {118 m.renderer = &nilRenderer{}121 return func(p *Program) {122 p.renderer = &nilRenderer{}123 }124}125126// WithANSICompressor removes redundant ANSI sequences to produce potentially
9 unmodified lines
deleted:screen_test.go-65 removed
1package tea23import (4 "bytes"5 "io"6 "testing"7)89func TestScreen(t *testing.T) {10 exercise := func(t *testing.T, fn func(io.Writer), expect []byte) {11 var w bytes.Buffer12 fn(&w)13 if !bytes.Equal(w.Bytes(), expect) {14 t.Errorf("expected %q, got %q", expect, w.Bytes())15 }16 }1718 t.Run("change scrolling region", func(t *testing.T) {19 exercise(t, func(w io.Writer) {20 changeScrollingRegion(w, 16, 22)21 }, []byte("\x1b[16;22r"))22 })2324 t.Run("line", func(t *testing.T) {25 t.Run("clear", func(t *testing.T) {26 exercise(t, clearLine, []byte("\x1b[2K"))27 })2829 t.Run("insert", func(t *testing.T) {30 exercise(t, func(w io.Writer) {31 insertLine(w, 12)32 }, []byte("\x1b[12L"))33 })34 })3536 t.Run("cursor", func(t *testing.T) {37 t.Run("hide", func(t *testing.T) {38 exercise(t, hideCursor, []byte("\x1b[?25l"))39 })4041 t.Run("show", func(t *testing.T) {42 exercise(t, showCursor, []byte("\x1b[?25h"))43 })4445 t.Run("up", func(t *testing.T) {46 exercise(t, cursorUp, []byte("\x1b[1A"))47 })4849 t.Run("down", func(t *testing.T) {50 exercise(t, cursorDown, []byte("\x1b[1B"))51 })5253 t.Run("move", func(t *testing.T) {54 exercise(t, func(w io.Writer) {55 moveCursor(w, 10, 20)56 }, []byte("\x1b[10;20H"))57 })5859 t.Run("back", func(t *testing.T) {60 exercise(t, func(w io.Writer) {61 cursorBack(w, 15)62 }, []byte("\x1b[15D"))63 })64 })65}
deleted:screen.go-53 removed
1package tea23import (4 "fmt"5 "io"67 te "github.com/muesli/termenv"8)910func hideCursor(w io.Writer) {11 fmt.Fprintf(w, te.CSI+te.HideCursorSeq)12}1314func showCursor(w io.Writer) {15 fmt.Fprintf(w, te.CSI+te.ShowCursorSeq)16}1718func clearLine(w io.Writer) {19 fmt.Fprintf(w, te.CSI+te.EraseLineSeq, 2)20}2122func cursorUp(w io.Writer) {23 fmt.Fprintf(w, te.CSI+te.CursorUpSeq, 1)24}2526func cursorDown(w io.Writer) {27 fmt.Fprintf(w, te.CSI+te.CursorDownSeq, 1)28}2930func insertLine(w io.Writer, numLines int) {31 fmt.Fprintf(w, te.CSI+"%dL", numLines)32}3334func moveCursor(w io.Writer, row, col int) {35 fmt.Fprintf(w, te.CSI+te.CursorPositionSeq, row, col)36}3738func changeScrollingRegion(w io.Writer, top, bottom int) {39 fmt.Fprintf(w, te.CSI+te.ChangeScrollingRegionSeq, top, bottom)40}4142func cursorBack(w io.Writer, n int) {43 fmt.Fprintf(w, te.CSI+te.CursorBackSeq, n)44}4546func enterAltScreen(w io.Writer) {47 fmt.Fprintf(w, te.CSI+te.AltScreenSeq)48 moveCursor(w, 0, 0)49}5051func exitAltScreen(w io.Writer) {52 fmt.Fprintf(w, te.CSI+te.ExitAltScreenSeq)53}
modified:standard_renderer.go+43 added-37 removed
8 unmodified lines
9 "time"1011 "github.com/muesli/ansi/compressor"12 "github.com/muesli/reflow/truncate"13 "github.com/muesli/termenv"14)1516const (17 // defaultFramerate specifies the maximum interval at which we should
6 unmodified lines
24//25// In cases where very high performance is needed the renderer can be told26// to exclude ranges of lines, allowing them to be written to directly.27type standardRenderer struct {27 out io.Writer28 out *termenv.Output29 buf bytes.Buffer30 queuedMessageLines []string31 framerate time.Duration32 ticker *time.Ticker
16 unmodified lines
49}5051// newRenderer creates a new renderer. Normally you'll want to initialize it52// with os.Stdout as the first argument.52func newRenderer(out io.Writer, mtx *sync.Mutex, useANSICompressor bool) renderer {53func newRenderer(out *termenv.Output, mtx *sync.Mutex, useANSICompressor bool) renderer {54 r := &standardRenderer{55 out: out,56 mtx: mtx,57 framerate: defaultFramerate,58 useANSICompressor: useANSICompressor,59 queuedMessageLines: []string{},60 }61 if r.useANSICompressor {61 r.out = &compressor.Writer{Forward: out}62 r.out = termenv.NewOutput(&compressor.Writer{Forward: out})63 }64 return r65}66
8 unmodified lines
7576// stop permanently halts the renderer, rendering the final frame.77func (r *standardRenderer) stop() {78 r.flush()78 clearLine(r.out)79 r.out.ClearLine()80 r.once.Do(func() {81 close(r.done)82 })8384 if r.useANSICompressor {84 if w, ok := r.out.(io.WriteCloser); ok {85 if w, ok := r.out.TTY().(io.WriteCloser); ok {86 _ = w.Close()87 }88 }89}9091// kill halts the renderer. The final frame will not be rendered.92func (r *standardRenderer) kill() {92 clearLine(r.out)93 r.out.ClearLine()94 r.once.Do(func() {95 close(r.done)96 })97}
24 unmodified lines
122 return123 }124125 // Output buffer125 out := new(bytes.Buffer)126 buf := &bytes.Buffer{}127 out := termenv.NewOutput(buf)128129 newLines := strings.Split(r.buf.String(), "\n")130 numLinesThisFlush := len(newLines)131 oldLines := strings.Split(r.lastRender, "\n")
14 unmodified lines
146 // this line as a performance optimization.147 if (len(newLines) <= len(oldLines)) && (len(newLines) > i && len(oldLines) > i) && (newLines[i] == oldLines[i]) {148 skipLines[i] = struct{}{}149 } else if _, exists := r.ignoreLines[i]; !exists {148 clearLine(out)150 out.ClearLine()151 }152151 cursorUp(out)153 out.CursorUp(1)154 }155156 if _, exists := r.ignoreLines[0]; !exists {157 // We need to return to the start of the line here to properly
4 unmodified lines
162 // We use this sequence in particular because it's part of the ANSI163 // standard (whereas others are proprietary to, say, VT100/VT52).164 // If cursor previous line (ESC[ + <n> + F) were better supported165 // we could use that above to eliminate this step.164 cursorBack(out, r.width)165 clearLine(out)166 out.CursorBack(r.width)167 out.ClearLine()168 }169 }170171 // Merge the set of lines we're skipping as a rendering optimization with
8 unmodified lines
180 for i := 0; i < len(newLines); i++ {181 if _, skip := skipLines[i]; skip {182 // Unless this is the last line, move the cursor down.183 if i < len(newLines)-1 {182 cursorDown(out)184 out.CursorDown(1)185 }186 } else {187 line := newLines[i]188
7 unmodified lines
196 if r.width > 0 {197 line = truncate.String(line, uint(r.width))198 }199198 _, _ = io.WriteString(out, line)200 _, _ = out.WriteString(line)201202 if i < len(newLines)-1 {201 _, _ = io.WriteString(out, "\r\n")203 _, _ = out.WriteString("\r\n")204 }205 }206 }207 r.linesRendered = numLinesThisFlush
3 unmodified lines
211 if r.altScreenActive {212 // This case fixes a bug in macOS terminal. In other terminals the213 // other case seems to do the job regardless of whether or not we're214 // using the full terminal window.213 moveCursor(out, r.linesRendered, 0)215 out.MoveCursor(r.linesRendered, 0)216 } else {215 cursorBack(out, r.width)217 out.CursorBack(r.width)218 }219218 _, _ = r.out.Write(out.Bytes())220 _, _ = r.out.Write(buf.Bytes())221 r.lastRender = r.buf.String()222 r.buf.Reset()223}224
46 unmodified lines
271 }272273 // Erase ignored lines274 if r.linesRendered > 0 {273 out := new(bytes.Buffer)275 buf := &bytes.Buffer{}276 out := termenv.NewOutput(buf)277278 for i := r.linesRendered - 1; i >= 0; i-- {279 if _, exists := r.ignoreLines[i]; exists {276 clearLine(out)280 out.ClearLine()281 }278 cursorUp(out)282 out.CursorUp(1)283 }280 moveCursor(out, r.linesRendered, 0) // put cursor back281 _, _ = r.out.Write(out.Bytes())284 out.MoveCursor(r.linesRendered, 0) // put cursor back285 _, _ = r.out.Write(buf.Bytes())286 }287}288289// clearIgnoredLines returns control of any ignored lines to the standard
24 unmodified lines
314func (r *standardRenderer) insertTop(lines []string, topBoundary, bottomBoundary int) {315 r.mtx.Lock()316 defer r.mtx.Unlock()317314 b := new(bytes.Buffer)318 buf := &bytes.Buffer{}319 out := termenv.NewOutput(buf)320316 changeScrollingRegion(b, topBoundary, bottomBoundary)317 moveCursor(b, topBoundary, 0)318 insertLine(b, len(lines))319 _, _ = io.WriteString(b, strings.Join(lines, "\r\n"))320 changeScrollingRegion(b, 0, r.height)321 out.ChangeScrollingRegion(topBoundary, bottomBoundary)322 out.MoveCursor(topBoundary, 0)323 out.InsertLines(len(lines))324 _, _ = out.WriteString(strings.Join(lines, "\r\n"))325 out.ChangeScrollingRegion(0, r.height)326327 // Move cursor back to where the main rendering routine expects it to be323 moveCursor(b, r.linesRendered, 0)328 out.MoveCursor(r.linesRendered, 0)329325 _, _ = r.out.Write(b.Bytes())330 _, _ = r.out.Write(buf.Bytes())331}332333// insertBottom effectively scrolls down. It inserts lines at the bottom of334// a given area designated to be a scrollable region, pushing everything else
7 unmodified lines
342func (r *standardRenderer) insertBottom(lines []string, topBoundary, bottomBoundary int) {343 r.mtx.Lock()344 defer r.mtx.Unlock()345341 b := new(bytes.Buffer)346 buf := &bytes.Buffer{}347 out := termenv.NewOutput(buf)348343 changeScrollingRegion(b, topBoundary, bottomBoundary)344 moveCursor(b, bottomBoundary, 0)345 _, _ = io.WriteString(b, "\r\n"+strings.Join(lines, "\r\n"))346 changeScrollingRegion(b, 0, r.height)349 out.ChangeScrollingRegion(topBoundary, bottomBoundary)350 out.MoveCursor(bottomBoundary, 0)351 _, _ = out.WriteString("\r\n" + strings.Join(lines, "\r\n"))352 out.ChangeScrollingRegion(0, r.height)353354 // Move cursor back to where the main rendering routine expects it to be349 moveCursor(b, r.linesRendered, 0)355 out.MoveCursor(r.linesRendered, 0)356351 _, _ = r.out.Write(b.Bytes())357 _, _ = r.out.Write(buf.Bytes())358}359360// handleMessages handles internal messages for the renderer.361func (r *standardRenderer) handleMessages(msg Msg) {
160 unmodified lines
modified:tea.go+32 added-13 removed
21 unmodified lines
2223 "github.com/containerd/console"24 isatty "github.com/mattn/go-isatty"25 "github.com/muesli/cancelreader"26 te "github.com/muesli/termenv"26 "github.com/muesli/termenv"27 "golang.org/x/term"28)2930// Msg contain data from the result of a IO operation. Msgs trigger the update
57 unmodified lines
88 msgs chan Msg89 errs chan error90 readLoopDone chan struct{}9192 output io.Writer // where to send output. this will usually be os.Stdout.93 input io.Reader // this will usually be os.Stdin.94 cancelReader cancelreader.CancelReader92 output *termenv.Output // where to send output. this will usually be os.Stdout.93 restoreOutput func() error94 input io.Reader // this will usually be os.Stdin.95 cancelReader cancelreader.CancelReader9697 renderer renderer98 altScreenActive bool99 altScreenWasActive bool // was the altscreen active before releasing the terminal?
153 unmodified lines
253func NewProgram(model Model, opts ...ProgramOption) *Program {254 p := &Program{255 mtx: &sync.Mutex{},256 initialModel: model,256 output: os.Stdout,257 input: os.Stdin,258 msgs: make(chan Msg),259 CatchPanics: true,260 killc: make(chan bool, 1),
3 unmodified lines
264 for _, opt := range opts {265 opt(p)266 }267268 // if no output was set, set it to stdout269 if p.output == nil {270 p.output = termenv.DefaultOutput()271272 // cache detected color values273 termenv.WithColorCache(true)(p.output)274 }275276 p.restoreOutput, _ = termenv.EnableVirtualTerminalProcessing(p.output)277278 return p279}280281// StartReturningModel initializes the program. Returns the final model.
156 unmodified lines
438 defer close(p.readLoopDone)439 }440 defer p.cancelReader.Close() // nolint:errcheck441432 if f, ok := p.output.(*os.File); ok && isatty.IsTerminal(f.Fd()) {442 if f, ok := p.output.TTY().(*os.File); ok && isatty.IsTerminal(f.Fd()) {443 // Get the initial terminal size and send it to the program.444 go func() {445 w, h, err := term.GetSize(int(f.Fd()))446 if err != nil {
89 unmodified lines
536 p.DisableMouseCellMotion()537 p.DisableMouseAllMotion()538539 case hideCursorMsg:530 hideCursor(p.output)540 p.output.HideCursor()541542 case execMsg:543 // NB: this blocks.544 p.exec(msg.cmd, msg.fn)
70 unmodified lines
615 p.ExitAltScreen()616 p.DisableMouseCellMotion()617 p.DisableMouseAllMotion()618 _ = p.restoreTerminalState()619620 if p.restoreOutput != nil {621 _ = p.restoreOutput()622 }623}624625// EnterAltScreen enters the alternate screen buffer, which consumes the entire626// terminal window. ExitAltScreen will return the terminal to its former state.
6 unmodified lines
633 if p.altScreenActive {634 return635 }636623 enterAltScreen(p.output)637 p.output.AltScreen()638 p.output.MoveCursor(0, 0)639640 p.altScreenActive = true641 if p.renderer != nil {642 p.renderer.setAltScreen(p.altScreenActive)
10 unmodified lines
653 if !p.altScreenActive {654 return655 }656642 exitAltScreen(p.output)657 p.output.ExitAltScreen()658659 p.altScreenActive = false660 if p.renderer != nil {661 p.renderer.setAltScreen(p.altScreenActive)
6 unmodified lines
668// Deprecated: Use the WithMouseCellMotion ProgramOption instead.669func (p *Program) EnableMouseCellMotion() {670 p.mtx.Lock()671 defer p.mtx.Unlock()657 fmt.Fprintf(p.output, te.CSI+te.EnableMouseCellMotionSeq)672673 p.output.EnableMouseCellMotion()674}675676// DisableMouseCellMotion disables Mouse Cell Motion tracking. This will be677// called automatically when exiting a Bubble Tea program.
1 unmodified line
679// Deprecated: The mouse will automatically be disabled when the program exits.680func (p *Program) DisableMouseCellMotion() {681 p.mtx.Lock()682 defer p.mtx.Unlock()667 fmt.Fprintf(p.output, te.CSI+te.DisableMouseCellMotionSeq)683684 p.output.DisableMouseCellMotion()685}686687// EnableMouseAllMotion enables mouse click, release, wheel and motion events,688// regardless of whether a mouse button is pressed. Many modern terminals
2 unmodified lines
691// Deprecated: Use the WithMouseAllMotion ProgramOption instead.692func (p *Program) EnableMouseAllMotion() {693 p.mtx.Lock()694 defer p.mtx.Unlock()678 fmt.Fprintf(p.output, te.CSI+te.EnableMouseAllMotionSeq)695696 p.output.EnableMouseAllMotion()697}698699// DisableMouseAllMotion disables All Motion mouse tracking. This will be700// called automatically when exiting a Bubble Tea program.
1 unmodified line
702// Deprecated: The mouse will automatically be disabled when the program exits.703func (p *Program) DisableMouseAllMotion() {704 p.mtx.Lock()705 defer p.mtx.Unlock()688 fmt.Fprintf(p.output, te.CSI+te.DisableMouseAllMotionSeq)706707 p.output.DisableMouseAllMotion()708}709710// ReleaseTerminal restores the original terminal state and cancels the input711// reader. You can return control to the Program with RestoreTerminal.
68 unmodified lines
modified:tty_windows.go-19 removed
2 unmodified lines
34package tea56import (7 "io"7 "os"89 "github.com/containerd/console"11 "golang.org/x/sys/windows"10)1112func (p *Program) initInput() error {13 // If input's a file, use console to manage it
9 unmodified lines
23 c := console.Current()24 p.console = c25 }2629 enableAnsiColors(p.output)3027 return nil28}2930// restoreInput restores stdout in the event that we placed it aside to handle
13 unmodified lines
44 return nil, err45 }46 return f, nil47}5253// enableAnsiColors enables support for ANSI color sequences in Windows54// default console. Note that this only works with Windows 10.55func enableAnsiColors(w io.Writer) {56 f, ok := w.(*os.File)57 if !ok {58 return59 }6061 stdout := windows.Handle(f.Fd())62 var originalMode uint326364 _ = windows.GetConsoleMode(stdout, &originalMode)65 _ = windows.SetConsoleMode(stdout, originalMode|windows.ENABLE_VIRTUAL_TERMINAL_PROCESSING)66}
modified:tty.go+2 added-2 removed
18 unmodified lines
19 return err20 }21 }2223 hideCursor(p.output)23 p.output.HideCursor()24 return nil25}2627// restoreTerminalState restores the terminal to the state prior to running the28// Bubble Tea program.29func (p Program) restoreTerminalState() error {30 showCursor(p.output)30 p.output.ShowCursor()3132 if p.console != nil {33 err := p.console.Reset()34 if err != nil {
46 unmodified lines