UNPKG

4.07 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//require( '../lib/termkit.js' ).getDetectedTerminal( function( error , term ) {
33const termkit = require( '..' ) ;
34const term = termkit.terminal ;
35
36
37
38term.grabInput() ;
39
40var history = [
41 "OMG my name was supa long, so much long I can't remember what it is, can you believe that?" ,
42 'John' , 'Jack' , 'Joey' , 'Billy' , 'Bob'
43] ;
44
45var autoComplete = [
46 'Barack Obama' , 'George W. Bush' , 'Bill Clinton' , 'George Bush' ,
47 'Ronald W. Reagan' , 'Jimmy Carter' , 'Gerald Ford' , 'Richard Nixon' ,
48 'Lyndon Johnson' , 'John F. Kennedy' , 'Dwight Eisenhower' , 'Harry Truman' , 'Franklin Roosevelt'
49] ;
50
51term.on( 'key' , function( key ) {
52 if ( key === 'CTRL_C' )
53 {
54 term.green( 'CTRL-C detected...\n' ) ;
55 terminate() ;
56 }
57} ) ;
58
59function question() {
60 term.green( 'Please enter your name: ' ) ;
61
62 var field = term.inputField( {
63 //y: term.height , x: 1 ,
64 echoChar: '*' ,
65 //*
66 //default: 'mkdir ""' ,
67 //cursorPosition: -2 ,
68 history: history ,
69 autoComplete: autoComplete ,
70 autoCompleteMenu: true ,
71 autoCompleteHint: true ,
72 hintStyle: term.brightBlack.italic ,
73 //*/
74 //maxLength: 3
75 } , function( error , input ) {
76
77 if ( error )
78 {
79 term.red.bold( "\nAn error occurs: " + error + "\n" ) ;
80 terminate() ;
81 }
82 else
83 {
84 term.green( "\nYour name is '%s'\n" , input ) ;
85 terminate() ;
86 }
87 } ) ;
88
89 //setTimeout( () => field.setCursorPosition( 3 ) , 1000 ) ;
90 //setTimeout( () => console.log( '\npos:' , field.getCursorPosition() ) , 1000 ) ;
91}
92
93async function asyncQuestion() {
94 term.green( 'Please enter your name: ' ) ;
95
96 var input = await term.inputField( {
97 //y: term.height , x: 1 ,
98 //echoChar: '*' ,
99 //*
100 //default: 'mkdir ""' ,
101 //cursorPosition: -2 ,
102 history: history ,
103 autoComplete: autoComplete ,
104 autoCompleteMenu: true ,
105 autoCompleteHint: true ,
106 hintStyle: term.brightBlack.italic ,
107 //*/
108 //maxLength: 3
109 } ).promise ;
110
111 term.green( "\nYour name is '%s'\n" , input ) ;
112 terminate() ;
113}
114
115
116
117function funkyCursor() {
118 //*
119 term.setCursorColorRgb(
120 Math.floor( 30 + Math.random() * 200 ) ,
121 Math.floor( 30 + Math.random() * 200 ) ,
122 Math.floor( 30 + Math.random() * 200 )
123 ) ;
124 //*/
125
126 /*
127 term.setCursorColor(
128 Math.floor( Math.random() * 8 ) ,
129 Math.floor( Math.random() * 8 )
130 ) ;
131 //*/
132
133 setTimeout( funkyCursor , 200 ) ;
134}
135
136
137
138function funkyBackground() {
139 term.setDefaultBgColorRgb(
140 Math.floor( 0 + Math.random() * 100 ) ,
141 Math.floor( 0 + Math.random() * 100 ) ,
142 Math.floor( 0 + Math.random() * 100 )
143 ) ;
144 term.setDefaultColorRgb(
145 Math.floor( 150 + Math.random() * 100 ) ,
146 Math.floor( 150 + Math.random() * 100 ) ,
147 Math.floor( 150 + Math.random() * 100 )
148 ) ;
149 setTimeout( funkyBackground , 2000 ) ;
150}
151
152
153
154function terminate() {
155 term.processExit() ;
156}
157
158
159term.bold.cyan( 'Input field test, type something and hit the ENTER key...\n' ) ;
160//question() ;
161asyncQuestion() ;
162//funkyCursor() ;
163//funkyBackground() ;
164