diff --git a/CliClient/app/app-gui.js b/CliClient/app/app-gui.js index 3bcc2aada..29daf3d1e 100644 --- a/CliClient/app/app-gui.js +++ b/CliClient/app/app-gui.js @@ -120,6 +120,7 @@ class AppGui { setupShortcuts() { const shortcuts = {}; + shortcuts['DELETE'] = 'rm $n'; shortcuts['t'] = 'todo toggle $n'; shortcuts['c'] = () => { this.widget('console').focus(); }; shortcuts[' '] = 'edit $n'; @@ -250,7 +251,6 @@ class AppGui { term.grabInput(); term.on('key', async (name, matches, data) => { - if (name === 'CTRL_C' ) { termutils.showCursor(term); term.fullscreen(false); diff --git a/CliClient/app/app.js b/CliClient/app/app.js index 5e77e7eee..665e6ef7f 100644 --- a/CliClient/app/app.js +++ b/CliClient/app/app.js @@ -251,6 +251,33 @@ class Application { return this.eventEmitter_.on(eventName, callback); } + setupCommand(cmd) { + const consoleWidget = this.gui_.widget('console'); + + cmd.setStdout((...object) => { + for (let i = 0; i < object.length; i++) { + consoleWidget.bufferPush(object[i]); + } + }); + + cmd.setPrompt(async (message, options) => { + consoleWidget.focus(); + + if (options.type == 'boolean') { + message += ' (' + options.answers.join('/') + ')'; + } + + var answer = await consoleWidget.waitForResult(message + ' '); + + if (options.type == 'boolean') { + if (answer === null) return false; + return answer === '' || answer.toLowerCase() == options.answers[0].toLowerCase(); + } + }); + + return cmd; + } + commands() { if (this.allCommandsLoaded_) return this.commands_; @@ -262,11 +289,7 @@ class Application { let CommandClass = require('./' + path); let cmd = new CommandClass(); if (!cmd.enabled()) return; - - cmd.setStdout((...object) => { - this.commandStdout(...object); - }); - + cmd = this.setupCommand(cmd); this.commands_[cmd.name()] = cmd; }); @@ -285,13 +308,6 @@ class Application { return output; } - commandStdout(...object) { - const consoleWidget = this.gui_.widget('console'); - for (let i = 0; i < object.length; i++) { - consoleWidget.bufferPush(object[i]); - } - } - async commandMetadata() { if (this.commandMetadata_) return this.commandMetadata_; @@ -333,12 +349,7 @@ class Application { } let cmd = new CommandClass(); - cmd.buffer_ = []; - - cmd.setStdout((...object) => { - this.commandStdout(...object); - }); - + cmd = this.setupCommand(cmd); this.commands_[name] = cmd; return this.commands_[name]; } diff --git a/CliClient/app/base-command.js b/CliClient/app/base-command.js index 50e176f10..62c6bca55 100644 --- a/CliClient/app/base-command.js +++ b/CliClient/app/base-command.js @@ -1,7 +1,10 @@ +import { _ } from 'lib/locale.js'; + class BaseCommand { constructor() { this.stdout_ = null; + this.prompt_ = null; } usage() { @@ -51,6 +54,18 @@ class BaseCommand { if (this.stdout_) this.stdout_(...object); } + setPrompt(fn) { + this.prompt_ = fn; + } + + async prompt(message, options = null) { + if (!this.prompt_) throw new Error('Prompt is undefined'); + if (!options) options = {}; + if (!options.type) options.type = 'boolean'; + if (!options.answers) options.answers = [_('Y'), _('n')]; + return await this.prompt_(message, options); + } + metadata() { return { name: this.name(), diff --git a/CliClient/app/command-rm.js b/CliClient/app/command-rm.js index c65cd4f12..4c752d788 100644 --- a/CliClient/app/command-rm.js +++ b/CliClient/app/command-rm.js @@ -7,6 +7,9 @@ import { Note } from 'lib/models/note.js'; import { BaseModel } from 'lib/base-model.js'; import { cliUtils } from './cli-utils.js'; + +import { reg } from 'lib/registry.js'; + class Command extends BaseCommand { usage() { @@ -39,10 +42,16 @@ class Command extends BaseCommand { const notes = await app().loadItems(BaseModel.TYPE_NOTE, pattern); if (!notes.length) throw new Error(_('Cannot find "%s".', pattern)); - const ok = force ? true : await cliUtils.promptConfirm(_('%d notes match this pattern. Delete them?', notes.length)); - if (!ok) return; - let ids = notes.map((n) => n.id); - await Note.batchDelete(ids); + + const ok = force ? true : await this.prompt(_('%d notes match this pattern. Delete them?', notes.length)); + + + reg.logger().info('OK', ok); + + // const ok = force ? true : await cliUtils.promptConfirm(_('%d notes match this pattern. Delete them?', notes.length)); + // if (!ok) return; + // let ids = notes.map((n) => n.id); + // await Note.batchDelete(ids); } } diff --git a/CliClient/locales/en_GB.po b/CliClient/locales/en_GB.po index 2097743f0..67060375e 100644 --- a/CliClient/locales/en_GB.po +++ b/CliClient/locales/en_GB.po @@ -85,6 +85,12 @@ msgid "" "source '%s'" msgstr "" +msgid "Y" +msgstr "" + +msgid "n" +msgstr "" + #, javascript-format msgid "Missing required argument: %s" msgstr "" @@ -100,12 +106,6 @@ msgstr "" msgid "Invalid answer: %s" msgstr "" -msgid "Y" -msgstr "" - -msgid "n" -msgstr "" - msgid "Displays the given note." msgstr "" diff --git a/CliClient/locales/fr_FR.po b/CliClient/locales/fr_FR.po index 30dd805fd..d7eb188e9 100644 --- a/CliClient/locales/fr_FR.po +++ b/CliClient/locales/fr_FR.po @@ -88,6 +88,12 @@ msgid "" "source '%s'" msgstr "" +msgid "Y" +msgstr "" + +msgid "n" +msgstr "" + #, javascript-format msgid "Missing required argument: %s" msgstr "" @@ -103,12 +109,6 @@ msgstr "" msgid "Invalid answer: %s" msgstr "Commande invalide : \"%s\"" -msgid "Y" -msgstr "" - -msgid "n" -msgstr "" - msgid "Displays the given note." msgstr "Affiche la note." diff --git a/CliClient/locales/joplin.pot b/CliClient/locales/joplin.pot index 2097743f0..67060375e 100644 --- a/CliClient/locales/joplin.pot +++ b/CliClient/locales/joplin.pot @@ -85,6 +85,12 @@ msgid "" "source '%s'" msgstr "" +msgid "Y" +msgstr "" + +msgid "n" +msgstr "" + #, javascript-format msgid "Missing required argument: %s" msgstr "" @@ -100,12 +106,6 @@ msgstr "" msgid "Invalid answer: %s" msgstr "" -msgid "Y" -msgstr "" - -msgid "n" -msgstr "" - msgid "Displays the given note." msgstr ""