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
Provenance
- Commit
6c449e5- Tree
b20f0cc- Author
Christian Muehlhaeuser <muesli@gmail.com>- Committer
Christian Muehlhaeuser <muesli@gmail.com>- Parent
5c4218e
13 files changed+111 added−219 removed
modified:examples/go.mod+1 added-1 removed
9 unmodified lines10 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 )16 17 replace github.com/charmbracelet/bubbletea => ../modified:examples/go.sum+4 added-5 removed
37 unmodified lines38 github.com/mattn/go-localereader v0.0.1/go.mod h1:8fBrzywKY7BI3czFoHkuzRoWE9C+EiG4R1k4Cjx5p88=39 github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=40 github.com/mattn/go-runewidth v0.0.10/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk=41 github.com/mattn/go-runewidth v0.0.12/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk=42-github.com/mattn/go-runewidth v0.0.13 h1:lTGmDsbAYt5DmK6OnoV7EuIF1wEIFAcxld6ypU4OSgU=42 github.com/mattn/go-runewidth v0.0.13/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=43+github.com/mattn/go-runewidth v0.0.14 h1:+xnbZSEeDbOIg5/mE6JF0w6n9duR1l3/WmbinWVwUuU=44+github.com/mattn/go-runewidth v0.0.14/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=45 github.com/microcosm-cc/bluemonday v1.0.17 h1:Z1a//hgsQ4yjC+8zEkV8IWySkXnsxmdSY642CTFQb5Y=46 github.com/microcosm-cc/bluemonday v1.0.17/go.mod h1:Z0r70sCuXHig8YpBzCc5eGHAap2K7e/u082ZUpDRRqM=47 github.com/muesli/ansi v0.0.0-20211018074035-2e021307bc4b h1:1XF24mVaiu7u+CFywTdcDo2ie1pzzhwjt6RHqzpMU34=48 github.com/muesli/ansi v0.0.0-20211018074035-2e021307bc4b/go.mod h1:fQuZ0gauxyBcmsdE3ZT4NasjaRdxmbCS0jRHsrWu3Ho=4 unmodified lines53 github.com/muesli/reflow v0.3.0/go.mod h1:pbwTDkVPibjO2kyvBQRBxTWEEGDGq0FlB1BIKtnHY/8=54 github.com/muesli/termenv v0.9.0/go.mod h1:R/LzAKf+suGs4IsO95y7+7DpFHO0KABgnZqtlyx2mBw=55 github.com/muesli/termenv v0.11.1-0.20220204035834-5ac8409525e0/go.mod h1:Bd5NYQ7pd+SrtBSrSNoBBmXlcY8+Xj4BMJgh8qcZrvs=56 github.com/muesli/termenv v0.11.1-0.20220212125758-44cd13922739/go.mod h1:Bd5NYQ7pd+SrtBSrSNoBBmXlcY8+Xj4BMJgh8qcZrvs=56-github.com/muesli/termenv v0.12.1-0.20220615005108-4e9068de9898 h1:0j+cbZdhLgpNxjg0nWCasHUA82fgWOXxxGgWNVOLS1I=57-github.com/muesli/termenv v0.12.1-0.20220615005108-4e9068de9898/go.mod h1:bN6sPNtkiahdhHv2Xm6RGU16LSCxfbIZvMfqjOCfrR4=57+github.com/muesli/termenv v0.13.0 h1:wK20DRpJdDX8b7Ek2QfhvqhRQFZ237RGRO0RQ/Iqdy0=58+github.com/muesli/termenv v0.13.0/go.mod h1:sP1+uffeLaEYpyOTb8pLCUctGcGLnoFjSn4YJK5e2bc=59 github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec=60 github.com/olekukonko/tablewriter v0.0.5/go.mod h1:hPp6KlRPjbx+hW8ykQs1w3UBbZlj6HuIJcUGPhkA7kY=61 github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=62 github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=18 unmodified lines81 golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=82 golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=83 golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=84 golang.org/x/sys v0.0.0-20220204135822-1c1b9b1eba6a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=84-golang.org/x/sys v0.0.0-20220209214540-3681064d5158/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=85 golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab h1:2QkjZIsXupsJbJIdSjjUOgWK3aEtzyuh2mPt3l/CkeU=86 golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=87 golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=88 golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 h1:JGgROgKl9N8DuW20oFS5gxc+lE67/N3FcwmBPMe7ArY=1 unmodified line90 golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=91 golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk=92 golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=93 golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=94-gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=94 gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=95 gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=96 gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=modified:tutorials/go.sum+6 added-5 removed
1+github.com/aymanbagabas/go-osc52 v1.0.3 h1:DTwqENW7X9arYimJrPeGZcV0ln14sGMt3pHZspWD+Mg=2+github.com/aymanbagabas/go-osc52 v1.0.3/go.mod h1:zT8H+Rk4VSabYN90pWyugflM3ZhpTZNC7cASDfUCdT4=3 github.com/containerd/console v1.0.3 h1:lIr7SlA5PxZyMV30bDW0MGbiOPXwc63yRuCP0ARubLw=4 github.com/containerd/console v1.0.3/go.mod h1:7LqA/THxQ86k76b8c/EMSiaJ3h1eZkMkXar0TQ1gf3U=5 github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=6 github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=5-github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=7 github.com/mattn/go-isatty v0.0.16 h1:bq3VjFmv/sOjHtdEhmkEV4x1AJtvUvOJ2PFAZ5+peKQ=8 github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=9 github.com/mattn/go-localereader v0.0.1 h1:ygSAOl7ZXTx4RdPYinUpg6W99U8jWvWi9Ye2JC/oIi4=10 github.com/mattn/go-localereader v0.0.1/go.mod h1:8fBrzywKY7BI3czFoHkuzRoWE9C+EiG4R1k4Cjx5p88=11 github.com/mattn/go-runewidth v0.0.12/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk=11-github.com/mattn/go-runewidth v0.0.13 h1:lTGmDsbAYt5DmK6OnoV7EuIF1wEIFAcxld6ypU4OSgU=12 github.com/mattn/go-runewidth v0.0.13/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=13+github.com/mattn/go-runewidth v0.0.14 h1:+xnbZSEeDbOIg5/mE6JF0w6n9duR1l3/WmbinWVwUuU=14+github.com/mattn/go-runewidth v0.0.14/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=15 github.com/muesli/ansi v0.0.0-20211018074035-2e021307bc4b h1:1XF24mVaiu7u+CFywTdcDo2ie1pzzhwjt6RHqzpMU34=16 github.com/muesli/ansi v0.0.0-20211018074035-2e021307bc4b/go.mod h1:fQuZ0gauxyBcmsdE3ZT4NasjaRdxmbCS0jRHsrWu3Ho=17 github.com/muesli/cancelreader v0.2.2 h1:3I4Kt4BQjOR54NavqnDogx/MIoWBFa0StPA8ELUXHmA=18 github.com/muesli/cancelreader v0.2.2/go.mod h1:3XuTXfFS2VjM+HTLZY9Ak0l6eUKfijIfMUZ4EgX0QYo=19 github.com/muesli/reflow v0.3.0 h1:IFsN6K9NfGtjeggFP+68I4chLZV2yIKsXJFNZ+eWh6s=20 github.com/muesli/reflow v0.3.0/go.mod h1:pbwTDkVPibjO2kyvBQRBxTWEEGDGq0FlB1BIKtnHY/8=19-github.com/muesli/termenv v0.11.1-0.20220212125758-44cd13922739 h1:QANkGiGr39l1EESqrE0gZw0/AJNYzIvoGLhIoVYtluI=20-github.com/muesli/termenv v0.11.1-0.20220212125758-44cd13922739/go.mod h1:Bd5NYQ7pd+SrtBSrSNoBBmXlcY8+Xj4BMJgh8qcZrvs=21+github.com/muesli/termenv v0.13.0 h1:wK20DRpJdDX8b7Ek2QfhvqhRQFZ237RGRO0RQ/Iqdy0=22+github.com/muesli/termenv v0.13.0/go.mod h1:sP1+uffeLaEYpyOTb8pLCUctGcGLnoFjSn4YJK5e2bc=23 github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=24 github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=25 github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=26 golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=27 golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=26-golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=28 golang.org/x/sys v0.0.0-20220204135822-1c1b9b1eba6a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=29 golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab h1:2QkjZIsXupsJbJIdSjjUOgWK3aEtzyuh2mPt3l/CkeU=30 golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=31 golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 h1:JGgROgKl9N8DuW20oFS5gxc+lE67/N3FcwmBPMe7ArY=4 unmodified linesmodified:go.mod+1 added-2 removed
7 unmodified lines8 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
1+github.com/aymanbagabas/go-osc52 v1.0.3 h1:DTwqENW7X9arYimJrPeGZcV0ln14sGMt3pHZspWD+Mg=2+github.com/aymanbagabas/go-osc52 v1.0.3/go.mod h1:zT8H+Rk4VSabYN90pWyugflM3ZhpTZNC7cASDfUCdT4=3 github.com/containerd/console v1.0.3 h1:lIr7SlA5PxZyMV30bDW0MGbiOPXwc63yRuCP0ARubLw=4 github.com/containerd/console v1.0.3/go.mod h1:7LqA/THxQ86k76b8c/EMSiaJ3h1eZkMkXar0TQ1gf3U=5 github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=6 github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=5-github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=7 github.com/mattn/go-isatty v0.0.16 h1:bq3VjFmv/sOjHtdEhmkEV4x1AJtvUvOJ2PFAZ5+peKQ=8 github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=9 github.com/mattn/go-localereader v0.0.1 h1:ygSAOl7ZXTx4RdPYinUpg6W99U8jWvWi9Ye2JC/oIi4=10 github.com/mattn/go-localereader v0.0.1/go.mod h1:8fBrzywKY7BI3czFoHkuzRoWE9C+EiG4R1k4Cjx5p88=11 github.com/mattn/go-runewidth v0.0.12/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk=11-github.com/mattn/go-runewidth v0.0.13 h1:lTGmDsbAYt5DmK6OnoV7EuIF1wEIFAcxld6ypU4OSgU=12 github.com/mattn/go-runewidth v0.0.13/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=13+github.com/mattn/go-runewidth v0.0.14 h1:+xnbZSEeDbOIg5/mE6JF0w6n9duR1l3/WmbinWVwUuU=14+github.com/mattn/go-runewidth v0.0.14/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=15 github.com/muesli/ansi v0.0.0-20211018074035-2e021307bc4b h1:1XF24mVaiu7u+CFywTdcDo2ie1pzzhwjt6RHqzpMU34=16 github.com/muesli/ansi v0.0.0-20211018074035-2e021307bc4b/go.mod h1:fQuZ0gauxyBcmsdE3ZT4NasjaRdxmbCS0jRHsrWu3Ho=17 github.com/muesli/cancelreader v0.2.2 h1:3I4Kt4BQjOR54NavqnDogx/MIoWBFa0StPA8ELUXHmA=18 github.com/muesli/cancelreader v0.2.2/go.mod h1:3XuTXfFS2VjM+HTLZY9Ak0l6eUKfijIfMUZ4EgX0QYo=19 github.com/muesli/reflow v0.3.0 h1:IFsN6K9NfGtjeggFP+68I4chLZV2yIKsXJFNZ+eWh6s=20 github.com/muesli/reflow v0.3.0/go.mod h1:pbwTDkVPibjO2kyvBQRBxTWEEGDGq0FlB1BIKtnHY/8=19-github.com/muesli/termenv v0.11.1-0.20220212125758-44cd13922739 h1:QANkGiGr39l1EESqrE0gZw0/AJNYzIvoGLhIoVYtluI=20-github.com/muesli/termenv v0.11.1-0.20220212125758-44cd13922739/go.mod h1:Bd5NYQ7pd+SrtBSrSNoBBmXlcY8+Xj4BMJgh8qcZrvs=21+github.com/muesli/termenv v0.13.0 h1:wK20DRpJdDX8b7Ek2QfhvqhRQFZ237RGRO0RQ/Iqdy0=22+github.com/muesli/termenv v0.13.0/go.mod h1:sP1+uffeLaEYpyOTb8pLCUctGcGLnoFjSn4YJK5e2bc=23 github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=24 github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=25 github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=26 golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=27 golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=26-golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=28 golang.org/x/sys v0.0.0-20220204135822-1c1b9b1eba6a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=29 golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab h1:2QkjZIsXupsJbJIdSjjUOgWK3aEtzyuh2mPt3l/CkeU=30 golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=31 golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 h1:JGgROgKl9N8DuW20oFS5gxc+lE67/N3FcwmBPMe7ArY=4 unmodified linesmodified:options_test.go+2 added-2 removed
7 unmodified lines8 func 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 })16 17 t.Run("input", func(t *testing.T) {77 unmodified linesmodified:options.go+14 added-10 removed
1 package tea2 3-import "io"3+import (4+ "io"5 6+ "github.com/muesli/termenv"7+)8+9 // ProgramOption is used to set options when initializing a Program. Program can10 // accept a variable number of options.11 //12 // Example usage:3 unmodified lines16 17 // WithOutput sets the output which, by default, is stdout. In most cases you18 // won't need to use this.19 func 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 }24 25 // WithInput sets the input which, by default, is stdin. In most cases you26 // won't need to use this.27 func 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 }33 34 // WithInputTTY open a new TTY for input (or console input device on Windows).7 unmodified lines42 // 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.45 func WithoutCatchPanics() ProgramOption {42- return func(m *Program) {43- m.CatchPanics = false46+ return func(p *Program) {47+ p.CatchPanics = false48 }49 }50 51 // WithAltScreen starts the program with the alternate screen buffer enabled65 unmodified lines117 // 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.120 func WithoutRenderer() ProgramOption {117- return func(m *Program) {118- m.renderer = &nilRenderer{}121+ return func(p *Program) {122+ p.renderer = &nilRenderer{}123 }124 }125 126 // WithANSICompressor removes redundant ANSI sequences to produce potentially9 unmodified linesdeleted:screen_test.go-65 removed
1-package tea2-3-import (4- "bytes"5- "io"6- "testing"7-)8-9-func 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- }17-18- 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- })23-24- t.Run("line", func(t *testing.T) {25- t.Run("clear", func(t *testing.T) {26- exercise(t, clearLine, []byte("\x1b[2K"))27- })28-29- t.Run("insert", func(t *testing.T) {30- exercise(t, func(w io.Writer) {31- insertLine(w, 12)32- }, []byte("\x1b[12L"))33- })34- })35-36- t.Run("cursor", func(t *testing.T) {37- t.Run("hide", func(t *testing.T) {38- exercise(t, hideCursor, []byte("\x1b[?25l"))39- })40-41- t.Run("show", func(t *testing.T) {42- exercise(t, showCursor, []byte("\x1b[?25h"))43- })44-45- t.Run("up", func(t *testing.T) {46- exercise(t, cursorUp, []byte("\x1b[1A"))47- })48-49- t.Run("down", func(t *testing.T) {50- exercise(t, cursorDown, []byte("\x1b[1B"))51- })52-53- 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- })58-59- 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
1-package tea2-3-import (4- "fmt"5- "io"6-7- te "github.com/muesli/termenv"8-)9-10-func hideCursor(w io.Writer) {11- fmt.Fprintf(w, te.CSI+te.HideCursorSeq)12-}13-14-func showCursor(w io.Writer) {15- fmt.Fprintf(w, te.CSI+te.ShowCursorSeq)16-}17-18-func clearLine(w io.Writer) {19- fmt.Fprintf(w, te.CSI+te.EraseLineSeq, 2)20-}21-22-func cursorUp(w io.Writer) {23- fmt.Fprintf(w, te.CSI+te.CursorUpSeq, 1)24-}25-26-func cursorDown(w io.Writer) {27- fmt.Fprintf(w, te.CSI+te.CursorDownSeq, 1)28-}29-30-func insertLine(w io.Writer, numLines int) {31- fmt.Fprintf(w, te.CSI+"%dL", numLines)32-}33-34-func moveCursor(w io.Writer, row, col int) {35- fmt.Fprintf(w, te.CSI+te.CursorPositionSeq, row, col)36-}37-38-func changeScrollingRegion(w io.Writer, top, bottom int) {39- fmt.Fprintf(w, te.CSI+te.ChangeScrollingRegionSeq, top, bottom)40-}41-42-func cursorBack(w io.Writer, n int) {43- fmt.Fprintf(w, te.CSI+te.CursorBackSeq, n)44-}45-46-func enterAltScreen(w io.Writer) {47- fmt.Fprintf(w, te.CSI+te.AltScreenSeq)48- moveCursor(w, 0, 0)49-}50-51-func exitAltScreen(w io.Writer) {52- fmt.Fprintf(w, te.CSI+te.ExitAltScreenSeq)53-}modified:standard_renderer.go+43 added-37 removed
8 unmodified lines9 "time"10 11 "github.com/muesli/ansi/compressor"12 "github.com/muesli/reflow/truncate"13+ "github.com/muesli/termenv"14 )15 16 const (17 // defaultFramerate specifies the maximum interval at which we should6 unmodified lines24 //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.27 type standardRenderer struct {27- out io.Writer28+ out *termenv.Output29 buf bytes.Buffer30 queuedMessageLines []string31 framerate time.Duration32 ticker *time.Ticker16 unmodified lines49 }50 51 // newRenderer creates a new renderer. Normally you'll want to initialize it52 // with os.Stdout as the first argument.52-func newRenderer(out io.Writer, mtx *sync.Mutex, useANSICompressor bool) renderer {53+func 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 lines75 76 // stop permanently halts the renderer, rendering the final frame.77 func (r *standardRenderer) stop() {78 r.flush()78- clearLine(r.out)79+ r.out.ClearLine()80 r.once.Do(func() {81 close(r.done)82 })83 84 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 }90 91 // kill halts the renderer. The final frame will not be rendered.92 func (r *standardRenderer) kill() {92- clearLine(r.out)93+ r.out.ClearLine()94 r.once.Do(func() {95 close(r.done)96 })97 }24 unmodified lines122 return123 }124 125 // Output buffer125- out := new(bytes.Buffer)126+ buf := &bytes.Buffer{}127+ out := termenv.NewOutput(buf)128 129 newLines := strings.Split(r.buf.String(), "\n")130 numLinesThisFlush := len(newLines)131 oldLines := strings.Split(r.lastRender, "\n")14 unmodified lines146 // 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 }152 151- cursorUp(out)153+ out.CursorUp(1)154 }155 156 if _, exists := r.ignoreLines[0]; !exists {157 // We need to return to the start of the line here to properly4 unmodified lines162 // 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 }170 171 // Merge the set of lines we're skipping as a rendering optimization with8 unmodified lines180 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 lines196 if r.width > 0 {197 line = truncate.String(line, uint(r.width))198 }199 198- _, _ = io.WriteString(out, line)200+ _, _ = out.WriteString(line)201 202 if i < len(newLines)-1 {201- _, _ = io.WriteString(out, "\r\n")203+ _, _ = out.WriteString("\r\n")204 }205 }206 }207 r.linesRendered = numLinesThisFlush3 unmodified lines211 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 }219 218- _, _ = 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 lines271 }272 273 // Erase ignored lines274 if r.linesRendered > 0 {273- out := new(bytes.Buffer)275+ buf := &bytes.Buffer{}276+ out := termenv.NewOutput(buf)277+278 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 }288 289 // clearIgnoredLines returns control of any ignored lines to the standard24 unmodified lines314 func (r *standardRenderer) insertTop(lines []string, topBoundary, bottomBoundary int) {315 r.mtx.Lock()316 defer r.mtx.Unlock()317 314- b := new(bytes.Buffer)318+ buf := &bytes.Buffer{}319+ out := termenv.NewOutput(buf)320 316- 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)326 327 // Move cursor back to where the main rendering routine expects it to be323- moveCursor(b, r.linesRendered, 0)328+ out.MoveCursor(r.linesRendered, 0)329 325- _, _ = r.out.Write(b.Bytes())330+ _, _ = r.out.Write(buf.Bytes())331 }332 333 // insertBottom effectively scrolls down. It inserts lines at the bottom of334 // a given area designated to be a scrollable region, pushing everything else7 unmodified lines342 func (r *standardRenderer) insertBottom(lines []string, topBoundary, bottomBoundary int) {343 r.mtx.Lock()344 defer r.mtx.Unlock()345 341- b := new(bytes.Buffer)346+ buf := &bytes.Buffer{}347+ out := termenv.NewOutput(buf)348 343- 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)353 354 // Move cursor back to where the main rendering routine expects it to be349- moveCursor(b, r.linesRendered, 0)355+ out.MoveCursor(r.linesRendered, 0)356 351- _, _ = r.out.Write(b.Bytes())357+ _, _ = r.out.Write(buf.Bytes())358 }359 360 // handleMessages handles internal messages for the renderer.361 func (r *standardRenderer) handleMessages(msg Msg) {160 unmodified linesmodified:tea.go+32 added-13 removed
21 unmodified lines22 23 "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 )29 30 // Msg contain data from the result of a IO operation. Msgs trigger the update57 unmodified lines88 msgs chan Msg89 errs chan error90 readLoopDone chan struct{}91 92- 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.CancelReader96 97 renderer renderer98 altScreenActive bool99 altScreenWasActive bool // was the altscreen active before releasing the terminal?153 unmodified lines253 func 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 lines264 for _, opt := range opts {265 opt(p)266 }267 268+ // if no output was set, set it to stdout269+ if p.output == nil {270+ p.output = termenv.DefaultOutput()271+272+ // cache detected color values273+ termenv.WithColorCache(true)(p.output)274+ }275+276+ p.restoreOutput, _ = termenv.EnableVirtualTerminalProcessing(p.output)277+278 return p279 }280 281 // StartReturningModel initializes the program. Returns the final model.156 unmodified lines438 defer close(p.readLoopDone)439 }440 defer p.cancelReader.Close() // nolint:errcheck441 432- 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 lines536 p.DisableMouseCellMotion()537 p.DisableMouseAllMotion()538 539 case hideCursorMsg:530- hideCursor(p.output)540+ p.output.HideCursor()541 542 case execMsg:543 // NB: this blocks.544 p.exec(msg.cmd, msg.fn)70 unmodified lines615 p.ExitAltScreen()616 p.DisableMouseCellMotion()617 p.DisableMouseAllMotion()618 _ = p.restoreTerminalState()619+620+ if p.restoreOutput != nil {621+ _ = p.restoreOutput()622+ }623 }624 625 // EnterAltScreen enters the alternate screen buffer, which consumes the entire626 // terminal window. ExitAltScreen will return the terminal to its former state.6 unmodified lines633 if p.altScreenActive {634 return635 }636 623- enterAltScreen(p.output)637+ p.output.AltScreen()638+ p.output.MoveCursor(0, 0)639 640 p.altScreenActive = true641 if p.renderer != nil {642 p.renderer.setAltScreen(p.altScreenActive)10 unmodified lines653 if !p.altScreenActive {654 return655 }656 642- exitAltScreen(p.output)657+ p.output.ExitAltScreen()658 659 p.altScreenActive = false660 if p.renderer != nil {661 p.renderer.setAltScreen(p.altScreenActive)6 unmodified lines668 // Deprecated: Use the WithMouseCellMotion ProgramOption instead.669 func (p *Program) EnableMouseCellMotion() {670 p.mtx.Lock()671 defer p.mtx.Unlock()657- fmt.Fprintf(p.output, te.CSI+te.EnableMouseCellMotionSeq)672+673+ p.output.EnableMouseCellMotion()674 }675 676 // DisableMouseCellMotion disables Mouse Cell Motion tracking. This will be677 // called automatically when exiting a Bubble Tea program.1 unmodified line679 // Deprecated: The mouse will automatically be disabled when the program exits.680 func (p *Program) DisableMouseCellMotion() {681 p.mtx.Lock()682 defer p.mtx.Unlock()667- fmt.Fprintf(p.output, te.CSI+te.DisableMouseCellMotionSeq)683+684+ p.output.DisableMouseCellMotion()685 }686 687 // EnableMouseAllMotion enables mouse click, release, wheel and motion events,688 // regardless of whether a mouse button is pressed. Many modern terminals2 unmodified lines691 // Deprecated: Use the WithMouseAllMotion ProgramOption instead.692 func (p *Program) EnableMouseAllMotion() {693 p.mtx.Lock()694 defer p.mtx.Unlock()678- fmt.Fprintf(p.output, te.CSI+te.EnableMouseAllMotionSeq)695+696+ p.output.EnableMouseAllMotion()697 }698 699 // DisableMouseAllMotion disables All Motion mouse tracking. This will be700 // called automatically when exiting a Bubble Tea program.1 unmodified line702 // Deprecated: The mouse will automatically be disabled when the program exits.703 func (p *Program) DisableMouseAllMotion() {704 p.mtx.Lock()705 defer p.mtx.Unlock()688- fmt.Fprintf(p.output, te.CSI+te.DisableMouseAllMotionSeq)706+707+ p.output.DisableMouseAllMotion()708 }709 710 // ReleaseTerminal restores the original terminal state and cancels the input711 // reader. You can return control to the Program with RestoreTerminal.68 unmodified linesmodified:tty_windows.go-19 removed
2 unmodified lines3 4 package tea5 6 import (7- "io"7 "os"8 9 "github.com/containerd/console"11- "golang.org/x/sys/windows"10 )11 12 func (p *Program) initInput() error {13 // If input's a file, use console to manage it9 unmodified lines23 c := console.Current()24 p.console = c25 }26 29- enableAnsiColors(p.output)30-27 return nil28 }29 30 // restoreInput restores stdout in the event that we placed it aside to handle13 unmodified lines44 return nil, err45 }46 return f, nil47 }52-53-// enableAnsiColors enables support for ANSI color sequences in Windows54-// default console. Note that this only works with Windows 10.55-func enableAnsiColors(w io.Writer) {56- f, ok := w.(*os.File)57- if !ok {58- return59- }60-61- stdout := windows.Handle(f.Fd())62- var originalMode uint3263-64- _ = windows.GetConsoleMode(stdout, &originalMode)65- _ = windows.SetConsoleMode(stdout, originalMode|windows.ENABLE_VIRTUAL_TERMINAL_PROCESSING)66-}modified:tty.go+2 added-2 removed
18 unmodified lines19 return err20 }21 }22 23- hideCursor(p.output)23+ p.output.HideCursor()24 return nil25 }26 27 // restoreTerminalState restores the terminal to the state prior to running the28 // Bubble Tea program.29 func (p Program) restoreTerminalState() error {30- showCursor(p.output)30+ p.output.ShowCursor()31 32 if p.console != nil {33 err := p.console.Reset()34 if err != nil {46 unmodified lines