UNPKG

3.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
35
36
37term.clear() ;
38
39var document = term.createDocument( {
40 palette: new termkit.Palette()
41 // backgroundAttr: { bgColor: 'magenta' , dim: true } ,
42} ) ;
43
44var button1 = new termkit.Button( {
45 parent: document ,
46 //content: '> button#1' ,
47 content: '> ^[fg:*royal-blue]button#1' ,
48 contentHasMarkup: true ,
49 value: 'b1' ,
50 x: 10 ,
51 y: 10 ,
52} ) ;
53
54var button2 = new termkit.Button( {
55 parent: document ,
56 content: '> button#2' ,
57 value: 'b2' ,
58 x: 13 ,
59 y: 12 ,
60 keyBindings: {
61 ENTER: 'submit' ,
62 CTRL_UP: 'submit' ,
63 CTRL_DOWN: 'submit'
64 } ,
65 actionKeyBindings: {
66 CTRL_UP: 'up' ,
67 CTRL_DOWN: 'down'
68 }
69} ) ;
70
71var toggleButton1 = new termkit.ToggleButton( {
72 parent: document ,
73 content: 'toggle#1' ,
74 //turnedOnLeftPadding: '☑ ' , turnedOffLeftPadding: '☐ ' ,
75 turnedOnLeftPadding: '✓ ' , turnedOffLeftPadding: '✗ ' ,
76 //value: true ,
77 x: 2 ,
78 y: 2 ,
79} ) ;
80
81var toggleButton2 = new termkit.ToggleButton( {
82 parent: document ,
83 content: 'toggle#2' ,
84 key: 'myKey' ,
85 //turnedOnLeftPadding: '☑ ' , turnedOffLeftPadding: '☐ ' ,
86 turnedOnLeftPadding: '✓ ' , turnedOffLeftPadding: '✗ ' ,
87 //value: true ,
88 x: 5 ,
89 y: 5 ,
90} ) ;
91
92//container1.draw() ;
93
94button1.on( 'submit' , onSubmit ) ;
95button2.on( 'submit' , onSubmit ) ;
96toggleButton1.on( 'toggle' , onToggle ) ;
97toggleButton1.on( 'submit' , onSubmit ) ;
98toggleButton2.on( 'toggle' , onToggle ) ;
99toggleButton2.on( 'submit' , onSubmit ) ;
100
101var counter = 0 ;
102
103function onSubmit( value , action ) {
104 term.saveCursor() ;
105 term.moveTo.styleReset.eraseLine( 1 , 22 , 'Submitted #%i: %J , action: %J\n' , counter ++ , value , action ) ;
106 term.restoreCursor() ;
107}
108
109function onToggle( value ) {
110 term.saveCursor() ;
111 term.moveTo.styleReset.eraseLine( 1 , 22 , 'Toggled #%i: %J\n' , counter ++ , value ) ;
112 term.restoreCursor() ;
113}
114
115
116
117document.focusNext() ;
118
119
120
121term.on( 'key' , function( key ) {
122 switch( key )
123 {
124 case 'CTRL_C' :
125 term.grabInput( false ) ;
126 term.hideCursor( false ) ;
127 term.styleReset() ;
128 term.clear() ;
129 process.exit() ;
130 break ;
131 }
132} ) ;
133
134
135