1 |
|
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 | ;
|
29 |
|
30 |
|
31 |
|
32 | require( '../lib/termkit.js' ).getDetectedTerminal( function( error , term ) {
|
33 |
|
34 | var menu_ ;
|
35 |
|
36 | term.grabInput( { mouse: 'motion' } ) ;
|
37 |
|
38 | term.on( 'key' , ( name ) => {
|
39 |
|
40 | if ( name === 'CTRL_C' )
|
41 | {
|
42 | if ( menu_ ) { menu_.stop() ; }
|
43 | term.green( 'CTRL-C received...\n' ) ;
|
44 | term.processExit() ;
|
45 | }
|
46 | } ) ;
|
47 |
|
48 |
|
49 |
|
50 | var items = [
|
51 | 'File' , 'Edit' , 'View' , 'History' , 'Bookmarks' , 'Tools' , 'Help'
|
52 | ] ;
|
53 |
|
54 | items.push( 'a very looooooooooooooong menu entry! '.repeat( 8 ) ) ;
|
55 | items.push( term.str( '^ra ^cvery ^glo^boo^yooo^moooooo^/ooong^ ^cmenu ^Yentry ^_with escape sequences! ' ).repeat( 8 ) ) ;
|
56 |
|
57 |
|
58 |
|
59 | function menu() {
|
60 | var options = {
|
61 | //ellipsis: true ,
|
62 | selectedLeftPadding: '*' ,
|
63 | extraLines: 2 ,
|
64 | //continueOnSubmit: true ,
|
65 | //keyBindings: { ENTER: 'submit' , UP: 'previous' , p: 'previous' , DOWN: 'next' , n: 'next' } ,
|
66 | //y: 1 ,
|
67 | //style: term.inverse ,
|
68 | //selectedStyle: term.dim.blue.bgGreen
|
69 | } ;
|
70 |
|
71 | menu_ = term.singleColumnMenu( items , options , function( error , response ) {
|
72 |
|
73 | if ( error )
|
74 | {
|
75 | term.red.bold( "\nAn error occurs: " + error + "\n" ) ;
|
76 | term.processExit() ;
|
77 | return ;
|
78 | }
|
79 |
|
80 | term.green( "\n#%s %s: %s (%s,%s)\n" ,
|
81 | response.selectedIndex ,
|
82 | response.submitted ? 'submitted' : 'selected' ,
|
83 | response.selectedText ,
|
84 | response.x ,
|
85 | response.y
|
86 | ) ;
|
87 |
|
88 | term.processExit() ;
|
89 | } ) ;
|
90 |
|
91 | //menu_.on( 'highlight' , eventData => console.error( '\neventData:' , eventData ) ) ;
|
92 |
|
93 | setTimeout( menu_.pause.bind( menu_ ) , 1500 ) ;
|
94 | setTimeout( menu_.resume.bind( menu_ ) , 3000 ) ;
|
95 | }
|
96 |
|
97 |
|
98 |
|
99 | async function asyncMenu() {
|
100 | var options = {
|
101 | //ellipsis: true ,
|
102 | selectedLeftPadding: '*' ,
|
103 | extraLines: 2 ,
|
104 | unsubmittableIndexes: [ false , true ] ,
|
105 | //continueOnSubmit: true ,
|
106 | //keyBindings: { ENTER: 'submit' , UP: 'previous' , p: 'previous' , DOWN: 'next' , n: 'next' } ,
|
107 | //y: 1 ,
|
108 | //style: term.inverse ,
|
109 | //selectedStyle: term.dim.blue.bgGreen
|
110 | } ;
|
111 |
|
112 | var response = await term.singleColumnMenu( items , options ).promise ;
|
113 |
|
114 | term.green( "\n#%s %s: %s (%s,%s)\n" ,
|
115 | response.selectedIndex ,
|
116 | response.submitted ? 'submitted' : 'selected' ,
|
117 | response.selectedText ,
|
118 | response.x ,
|
119 | response.y
|
120 | ) ;
|
121 |
|
122 | term.processExit() ;
|
123 | }
|
124 |
|
125 |
|
126 |
|
127 | //term.clear() ;
|
128 | term.bold.cyan( '\n\nSelect one item from the menu!' ) ;
|
129 |
|
130 | menu() ;
|
131 | //asyncMenu() ;
|
132 | } ) ;
|
133 |
|
134 |
|