1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-24 10:27:10 +02:00

Handle prompt

This commit is contained in:
Laurent Cozic 2017-10-07 18:07:38 +01:00
parent 824b385e83
commit 6384fe3b7d
7 changed files with 76 additions and 41 deletions

View File

@ -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);

View File

@ -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];
}

View File

@ -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(),

View File

@ -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);
}
}

View File

@ -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 ""

View File

@ -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."

View File

@ -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 ""