UNPKG

5.27 kBJavaScriptView Raw
1#!/usr/bin/env node
2/*
3 Terminal Kit
4
5 Copyright (c) 2009 - 2020 Cédric Ronvel
6
7 The MIT License (MIT)
8
9 Permission is hereby granted, free of charge, to any person obtaining a copy
10 of this software and associated documentation files (the "Software"), to deal
11 in the Software without restriction, including without limitation the rights
12 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 copies of the Software, and to permit persons to whom the Software is
14 furnished to do so, subject to the following conditions:
15
16 The above copyright notice and this permission notice shall be included in all
17 copies or substantial portions of the Software.
18
19 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 SOFTWARE.
26*/
27
28"use strict" ;
29
30
31
32const termkit = require( '..' ) ;
33const term = termkit.terminal ;
34
35var isHd = process.argv[ 2 ] === 'hd' ;
36
37
38
39function terminate() {
40 setTimeout( () => {
41 //term.brightBlack( 'About to exit...\n' ) ;
42 term.grabInput( false ) ;
43 term.fullscreen( false ) ;
44 term.applicationKeypad( false ) ;
45 term.beep() ;
46
47 // Add a 100ms delay, so the terminal will be ready when the process effectively exit, preventing bad escape sequences drop
48 setTimeout( () => { process.exit() ; } , 100 ) ;
49 } , 100 ) ;
50}
51
52
53
54term.fullscreen() ;
55term.moveTo( 1 , 1 ).bgWhite.blue( 'Welcome to Neon! ' ).bgWhite.green( 'Ctrl-C to quit' ) ;
56term.grabInput() ;
57
58
59
60var attrs , emptyAttrs , attrsIndex = 0 , emptyAttrsIndex = 0 ;
61
62if ( isHd ) {
63 attrs = [
64 termkit.ScreenBufferHD.DEFAULT_ATTR ,
65 {
66 color: { r: 200 , g: 50 , b: 50 } , bgColor: { r: 0 , g: 0 , b: 0 }
67 } ,
68 {
69 color: { r: 230 , g: 70 , b: 70 } , bgColor: { r: 0 , g: 0 , b: 0 }
70 } ,
71 {
72 color: { r: 200 , g: 50 , b: 50 } , bgColor: { r: 0 , g: 0 , b: 70 } , bold: true , italic: true
73 } ,
74 {
75 color: { r: 30 , g: 170 , b: 170 } , bgColor: { r: 70 , g: 0 , b: 0 }
76 }
77 ] ;
78
79 emptyAttrs = [
80 { bgColor: { r: 200 , g: 200 , b: 0 } } ,
81 { bgColor: { r: 250 , g: 250 , b: 0 } } ,
82 { bgColor: { r: 230 , g: 10 , b: 0 } } ,
83 { bgColor: { r: 0 , g: 10 , b: 230 } } ,
84 termkit.ScreenBufferHD.DEFAULT_ATTR
85 ] ;
86}
87else {
88 attrs = [
89 termkit.ScreenBuffer.DEFAULT_ATTR ,
90 { color: 'red' , bgColor: 'black' } ,
91 { color: 'green' , bgColor: 'black' } ,
92 { color: 'blue' , bgColor: 'black' } ,
93 {
94 color: 'red' , bgColor: 'black' , bold: true , italic: true
95 } ,
96 { color: 'red' , bgColor: 'yellow' }
97 ] ;
98
99 emptyAttrs = [
100 { bgColor: 'yellow' } ,
101 { bgColor: 'brightYellow' } ,
102 { bgColor: 'red' } ,
103 { bgColor: 'blue' } ,
104 termkit.ScreenBuffer.DEFAULT_ATTR
105 ] ;
106}
107
108
109
110term.on( 'key' , ( key , matches , data ) => {
111
112 var draw , drawCursor ;
113
114
115 if ( data.isCharacter ) {
116 tbuf.insert( key , attrs[ attrsIndex ] ) ;
117 draw = drawCursor = true ;
118 }
119 else {
120 switch ( key ) {
121 case 'CTRL_K' :
122 term.saveCursor() ;
123 term.moveTo.styleReset.eraseLine.eraseDisplayBelow( 1 , 12 , 'Current: %Y\n%Y' , tbuf.getText() , tbuf.buffer.map( line => line.map( cell => JSON.stringify( cell ) ) ) ) ;
124 term.restoreCursor() ;
125 break ;
126 case 'CTRL_S' :
127 attrsIndex = ( attrsIndex + 1 ) % attrs.length ;
128 break ;
129 case 'CTRL_B' :
130 emptyAttrsIndex = ( emptyAttrsIndex + 1 ) % emptyAttrs.length ;
131 tbuf.setEmptyCellAttr( emptyAttrs[ emptyAttrsIndex ] ) ;
132 draw = drawCursor = true ;
133 break ;
134 case 'UP' :
135 tbuf.move( 0 , -1 ) ;
136 drawCursor = true ;
137 break ;
138 case 'DOWN' :
139 tbuf.move( 0 , 1 ) ;
140 drawCursor = true ;
141 break ;
142 case 'LEFT' :
143 //tbuf.move( -1 , 0 ) ;
144 tbuf.moveBackward() ;
145 drawCursor = true ;
146 break ;
147 case 'RIGHT' :
148 //tbuf.move( 1 , 0 ) ;
149 tbuf.moveForward() ;
150 drawCursor = true ;
151 break ;
152 case 'END' :
153 tbuf.moveToEndOfLine() ;
154 drawCursor = true ;
155 break ;
156 case 'HOME' :
157 tbuf.moveToColumn( 0 ) ;
158 drawCursor = true ;
159 break ;
160 case 'ENTER' :
161 tbuf.newLine() ;
162 draw = drawCursor = true ;
163 break ;
164 case 'DELETE' :
165 tbuf.delete( 1 ) ;
166 draw = drawCursor = true ;
167 break ;
168 case 'BACKSPACE' :
169 tbuf.backDelete( 1 ) ;
170 draw = drawCursor = true ;
171 break ;
172 case 'TAB' :
173 tbuf.insert( '\t' , attrs[ attrsIndex ] ) ;
174 draw = drawCursor = true ;
175 break ;
176 case 'CTRL_C' :
177 terminate() ;
178 break ;
179 }
180 }
181
182
183 if ( draw ) {
184 tbuf.draw() ;
185 sbuf.draw() ;
186 }
187
188 if ( drawCursor ) {
189 tbuf.drawCursor() ;
190 sbuf.drawCursor() ;
191 }
192} ) ;
193
194
195
196var sbuf ;
197
198if ( isHd ) {
199 sbuf = termkit.ScreenBufferHD.create( {
200 dst: term , width: 20 , height: 8 , x: 2 , y: 2
201 } ) ;
202}
203else {
204 sbuf = termkit.ScreenBuffer.create( {
205 dst: term , width: 20 , height: 8 , x: 2 , y: 2
206 } ) ;
207}
208
209var tbuf = termkit.TextBuffer.create( { dst: sbuf } ) ;
210tbuf.setEmptyCellAttr( emptyAttrs[ emptyAttrsIndex ] ) ;
211
212tbuf.draw() ;
213sbuf.draw() ;
214tbuf.drawCursor() ;
215sbuf.drawCursor() ;
216
217
218
219//console.log( '\n' , global.deb ) ;
220
221