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
Provenance
- Commit
cb801d8- Tree
2986963- Author
Ayman Bagabas <ayman.bagabas@gmail.com>- Committer
GitHub <noreply@github.com>- Parent
09f4ef0
20 files changed+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 key2-[70D[1A[70D[2KHi. 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 key2+[70D[A[70D[2KHi. This program will exit in 9 seconds. To quit sooner press any key.3 [70D[2K[?2004l[?25h[?1002l[?1003l[?1006lmodified:examples/go.mod+9 added-5 removed
2 unmodified lines3 go 1.184 5 require (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-4b79243a35167 unmodified lines19 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 // indirect6 unmodified lines38 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 )50 51 replace github.com/charmbracelet/bubbletea => ../modified:examples/go.sum+17 added-8 removed
17 unmodified lines18 github.com/charmbracelet/harmonica v0.2.0 h1:8NxJWRWg/bzKqqEaaeFNipOu77YR5t8aSwG4pgaUBiQ=19 github.com/charmbracelet/harmonica v0.2.0/go.mod h1:KSri/1RMQOZLbw7AHqgcBycp8pgJnQMYYT8QZRqZ1Ao=20 github.com/charmbracelet/lipgloss v0.10.0 h1:KWeXFSexGcfahHX+54URiZGkBFazf70JNMtwg/AFW3s=21 github.com/charmbracelet/lipgloss v0.10.0/go.mod h1:Wig9DSfvANsxqkRsqj6x87irdy123SR4dOXlKa91ciE=22-github.com/charmbracelet/x/exp/golden v0.0.0-20240222125807-0344fda748f8 h1:kyT+aGp1z5jwlus3OY0cP6FuT05jYeeExx/4TYxnyrs=23-github.com/charmbracelet/x/exp/golden v0.0.0-20240222125807-0344fda748f8/go.mod h1:wDlXFlCrmJ8J+swcL/MnGUuYnqgQdW9rhSD61oNMb6U=22+github.com/charmbracelet/x/ansi v0.1.1 h1:CGAduulr6egay/YVbGc8Hsu8deMg1xZ/bkaXTPi1JDk=23+github.com/charmbracelet/x/ansi v0.1.1/go.mod h1:dk73KoMTT5AX5BsX0KrqhsTqAnhZZoCBjs7dGWp4Ktw=24+github.com/charmbracelet/x/exp/golden v0.0.0-20240521172236-71f88323a7ca h1:Cw9p8EJdhDGIWICF34TIxTcQrAdzBdgkvaLA4AmqDVk=25+github.com/charmbracelet/x/exp/golden v0.0.0-20240521172236-71f88323a7ca/go.mod h1:wDlXFlCrmJ8J+swcL/MnGUuYnqgQdW9rhSD61oNMb6U=26 github.com/charmbracelet/x/exp/teatest v0.0.0-20240229115032-4b79243a3516 h1:7IZFEUZpEgjlTSd7P1MRRhGXs7t4F6mENeMw17TxnQs=27 github.com/charmbracelet/x/exp/teatest v0.0.0-20240229115032-4b79243a3516/go.mod h1:SG24wGkG/mix5V2dZLXfQ6Bod43HGvk9CkTDxATwKN4=28+github.com/charmbracelet/x/input v0.1.0 h1:TEsGSfZYQyOtp+STIjyBq6tpRaorH0qpwZUj8DavAhQ=29+github.com/charmbracelet/x/input v0.1.0/go.mod h1:ZZwaBxPF7IG8gWWzPUVqHEtWhc1+HXJPNuerJGRGZ28=30+github.com/charmbracelet/x/term v0.1.1 h1:3cosVAiPOig+EV4X9U+3LDgtwwAoEzJjNdwbXDjF6yI=31+github.com/charmbracelet/x/term v0.1.1/go.mod h1:wB1fHt5ECsu3mXYusyzcngVWWlu1KKUmmLhfgr/Flxw=32+github.com/charmbracelet/x/windows v0.1.0 h1:gTaxdvzDM5oMa/I2ZNF7wN78X/atWemG9Wph7Ika2k4=33+github.com/charmbracelet/x/windows v0.1.0/go.mod h1:GLEO/l+lizvFDBPLIOk+49gdX49L9YWMB5t+DZd0jkQ=34 github.com/dlclark/regexp2 v1.4.0 h1:F1rxgk7p4uKjwIQxBs9oAXe5CqrXlCduYEJvrF4u93E=35 github.com/dlclark/regexp2 v1.4.0/go.mod h1:2pZnwuY/m+8K6iRw6wQdMtk+rH5tNGR1i55kozfMjCc=36 github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=37 github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=32 unmodified lines70 github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=71 github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=72 github.com/sahilm/fuzzy v0.1.1-0.20230530133925-c48e322e2a8f h1:MvTmaQdww/z0Q4wrYjDSCcZ78NoftLQyHBSLW/Cx79Y=73 github.com/sahilm/fuzzy v0.1.1-0.20230530133925-c48e322e2a8f/go.mod h1:VFvziUEIMCrT6A6tw2RFIXPXXmzXbOsSHF0DOI8ZK9Y=74+github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e h1:JVG44RsyaB9T2KIHavMF/ppJZNG9ZpyihvCd0w101no=75+github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e/go.mod h1:RbqR21r5mrJuqunuUZ/Dhy/avygyECGrLceyNeo4LiM=76 github.com/yuin/goldmark v1.3.7/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=77 github.com/yuin/goldmark v1.5.4 h1:2uY/xC0roWy8IBEGLgB1ywIoEJFGmRrX21YQcvGZzjU=78 github.com/yuin/goldmark v1.5.4/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=79 github.com/yuin/goldmark-emoji v1.0.2 h1:c/RgTShNgHTtc6xdz2KKI74jJr6rWi7FPgnP9GAsO5s=80 github.com/yuin/goldmark-emoji v1.0.2/go.mod h1:RhP/RWpexdp+KHs7ghKnifRoIs/Bq4nDS7tRbCkOwKY=81+golang.org/x/exp v0.0.0-20220909182711-5c715a9e8561 h1:MDc5xs78ZrZr3HMQugiXOAkSZtfTpbJLDr/lwfgO53E=82 golang.org/x/net v0.23.0 h1:7EYJ93RZ9vYSZAIb2x3lnuvqO5zneoD6IvWjuhfxjTs=83 golang.org/x/net v0.23.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg=73-golang.org/x/sync v0.6.0 h1:5BMeUDZ7vkXGfEr1x9B4bRcTH4lpkTkpdh0T/J+qjbQ=74-golang.org/x/sync v0.6.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=84+golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M=85+golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=86 golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=87 golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=77-golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4=78-golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=79-golang.org/x/term v0.18.0 h1:FcHjZXDMxI8mM3nwhX9HlKop4C0YQvCVCdwYl2wOtE8=80-golang.org/x/term v0.18.0/go.mod h1:ILwASektA3OnRv7amZ1xhE/KTR+u50pbXfZ03+6Nx58=88+golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y=89+golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=90 golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=91 golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=modified:tutorials/go.mod+8 added-9 removed
3 unmodified lines4 5 require github.com/charmbracelet/bubbletea v0.25.06 7 require (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 )23 24 replace github.com/charmbracelet/bubbletea => ../modified:tutorials/go.sum+17 added-21 removed
1-github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiErDT4WkJ2k=2-github.com/aymanbagabas/go-osc52/v2 v2.0.1/go.mod h1:uYgXzlJ7ZpABp8OJ+exZzJJhRNQ2ASbcXHWsFqH8hp8=1+github.com/charmbracelet/x/ansi v0.1.1 h1:CGAduulr6egay/YVbGc8Hsu8deMg1xZ/bkaXTPi1JDk=2+github.com/charmbracelet/x/ansi v0.1.1/go.mod h1:dk73KoMTT5AX5BsX0KrqhsTqAnhZZoCBjs7dGWp4Ktw=3+github.com/charmbracelet/x/input v0.1.0 h1:TEsGSfZYQyOtp+STIjyBq6tpRaorH0qpwZUj8DavAhQ=4+github.com/charmbracelet/x/input v0.1.0/go.mod h1:ZZwaBxPF7IG8gWWzPUVqHEtWhc1+HXJPNuerJGRGZ28=5+github.com/charmbracelet/x/term v0.1.1 h1:3cosVAiPOig+EV4X9U+3LDgtwwAoEzJjNdwbXDjF6yI=6+github.com/charmbracelet/x/term v0.1.1/go.mod h1:wB1fHt5ECsu3mXYusyzcngVWWlu1KKUmmLhfgr/Flxw=7+github.com/charmbracelet/x/windows v0.1.0 h1:gTaxdvzDM5oMa/I2ZNF7wN78X/atWemG9Wph7Ika2k4=8+github.com/charmbracelet/x/windows v0.1.0/go.mod h1:GLEO/l+lizvFDBPLIOk+49gdX49L9YWMB5t+DZd0jkQ=9 github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f h1:Y/CXytFA4m6baUTXGLOoWe4PQhGxaX0KpnayAqC48p4=10 github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f/go.mod h1:vw97MGsxSvLiUE2X8qFplwetxpGLQrlU1Q9AUEIzCaM=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=7-github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=8-github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=11 github.com/mattn/go-localereader v0.0.1 h1:ygSAOl7ZXTx4RdPYinUpg6W99U8jWvWi9Ye2JC/oIi4=12 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=13 github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U=14 github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=15 github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6 h1:ZK8zHtRHOkbHy6Mmr5D264iyp3TiX5OmNcI5cIARiQI=16 github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6/go.mod h1:CJlz5H+gyd6CUWT45Oy4q24RdLyn7Md9Vj2/ldJBSIo=17 github.com/muesli/cancelreader v0.2.2 h1:3I4Kt4BQjOR54NavqnDogx/MIoWBFa0StPA8ELUXHmA=18 github.com/muesli/cancelreader v0.2.2/go.mod h1:3XuTXfFS2VjM+HTLZY9Ak0l6eUKfijIfMUZ4EgX0QYo=18-github.com/muesli/reflow v0.3.0 h1:IFsN6K9NfGtjeggFP+68I4chLZV2yIKsXJFNZ+eWh6s=19-github.com/muesli/reflow v0.3.0/go.mod h1:pbwTDkVPibjO2kyvBQRBxTWEEGDGq0FlB1BIKtnHY/8=20-github.com/muesli/termenv v0.15.2 h1:GohcuySI0QmI3wN8Ok9PtKGkgkFIk7y6Vpb5PvrY+Wo=21-github.com/muesli/termenv v0.15.2/go.mod h1:Epx+iuz8sNs7mNKhxzH4fWXGNpZwUaJKRS1noLXviQ8=22-github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=19 github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=24-github.com/rivo/uniseg v0.4.6 h1:Sovz9sDSwbOz9tgUy8JpT+KgCkPYJEN/oYzlJiYTNLg=25-github.com/rivo/uniseg v0.4.6/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=26-golang.org/x/sync v0.6.0 h1:5BMeUDZ7vkXGfEr1x9B4bRcTH4lpkTkpdh0T/J+qjbQ=27-golang.org/x/sync v0.6.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=20+github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=21+github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=22+github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e h1:JVG44RsyaB9T2KIHavMF/ppJZNG9ZpyihvCd0w101no=23+github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e/go.mod h1:RbqR21r5mrJuqunuUZ/Dhy/avygyECGrLceyNeo4LiM=24+golang.org/x/exp v0.0.0-20220909182711-5c715a9e8561 h1:MDc5xs78ZrZr3HMQugiXOAkSZtfTpbJLDr/lwfgO53E=25+golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M=26+golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=27 golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=29-golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=30-golang.org/x/sys v0.17.0 h1:25cE3gD+tdBA7lp7QfhuV+rJiE9YXTcS3VG1SqssI/Y=31-golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=32-golang.org/x/term v0.17.0 h1:mkTF7LCd6WGJNL3K1Ad7kwxNfYAW6a8a8QqtMblp/4U=33-golang.org/x/term v0.17.0/go.mod h1:lLRBjIVuehSbZlaOtGMbcMncT+aqLLLmKrsjNrUguwk=28+golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y=29+golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=30 golang.org/x/text v0.13.0 h1:ablQoSUd0tRdKxZewP80B+BaqeKJuVhuRxj/dkrun3k=31 golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=modified:exec.go+1 added-1 removed
107 unmodified lines108 return109 }110 111 c.SetStdin(p.input)112- c.SetStdout(p.output.TTY())112+ c.SetStdout(p.output)113 c.SetStderr(os.Stderr)114 115 // Execute system command.116 if err := c.Run(); err != nil {13 unmodified linesmodified:go.mod+6 added-7 removed
1 unmodified line2 3 go 1.184 5 require (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 )15 16 require (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
1-github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiErDT4WkJ2k=2-github.com/aymanbagabas/go-osc52/v2 v2.0.1/go.mod h1:uYgXzlJ7ZpABp8OJ+exZzJJhRNQ2ASbcXHWsFqH8hp8=1+github.com/charmbracelet/x/ansi v0.1.1 h1:CGAduulr6egay/YVbGc8Hsu8deMg1xZ/bkaXTPi1JDk=2+github.com/charmbracelet/x/ansi v0.1.1/go.mod h1:dk73KoMTT5AX5BsX0KrqhsTqAnhZZoCBjs7dGWp4Ktw=3+github.com/charmbracelet/x/input v0.1.0 h1:TEsGSfZYQyOtp+STIjyBq6tpRaorH0qpwZUj8DavAhQ=4+github.com/charmbracelet/x/input v0.1.0/go.mod h1:ZZwaBxPF7IG8gWWzPUVqHEtWhc1+HXJPNuerJGRGZ28=5+github.com/charmbracelet/x/term v0.1.1 h1:3cosVAiPOig+EV4X9U+3LDgtwwAoEzJjNdwbXDjF6yI=6+github.com/charmbracelet/x/term v0.1.1/go.mod h1:wB1fHt5ECsu3mXYusyzcngVWWlu1KKUmmLhfgr/Flxw=7+github.com/charmbracelet/x/windows v0.1.0 h1:gTaxdvzDM5oMa/I2ZNF7wN78X/atWemG9Wph7Ika2k4=8+github.com/charmbracelet/x/windows v0.1.0/go.mod h1:GLEO/l+lizvFDBPLIOk+49gdX49L9YWMB5t+DZd0jkQ=9 github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f h1:Y/CXytFA4m6baUTXGLOoWe4PQhGxaX0KpnayAqC48p4=10 github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f/go.mod h1:vw97MGsxSvLiUE2X8qFplwetxpGLQrlU1Q9AUEIzCaM=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=7-github.com/mattn/go-isatty v0.0.18 h1:DOKFKCQ7FNG2L1rbrmstDN4QVRdS89Nkh85u68Uwp98=8-github.com/mattn/go-isatty v0.0.18/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=11 github.com/mattn/go-localereader v0.0.1 h1:ygSAOl7ZXTx4RdPYinUpg6W99U8jWvWi9Ye2JC/oIi4=12 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=13 github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U=14 github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=15 github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6 h1:ZK8zHtRHOkbHy6Mmr5D264iyp3TiX5OmNcI5cIARiQI=16 github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6/go.mod h1:CJlz5H+gyd6CUWT45Oy4q24RdLyn7Md9Vj2/ldJBSIo=17 github.com/muesli/cancelreader v0.2.2 h1:3I4Kt4BQjOR54NavqnDogx/MIoWBFa0StPA8ELUXHmA=18 github.com/muesli/cancelreader v0.2.2/go.mod h1:3XuTXfFS2VjM+HTLZY9Ak0l6eUKfijIfMUZ4EgX0QYo=18-github.com/muesli/reflow v0.3.0 h1:IFsN6K9NfGtjeggFP+68I4chLZV2yIKsXJFNZ+eWh6s=19-github.com/muesli/reflow v0.3.0/go.mod h1:pbwTDkVPibjO2kyvBQRBxTWEEGDGq0FlB1BIKtnHY/8=20-github.com/muesli/termenv v0.15.2 h1:GohcuySI0QmI3wN8Ok9PtKGkgkFIk7y6Vpb5PvrY+Wo=21-github.com/muesli/termenv v0.15.2/go.mod h1:Epx+iuz8sNs7mNKhxzH4fWXGNpZwUaJKRS1noLXviQ8=22-github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=19 github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=24-github.com/rivo/uniseg v0.4.6 h1:Sovz9sDSwbOz9tgUy8JpT+KgCkPYJEN/oYzlJiYTNLg=25-github.com/rivo/uniseg v0.4.6/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=20+github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=21+github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=22+github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e h1:JVG44RsyaB9T2KIHavMF/ppJZNG9ZpyihvCd0w101no=23+github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e/go.mod h1:RbqR21r5mrJuqunuUZ/Dhy/avygyECGrLceyNeo4LiM=24+golang.org/x/exp v0.0.0-20220909182711-5c715a9e8561 h1:MDc5xs78ZrZr3HMQugiXOAkSZtfTpbJLDr/lwfgO53E=25 golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M=26 golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=27 golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=29-golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=28 golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y=29 golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=32-golang.org/x/term v0.20.0 h1:VnkxpohqXaOBYJtBmEppKUG6mXpi+4O6purfc2+sMhw=33-golang.org/x/term v0.20.0/go.mod h1:8UkIAJTvZgivsXaD6/pH6U9ecQzZ45awqEOzuCvwpFY=30 golang.org/x/text v0.3.8 h1:nAL+RVCQ9uMn3vJZbV+MRnydTJFPf8qqY42YiA6MrqY=31 golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ=modified:inputreader_windows.go+2 added-1 removed
7 unmodified lines8 "io"9 "os"10 "sync"11 12+ "github.com/charmbracelet/x/term"13 "github.com/erikgeiser/coninput"14 "github.com/muesli/cancelreader"15 "golang.org/x/sys/windows"16 )11 unmodified lines28 func 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 }35 36 conin, err := coninput.NewStdinHandle()71 unmodified linesmodified:nil_renderer.go+1 added
20 unmodified lines21 func (n nilRenderer) disableBracketedPaste() {}22 func (n nilRenderer) enableMouseSGRMode() {}23 func (n nilRenderer) disableMouseSGRMode() {}24 func (n nilRenderer) bracketedPasteActive() bool { return false }25+func (n nilRenderer) setWindowTitle(_ string) {}modified:options_test.go+3 added-3 removed
1 package tea2 3 import (4 "bytes"5+ "os"6 "sync/atomic"7 "testing"8 )9 10 func 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 })18 19 t.Run("custom input", func(t *testing.T) {46 unmodified lines66 t.Run("custom input", func(t *testing.T) {67 var b bytes.Buffer68 exercise(t, WithInput(&b), customInput)69 })69-70 })71 72 t.Run("startup options", func(t *testing.T) {73 exercise := func(t *testing.T, opt ProgramOption, expect startupOptions) {59 unmodified linesmodified:options.go+1 added-7 removed
2 unmodified lines3 import (4 "context"5 "io"6 "sync/atomic"7-8- "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.15 unmodified lines26 // WithOutput sets the output which, by default, is stdout. In most cases you27 // won't need to use this.28 func 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 }33 34 // WithInput sets the input which, by default, is stdin. In most cases you185 unmodified linesmodified:renderer.go+3 added
65 unmodified lines66 67 // bracketedPasteActive reports whether bracketed paste mode is68 // currently enabled.69 bracketedPasteActive() bool70+71+ // setWindowTitle sets the terminal window title.72+ setWindowTitle(string)73 }74 75 // repaintMsg forces a full repaint.76 type repaintMsg struct{}modified:screen_test.go+9 added-9 removed
12 unmodified lines13 }{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 }60 61 for _, test := range tests {20 unmodified linesmodified:screen.go+1 added-1 removed
196 unmodified lines197 }198 199 // SetWindowTitle sets the terminal window title.200 func (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 lines7 "strings"8 "sync"9 "time"10 11+ "github.com/charmbracelet/x/ansi"12 "github.com/muesli/ansi/compressor"12- "github.com/muesli/reflow/truncate"13- "github.com/muesli/termenv"13 )14 15 const (16 // defaultFramerate specifies the maximum interval at which we should8 unmodified lines25 // 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 {28 mtx *sync.Mutex30- out *termenv.Output29+ out io.Writer30 31 buf bytes.Buffer32 queuedMessageLines []string33 framerate time.Duration22 unmodified lines56 }57 58 // newRenderer creates a new renderer. Normally you'll want to initialize it59 // with os.Stdout as the first argument.61-func newRenderer(out *termenv.Output, useANSICompressor bool, fps int) renderer {60+func newRenderer(out io.Writer, useANSICompressor bool, fps int) renderer {61 if fps < 1 {62 fps = defaultFPS63 } else if fps > maxFPS {64 fps = maxFPS6 unmodified lines71 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 lines106 107 r.mtx.Lock()108 defer r.mtx.Unlock()109 111- r.out.ClearLine()110+ r.execute(ansi.EraseEntireLine)111 112 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 }118 119+// execute writes a sequence to the terminal.120+func (r *standardRenderer) execute(seq string) {121+ _, _ = io.WriteString(r.out, seq)122+}123+124 // kill halts the renderer. The final frame will not be rendered.125 func (r *standardRenderer) kill() {126 // Stop the renderer before acquiring the mutex to avoid a deadlock.127 r.once.Do(func() {2 unmodified lines130 131 r.mtx.Lock()132 defer r.mtx.Unlock()133 130- r.out.ClearLine()134+ r.execute(ansi.EraseEntireLine)135 }136 137 // listen waits for ticks on the ticker, or a signal to stop the renderer.138 func (r *standardRenderer) listen() {20 unmodified lines159 }160 161 // Output buffer162 buf := &bytes.Buffer{}159- out := termenv.NewOutput(buf)163 164 newLines := strings.Split(r.buf.String(), "\n")165 166 // If we know the output's height, we can use it to determine how many15 unmodified lines182 // 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 }195 193- out.CursorUp(1)196+ buf.WriteString(ansi.CursorUp1)197 }198 199 if _, exists := r.ignoreLines[0]; !exists {200 // We need to return to the start of the line here to properly4 unmodified lines205 // 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 }213 214 // Merge the set of lines we're skipping as a rendering optimization with4 unmodified lines219 220 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 lines231 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 line10 unmodified lines250 // 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 }256 254- _, _ = out.WriteString(line)257+ _, _ = buf.WriteString(line)258 259 if i < len(newLines)-1 {257- _, _ = out.WriteString("\r\n")260+ _, _ = buf.WriteString("\r\n")261 }262 }263 }264 r.linesRendered = numLinesThisFlush3 unmodified lines268 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 }276 277 _, _ = r.out.Write(buf.Bytes())278 r.lastRender = r.buf.String()25 unmodified lines304 func (r *standardRenderer) clearScreen() {305 r.mtx.Lock()306 defer r.mtx.Unlock()307 305- r.out.ClearScreen()306- r.out.MoveCursor(1, 1)308+ r.execute(ansi.EraseEntireDisplay)309+ r.execute(ansi.MoveCursorOrigin)310 311 r.repaint()312 }313 12 unmodified lines326 return327 }328 329 r.altScreenActive = true327- r.out.AltScreen()330+ r.execute(ansi.EnableAltScreenBuffer)331 332 // 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)340 341 // 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 }349 350 r.repaint()351 }6 unmodified lines358 return359 }360 361 r.altScreenActive = false359- r.out.ExitAltScreen()362+ r.execute(ansi.DisableAltScreenBuffer)363 364 // 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 }372 373 r.repaint()374 }2 unmodified lines377 r.mtx.Lock()378 defer r.mtx.Unlock()379 380 r.cursorHidden = false378- r.out.ShowCursor()381+ r.execute(ansi.ShowCursor)382 }383 384 func (r *standardRenderer) hideCursor() {385 r.mtx.Lock()386 defer r.mtx.Unlock()387 388 r.cursorHidden = true386- r.out.HideCursor()389+ r.execute(ansi.HideCursor)390 }391 392 func (r *standardRenderer) enableMouseCellMotion() {393 r.mtx.Lock()394 defer r.mtx.Unlock()395 393- r.out.EnableMouseCellMotion()396+ r.execute(ansi.EnableMouseCellMotion)397 }398 399 func (r *standardRenderer) disableMouseCellMotion() {400 r.mtx.Lock()401 defer r.mtx.Unlock()402 400- r.out.DisableMouseCellMotion()403+ r.execute(ansi.DisableMouseCellMotion)404 }405 406 func (r *standardRenderer) enableMouseAllMotion() {407 r.mtx.Lock()408 defer r.mtx.Unlock()409 407- r.out.EnableMouseAllMotion()410+ r.execute(ansi.EnableMouseAllMotion)411 }412 413 func (r *standardRenderer) disableMouseAllMotion() {414 r.mtx.Lock()415 defer r.mtx.Unlock()416 414- r.out.DisableMouseAllMotion()417+ r.execute(ansi.DisableMouseAllMotion)418 }419 420 func (r *standardRenderer) enableMouseSGRMode() {421 r.mtx.Lock()422 defer r.mtx.Unlock()423 421- r.out.EnableMouseExtendedMode()424+ r.execute(ansi.EnableMouseSgrExt)425 }426 427 func (r *standardRenderer) disableMouseSGRMode() {428 r.mtx.Lock()429 defer r.mtx.Unlock()430 428- r.out.DisableMouseExtendedMode()431+ r.execute(ansi.DisableMouseSgrExt)432 }433 434 func (r *standardRenderer) enableBracketedPaste() {435 r.mtx.Lock()436 defer r.mtx.Unlock()437 435- r.out.EnableBracketedPaste()438+ r.execute(ansi.EnableBracketedPaste)439 r.bpActive = true440 }441 442 func (r *standardRenderer) disableBracketedPaste() {443 r.mtx.Lock()444 defer r.mtx.Unlock()445 443- r.out.DisableBracketedPaste()446+ r.execute(ansi.DisableBracketedPaste)447 r.bpActive = false448 }449 450 func (r *standardRenderer) bracketedPasteActive() bool {2 unmodified lines453 454 return r.bpActive455 }456 457+// setWindowTitle sets the terminal window title.458+func (r *standardRenderer) setWindowTitle(title string) {459+ r.execute(ansi.SetWindowTitle(title))460+}461+462 // setIgnoredLines specifies lines not to be touched by the standard Bubble Tea463 // renderer.464 func (r *standardRenderer) setIgnoredLines(from int, to int) {465 // Lock if we're going to be clearing some lines since we don't want12 unmodified lines478 479 // Erase ignored lines480 if r.linesRendered > 0 {481 buf := &bytes.Buffer{}474- out := termenv.NewOutput(buf)482 483 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 lines520 r.mtx.Lock()521 defer r.mtx.Unlock()522 523 buf := &bytes.Buffer{}517- out := termenv.NewOutput(buf)524 519- 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))530 531 // 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))533 534 _, _ = r.out.Write(buf.Bytes())535 }536 10 unmodified lines547 r.mtx.Lock()548 defer r.mtx.Unlock()549 550 buf := &bytes.Buffer{}545- out := termenv.NewOutput(buf)551 547- 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))556 557 // 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))559 560 _, _ = r.out.Write(buf.Bytes())561 }562 163 unmodified linesmodified:tea.go+15 added-22 removed
19 unmodified lines20 "sync"21 "sync/atomic"22 "syscall"23 24+ "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 )28 29 // ErrProgramKilled is returned by [Program.Run] when the program got killed.30 var ErrProgramKilled = errors.New("program was killed")109 unmodified lines140 errs chan error141 finished chan struct{}142 143 // 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 renderer149 150 // 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{}157 158 // was the altscreen active before releasing the terminal?159 altScreenWasActive bool160 ignoreSignals uint3237 unmodified lines198 p.ctx, p.cancel = context.WithCancel(p.ctx)199 200 // if no output was set, set it to stdout201 if p.output == nil {201- p.output = termenv.DefaultOutput()202-203- // cache detected color values204- termenv.WithColorCache(true)(p.output)202+ p.output = os.Stdout203 }204 207- p.restoreOutput, _ = termenv.EnableVirtualTerminalProcessing(p.output)208-205 return p206 }207 208 func (p *Program) handleSignals() chan struct{} {35 unmodified lines244 // handleResize handles terminal resize events.245 func (p *Program) handleResize() chan struct{} {246 ch := make(chan struct{})247 252- 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()251 252 // Listen for window resizes.179 unmodified lines432 // 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 }443 444 f, err := openInputTTY()192 unmodified lines637 }638 }639 640 _ = p.restoreTerminalState()645- if p.restoreOutput != nil {646- _ = p.restoreOutput()647- }641 p.finished <- struct{}{}642 }643 644 // ReleaseTerminal restores the original terminal state and cancels the input72 unmodified linesmodified:tty_unix.go+8 added-4 removed
5 unmodified lines6 import (7 "fmt"8 "os"9 10- "golang.org/x/term"10+ "github.com/charmbracelet/x/term"11 )12 13 func (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 }22 23+ if f, ok := p.output.(term.File); ok && term.IsTerminal(f.Fd()) {24+ p.ttyOutput = f25+ }26+27 return nil28 }29 30 func openInputTTY() (*os.File, error) {6 unmodified linesmodified:tty_windows.go+26 added-8 removed
5 unmodified lines6 import (7 "fmt"8 "os"9 10+ "github.com/charmbracelet/x/term"11 "golang.org/x/sys/windows"11- "golang.org/x/term"12 )13 14 func (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 }24 25 // 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 }30 31- 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 }35 36+ // 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+ }43+44+ 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+ }48+49+ 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+ }53+54 return55 }56 57 // Open the Windows equivalent of a TTY.58 func 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, nil1 unmodified linemodified:tty.go+10 added-7 removed
2 unmodified lines3 import (4 "errors"5 "fmt"6 "io"7- "os"7 "time"8 9+ "github.com/charmbracelet/x/term"10 "github.com/muesli/cancelreader"11- "golang.org/x/term"11 )12 13 func (p *Program) initTerminal() error {14 if err := p.initInput(); err != nil {24 unmodified lines39 }40 41 // restoreInput restores the tty input to its original state.42 func (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 }55 56 // initCancelReader (re)commences reading inputs.35 unmodified lines92 93 // checkResize detects the current size of the output and informs the program94 // via a WindowSizeMsg.95 func (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 }100 98- 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