From e4435a4fb6d6d1b18b133f76e3b4814f60fdce46 Mon Sep 17 00:00:00 2001
From: Laurent Cozic <laurent@cozic.net>
Date: Thu, 26 Oct 2017 23:22:36 +0100
Subject: [PATCH] Added ren command

---
 CliClient/app/app.js          | 16 +++++++----
 CliClient/app/command-edit.js |  2 --
 CliClient/app/command-ren.js  | 52 +++++++++++++++++++++++++++++++++++
 CliClient/locales/en_GB.po    |  6 ++--
 CliClient/locales/fr_FR.po    | 11 +++++---
 CliClient/locales/joplin.pot  |  6 ++--
 6 files changed, 76 insertions(+), 17 deletions(-)
 create mode 100644 CliClient/app/command-ren.js

diff --git a/CliClient/app/app.js b/CliClient/app/app.js
index 965eb8536b..07c11a81f0 100644
--- a/CliClient/app/app.js
+++ b/CliClient/app/app.js
@@ -92,12 +92,12 @@ class Application {
 		let output = await this.loadItems(type, pattern, options);
 
 		if (output.length > 1) {
-			output.sort((a, b) => { return a.user_updated_time < b.user_updated_time ? +1 : -1; });
+			// output.sort((a, b) => { return a.user_updated_time < b.user_updated_time ? +1 : -1; });
 
-			let answers = { 0: _('[Cancel]') };
-			for (let i = 0; i < output.length; i++) {
-				answers[i + 1] = output[i].title;
-			}
+			// let answers = { 0: _('[Cancel]') };
+			// for (let i = 0; i < output.length; i++) {
+			// 	answers[i + 1] = output[i].title;
+			// }
 
 			// Not really useful with new UI?
 			throw new Error(_('More than one item match "%s". Please narrow down your query.', pattern));
@@ -113,6 +113,12 @@ class Application {
 	}
 
 	async loadItems(type, pattern, options = null) {
+		if (type === 'folderOrNote') {
+			const folders = await this.loadItems(BaseModel.TYPE_FOLDER, pattern, options);
+			if (folders.length) return folders;
+			return await this.loadItems(BaseModel.TYPE_NOTE, pattern, options);
+		}
+
 		pattern = pattern ? pattern.toString() : '';
 
 		if (type == BaseModel.TYPE_FOLDER && (pattern == Folder.conflictFolderTitle() || pattern == Folder.conflictFolderId())) return [Folder.conflictFolder()];
diff --git a/CliClient/app/command-edit.js b/CliClient/app/command-edit.js
index 8eb4a64367..f0c0b7b5a1 100644
--- a/CliClient/app/command-edit.js
+++ b/CliClient/app/command-edit.js
@@ -74,8 +74,6 @@ class Command extends BaseCommand {
 
 			this.logger().info('Disabling fullscreen...');
 
-			//this.stdout(_('Starting to edit note. Close the editor to get back to the prompt.'));
-
 			app().gui().showModalOverlay(_('Starting to edit note. Close the editor to get back to the prompt.'));
 			app().gui().forceRender();
 			const termState = app().gui().term().saveState();
diff --git a/CliClient/app/command-ren.js b/CliClient/app/command-ren.js
new file mode 100644
index 0000000000..65b6e9b45e
--- /dev/null
+++ b/CliClient/app/command-ren.js
@@ -0,0 +1,52 @@
+import { BaseCommand } from './base-command.js';
+import { app } from './app.js';
+import { _ } from 'lib/locale.js';
+import { BaseModel } from 'lib/base-model.js';
+import { Folder } from 'lib/models/folder.js';
+import { Note } from 'lib/models/note.js';
+
+class Command extends BaseCommand {
+
+	usage() {
+		return 'ren <item> <name>';
+	}
+
+	description() {
+		return _('Renames the given <item> (note or notebook) to <name>.');
+	}
+
+	async action(args) {
+		const pattern = args['item'];
+		const name = args['name'];
+
+		const item = await app().loadItem('folderOrNote', pattern);
+		if (!item) throw new Error(_('Cannot find "%s".', pattern));
+
+		const newItem = {
+			id: item.id,
+			title: name,
+			type_: item.type_,
+		};
+
+		if (item.type_ === BaseModel.TYPE_FOLDER) {
+			await Folder.save(newItem);
+		} else {
+			await Note.save(newItem);
+		}
+
+
+		
+		// const folder = await Folder.loadByField('title', destination);
+		// if (!folder) throw new Error(_('Cannot find "%s".', destination));
+
+		// const notes = await app().loadItems(BaseModel.TYPE_NOTE, pattern);
+		// if (!notes.length) throw new Error(_('Cannot find "%s".', pattern));
+
+		// for (let i = 0; i < notes.length; i++) {
+		// 	await Note.moveToFolder(notes[i].id, folder.id);
+		// }
+	}
+
+}
+
+module.exports = Command;
\ No newline at end of file
diff --git a/CliClient/locales/en_GB.po b/CliClient/locales/en_GB.po
index f6fe04aff9..a736fcce82 100644
--- a/CliClient/locales/en_GB.po
+++ b/CliClient/locales/en_GB.po
@@ -69,9 +69,6 @@ msgstr ""
 msgid "Press Ctrl+D or type \"exit\" to exit the application"
 msgstr ""
 
-msgid "[Cancel]"
-msgstr ""
-
 #, javascript-format
 msgid "More than one item match \"%s\". Please narrow down your query."
 msgstr ""
@@ -374,6 +371,9 @@ msgstr ""
 msgid "Moves the notes matching <note> to [notebook]."
 msgstr ""
 
+msgid "Renames the given <item> (note or notebook) to <name>."
+msgstr ""
+
 msgid "Deletes the given notebook."
 msgstr ""
 
diff --git a/CliClient/locales/fr_FR.po b/CliClient/locales/fr_FR.po
index 29330feba2..e104a510b0 100644
--- a/CliClient/locales/fr_FR.po
+++ b/CliClient/locales/fr_FR.po
@@ -78,10 +78,6 @@ msgstr "Impossible de déplacer la note vers le carnet \"%s\""
 msgid "Press Ctrl+D or type \"exit\" to exit the application"
 msgstr ""
 
-#, fuzzy
-msgid "[Cancel]"
-msgstr "Annulation..."
-
 #, javascript-format
 msgid "More than one item match \"%s\". Please narrow down your query."
 msgstr ""
@@ -417,6 +413,9 @@ msgstr "Créer une nouvelle tâche."
 msgid "Moves the notes matching <note> to [notebook]."
 msgstr "Supprime les objets correspondants à <motif>."
 
+msgid "Renames the given <item> (note or notebook) to <name>."
+msgstr ""
+
 #, fuzzy
 msgid "Deletes the given notebook."
 msgstr "Supprime le carnet."
@@ -859,6 +858,10 @@ msgstr ""
 msgid "Welcome"
 msgstr "Bienvenue"
 
+#, fuzzy
+#~ msgid "[Cancel]"
+#~ msgstr "Annulation..."
+
 #~ msgid "Todo filter"
 #~ msgstr "Filtre des tâches"
 
diff --git a/CliClient/locales/joplin.pot b/CliClient/locales/joplin.pot
index f6fe04aff9..a736fcce82 100644
--- a/CliClient/locales/joplin.pot
+++ b/CliClient/locales/joplin.pot
@@ -69,9 +69,6 @@ msgstr ""
 msgid "Press Ctrl+D or type \"exit\" to exit the application"
 msgstr ""
 
-msgid "[Cancel]"
-msgstr ""
-
 #, javascript-format
 msgid "More than one item match \"%s\". Please narrow down your query."
 msgstr ""
@@ -374,6 +371,9 @@ msgstr ""
 msgid "Moves the notes matching <note> to [notebook]."
 msgstr ""
 
+msgid "Renames the given <item> (note or notebook) to <name>."
+msgstr ""
+
 msgid "Deletes the given notebook."
 msgstr ""