1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-12 08:54:00 +02:00
joplin/CliClient/app/command-ls.js

122 lines
3.6 KiB
JavaScript
Raw Normal View History

2017-07-10 22:03:46 +02:00
import { BaseCommand } from './base-command.js';
import { app } from './app.js';
import { _ } from 'lib/locale.js';
2017-07-17 20:46:09 +02:00
import { BaseModel } from 'lib/base-model.js';
2017-07-10 22:03:46 +02:00
import { Folder } from 'lib/models/folder.js';
2017-07-31 21:47:58 +02:00
import { Setting } from 'lib/models/setting.js';
2017-07-10 22:03:46 +02:00
import { Note } from 'lib/models/note.js';
import { sprintf } from 'sprintf-js';
2017-07-12 22:39:47 +02:00
import { time } from 'lib/time-utils.js';
2017-08-03 19:48:14 +02:00
import { cliUtils } from './cli-utils.js';
2017-07-10 22:03:46 +02:00
class Command extends BaseCommand {
usage() {
2017-08-04 18:02:43 +02:00
return 'ls [note-pattern]';
2017-07-10 22:03:46 +02:00
}
description() {
2017-08-04 18:02:43 +02:00
return _('Displays the notes in the current notebook. Use `ls /` to display the list of notebooks.');
2017-07-10 22:03:46 +02:00
}
options() {
return [
2017-07-28 20:13:07 +02:00
['-n, --limit <num>', _('Displays only the first top <num> notes.')],
['-s, --sort <field>', _('Sorts the item by <field> (eg. title, updated_time, created_time).')],
2017-07-18 20:21:03 +02:00
['-r, --reverse', _('Reverses the sorting order.')],
2017-07-28 20:13:07 +02:00
['-t, --type <type>', _('Displays only the items of the specific type(s). Can be `n` for notes, `t` for todos, or `nt` for notes and todos (eg. `-tt` would display only the todos, while `-ttd` would display notes and todos.')],
['-f, --format <format>', _('Either "text" or "json"')],
2017-07-18 20:21:03 +02:00
['-l, --long', _('Use long list format. Format is ID, NOTE_COUNT (for notebook), DATE, TODO_CHECKED (for todos), TITLE')],
2017-07-10 22:03:46 +02:00
];
}
async action(args) {
2017-08-04 18:02:43 +02:00
let pattern = args['note-pattern'];
2017-07-10 22:03:46 +02:00
let items = [];
let options = args.options;
let queryOptions = {};
if (options.limit) queryOptions.limit = options.limit;
2017-07-10 22:03:46 +02:00
if (options.sort) {
queryOptions.orderBy = options.sort;
queryOptions.orderByDir = 'ASC';
}
if (options.reverse === true) queryOptions.orderByDir = queryOptions.orderByDir == 'ASC' ? 'DESC' : 'ASC';
queryOptions.caseInsensitive = true;
if (options.type) {
queryOptions.itemTypes = [];
if (options.type.indexOf('n') >= 0) queryOptions.itemTypes.push('note');
if (options.type.indexOf('t') >= 0) queryOptions.itemTypes.push('todo');
}
if (pattern) queryOptions.titlePattern = pattern;
2017-07-31 21:47:58 +02:00
queryOptions.uncompletedTodosOnTop = Setting.value('uncompletedTodosOnTop');
2017-07-10 22:03:46 +02:00
2017-07-12 22:39:47 +02:00
let modelType = null;
2017-07-11 20:17:23 +02:00
if (pattern == '/' || !app().currentFolder()) {
2017-07-15 17:35:40 +02:00
queryOptions.includeConflictFolder = true;
2017-07-10 22:03:46 +02:00
items = await Folder.all(queryOptions);
2017-07-12 22:39:47 +02:00
modelType = Folder.modelType();
2017-07-10 22:03:46 +02:00
} else {
if (!app().currentFolder()) throw new Error(_('Please select a notebook first.'));
items = await Note.previews(app().currentFolder().id, queryOptions);
2017-07-12 22:39:47 +02:00
modelType = Note.modelType();
2017-07-10 22:03:46 +02:00
}
if (options.format && options.format == 'json') {
this.log(JSON.stringify(items));
} else {
2017-07-12 22:39:47 +02:00
let hasTodos = false;
for (let i = 0; i < items.length; i++) {
let item = items[i];
if (item.is_todo) {
hasTodos = true;
break;
}
}
let seenTitles = [];
2017-07-12 22:39:47 +02:00
let rows = [];
2017-07-17 20:46:09 +02:00
let shortIdShown = false;
2017-07-10 22:03:46 +02:00
for (let i = 0; i < items.length; i++) {
let item = items[i];
2017-07-12 22:39:47 +02:00
let row = [];
if (options.long) {
2017-07-17 20:46:09 +02:00
row.push(BaseModel.shortId(item.id));
shortIdShown = true;
2017-07-12 22:39:47 +02:00
if (modelType == Folder.modelType()) {
row.push(await Folder.noteCount(item.id));
}
2017-08-21 19:56:40 +02:00
row.push(time.unixMsToLocalDateTime(item.user_updated_time));
2017-07-10 22:03:46 +02:00
}
2017-07-30 21:51:18 +02:00
let title = item.title;
2017-07-17 20:46:09 +02:00
if (!shortIdShown && (seenTitles.indexOf(item.title) >= 0 || !item.title)) {
title += ' (' + BaseModel.shortId(item.id) + ')';
} else {
seenTitles.push(item.title);
}
2017-07-12 22:39:47 +02:00
if (hasTodos) {
if (item.is_todo) {
row.push(sprintf('[%s]', !!item.todo_completed ? 'X' : ' '));
} else {
row.push(' ');
}
}
row.push(title);
rows.push(row);
2017-07-10 22:03:46 +02:00
}
2017-07-12 22:39:47 +02:00
2017-08-03 19:48:14 +02:00
cliUtils.printArray(this.log, rows);
2017-07-10 22:03:46 +02:00
}
2017-07-10 22:59:58 +02:00
2017-07-10 22:03:46 +02:00
}
}
module.exports = Command;