2022-11-01 17:28:14 +02:00
const BaseCommand = require ( './base-command' ) . default ;
2017-11-03 02:09:34 +02:00
const { app } = require ( './app.js' ) ;
const { renderCommandHelp } = require ( './help-utils.js' ) ;
2020-11-07 17:59:37 +02:00
const { _ } = require ( '@joplin/lib/locale' ) ;
2017-11-03 02:09:34 +02:00
const { cliUtils } = require ( './cli-utils.js' ) ;
2017-08-04 19:11:10 +02:00
class Command extends BaseCommand {
usage ( ) {
return 'help [command]' ;
}
description ( ) {
return _ ( 'Displays usage information.' ) ;
}
2017-10-29 17:41:30 +02:00
allCommands ( ) {
2017-12-07 20:12:46 +02:00
const commands = app ( ) . commands ( app ( ) . uiType ( ) ) ;
2020-03-14 01:46:14 +02:00
const output = [ ] ;
for ( const n in commands ) {
2017-10-29 17:41:30 +02:00
if ( ! commands . hasOwnProperty ( n ) ) continue ;
const command = commands [ n ] ;
if ( command . hidden ( ) ) continue ;
if ( ! command . enabled ( ) ) continue ;
output . push ( command ) ;
}
2019-07-30 09:35:42 +02:00
output . sort ( ( a , b ) => ( a . name ( ) < b . name ( ) ? - 1 : + 1 ) ) ;
2017-10-29 17:41:30 +02:00
return output ;
}
2017-08-04 19:11:10 +02:00
async action ( args ) {
2017-10-09 00:34:01 +02:00
const stdoutWidth = app ( ) . commandStdoutMaxWidth ( ) ;
2017-08-04 19:11:10 +02:00
2018-02-17 00:53:53 +02:00
if ( args . command === 'shortcuts' || args . command === 'keymap' ) {
2023-10-30 13:32:14 +02:00
this . stdout ( _ ( 'For information on how to customise the shortcuts please visit %s' , 'https://joplinapp.org/help/apps/terminal#shortcuts' ) ) ;
2018-02-17 01:17:55 +02:00
this . stdout ( '' ) ;
2019-07-30 09:35:42 +02:00
if (
app ( )
. gui ( )
. isDummy ( )
) {
2017-10-29 17:41:30 +02:00
throw new Error ( _ ( 'Shortcuts are not available in CLI mode.' ) ) ;
}
2019-07-30 09:35:42 +02:00
const keymap = app ( )
. gui ( )
. keymap ( ) ;
2017-10-09 00:34:01 +02:00
2020-03-14 01:46:14 +02:00
const rows = [ ] ;
2017-10-09 00:34:01 +02:00
2018-02-17 00:53:53 +02:00
for ( let i = 0 ; i < keymap . length ; i ++ ) {
const item = keymap [ i ] ;
2020-05-21 10:14:33 +02:00
const keys = item . keys . map ( k => ( k === ' ' ? '(SPACE)' : k ) ) ;
2018-02-17 00:53:53 +02:00
rows . push ( [ keys . join ( ', ' ) , item . command ] ) ;
2017-10-09 00:34:01 +02:00
}
cliUtils . printArray ( this . stdout . bind ( this ) , rows ) ;
2017-10-29 17:41:30 +02:00
} else if ( args . command === 'all' ) {
const commands = this . allCommands ( ) ;
2020-05-21 10:14:33 +02:00
const output = commands . map ( c => renderCommandHelp ( c ) ) ;
2017-10-29 17:41:30 +02:00
this . stdout ( output . join ( '\n\n' ) ) ;
2017-10-09 00:34:01 +02:00
} else if ( args . command ) {
const command = app ( ) . findCommandByName ( args [ 'command' ] ) ;
if ( ! command ) throw new Error ( _ ( 'Cannot find "%s".' , args . command ) ) ;
this . stdout ( renderCommandHelp ( command , stdoutWidth ) ) ;
} else {
2020-05-21 10:14:33 +02:00
const commandNames = this . allCommands ( ) . map ( a => a . name ( ) ) ;
2017-10-09 00:34:01 +02:00
2017-12-15 09:31:57 +02:00
this . stdout ( _ ( 'Type `help [command]` for more information about a command; or type `help all` for the complete usage information.' ) ) ;
2017-10-17 19:18:31 +02:00
this . stdout ( '' ) ;
this . stdout ( _ ( 'The possible commands are:' ) ) ;
this . stdout ( '' ) ;
this . stdout ( commandNames . join ( ', ' ) ) ;
this . stdout ( '' ) ;
2018-05-05 16:22:14 +02:00
this . stdout ( _ ( 'In any command, a note or notebook can be referred to by title or ID, or using the shortcuts `$n` or `$b` for, respectively, the currently selected note or notebook. `$c` can be used to refer to the currently selected item.' ) ) ;
2017-10-29 22:40:53 +02:00
this . stdout ( '' ) ;
2017-10-31 00:29:17 +02:00
this . stdout ( _ ( 'To move from one pane to another, press Tab or Shift+Tab.' ) ) ;
2017-10-24 23:09:22 +02:00
this . stdout ( _ ( 'Use the arrows and page up/down to scroll the lists and text areas (including this console).' ) ) ;
2018-04-16 16:29:34 +02:00
this . stdout ( _ ( 'To maximise/minimise the console, press "tc".' ) ) ;
2017-10-17 19:18:31 +02:00
this . stdout ( _ ( 'To enter command line mode, press ":"' ) ) ;
this . stdout ( _ ( 'To exit command line mode, press ESCAPE' ) ) ;
2018-02-17 01:17:55 +02:00
this . stdout ( _ ( 'For the list of keyboard shortcuts and config options, type `help keymap`' ) ) ;
2017-10-09 00:34:01 +02:00
}
2017-08-04 22:13:29 +02:00
2019-07-30 09:35:42 +02:00
app ( )
. gui ( )
. showConsole ( ) ;
app ( )
. gui ( )
. maximizeConsole ( ) ;
2017-08-04 19:11:10 +02:00
}
}
2019-07-30 09:35:42 +02:00
module . exports = Command ;