1 | #!/usr/bin/env node
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 | "use strict" ;
|
29 |
|
30 |
|
31 |
|
32 | const termkit = require( '../../lib/termkit.js' ) ;
|
33 | const term = termkit.terminal ;
|
34 |
|
35 | term.clear() ;
|
36 |
|
37 |
|
38 |
|
39 | var document = term.createDocument( {
|
40 |
|
41 | } ) ;
|
42 |
|
43 |
|
44 |
|
45 | var placeHolder = 'console.log( "Hello world!" ) ;' ;
|
46 |
|
47 | try {
|
48 | var StateMachine = require( 'text-machine' ) ;
|
49 |
|
50 | var stateMachine = new StateMachine( {
|
51 | program: require( 'text-machine/sample/javascript.js' ) ,
|
52 | api: termkit.TextBuffer.TextMachineApi
|
53 | } ) ;
|
54 | }
|
55 | catch( error ) {
|
56 | placeHolder = 'Try to:\n"npm install text-machine"\n... to enjoy a mini demo of\na Javascript syntax highlighter!' ;
|
57 | }
|
58 |
|
59 |
|
60 |
|
61 | var textBox = new termkit.EditableTextBox( {
|
62 | parent: document ,
|
63 | content: placeHolder ,
|
64 | attr: { bgColor: 'black' } ,
|
65 |
|
66 | x: 10 ,
|
67 | y: 2 ,
|
68 | width: 50 ,
|
69 | height: 20 ,
|
70 | scrollable: true ,
|
71 | vScrollBar: true ,
|
72 |
|
73 | wordWrap: true ,
|
74 | stateMachine: stateMachine
|
75 | } ) ;
|
76 |
|
77 | document.giveFocusTo( textBox ) ;
|
78 |
|
79 | term.on( 'key' , function( key ) {
|
80 |
|
81 | switch( key ) {
|
82 | case 'CTRL_C' :
|
83 | term.grabInput( false ) ;
|
84 | term.hideCursor( false ) ;
|
85 | term.styleReset() ;
|
86 | term.clear() ;
|
87 | process.exit() ;
|
88 | break ;
|
89 |
|
90 | case 'CTRL_K' :
|
91 | term.saveCursor() ;
|
92 | term.moveTo( 1 , 25 ) ;
|
93 | term.styleReset() ;
|
94 | term.eraseDisplayBelow() ;
|
95 | term( "Content: %s" , textBox.getContent().replace( /\n/g , '\\n' ).replace( /\t/g , '\\t' ) ) ;
|
96 | term.restoreCursor() ;
|
97 | break ;
|
98 |
|
99 | case 'CTRL_W' :
|
100 | textBox.textBuffer.wrapAllLines( 20 ) ;
|
101 | textBox.draw() ;
|
102 | break ;
|
103 | }
|
104 | } ) ;
|
105 |
|