2017-11-03 02:09:34 +02:00
|
|
|
const { BaseCommand } = require('./base-command.js');
|
|
|
|
const { app } = require('./app.js');
|
|
|
|
const { _ } = require('lib/locale.js');
|
2017-12-14 20:12:14 +02:00
|
|
|
const BaseModel = require('lib/BaseModel.js');
|
|
|
|
const Folder = require('lib/models/Folder.js');
|
|
|
|
const Note = require('lib/models/Note.js');
|
2017-11-03 02:09:34 +02:00
|
|
|
const { sprintf } = require('sprintf-js');
|
|
|
|
const { time } = require('lib/time-utils.js');
|
|
|
|
const { uuid } = require('lib/uuid.js');
|
2017-07-17 21:56:14 +02:00
|
|
|
|
|
|
|
class Command extends BaseCommand {
|
|
|
|
|
|
|
|
usage() {
|
2017-07-28 20:13:07 +02:00
|
|
|
return 'search <pattern> [notebook]';
|
2017-07-17 21:56:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
description() {
|
2017-07-18 20:21:03 +02:00
|
|
|
return _('Searches for the given <pattern> in all the notes.');
|
2017-07-17 21:56:14 +02:00
|
|
|
}
|
|
|
|
|
2017-10-24 23:37:36 +02:00
|
|
|
compatibleUis() {
|
|
|
|
return ['gui'];
|
|
|
|
}
|
|
|
|
|
2017-07-17 21:56:14 +02:00
|
|
|
async action(args) {
|
|
|
|
let pattern = args['pattern'];
|
|
|
|
let folderTitle = args['notebook'];
|
|
|
|
|
|
|
|
let folder = null;
|
|
|
|
if (folderTitle) {
|
|
|
|
folder = await Folder.loadByTitle(folderTitle);
|
2017-07-18 20:21:03 +02:00
|
|
|
if (!folder) throw new Error(_('Cannot find "%s".', folderTitle));
|
2017-07-17 21:56:14 +02:00
|
|
|
}
|
|
|
|
|
2017-10-23 22:34:04 +02:00
|
|
|
const searchId = uuid.create();
|
|
|
|
|
|
|
|
this.dispatch({
|
|
|
|
type: 'SEARCH_ADD',
|
|
|
|
search: {
|
|
|
|
id: searchId,
|
|
|
|
title: pattern,
|
|
|
|
query_pattern: pattern,
|
|
|
|
query_folder_id: folder ? folder.id : '',
|
|
|
|
type_: BaseModel.TYPE_SEARCH,
|
|
|
|
},
|
2017-07-17 21:56:14 +02:00
|
|
|
});
|
|
|
|
|
2017-10-23 22:34:04 +02:00
|
|
|
this.dispatch({
|
|
|
|
type: 'SEARCH_SELECT',
|
2017-10-23 23:48:29 +02:00
|
|
|
id: searchId,
|
2017-10-23 22:34:04 +02:00
|
|
|
});
|
2017-07-17 21:56:14 +02:00
|
|
|
|
2017-10-23 22:34:04 +02:00
|
|
|
// let fields = Note.previewFields();
|
|
|
|
// fields.push('body');
|
|
|
|
// const notes = await Note.previews(folder ? folder.id : null, {
|
|
|
|
// fields: fields,
|
|
|
|
// anywherePattern: '*' + pattern + '*',
|
|
|
|
// });
|
2017-07-17 21:56:14 +02:00
|
|
|
|
2017-10-23 22:34:04 +02:00
|
|
|
// const fragmentLength = 50;
|
2017-07-17 21:56:14 +02:00
|
|
|
|
2017-10-23 22:34:04 +02:00
|
|
|
// let parents = {};
|
2017-07-17 21:56:14 +02:00
|
|
|
|
2017-10-23 22:34:04 +02:00
|
|
|
// for (let i = 0; i < notes.length; i++) {
|
|
|
|
// const note = notes[i];
|
|
|
|
// const parent = parents[note.parent_id] ? parents[note.parent_id] : await Folder.load(note.parent_id);
|
|
|
|
// parents[note.parent_id] = parent;
|
|
|
|
|
|
|
|
// const idx = note.body.indexOf(pattern);
|
|
|
|
// let line = '';
|
|
|
|
// if (idx >= 0) {
|
|
|
|
// let fragment = note.body.substr(Math.max(0, idx - fragmentLength / 2), fragmentLength);
|
|
|
|
// fragment = fragment.replace(/\n/g, ' ');
|
|
|
|
// line = sprintf('%s: %s / %s: %s', BaseModel.shortId(note.id), parent.title, note.title, fragment);
|
|
|
|
// } else {
|
|
|
|
// line = sprintf('%s: %s / %s', BaseModel.shortId(note.id), parent.title, note.title);
|
|
|
|
// }
|
|
|
|
|
|
|
|
// this.stdout(line);
|
|
|
|
// }
|
2017-07-17 21:56:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = Command;
|