Skip to content
Myr

refactor: use x/term and x/ansi for renderer sequences (#962)

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

* refactor: use x/exp/term for renderer sequences This replaces termenv.Output with raw sequences and a bytes.Buffer to handle vt screen and cursor events. * chore: update dependencies * refactor: use x/term

authored by Ayman Bagabas · committed by GitHub · parent 09f4ef0 · Browse files · 20 files+216 added−191 removed
This commit

State

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

Diff stats

20 files+216 added−191 removed

  • other:+216 added−191 removed

20 files+216 added−191 removed

modified:examples/simple/testdata/TestApp.golden+2 added-2 removed
1[?25l[?2004h Hi. This program will exit in 10 seconds. To quit sooner press any key2Hi. This program will exit in 9 seconds. To quit sooner press any key.1[?25l[?2004h Hi. This program will exit in 10 seconds. To quit sooner press any key2Hi. This program will exit in 9 seconds. To quit sooner press any key.3[?2004l[?25h[?1002l[?1003l[?1006l
modified:examples/go.mod+9 added-5 removed
2 unmodified lines
3go 1.1845require (6 github.com/charmbracelet/bubbles v0.18.07 github.com/charmbracelet/bubbletea v0.25.07 github.com/charmbracelet/bubbletea v0.26.28 github.com/charmbracelet/glamour v0.7.09 github.com/charmbracelet/harmonica v0.2.010 github.com/charmbracelet/lipgloss v0.10.011 github.com/charmbracelet/x/exp/teatest v0.0.0-20240229115032-4b79243a3516
7 unmodified lines
19 github.com/atotto/clipboard v0.1.4 // indirect20 github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect21 github.com/aymanbagabas/go-udiff v0.2.0 // indirect22 github.com/aymerick/douceur v0.2.0 // indirect23 github.com/charmbracelet/x/exp/golden v0.0.0-20240222125807-0344fda748f8 // indirect23 github.com/charmbracelet/x/ansi v0.1.1 // indirect24 github.com/charmbracelet/x/exp/golden v0.0.0-20240521172236-71f88323a7ca // indirect25 github.com/charmbracelet/x/input v0.1.0 // indirect26 github.com/charmbracelet/x/term v0.1.1 // indirect27 github.com/charmbracelet/x/windows v0.1.0 // indirect28 github.com/dlclark/regexp2 v1.4.0 // indirect29 github.com/dustin/go-humanize v1.0.1 // indirect30 github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f // indirect31 github.com/gorilla/css v1.0.0 // indirect
6 unmodified lines
38 github.com/muesli/termenv v0.15.2 // indirect39 github.com/olekukonko/tablewriter v0.0.5 // indirect40 github.com/rivo/uniseg v0.4.7 // indirect41 github.com/sahilm/fuzzy v0.1.1-0.20230530133925-c48e322e2a8f // indirect42 github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect43 github.com/yuin/goldmark v1.5.4 // indirect44 github.com/yuin/goldmark-emoji v1.0.2 // indirect45 golang.org/x/net v0.23.0 // indirect41 golang.org/x/sync v0.6.0 // indirect42 golang.org/x/sys v0.18.0 // indirect43 golang.org/x/term v0.18.0 // indirect46 golang.org/x/sync v0.7.0 // indirect47 golang.org/x/sys v0.20.0 // indirect48 golang.org/x/text v0.14.0 // indirect49)5051replace github.com/charmbracelet/bubbletea => ../
modified:examples/go.sum+17 added-8 removed
17 unmodified lines
18github.com/charmbracelet/harmonica v0.2.0 h1:8NxJWRWg/bzKqqEaaeFNipOu77YR5t8aSwG4pgaUBiQ=19github.com/charmbracelet/harmonica v0.2.0/go.mod h1:KSri/1RMQOZLbw7AHqgcBycp8pgJnQMYYT8QZRqZ1Ao=20github.com/charmbracelet/lipgloss v0.10.0 h1:KWeXFSexGcfahHX+54URiZGkBFazf70JNMtwg/AFW3s=21github.com/charmbracelet/lipgloss v0.10.0/go.mod h1:Wig9DSfvANsxqkRsqj6x87irdy123SR4dOXlKa91ciE=22github.com/charmbracelet/x/exp/golden v0.0.0-20240222125807-0344fda748f8 h1:kyT+aGp1z5jwlus3OY0cP6FuT05jYeeExx/4TYxnyrs=23github.com/charmbracelet/x/exp/golden v0.0.0-20240222125807-0344fda748f8/go.mod h1:wDlXFlCrmJ8J+swcL/MnGUuYnqgQdW9rhSD61oNMb6U=22github.com/charmbracelet/x/ansi v0.1.1 h1:CGAduulr6egay/YVbGc8Hsu8deMg1xZ/bkaXTPi1JDk=23github.com/charmbracelet/x/ansi v0.1.1/go.mod h1:dk73KoMTT5AX5BsX0KrqhsTqAnhZZoCBjs7dGWp4Ktw=24github.com/charmbracelet/x/exp/golden v0.0.0-20240521172236-71f88323a7ca h1:Cw9p8EJdhDGIWICF34TIxTcQrAdzBdgkvaLA4AmqDVk=25github.com/charmbracelet/x/exp/golden v0.0.0-20240521172236-71f88323a7ca/go.mod h1:wDlXFlCrmJ8J+swcL/MnGUuYnqgQdW9rhSD61oNMb6U=26github.com/charmbracelet/x/exp/teatest v0.0.0-20240229115032-4b79243a3516 h1:7IZFEUZpEgjlTSd7P1MRRhGXs7t4F6mENeMw17TxnQs=27github.com/charmbracelet/x/exp/teatest v0.0.0-20240229115032-4b79243a3516/go.mod h1:SG24wGkG/mix5V2dZLXfQ6Bod43HGvk9CkTDxATwKN4=28github.com/charmbracelet/x/input v0.1.0 h1:TEsGSfZYQyOtp+STIjyBq6tpRaorH0qpwZUj8DavAhQ=29github.com/charmbracelet/x/input v0.1.0/go.mod h1:ZZwaBxPF7IG8gWWzPUVqHEtWhc1+HXJPNuerJGRGZ28=30github.com/charmbracelet/x/term v0.1.1 h1:3cosVAiPOig+EV4X9U+3LDgtwwAoEzJjNdwbXDjF6yI=31github.com/charmbracelet/x/term v0.1.1/go.mod h1:wB1fHt5ECsu3mXYusyzcngVWWlu1KKUmmLhfgr/Flxw=32github.com/charmbracelet/x/windows v0.1.0 h1:gTaxdvzDM5oMa/I2ZNF7wN78X/atWemG9Wph7Ika2k4=33github.com/charmbracelet/x/windows v0.1.0/go.mod h1:GLEO/l+lizvFDBPLIOk+49gdX49L9YWMB5t+DZd0jkQ=34github.com/dlclark/regexp2 v1.4.0 h1:F1rxgk7p4uKjwIQxBs9oAXe5CqrXlCduYEJvrF4u93E=35github.com/dlclark/regexp2 v1.4.0/go.mod h1:2pZnwuY/m+8K6iRw6wQdMtk+rH5tNGR1i55kozfMjCc=36github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=37github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
32 unmodified lines
70github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=71github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=72github.com/sahilm/fuzzy v0.1.1-0.20230530133925-c48e322e2a8f h1:MvTmaQdww/z0Q4wrYjDSCcZ78NoftLQyHBSLW/Cx79Y=73github.com/sahilm/fuzzy v0.1.1-0.20230530133925-c48e322e2a8f/go.mod h1:VFvziUEIMCrT6A6tw2RFIXPXXmzXbOsSHF0DOI8ZK9Y=74github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e h1:JVG44RsyaB9T2KIHavMF/ppJZNG9ZpyihvCd0w101no=75github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e/go.mod h1:RbqR21r5mrJuqunuUZ/Dhy/avygyECGrLceyNeo4LiM=76github.com/yuin/goldmark v1.3.7/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=77github.com/yuin/goldmark v1.5.4 h1:2uY/xC0roWy8IBEGLgB1ywIoEJFGmRrX21YQcvGZzjU=78github.com/yuin/goldmark v1.5.4/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=79github.com/yuin/goldmark-emoji v1.0.2 h1:c/RgTShNgHTtc6xdz2KKI74jJr6rWi7FPgnP9GAsO5s=80github.com/yuin/goldmark-emoji v1.0.2/go.mod h1:RhP/RWpexdp+KHs7ghKnifRoIs/Bq4nDS7tRbCkOwKY=81golang.org/x/exp v0.0.0-20220909182711-5c715a9e8561 h1:MDc5xs78ZrZr3HMQugiXOAkSZtfTpbJLDr/lwfgO53E=82golang.org/x/net v0.23.0 h1:7EYJ93RZ9vYSZAIb2x3lnuvqO5zneoD6IvWjuhfxjTs=83golang.org/x/net v0.23.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg=73golang.org/x/sync v0.6.0 h1:5BMeUDZ7vkXGfEr1x9B4bRcTH4lpkTkpdh0T/J+qjbQ=74golang.org/x/sync v0.6.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=84golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M=85golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=86golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=87golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=77golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4=78golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=79golang.org/x/term v0.18.0 h1:FcHjZXDMxI8mM3nwhX9HlKop4C0YQvCVCdwYl2wOtE8=80golang.org/x/term v0.18.0/go.mod h1:ILwASektA3OnRv7amZ1xhE/KTR+u50pbXfZ03+6Nx58=88golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y=89golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=90golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=91golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
modified:tutorials/go.mod+8 added-9 removed
3 unmodified lines
45require github.com/charmbracelet/bubbletea v0.25.067require (8 github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect8 github.com/charmbracelet/x/ansi v0.1.1 // indirect9 github.com/charmbracelet/x/input v0.1.0 // indirect10 github.com/charmbracelet/x/term v0.1.1 // indirect11 github.com/charmbracelet/x/windows v0.1.0 // indirect12 github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f // indirect10 github.com/lucasb-eyer/go-colorful v1.2.0 // indirect11 github.com/mattn/go-isatty v0.0.20 // indirect13 github.com/mattn/go-localereader v0.0.1 // indirect14 github.com/mattn/go-runewidth v0.0.15 // indirect15 github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6 // indirect16 github.com/muesli/cancelreader v0.2.2 // indirect16 github.com/muesli/reflow v0.3.0 // indirect17 github.com/muesli/termenv v0.15.2 // indirect18 github.com/rivo/uniseg v0.4.6 // indirect19 golang.org/x/sync v0.6.0 // indirect20 golang.org/x/sys v0.17.0 // indirect21 golang.org/x/term v0.17.0 // indirect17 github.com/rivo/uniseg v0.4.7 // indirect18 github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect19 golang.org/x/sync v0.7.0 // indirect20 golang.org/x/sys v0.20.0 // indirect21 golang.org/x/text v0.13.0 // indirect22)2324replace github.com/charmbracelet/bubbletea => ../
modified:tutorials/go.sum+17 added-21 removed
1github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiErDT4WkJ2k=2github.com/aymanbagabas/go-osc52/v2 v2.0.1/go.mod h1:uYgXzlJ7ZpABp8OJ+exZzJJhRNQ2ASbcXHWsFqH8hp8=1github.com/charmbracelet/x/ansi v0.1.1 h1:CGAduulr6egay/YVbGc8Hsu8deMg1xZ/bkaXTPi1JDk=2github.com/charmbracelet/x/ansi v0.1.1/go.mod h1:dk73KoMTT5AX5BsX0KrqhsTqAnhZZoCBjs7dGWp4Ktw=3github.com/charmbracelet/x/input v0.1.0 h1:TEsGSfZYQyOtp+STIjyBq6tpRaorH0qpwZUj8DavAhQ=4github.com/charmbracelet/x/input v0.1.0/go.mod h1:ZZwaBxPF7IG8gWWzPUVqHEtWhc1+HXJPNuerJGRGZ28=5github.com/charmbracelet/x/term v0.1.1 h1:3cosVAiPOig+EV4X9U+3LDgtwwAoEzJjNdwbXDjF6yI=6github.com/charmbracelet/x/term v0.1.1/go.mod h1:wB1fHt5ECsu3mXYusyzcngVWWlu1KKUmmLhfgr/Flxw=7github.com/charmbracelet/x/windows v0.1.0 h1:gTaxdvzDM5oMa/I2ZNF7wN78X/atWemG9Wph7Ika2k4=8github.com/charmbracelet/x/windows v0.1.0/go.mod h1:GLEO/l+lizvFDBPLIOk+49gdX49L9YWMB5t+DZd0jkQ=9github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f h1:Y/CXytFA4m6baUTXGLOoWe4PQhGxaX0KpnayAqC48p4=10github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f/go.mod h1:vw97MGsxSvLiUE2X8qFplwetxpGLQrlU1Q9AUEIzCaM=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=7github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=8github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=11github.com/mattn/go-localereader v0.0.1 h1:ygSAOl7ZXTx4RdPYinUpg6W99U8jWvWi9Ye2JC/oIi4=12github.com/mattn/go-localereader v0.0.1/go.mod h1:8fBrzywKY7BI3czFoHkuzRoWE9C+EiG4R1k4Cjx5p88=11github.com/mattn/go-runewidth v0.0.12/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk=13github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U=14github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=15github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6 h1:ZK8zHtRHOkbHy6Mmr5D264iyp3TiX5OmNcI5cIARiQI=16github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6/go.mod h1:CJlz5H+gyd6CUWT45Oy4q24RdLyn7Md9Vj2/ldJBSIo=17github.com/muesli/cancelreader v0.2.2 h1:3I4Kt4BQjOR54NavqnDogx/MIoWBFa0StPA8ELUXHmA=18github.com/muesli/cancelreader v0.2.2/go.mod h1:3XuTXfFS2VjM+HTLZY9Ak0l6eUKfijIfMUZ4EgX0QYo=18github.com/muesli/reflow v0.3.0 h1:IFsN6K9NfGtjeggFP+68I4chLZV2yIKsXJFNZ+eWh6s=19github.com/muesli/reflow v0.3.0/go.mod h1:pbwTDkVPibjO2kyvBQRBxTWEEGDGq0FlB1BIKtnHY/8=20github.com/muesli/termenv v0.15.2 h1:GohcuySI0QmI3wN8Ok9PtKGkgkFIk7y6Vpb5PvrY+Wo=21github.com/muesli/termenv v0.15.2/go.mod h1:Epx+iuz8sNs7mNKhxzH4fWXGNpZwUaJKRS1noLXviQ8=22github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=19github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=24github.com/rivo/uniseg v0.4.6 h1:Sovz9sDSwbOz9tgUy8JpT+KgCkPYJEN/oYzlJiYTNLg=25github.com/rivo/uniseg v0.4.6/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=26golang.org/x/sync v0.6.0 h1:5BMeUDZ7vkXGfEr1x9B4bRcTH4lpkTkpdh0T/J+qjbQ=27golang.org/x/sync v0.6.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=20github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=21github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=22github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e h1:JVG44RsyaB9T2KIHavMF/ppJZNG9ZpyihvCd0w101no=23github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e/go.mod h1:RbqR21r5mrJuqunuUZ/Dhy/avygyECGrLceyNeo4LiM=24golang.org/x/exp v0.0.0-20220909182711-5c715a9e8561 h1:MDc5xs78ZrZr3HMQugiXOAkSZtfTpbJLDr/lwfgO53E=25golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M=26golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=27golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=29golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=30golang.org/x/sys v0.17.0 h1:25cE3gD+tdBA7lp7QfhuV+rJiE9YXTcS3VG1SqssI/Y=31golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=32golang.org/x/term v0.17.0 h1:mkTF7LCd6WGJNL3K1Ad7kwxNfYAW6a8a8QqtMblp/4U=33golang.org/x/term v0.17.0/go.mod h1:lLRBjIVuehSbZlaOtGMbcMncT+aqLLLmKrsjNrUguwk=28golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y=29golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=30golang.org/x/text v0.13.0 h1:ablQoSUd0tRdKxZewP80B+BaqeKJuVhuRxj/dkrun3k=31golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
modified:exec.go+1 added-1 removed
107 unmodified lines
108 return109 }110111 c.SetStdin(p.input)112 c.SetStdout(p.output.TTY())112 c.SetStdout(p.output)113 c.SetStderr(os.Stderr)114115 // Execute system command.116 if err := c.Run(); err != nil {
13 unmodified lines
modified:go.mod+6 added-7 removed
1 unmodified line
23go 1.1845require (6 github.com/charmbracelet/x/ansi v0.1.17 github.com/charmbracelet/x/term v0.1.18 github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f9 github.com/mattn/go-localereader v0.0.110 github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f611 github.com/muesli/cancelreader v0.2.210 github.com/muesli/reflow v0.3.011 github.com/muesli/termenv v0.15.212 golang.org/x/sync v0.7.013 golang.org/x/sys v0.20.014 golang.org/x/term v0.20.014)1516require (18 github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect19 github.com/lucasb-eyer/go-colorful v1.2.0 // indirect20 github.com/mattn/go-isatty v0.0.18 // indirect17 github.com/charmbracelet/x/input v0.1.0 // indirect18 github.com/charmbracelet/x/windows v0.1.0 // indirect19 github.com/mattn/go-runewidth v0.0.15 // indirect22 github.com/rivo/uniseg v0.4.6 // indirect20 github.com/rivo/uniseg v0.4.7 // indirect21 github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect22 golang.org/x/text v0.3.8 // indirect23)
modified:go.sum+13 added-17 removed
1github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiErDT4WkJ2k=2github.com/aymanbagabas/go-osc52/v2 v2.0.1/go.mod h1:uYgXzlJ7ZpABp8OJ+exZzJJhRNQ2ASbcXHWsFqH8hp8=1github.com/charmbracelet/x/ansi v0.1.1 h1:CGAduulr6egay/YVbGc8Hsu8deMg1xZ/bkaXTPi1JDk=2github.com/charmbracelet/x/ansi v0.1.1/go.mod h1:dk73KoMTT5AX5BsX0KrqhsTqAnhZZoCBjs7dGWp4Ktw=3github.com/charmbracelet/x/input v0.1.0 h1:TEsGSfZYQyOtp+STIjyBq6tpRaorH0qpwZUj8DavAhQ=4github.com/charmbracelet/x/input v0.1.0/go.mod h1:ZZwaBxPF7IG8gWWzPUVqHEtWhc1+HXJPNuerJGRGZ28=5github.com/charmbracelet/x/term v0.1.1 h1:3cosVAiPOig+EV4X9U+3LDgtwwAoEzJjNdwbXDjF6yI=6github.com/charmbracelet/x/term v0.1.1/go.mod h1:wB1fHt5ECsu3mXYusyzcngVWWlu1KKUmmLhfgr/Flxw=7github.com/charmbracelet/x/windows v0.1.0 h1:gTaxdvzDM5oMa/I2ZNF7wN78X/atWemG9Wph7Ika2k4=8github.com/charmbracelet/x/windows v0.1.0/go.mod h1:GLEO/l+lizvFDBPLIOk+49gdX49L9YWMB5t+DZd0jkQ=9github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f h1:Y/CXytFA4m6baUTXGLOoWe4PQhGxaX0KpnayAqC48p4=10github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f/go.mod h1:vw97MGsxSvLiUE2X8qFplwetxpGLQrlU1Q9AUEIzCaM=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=7github.com/mattn/go-isatty v0.0.18 h1:DOKFKCQ7FNG2L1rbrmstDN4QVRdS89Nkh85u68Uwp98=8github.com/mattn/go-isatty v0.0.18/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=11github.com/mattn/go-localereader v0.0.1 h1:ygSAOl7ZXTx4RdPYinUpg6W99U8jWvWi9Ye2JC/oIi4=12github.com/mattn/go-localereader v0.0.1/go.mod h1:8fBrzywKY7BI3czFoHkuzRoWE9C+EiG4R1k4Cjx5p88=11github.com/mattn/go-runewidth v0.0.12/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk=13github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U=14github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=15github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6 h1:ZK8zHtRHOkbHy6Mmr5D264iyp3TiX5OmNcI5cIARiQI=16github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6/go.mod h1:CJlz5H+gyd6CUWT45Oy4q24RdLyn7Md9Vj2/ldJBSIo=17github.com/muesli/cancelreader v0.2.2 h1:3I4Kt4BQjOR54NavqnDogx/MIoWBFa0StPA8ELUXHmA=18github.com/muesli/cancelreader v0.2.2/go.mod h1:3XuTXfFS2VjM+HTLZY9Ak0l6eUKfijIfMUZ4EgX0QYo=18github.com/muesli/reflow v0.3.0 h1:IFsN6K9NfGtjeggFP+68I4chLZV2yIKsXJFNZ+eWh6s=19github.com/muesli/reflow v0.3.0/go.mod h1:pbwTDkVPibjO2kyvBQRBxTWEEGDGq0FlB1BIKtnHY/8=20github.com/muesli/termenv v0.15.2 h1:GohcuySI0QmI3wN8Ok9PtKGkgkFIk7y6Vpb5PvrY+Wo=21github.com/muesli/termenv v0.15.2/go.mod h1:Epx+iuz8sNs7mNKhxzH4fWXGNpZwUaJKRS1noLXviQ8=22github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=19github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=24github.com/rivo/uniseg v0.4.6 h1:Sovz9sDSwbOz9tgUy8JpT+KgCkPYJEN/oYzlJiYTNLg=25github.com/rivo/uniseg v0.4.6/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=20github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=21github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=22github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e h1:JVG44RsyaB9T2KIHavMF/ppJZNG9ZpyihvCd0w101no=23github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e/go.mod h1:RbqR21r5mrJuqunuUZ/Dhy/avygyECGrLceyNeo4LiM=24golang.org/x/exp v0.0.0-20220909182711-5c715a9e8561 h1:MDc5xs78ZrZr3HMQugiXOAkSZtfTpbJLDr/lwfgO53E=25golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M=26golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=27golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=29golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=28golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y=29golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=32golang.org/x/term v0.20.0 h1:VnkxpohqXaOBYJtBmEppKUG6mXpi+4O6purfc2+sMhw=33golang.org/x/term v0.20.0/go.mod h1:8UkIAJTvZgivsXaD6/pH6U9ecQzZ45awqEOzuCvwpFY=30golang.org/x/text v0.3.8 h1:nAL+RVCQ9uMn3vJZbV+MRnydTJFPf8qqY42YiA6MrqY=31golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ=
modified:inputreader_windows.go+2 added-1 removed
7 unmodified lines
8 "io"9 "os"10 "sync"1112 "github.com/charmbracelet/x/term"13 "github.com/erikgeiser/coninput"14 "github.com/muesli/cancelreader"15 "golang.org/x/sys/windows"16)
11 unmodified lines
28func newInputReader(r io.Reader) (cancelreader.CancelReader, error) {29 fallback := func(io.Reader) (cancelreader.CancelReader, error) {30 return cancelreader.NewReader(r)31 }31 if f, ok := r.(*os.File); !ok || f.Fd() != os.Stdin.Fd() {32 if f, ok := r.(term.File); !ok || f.Fd() != os.Stdin.Fd() {33 return fallback(r)34 }3536 conin, err := coninput.NewStdinHandle()
71 unmodified lines
modified:nil_renderer.go+1 added
20 unmodified lines
21func (n nilRenderer) disableBracketedPaste() {}22func (n nilRenderer) enableMouseSGRMode() {}23func (n nilRenderer) disableMouseSGRMode() {}24func (n nilRenderer) bracketedPasteActive() bool { return false }25func (n nilRenderer) setWindowTitle(_ string) {}
modified:options_test.go+3 added-3 removed
1package tea23import (4 "bytes"5 "os"6 "sync/atomic"7 "testing"8)910func TestOptions(t *testing.T) {11 t.Run("output", func(t *testing.T) {12 var b bytes.Buffer13 p := NewProgram(nil, WithOutput(&b))13 if p.output.TTY() != nil {14 t.Errorf("expected output to custom, got %v", p.output.TTY().Fd())14 if f, ok := p.output.(*os.File); ok {15 t.Errorf("expected output to custom, got %v", f.Fd())16 }17 })1819 t.Run("custom input", func(t *testing.T) {
46 unmodified lines
66 t.Run("custom input", func(t *testing.T) {67 var b bytes.Buffer68 exercise(t, WithInput(&b), customInput)69 })6970 })7172 t.Run("startup options", func(t *testing.T) {73 exercise := func(t *testing.T, opt ProgramOption, expect startupOptions) {
59 unmodified lines
modified:options.go+1 added-7 removed
2 unmodified lines
3import (4 "context"5 "io"6 "sync/atomic"78 "github.com/muesli/termenv"7)89// ProgramOption is used to set options when initializing a Program. Program can10// accept a variable number of options.
15 unmodified lines
26// WithOutput sets the output which, by default, is stdout. In most cases you27// won't need to use this.28func WithOutput(output io.Writer) ProgramOption {29 return func(p *Program) {32 if o, ok := output.(*termenv.Output); ok {33 p.output = o30 p.output = output34 } else {35 p.output = termenv.NewOutput(output, termenv.WithColorCache(true))36 }31 }32}3334// WithInput sets the input which, by default, is stdin. In most cases you
185 unmodified lines
modified:renderer.go+3 added
65 unmodified lines
6667 // bracketedPasteActive reports whether bracketed paste mode is68 // currently enabled.69 bracketedPasteActive() bool7071 // setWindowTitle sets the terminal window title.72 setWindowTitle(string)73}7475// repaintMsg forces a full repaint.76type repaintMsg struct{}
modified:screen_test.go+9 added-9 removed
12 unmodified lines
13 }{14 {15 name: "clear_screen",16 cmds: []Cmd{ClearScreen},17 expected: "\x1b[?25l\x1b[?2004h\x1b[2J\x1b[1;1H\x1b[1;1H\rsuccess\r\n\x1b[0D\x1b[2K\x1b[?2004l\x1b[?25h\x1b[?1002l\x1b[?1003l\x1b[?1006l",17 expected: "\x1b[?25l\x1b[?2004h\x1b[2J\x1b[1;1H\rsuccess\r\n\x1b[D\x1b[2K\x1b[?2004l\x1b[?25h\x1b[?1002l\x1b[?1003l\x1b[?1006l",18 },19 {20 name: "altscreen",21 cmds: []Cmd{EnterAltScreen, ExitAltScreen},22 expected: "\x1b[?25l\x1b[?2004h\x1b[?1049h\x1b[2J\x1b[1;1H\x1b[1;1H\x1b[?25l\x1b[?1049l\x1b[?25l\rsuccess\r\n\x1b[0D\x1b[2K\x1b[?2004l\x1b[?25h\x1b[?1002l\x1b[?1003l\x1b[?1006l",22 expected: "\x1b[?25l\x1b[?2004h\x1b[?1049h\x1b[2J\x1b[1;1H\x1b[?25l\x1b[?1049l\x1b[?25l\rsuccess\r\n\x1b[D\x1b[2K\x1b[?2004l\x1b[?25h\x1b[?1002l\x1b[?1003l\x1b[?1006l",23 },24 {25 name: "altscreen_autoexit",26 cmds: []Cmd{EnterAltScreen},27 expected: "\x1b[?25l\x1b[?2004h\x1b[?1049h\x1b[2J\x1b[1;1H\x1b[1;1H\x1b[?25l\rsuccess\r\n\x1b[2;0H\x1b[2K\x1b[?2004l\x1b[?25h\x1b[?1002l\x1b[?1003l\x1b[?1006l\x1b[?1049l\x1b[?25h",27 expected: "\x1b[?25l\x1b[?2004h\x1b[?1049h\x1b[2J\x1b[1;1H\x1b[?25l\rsuccess\r\n\x1b[2;0H\x1b[2K\x1b[?2004l\x1b[?25h\x1b[?1002l\x1b[?1003l\x1b[?1006l\x1b[?1049l\x1b[?25h",28 },29 {30 name: "mouse_cellmotion",31 cmds: []Cmd{EnableMouseCellMotion},32 expected: "\x1b[?25l\x1b[?2004h\x1b[?1002h\x1b[?1006h\rsuccess\r\n\x1b[0D\x1b[2K\x1b[?2004l\x1b[?25h\x1b[?1002l\x1b[?1003l\x1b[?1006l",32 expected: "\x1b[?25l\x1b[?2004h\x1b[?1002h\x1b[?1006h\rsuccess\r\n\x1b[D\x1b[2K\x1b[?2004l\x1b[?25h\x1b[?1002l\x1b[?1003l\x1b[?1006l",33 },34 {35 name: "mouse_allmotion",36 cmds: []Cmd{EnableMouseAllMotion},37 expected: "\x1b[?25l\x1b[?2004h\x1b[?1003h\x1b[?1006h\rsuccess\r\n\x1b[0D\x1b[2K\x1b[?2004l\x1b[?25h\x1b[?1002l\x1b[?1003l\x1b[?1006l",37 expected: "\x1b[?25l\x1b[?2004h\x1b[?1003h\x1b[?1006h\rsuccess\r\n\x1b[D\x1b[2K\x1b[?2004l\x1b[?25h\x1b[?1002l\x1b[?1003l\x1b[?1006l",38 },39 {40 name: "mouse_disable",41 cmds: []Cmd{EnableMouseAllMotion, DisableMouse},42 expected: "\x1b[?25l\x1b[?2004h\x1b[?1003h\x1b[?1006h\x1b[?1002l\x1b[?1003l\x1b[?1006l\rsuccess\r\n\x1b[0D\x1b[2K\x1b[?2004l\x1b[?25h\x1b[?1002l\x1b[?1003l\x1b[?1006l",42 expected: "\x1b[?25l\x1b[?2004h\x1b[?1003h\x1b[?1006h\x1b[?1002l\x1b[?1003l\x1b[?1006l\rsuccess\r\n\x1b[D\x1b[2K\x1b[?2004l\x1b[?25h\x1b[?1002l\x1b[?1003l\x1b[?1006l",43 },44 {45 name: "cursor_hide",46 cmds: []Cmd{HideCursor},47 expected: "\x1b[?25l\x1b[?2004h\x1b[?25l\rsuccess\r\n\x1b[0D\x1b[2K\x1b[?2004l\x1b[?25h\x1b[?1002l\x1b[?1003l\x1b[?1006l",47 expected: "\x1b[?25l\x1b[?2004h\x1b[?25l\rsuccess\r\n\x1b[D\x1b[2K\x1b[?2004l\x1b[?25h\x1b[?1002l\x1b[?1003l\x1b[?1006l",48 },49 {50 name: "cursor_hideshow",51 cmds: []Cmd{HideCursor, ShowCursor},52 expected: "\x1b[?25l\x1b[?2004h\x1b[?25l\x1b[?25h\rsuccess\r\n\x1b[0D\x1b[2K\x1b[?2004l\x1b[?25h\x1b[?1002l\x1b[?1003l\x1b[?1006l",52 expected: "\x1b[?25l\x1b[?2004h\x1b[?25l\x1b[?25h\rsuccess\r\n\x1b[D\x1b[2K\x1b[?2004l\x1b[?25h\x1b[?1002l\x1b[?1003l\x1b[?1006l",53 },54 {55 name: "bp_stop_start",56 cmds: []Cmd{DisableBracketedPaste, EnableBracketedPaste},57 expected: "\x1b[?25l\x1b[?2004h\x1b[?2004l\x1b[?2004h\rsuccess\r\n\x1b[0D\x1b[2K\x1b[?2004l\x1b[?25h\x1b[?1002l\x1b[?1003l\x1b[?1006l",57 expected: "\x1b[?25l\x1b[?2004h\x1b[?2004l\x1b[?2004h\rsuccess\r\n\x1b[D\x1b[2K\x1b[?2004l\x1b[?25h\x1b[?1002l\x1b[?1003l\x1b[?1006l",58 },59 }6061 for _, test := range tests {
20 unmodified lines
modified:screen.go+1 added-1 removed
196 unmodified lines
197}198199// SetWindowTitle sets the terminal window title.200func (p *Program) SetWindowTitle(title string) {201 p.output.SetWindowTitle(title)201 p.renderer.setWindowTitle(title)202}
modified:standard_renderer.go+64 added-59 removed
6 unmodified lines
7 "strings"8 "sync"9 "time"1011 "github.com/charmbracelet/x/ansi"12 "github.com/muesli/ansi/compressor"12 "github.com/muesli/reflow/truncate"13 "github.com/muesli/termenv"13)1415const (16 // defaultFramerate specifies the maximum interval at which we should
8 unmodified lines
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 {28 mtx *sync.Mutex30 out *termenv.Output29 out io.Writer3031 buf bytes.Buffer32 queuedMessageLines []string33 framerate time.Duration
22 unmodified lines
56}5758// newRenderer creates a new renderer. Normally you'll want to initialize it59// with os.Stdout as the first argument.61func newRenderer(out *termenv.Output, useANSICompressor bool, fps int) renderer {60func newRenderer(out io.Writer, useANSICompressor bool, fps int) renderer {61 if fps < 1 {62 fps = defaultFPS63 } else if fps > maxFPS {64 fps = maxFPS
6 unmodified lines
71 useANSICompressor: useANSICompressor,72 queuedMessageLines: []string{},73 }74 if r.useANSICompressor {76 r.out = termenv.NewOutput(&compressor.Writer{Forward: out})75 r.out = &compressor.Writer{Forward: out}76 }77 return r78}79
26 unmodified lines
106107 r.mtx.Lock()108 defer r.mtx.Unlock()109111 r.out.ClearLine()110 r.execute(ansi.EraseEntireLine)111112 if r.useANSICompressor {114 if w, ok := r.out.TTY().(io.WriteCloser); ok {113 if w, ok := r.out.(io.WriteCloser); ok {114 _ = w.Close()115 }116 }117}118119// execute writes a sequence to the terminal.120func (r *standardRenderer) execute(seq string) {121 _, _ = io.WriteString(r.out, seq)122}123124// kill halts the renderer. The final frame will not be rendered.125func (r *standardRenderer) kill() {126 // Stop the renderer before acquiring the mutex to avoid a deadlock.127 r.once.Do(func() {
2 unmodified lines
130131 r.mtx.Lock()132 defer r.mtx.Unlock()133130 r.out.ClearLine()134 r.execute(ansi.EraseEntireLine)135}136137// listen waits for ticks on the ticker, or a signal to stop the renderer.138func (r *standardRenderer) listen() {
20 unmodified lines
159 }160161 // Output buffer162 buf := &bytes.Buffer{}159 out := termenv.NewOutput(buf)163164 newLines := strings.Split(r.buf.String(), "\n")165166 // If we know the output's height, we can use it to determine how many
15 unmodified lines
182 // if we are clearing queued messages, we want to clear all lines, since183 // printing messages allows for native terminal word-wrap, we184 // don't have control over the queued lines185 if flushQueuedMessages {183 out.ClearLine()186 buf.WriteString(ansi.EraseEntireLine)187 } else if (len(newLines) <= len(oldLines)) && (len(newLines) > i && len(oldLines) > i) && (newLines[i] == oldLines[i]) {188 // If the number of lines we want to render hasn't increased and189 // new line is the same as the old line we can skip rendering for190 // this line as a performance optimization.191 skipLines[i] = struct{}{}192 } else if _, exists := r.ignoreLines[i]; !exists {190 out.ClearLine()193 buf.WriteString(ansi.EraseEntireLine)194 }195193 out.CursorUp(1)196 buf.WriteString(ansi.CursorUp1)197 }198199 if _, exists := r.ignoreLines[0]; !exists {200 // We need to return to the start of the line here to properly
4 unmodified lines
205 // We use this sequence in particular because it's part of the ANSI206 // standard (whereas others are proprietary to, say, VT100/VT52).207 // If cursor previous line (ESC[ + <n> + F) were better supported208 // we could use that above to eliminate this step.206 out.CursorBack(r.width)207 out.ClearLine()209 buf.WriteString(ansi.CursorLeft(r.width))210 buf.WriteString(ansi.EraseEntireLine)211 }212 }213214 // Merge the set of lines we're skipping as a rendering optimization with
4 unmodified lines
219220 if flushQueuedMessages {221 // Dump the lines we've queued up for printing222 for _, line := range r.queuedMessageLines {220 _, _ = out.WriteString(line)221 _, _ = out.WriteString("\r\n")223 _, _ = buf.WriteString(line)224 _, _ = buf.WriteString("\r\n")225 }226 // clear the queued message lines227 r.queuedMessageLines = []string{}228 }
2 unmodified lines
231 for i := 0; i < len(newLines); i++ {232 if _, skip := skipLines[i]; skip {233 // Unless this is the last line, move the cursor down.234 if i < len(newLines)-1 {232 out.CursorDown(1)235 buf.WriteString(ansi.CursorDown1)236 }237 } else {238 if i == 0 && r.lastRender == "" {239 // On first render, reset the cursor to the start of the line
10 unmodified lines
250 // Note that on Windows we only get the width of the window on251 // program initialization, so after a resize this won't perform252 // correctly (signal SIGWINCH is not supported on Windows).253 if r.width > 0 {251 line = truncate.String(line, uint(r.width))254 line = ansi.Truncate(line, r.width, "")255 }256254 _, _ = out.WriteString(line)257 _, _ = buf.WriteString(line)258259 if i < len(newLines)-1 {257 _, _ = out.WriteString("\r\n")260 _, _ = buf.WriteString("\r\n")261 }262 }263 }264 r.linesRendered = numLinesThisFlush
3 unmodified lines
268 if r.altScreenActive {269 // This case fixes a bug in macOS terminal. In other terminals the270 // other case seems to do the job regardless of whether or not we're271 // using the full terminal window.269 out.MoveCursor(r.linesRendered, 0)272 buf.WriteString(ansi.MoveCursor(r.linesRendered, 0))273 } else {271 out.CursorBack(r.width)274 buf.WriteString(ansi.CursorLeft(r.width))275 }276277 _, _ = r.out.Write(buf.Bytes())278 r.lastRender = r.buf.String()
25 unmodified lines
304func (r *standardRenderer) clearScreen() {305 r.mtx.Lock()306 defer r.mtx.Unlock()307305 r.out.ClearScreen()306 r.out.MoveCursor(1, 1)308 r.execute(ansi.EraseEntireDisplay)309 r.execute(ansi.MoveCursorOrigin)310311 r.repaint()312}313
12 unmodified lines
326 return327 }328329 r.altScreenActive = true327 r.out.AltScreen()330 r.execute(ansi.EnableAltScreenBuffer)331332 // Ensure that the terminal is cleared, even when it doesn't support333 // alt screen (or alt screen support is disabled, like GNU screen by334 // default).335 //336 // Note: we can't use r.clearScreen() here because the mutex is already337 // locked.335 r.out.ClearScreen()336 r.out.MoveCursor(1, 1)338 r.execute(ansi.EraseEntireDisplay)339 r.execute(ansi.MoveCursorOrigin)340341 // cmd.exe and other terminals keep separate cursor states for the AltScreen342 // and the main buffer. We have to explicitly reset the cursor visibility343 // whenever we enter AltScreen.344 if r.cursorHidden {342 r.out.HideCursor()345 r.execute(ansi.HideCursor)346 } else {344 r.out.ShowCursor()347 r.execute(ansi.ShowCursor)348 }349350 r.repaint()351}
6 unmodified lines
358 return359 }360361 r.altScreenActive = false359 r.out.ExitAltScreen()362 r.execute(ansi.DisableAltScreenBuffer)363364 // cmd.exe and other terminals keep separate cursor states for the AltScreen365 // and the main buffer. We have to explicitly reset the cursor visibility366 // whenever we exit AltScreen.367 if r.cursorHidden {365 r.out.HideCursor()368 r.execute(ansi.HideCursor)369 } else {367 r.out.ShowCursor()370 r.execute(ansi.ShowCursor)371 }372373 r.repaint()374}
2 unmodified lines
377 r.mtx.Lock()378 defer r.mtx.Unlock()379380 r.cursorHidden = false378 r.out.ShowCursor()381 r.execute(ansi.ShowCursor)382}383384func (r *standardRenderer) hideCursor() {385 r.mtx.Lock()386 defer r.mtx.Unlock()387388 r.cursorHidden = true386 r.out.HideCursor()389 r.execute(ansi.HideCursor)390}391392func (r *standardRenderer) enableMouseCellMotion() {393 r.mtx.Lock()394 defer r.mtx.Unlock()395393 r.out.EnableMouseCellMotion()396 r.execute(ansi.EnableMouseCellMotion)397}398399func (r *standardRenderer) disableMouseCellMotion() {400 r.mtx.Lock()401 defer r.mtx.Unlock()402400 r.out.DisableMouseCellMotion()403 r.execute(ansi.DisableMouseCellMotion)404}405406func (r *standardRenderer) enableMouseAllMotion() {407 r.mtx.Lock()408 defer r.mtx.Unlock()409407 r.out.EnableMouseAllMotion()410 r.execute(ansi.EnableMouseAllMotion)411}412413func (r *standardRenderer) disableMouseAllMotion() {414 r.mtx.Lock()415 defer r.mtx.Unlock()416414 r.out.DisableMouseAllMotion()417 r.execute(ansi.DisableMouseAllMotion)418}419420func (r *standardRenderer) enableMouseSGRMode() {421 r.mtx.Lock()422 defer r.mtx.Unlock()423421 r.out.EnableMouseExtendedMode()424 r.execute(ansi.EnableMouseSgrExt)425}426427func (r *standardRenderer) disableMouseSGRMode() {428 r.mtx.Lock()429 defer r.mtx.Unlock()430428 r.out.DisableMouseExtendedMode()431 r.execute(ansi.DisableMouseSgrExt)432}433434func (r *standardRenderer) enableBracketedPaste() {435 r.mtx.Lock()436 defer r.mtx.Unlock()437435 r.out.EnableBracketedPaste()438 r.execute(ansi.EnableBracketedPaste)439 r.bpActive = true440}441442func (r *standardRenderer) disableBracketedPaste() {443 r.mtx.Lock()444 defer r.mtx.Unlock()445443 r.out.DisableBracketedPaste()446 r.execute(ansi.DisableBracketedPaste)447 r.bpActive = false448}449450func (r *standardRenderer) bracketedPasteActive() bool {
2 unmodified lines
453454 return r.bpActive455}456457// setWindowTitle sets the terminal window title.458func (r *standardRenderer) setWindowTitle(title string) {459 r.execute(ansi.SetWindowTitle(title))460}461462// setIgnoredLines specifies lines not to be touched by the standard Bubble Tea463// renderer.464func (r *standardRenderer) setIgnoredLines(from int, to int) {465 // Lock if we're going to be clearing some lines since we don't want
12 unmodified lines
478479 // Erase ignored lines480 if r.linesRendered > 0 {481 buf := &bytes.Buffer{}474 out := termenv.NewOutput(buf)482483 for i := r.linesRendered - 1; i >= 0; i-- {484 if _, exists := r.ignoreLines[i]; exists {478 out.ClearLine()485 buf.WriteString(ansi.EraseEntireLine)486 }480 out.CursorUp(1)487 buf.WriteString(ansi.CursorUp1)488 }482 out.MoveCursor(r.linesRendered, 0) // put cursor back489 buf.WriteString(ansi.MoveCursor(r.linesRendered, 0)) // put cursor back490 _, _ = r.out.Write(buf.Bytes())491 }492}493
26 unmodified lines
520 r.mtx.Lock()521 defer r.mtx.Unlock()522523 buf := &bytes.Buffer{}517 out := termenv.NewOutput(buf)524519 out.ChangeScrollingRegion(topBoundary, bottomBoundary)520 out.MoveCursor(topBoundary, 0)521 out.InsertLines(len(lines))522 _, _ = out.WriteString(strings.Join(lines, "\r\n"))523 out.ChangeScrollingRegion(0, r.height)525 buf.WriteString(ansi.SetScrollingRegion(topBoundary, bottomBoundary))526 buf.WriteString(ansi.MoveCursor(topBoundary, 0))527 buf.WriteString(ansi.InsertLine(len(lines)))528 _, _ = buf.WriteString(strings.Join(lines, "\r\n"))529 buf.WriteString(ansi.SetScrollingRegion(0, r.height))530531 // Move cursor back to where the main rendering routine expects it to be526 out.MoveCursor(r.linesRendered, 0)532 buf.WriteString(ansi.MoveCursor(r.linesRendered, 0))533534 _, _ = r.out.Write(buf.Bytes())535}536
10 unmodified lines
547 r.mtx.Lock()548 defer r.mtx.Unlock()549550 buf := &bytes.Buffer{}545 out := termenv.NewOutput(buf)551547 out.ChangeScrollingRegion(topBoundary, bottomBoundary)548 out.MoveCursor(bottomBoundary, 0)549 _, _ = out.WriteString("\r\n" + strings.Join(lines, "\r\n"))550 out.ChangeScrollingRegion(0, r.height)552 buf.WriteString(ansi.SetScrollingRegion(topBoundary, bottomBoundary))553 buf.WriteString(ansi.MoveCursor(bottomBoundary, 0))554 _, _ = buf.WriteString("\r\n" + strings.Join(lines, "\r\n"))555 buf.WriteString(ansi.SetScrollingRegion(0, r.height))556557 // Move cursor back to where the main rendering routine expects it to be553 out.MoveCursor(r.linesRendered, 0)558 buf.WriteString(ansi.MoveCursor(r.linesRendered, 0))559560 _, _ = r.out.Write(buf.Bytes())561}562
163 unmodified lines
modified:tea.go+15 added-22 removed
19 unmodified lines
20 "sync"21 "sync/atomic"22 "syscall"2324 "github.com/charmbracelet/x/term"25 "github.com/muesli/cancelreader"25 "github.com/muesli/termenv"26 "golang.org/x/sync/errgroup"27 "golang.org/x/term"27)2829// ErrProgramKilled is returned by [Program.Run] when the program got killed.30var ErrProgramKilled = errors.New("program was killed")
109 unmodified lines
140 errs chan error141 finished chan struct{}142143 // where to send output, this will usually be os.Stdout.145 output *termenv.Output146 restoreOutput func() error147 renderer renderer144 output io.Writer145 // ttyOutput is null if output is not a TTY.146 ttyOutput term.File147 previousOutputState *term.State148 renderer renderer149150 // where to read inputs from, this will usually be os.Stdin.151 input io.Reader151 // tty is null if input is not a TTY.152 tty *os.File153 previousTtyState *term.State154 cancelReader cancelreader.CancelReader155 readLoopDone chan struct{}152 // ttyInput is null if input is not a TTY.153 ttyInput term.File154 previousTtyInputState *term.State155 cancelReader cancelreader.CancelReader156 readLoopDone chan struct{}157158 // was the altscreen active before releasing the terminal?159 altScreenWasActive bool160 ignoreSignals uint32
37 unmodified lines
198 p.ctx, p.cancel = context.WithCancel(p.ctx)199200 // if no output was set, set it to stdout201 if p.output == nil {201 p.output = termenv.DefaultOutput()202203 // cache detected color values204 termenv.WithColorCache(true)(p.output)202 p.output = os.Stdout203 }204207 p.restoreOutput, _ = termenv.EnableVirtualTerminalProcessing(p.output)208205 return p206}207208func (p *Program) handleSignals() chan struct{} {
35 unmodified lines
244// handleResize handles terminal resize events.245func (p *Program) handleResize() chan struct{} {246 ch := make(chan struct{})247252 if f, ok := p.output.TTY().(*os.File); ok && term.IsTerminal(int(f.Fd())) {248 if p.ttyOutput != nil {249 // Get the initial terminal size and send it to the program.250 go p.checkResize()251252 // Listen for window resizes.
179 unmodified lines
432 // input. This will allow things to "just work" in cases where data was433 // piped in or redirected to the application.434 //435 // To disable input entirely pass nil to the [WithInput] program option.440 f, isFile := p.input.(*os.File)436 f, isFile := p.input.(term.File)437 if !isFile {438 break439 }444 if term.IsTerminal(int(f.Fd())) {440 if term.IsTerminal(f.Fd()) {441 break442 }443444 f, err := openInputTTY()
192 unmodified lines
637 }638 }639640 _ = p.restoreTerminalState()645 if p.restoreOutput != nil {646 _ = p.restoreOutput()647 }641 p.finished <- struct{}{}642}643644// ReleaseTerminal restores the original terminal state and cancels the input
72 unmodified lines
modified:tty_unix.go+8 added-4 removed
5 unmodified lines
6import (7 "fmt"8 "os"910 "golang.org/x/term"10 "github.com/charmbracelet/x/term"11)1213func (p *Program) initInput() (err error) {14 // Check if input is a terminal15 if f, ok := p.input.(*os.File); ok && term.IsTerminal(int(f.Fd())) {16 p.tty = f17 p.previousTtyState, err = term.MakeRaw(int(p.tty.Fd()))15 if f, ok := p.input.(term.File); ok && term.IsTerminal(f.Fd()) {16 p.ttyInput = f17 p.previousTtyInputState, err = term.MakeRaw(p.ttyInput.Fd())18 if err != nil {19 return fmt.Errorf("error entering raw mode: %w", err)20 }21 }2223 if f, ok := p.output.(term.File); ok && term.IsTerminal(f.Fd()) {24 p.ttyOutput = f25 }2627 return nil28}2930func openInputTTY() (*os.File, error) {
6 unmodified lines
modified:tty_windows.go+26 added-8 removed
5 unmodified lines
6import (7 "fmt"8 "os"910 "github.com/charmbracelet/x/term"11 "golang.org/x/sys/windows"11 "golang.org/x/term"12)1314func (p *Program) initInput() (err error) {15 // Save stdin state and enable VT input16 // We enable VT processing using Termenv, but we also need to enable VT16 // We also need to enable VT17 // input here.18 if f, ok := p.input.(*os.File); ok && term.IsTerminal(int(f.Fd())) {19 p.tty = f20 p.previousTtyState, err = term.MakeRaw(int(p.tty.Fd()))18 if f, ok := p.input.(term.File); ok && term.IsTerminal(f.Fd()) {19 p.ttyInput = f20 p.previousTtyInputState, err = term.MakeRaw(p.ttyInput.Fd())21 if err != nil {22 return err23 }2425 // Enable VT input26 var mode uint3227 if err := windows.GetConsoleMode(windows.Handle(p.tty.Fd()), &mode); err != nil {27 if err := windows.GetConsoleMode(windows.Handle(p.ttyInput.Fd()), &mode); err != nil {28 return fmt.Errorf("error getting console mode: %w", err)29 }3031 if err := windows.SetConsoleMode(windows.Handle(p.tty.Fd()), mode|windows.ENABLE_VIRTUAL_TERMINAL_INPUT); err != nil {31 if err := windows.SetConsoleMode(windows.Handle(p.ttyInput.Fd()), mode|windows.ENABLE_VIRTUAL_TERMINAL_INPUT); err != nil {32 return fmt.Errorf("error setting console mode: %w", err)33 }34 }3536 // Save output screen buffer state and enable VT processing.37 if f, ok := p.output.(term.File); ok && term.IsTerminal(f.Fd()) {38 p.ttyOutput = f39 p.previousOutputState, err = term.GetState(f.Fd())40 if err != nil {41 return err42 }4344 var mode uint3245 if err := windows.GetConsoleMode(windows.Handle(p.ttyOutput.Fd()), &mode); err != nil {46 return fmt.Errorf("error getting console mode: %w", err)47 }4849 if err := windows.SetConsoleMode(windows.Handle(p.ttyOutput.Fd()), mode|windows.ENABLE_VIRTUAL_TERMINAL_PROCESSING); err != nil {50 return fmt.Errorf("error setting console mode: %w", err)51 }52 }5354 return55}5657// Open the Windows equivalent of a TTY.58func openInputTTY() (*os.File, error) {41 f, err := os.OpenFile("CONIN$", os.O_RDWR, 0644)59 f, err := os.OpenFile("CONIN$", os.O_RDWR, 0o644)60 if err != nil {61 return nil, err62 }63 return f, nil
1 unmodified line
modified:tty.go+10 added-7 removed
2 unmodified lines
3import (4 "errors"5 "fmt"6 "io"7 "os"7 "time"89 "github.com/charmbracelet/x/term"10 "github.com/muesli/cancelreader"11 "golang.org/x/term"11)1213func (p *Program) initTerminal() error {14 if err := p.initInput(); err != nil {
24 unmodified lines
39}4041// restoreInput restores the tty input to its original state.42func (p *Program) restoreInput() error {44 if p.tty != nil && p.previousTtyState != nil {45 if err := term.Restore(int(p.tty.Fd()), p.previousTtyState); err != nil {43 if p.ttyInput != nil && p.previousTtyInputState != nil {44 if err := term.Restore(p.ttyInput.Fd(), p.previousTtyInputState); err != nil {45 return fmt.Errorf("error restoring console: %w", err)46 }47 }48 if p.ttyOutput != nil && p.previousOutputState != nil {49 if err := term.Restore(p.ttyOutput.Fd(), p.previousOutputState); err != nil {50 return fmt.Errorf("error restoring console: %w", err)51 }52 }53 return nil54}5556// initCancelReader (re)commences reading inputs.
35 unmodified lines
9293// checkResize detects the current size of the output and informs the program94// via a WindowSizeMsg.95func (p *Program) checkResize() {92 f, ok := p.output.TTY().(*os.File)93 if !ok || !term.IsTerminal(int(f.Fd())) {96 if p.ttyOutput == nil {97 // can't query window size98 return99 }10098 w, h, err := term.GetSize(int(f.Fd()))101 w, h, err := term.GetSize(p.ttyOutput.Fd())102 if err != nil {103 select {104 case <-p.ctx.Done():105 case p.errs <- err:
10 unmodified lines