UNPKG

3.3 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
36//console.error( "\n\n\n\n\n\n\n\n" ) ;
37var termkit = require( '../../lib/termkit.js' ) ;
38var term = termkit.terminal ;
39
40
41
42term.clear() ;
43
44var document = term.createDocument() ;
45
46
47var form = new termkit.Form( {
48 parent: document ,
49 x: 10 ,
50 y: 3 ,
51 width: 40 ,
52 inputs: [
53 {
54 key: 'login' ,
55 label: 'Login: ' ,
56 content: 'login@bob.net' ,
57
58 // Validators are not yet implemented
59 validator: { type: 'string' }
60 } ,
61 {
62 key: 'password' ,
63 label: 'Password: ' ,
64 hiddenContent: true ,
65 //textAttr: { bgColor: 'blue' , hiddenContent: true } ,
66 validator: { type: 'string' }
67 } ,
68 {
69 key: 'firstName' ,
70 label: 'first name: ' ,
71 validator: { type: 'string' }
72 } ,
73 {
74 key: 'lastName' ,
75 label: 'last name: ' ,
76 validator: { type: 'string' }
77 } ,
78 {
79 key: 'age' ,
80 label: 'age: ' ,
81 validator: { type: 'string' }
82 } ,
83 {
84 key: 'v1' ,
85 label: 'v1: ' ,
86 type: 'select' ,
87 value: 2 ,
88 items: [
89 { content: 'one' , value: 1 } ,
90 { content: 'two' , value: 2 } ,
91 { content: 'three' , value: 3 } ,
92 { content: 'four' , value: 4 }
93 ]
94 } ,
95 {
96 key: 'v2' ,
97 label: 'v2: ' ,
98 type: 'selectMulti' ,
99 //value: 2 ,
100 items: [
101 { content: 'un' , key: 'un' } ,
102 { content: 'deux' , key: 'deux' } ,
103 { content: 'trois' , key: 'trois' }
104 ]
105 } ,
106 {
107 key: 'comment' ,
108 label: 'comment: ' ,
109 content: 'multi\nline\ncontent' ,
110 height: 5 ,
111 scrollable: true ,
112 vScrollBar: true ,
113 validator: { type: 'string' }
114 } ,
115 ] ,
116 buttons: [
117 {
118 content: '<Ok>' ,
119 value: 'ok'
120 } ,
121 {
122 content: '<Cancel>' ,
123 value: 'cancel'
124 }
125 ]
126} ) ;
127
128
129
130form.on( 'submit' , onSubmit ) ;
131
132function onSubmit( value )
133{
134 //console.error( 'Submitted: ' , value ) ;
135 term.saveCursor() ;
136 term.moveTo.styleReset.eraseLine( 1 , 24 , 'Submitted: %J\n' , value ) ;
137 term.restoreCursor() ;
138}
139
140
141
142document.giveFocusTo( form ) ;
143
144term.on( 'key' , function( key ) {
145 switch( key )
146 {
147 case 'CTRL_C' :
148 term.grabInput( false ) ;
149 term.hideCursor( false ) ;
150 term.styleReset() ;
151 term.clear() ;
152 process.exit() ;
153 break ;
154 }
155} ) ;
156
157
158