@@ -13,20 +13,23 @@ import {
1313 close ,
1414 createInput ,
1515 createTerm ,
16+ CSI ,
1617 type CursorEvent ,
18+ DSR ,
19+ ESC ,
1720 fixed ,
1821 grow ,
1922 type Op ,
2023 open ,
2124 rgba ,
25+ SHOWCURSOR ,
2226 text ,
2327} from "../mod.ts" ;
2428import { cursor , settings } from "../settings.ts" ;
2529import { validated } from "../validate.ts" ;
2630
27- let write = ( b : Uint8Array ) => Deno . stdout . writeSync ( b ) ;
2831let encode = ( s : string ) => new TextEncoder ( ) . encode ( s ) ;
29- let esc = ( s : string ) => write ( encode ( s ) ) ;
32+ let write = ( b : Uint8Array ) => Deno . stdout . writeSync ( b ) ;
3033
3134let GREEN = rgba ( 80 , 250 , 123 ) ;
3235let GRAY = rgba ( 100 , 100 , 100 ) ;
@@ -44,7 +47,7 @@ let BRAILLE = ["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "
4447
4548function * queryCursor ( ) : Operation < CursorEvent > {
4649 let parser = yield * until ( createInput ( { escLatency : 100 } ) ) ;
47- esc ( "\x1b[6n" ) ;
50+ write ( DSR ( ) ) ;
4851
4952 let buf = new Uint8Array ( 32 ) ;
5053 while ( true ) {
@@ -67,7 +70,7 @@ function waitKey() {
6770 for ( let i = 0 ; i < n ; i ++ ) {
6871 if ( buf [ i ] === 0x03 ) {
6972 Deno . stdin . setRaw ( false ) ;
70- esc ( "\x1b[?25h" ) ;
73+ write ( SHOWCURSOR ( ) ) ;
7174 Deno . exit ( 0 ) ;
7275 }
7376 }
@@ -111,13 +114,13 @@ function* transaction(
111114) : Operation < void > {
112115 let { columns } = Deno . consoleSize ( ) ;
113116
114- esc ( "\n" . repeat ( height ) ) ;
117+ write ( encode ( "\n" . repeat ( height ) ) ) ;
115118
116119 let pos = yield * queryCursor ( ) ;
117120 /** 1-based terminal row where the region starts */
118- let row = pos . top - height + 1 ;
121+ let row = pos . row - height + 1 ;
119122
120- esc ( "\x1b[s" ) ;
123+ write ( ESC ( "7" ) ) ;
121124 let tty = settings ( cursor ( false ) ) ;
122125 write ( tty . apply ) ;
123126
@@ -131,17 +134,17 @@ function* transaction(
131134 }
132135
133136 write ( tty . revert ) ;
134- esc ( "\x1b[u" ) ;
135- esc ( "\n" ) ;
137+ write ( ESC ( "8" ) ) ;
138+ write ( encode ( "\n" ) ) ;
136139}
137140
138141function say ( msg : string ) {
139- esc ( msg + "\n" ) ;
142+ write ( encode ( msg + "\n" ) ) ;
140143}
141144
142145function pause ( ) {
143146 waitKey ( ) ;
144- esc ( "\n" ) ;
147+ write ( encode ( "\n" ) ) ;
145148}
146149
147150await main ( function * ( ) {
@@ -157,13 +160,13 @@ await main(function* () {
157160 say ( "" ) ;
158161
159162 // Demo 1: Spinner box
160- esc ( "\n\n\n" ) ;
163+ write ( encode ( "\n\n\n" ) ) ;
161164
162165 let pos = yield * queryCursor ( ) ;
163166 /** 1-based terminal row where the region starts */
164- let row = pos . top - 2 ;
167+ let row = pos . row - 2 ;
165168
166- esc ( "\x1b[s" ) ;
169+ write ( ESC ( "7" ) ) ;
167170
168171 let frames = 30 ;
169172 let term = validated (
@@ -195,14 +198,16 @@ await main(function* () {
195198 yield * sleep ( 80 ) ;
196199 }
197200
198- esc ( "\x1b[u" ) ;
199- esc ( "\x1b[ 0m") ;
200- esc ( "\n" ) ;
201+ write ( ESC ( "8" ) ) ;
202+ write ( CSI ( " 0m") ) ;
203+ write ( encode ( "\n" ) ) ;
201204
202205 yield * sleep ( 500 ) ;
203206
204- esc (
205- "\nRegions can be multi-line, but they can be a single line too. (continue...)" ,
207+ write (
208+ encode (
209+ "\nRegions can be multi-line, but they can be a single line too. (continue...)" ,
210+ ) ,
206211 ) ;
207212 pause ( ) ;
208213
@@ -251,9 +256,9 @@ await main(function* () {
251256 50 ,
252257 ) ;
253258
254- esc ( "\x1b[ 0m") ;
259+ write ( CSI ( " 0m") ) ;
255260 yield * sleep ( 500 ) ;
256- esc ( "\nGoodbye sadness with limitless sky. (continue...)" ) ;
261+ write ( encode ( "\nGoodbye sadness with limitless sky. (continue...)" ) ) ;
257262 pause ( ) ;
258263
259264 // Demo 3: Nyan cat
@@ -331,7 +336,8 @@ await main(function* () {
331336 60 ,
332337 ) ;
333338
334- esc ( "\x1b[0m\n" ) ;
339+ write ( CSI ( "0m" ) ) ;
340+ write ( encode ( "\n" ) ) ;
335341 write ( tty . revert ) ;
336342 Deno . stdin . setRaw ( false ) ;
337343} ) ;
0 commit comments