UNPKG

5.41 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
32/* jshint unused:false */
33
34
35
36require( '../lib/termkit.js' ).getDetectedTerminal( function( error , term ) {
37
38
39 function terminate()
40 {
41 setTimeout( function() {
42 //term.brightBlack( 'About to exit...\n' ) ;
43 term.grabInput( false ) ;
44 term.fullscreen( false ) ;
45 term.applicationKeypad( false ) ;
46 term.beep() ;
47
48 // Add a 100ms delay, so the terminal will be ready when the process effectively exit, preventing bad escape sequences drop
49 setTimeout( function() { process.exit() ; } , 100 ) ;
50 } , 100 ) ;
51 }
52
53
54
55 // Switch to fullscreen (alternate) buffer mode
56 term.fullscreen() ;
57
58 // Set Application Keypad mode, but it does not works on every box (sometime numlock should be off for this to work)
59 term.applicationKeypad() ;
60
61 term.moveTo( 1 , 1 )
62 .bgWhite.blue( 'Arrows: move, CTRL+Arrows: draw, SHIFT+Arrows: draw brighter\nTAB: switch mode, ENTER/SPACE: in-place drawing 1-8: change color\n' )
63 .bgWhite.green( 'CTRL-C to quit (startup size: %ix%i)\n' , term.width , term.height )
64 .moveTo( 40 , 20 ) ;
65
66
67
68 term.grabInput( { mouse: 'button' , focus: true } ) ;
69 term.requestCursorLocation().requestScreenSize() ;
70
71
72 var color = 2 , x = '' , y = '' , width = '' , height = '' ;
73 var mode = 0 , modeString = [ 'move' , 'draw' , 'draw brigther' ] ;
74
75 term.on( 'key' , function( key , matches , data ) {
76
77 switch ( key )
78 {
79 case '1' :
80 case '2' :
81 case '3' :
82 case '4' :
83 case '5' :
84 case '6' :
85 case '7' :
86 case '8' :
87 color = parseInt( key ) - 1 ;
88 break ;
89 case 'ALT_UP' : term.up( 1 ) ; break ;
90 case 'ALT_DOWN' : term.down( 1 ) ; break ;
91 case 'ALT_LEFT' : term.left( 1 ) ; break ;
92 case 'ALT_RIGHT' : term.right( 1 ) ; break ;
93 case 'CTRL_UP' : term.bgColor( color , ' ' ).left.up( 1 , 1 ).bgColor( color , ' ' ).left( 1 ) ; break ;
94 case 'CTRL_DOWN' : term.bgColor( color , ' ' ).left.down( 1 , 1 ).bgColor( color , ' ' ).left( 1 ) ; break ;
95 case 'CTRL_LEFT' : term.bgColor( color , ' ' ).left( 2 ).bgColor( color , ' ' ).left( 1 ) ; break ;
96 case 'CTRL_RIGHT' : term.bgColor( color , ' ' ).left( 1 ) ; break ;
97 case 'SHIFT_UP' : term.bgColor( color + 8 , ' ' ).left.up( 1 , 1 ).bgColor( color + 8 , ' ' ).left( 1 ) ; break ;
98 case 'SHIFT_DOWN' : term.bgColor( color + 8 , ' ' ).left.down( 1 , 1 ).bgColor( color + 8 , ' ' ).left( 1 ) ; break ;
99 case 'SHIFT_LEFT' : term.bgColor( color + 8 , ' ' ).left( 2 ).bgColor( color + 8 , ' ' ).left( 1 ) ; break ;
100 case 'SHIFT_RIGHT' : term.bgColor( color + 8 , ' ' ).left( 1 ) ; break ;
101 case 'UP' :
102 if ( ! mode ) { term.up( 1 ) ; }
103 else { term.bgColor( color + 8 * ( mode - 1 ) , ' ' ).left.up( 1 , 1 ).bgColor( color + 8 * ( mode - 1 ) , ' ' ).left( 1 ) ; }
104 break ;
105 case 'DOWN' :
106 if ( ! mode ) { term.down( 1 ) ; }
107 else { term.bgColor( color + 8 * ( mode - 1 ) , ' ' ).left.down( 1 , 1 ).bgColor( color + 8 * ( mode - 1 ) , ' ' ).left( 1 ) ; }
108 break ;
109 case 'LEFT' :
110 if ( ! mode ) { term.left( 1 ) ; }
111 else { term.bgColor( color + 8 * ( mode - 1 ) , ' ' ).left( 2 ).bgColor( color + 8 * ( mode - 1 ) , ' ' ).left( 1 ) ; }
112 break ;
113 case 'RIGHT' :
114 if ( ! mode ) { term.right( 1 ) ; }
115 else { term.bgColor( color + 8 * ( mode - 1 ) , ' ' ).left( 1 ) ; }
116 break ;
117 case 'ENTER' : term.bgColor( color , ' ' ).left( 1 ) ; break ;
118 case ' ' : term.bgColor( color + 8 , ' ' ).left( 1 ) ; break ;
119 case 'TAB' : mode = ( mode + 1 ) % 3 ; break ;
120 case 'CTRL_C' : terminate() ; break ;
121 }
122
123 term.requestCursorLocation()
124 .saveCursor()
125 .moveTo( 1 , term.height )
126 .eraseLine()
127 .bgWhite.blue(
128 'Arrow mode: ' + modeString[ mode ] +
129 ' -- Cur pos: ' + x + ',' + y +
130 ' -- Scr size: ' + term.width + ',' + term.height +
131 ' -- Key: ' + key
132 )
133 .restoreCursor() ;
134 } ) ;
135
136
137
138 term.on( 'terminal' , function( name , data ) {
139
140 switch ( name )
141 {
142 case 'CURSOR_LOCATION' : x = data.x ; y = data.y ; break ;
143 case 'SCREEN_SIZE' : width = data.width ; height = data.height ; break ;
144 }
145 } ) ;
146
147
148
149 term.on( 'mouse' , function( name , data ) {
150 // console.log( "'mouse' event:" , name , data ) ;
151 } ) ;
152
153
154
155 term.on( 'unknown' , function( buffer ) {
156 // console.log( "'unknown' event, buffer:" , buffer ) ;
157 } ) ;
158
159
160} ) ;
161