Skip to content

Commit

Permalink
Update singleLineMenu to support ESC escape key
Browse files Browse the repository at this point in the history
  • Loading branch information
hfhchan authored Oct 2, 2018
1 parent e91c22a commit 1a1ed39
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions lib/singleLineMenu.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ var defaultKeyBindings = {
TAB: 'cycleNext' ,
SHIFT_TAB: 'cyclePrevious' ,
HOME: 'first' ,
END: 'last'
END: 'last',
ESCAPE: 'escape'
} ;


Expand All @@ -60,6 +61,7 @@ var defaultKeyBindings = {
* style `function` the style of unselected items, default to `term`
* selectedStyle `function` the style of the selected item, default to `term.dim.blue.bgGreen`
* keyBindings `Object` overide default key bindings
* cancelable `boolean` if ESCAPE is pressed, it exits, calling the callback with undefined values
* exitOnUnexpectedKey `boolean` if an unexpected key is pressed, it exits, calling the callback with undefined values
* callback( error , response ), where:
* error
Expand All @@ -69,6 +71,7 @@ var defaultKeyBindings = {
* x `number` the x coordinate of the selected menu item (the first character)
* y `number` the y coordinate of the selected menu item (same coordinate for all items since it's a single line menu)
* unexpectedKey `string` when 'exitOnUnexpectedKey' option is set, this contains the key that produced the exit
* canceled `bool` when 'cancelable' option is set, this is set to true
*/
module.exports = function singleLineMenu( menuItems_ , options , callback ) {
if ( arguments.length < 1 ) { throw new Error( '[terminal] singleLineMenu() needs at least an array of menuItems' ) ; }
Expand Down Expand Up @@ -293,7 +296,14 @@ module.exports = function singleLineMenu( menuItems_ , options , callback ) {
if ( selectedPage < menuPages.length - 1 ) { selectedPage ++ ; selectedIndex = 0 ; }
redraw() ;
break ;

case 'escape' :
if ( options.cancelable ) {
cleanup( undefined , { canceled: true } ) ;
}
if ( options.exitOnUnexpectedKey ) {
cleanup( undefined , { unexpectedKey: key , unexpectedKeyData: data } ) ;
}
break ;
default :
if ( options.exitOnUnexpectedKey ) {
cleanup( undefined , { unexpectedKey: key , unexpectedKeyData: data } ) ;
Expand Down

0 comments on commit 1a1ed39

Please sign in to comment.