UNPKG

4.47 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
32var term = require( '..' ).terminal ;
33
34term.grabInput( { mouse: 'motion' } ) ;
35
36var items = [
37 '汉字汉字汉字汉字汉字汉字汉字汉字' , '汉字汉字汉字汉字汉字汉字汉字汉字' ,
38 'File' , 'Edit' , 'View' , 'History' , 'Bookmarks' , 'Tools' , 'Help' ,
39 term.str( '^RR^Ya^Gi^Cn^Bb^Mow^ Warrior' ) ,
40 '汉字汉字汉字汉字汉字汉字汉字汉字汉字汉字汉字汉字汉字汉字汉字汉字汉字汉字汉字汉字汉字汉字汉字汉字' ,
41 '汉字汉字汉字汉字汉字汉字汉字汉字' , '汉字汉字汉字汉字汉字汉字汉字汉字' , '汉字汉字汉字汉字汉字汉字汉字汉字' , '汉字汉字汉字汉字汉字汉字汉字汉字' , '汉字汉字汉字汉字汉字汉字汉字汉字'
42] ;
43
44function menu() {
45 var options = {
46 y: 1 ,
47 style: term.inverse ,
48 selectedStyle: term.dim.blue.bgGreen
49 } ;
50
51 term.singleLineMenu( items , options , ( error2 , response ) => {
52 if ( error2 ) {
53 term.red.bold( "\nAn error occurs: " + error + "\n" ) ;
54 terminate() ;
55 return ;
56 }
57
58 term.green( "\n#%s selected: %s (%s,%s)\n" , response.selectedIndex , response.selectedText , response.x , response.y ) ;
59 terminate() ;
60 } ) ;
61
62 //menu_.on( 'highlight' , eventData => console.error( '\neventData:' , eventData ) ) ;
63}
64
65function menuAlign( align ) {
66 var options = {
67 y: 1 ,
68 align ,
69 fillIn: true ,
70 style: term.inverse ,
71 selectedStyle: term.dim.blue.bgGreen
72 } ;
73
74 term.singleLineMenu( items , options , ( error2 , response ) => {
75 if ( error2 ) {
76 term.red.bold( "\nAn error occurs: " + error + "\n" ) ;
77 terminate() ;
78 return ;
79 }
80
81 term.green( "\n#%s selected: %s (%s,%s)\n" , response.selectedIndex , response.selectedText , response.x , response.y ) ;
82 terminate() ;
83 } ) ;
84
85 //menu_.on( 'highlight' , eventData => console.error( '\neventData:' , eventData ) ) ;
86}
87
88function menuSelected( selectedIndex ) {
89 var options = {
90 y: 1 ,
91 selectedIndex ,
92 style: term.inverse ,
93 selectedStyle: term.dim.blue.bgGreen
94 } ;
95
96 term.singleLineMenu( items , options , ( error2 , response ) => {
97 if ( error2 ) {
98 term.red.bold( "\nAn error occurs: " + error + "\n" ) ;
99 terminate() ;
100 return ;
101 }
102
103 term.green( "\n#%s selected: %s (%s,%s)\n" , response.selectedIndex , response.selectedText , response.x , response.y ) ;
104 terminate() ;
105 } ) ;
106}
107
108async function menuAsync() {
109 var options = {
110 y: 1 ,
111 style: term.inverse ,
112 selectedStyle: term.dim.blue.bgGreen
113 } ;
114
115 var response = await term.singleLineMenu( items , options ).promise ;
116 term.green( "\n#%s selected: %s (%s,%s)\n" , response.selectedIndex , response.selectedText , response.x , response.y ) ;
117 terminate() ;
118}
119
120function terminate() {
121 term.grabInput( false ) ;
122 // Add a 100ms delay, so the terminal will be ready when the process effectively exit, preventing bad escape sequences drop
123 setTimeout( () => { process.exit() ; } , 100 ) ;
124}
125
126term.clear() ;
127term.bold.cyan( '\n\nSelect one item from the menu!' ) ;
128
129switch ( process.argv[ 2 ] ) {
130 case 'center' :
131 menuAlign( 'center' ) ;
132 break ;
133 case 'right' :
134 menuAlign( 'right' ) ;
135 break ;
136 case 'left' :
137 menuAlign( 'left' ) ;
138 break ;
139 case 'selected' :
140 menuSelected( parseInt( process.argv[ 3 ] , 10 ) || 0 ) ;
141 break ;
142 case 'menuAsync' :
143 menuAsync() ;
144 break ;
145 default :
146 menu() ;
147 break ;
148}