2017-11-03 00:09:34 +00:00
const { BaseCommand } = require ( './base-command.js' );
const { app } = require ( './app.js' );
const { _ } = require ( 'lib/locale.js' );
2017-12-14 18:12:14 +00:00
const BaseModel = require ( 'lib/BaseModel.js' );
const Folder = require ( 'lib/models/Folder.js' );
const Setting = require ( 'lib/models/Setting.js' );
const Note = require ( 'lib/models/Note.js' );
2017-11-03 00:09:34 +00:00
const { sprintf } = require ( 'sprintf-js' );
const { time } = require ( 'lib/time-utils.js' );
const { cliUtils } = require ( './cli-utils.js' );
2017-07-10 20:03:46 +00:00
class Command extends BaseCommand {
usage () {
2017-08-04 18:02:43 +02:00
return 'ls [note-pattern]' ;
2017-07-10 20:03:46 +00: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 20:03:46 +00:00
}
2017-10-24 22:37:36 +01:00
enabled () {
2020-03-08 09:17:10 +00:00
return true ;
2017-10-24 22:37:36 +01:00
}
2018-09-04 00:42:22 -04:00
2017-07-10 20:03:46 +00:00
options () {
2020-03-08 09:17:10 +00:00
return [
[ '-n, --limit <num>' , _ ( 'Displays only the first top <num> notes.' )],
[ '-s, --sort <field>' , _ ( 'Sorts the item by <field> (eg. title, updated_time, created_time).' )],
[ '-r, --reverse' , _ ( 'Reverses the sorting order.' )],
[ '-t, --type <type>' , _ ( 'Displays only the items of the specific type(s). Can be `n` for notes, `t` for to-dos, or `nt` for notes and to-dos (eg. `-tt` would display only the to-dos, while `-ttd` would display notes and to-dos.' )],
[ '-f, --format <format>' , _ ( 'Either "text" or "json"' )],
[ '-l, --long' , _ ( 'Use long list format. Format is ID, NOTE_COUNT (for notebook), DATE, TODO_CHECKED (for to-dos), TITLE' )],
];
2017-07-10 20:03:46 +00:00
}
async action ( args ) {
2020-03-13 23:46:14 +00:00
const pattern = args [ 'note-pattern' ];
2017-07-10 20:03:46 +00:00
let items = [];
2020-03-13 23:46:14 +00:00
const options = args . options ;
2017-07-10 20:03:46 +00:00
2020-03-13 23:46:14 +00:00
const queryOptions = {};
2017-07-10 20:47:01 +00:00
if ( options . limit ) queryOptions . limit = options . limit ;
2017-07-10 20:03:46 +00: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 19:47:58 +00:00
queryOptions . uncompletedTodosOnTop = Setting . value ( 'uncompletedTodosOnTop' );
2017-07-10 20:03:46 +00:00
2017-07-12 21:39:47 +01:00
let modelType = null ;
2017-07-11 18:17:23 +00:00
if ( pattern == '/' || ! app (). currentFolder ()) {
2017-07-15 16:35:40 +01:00
queryOptions . includeConflictFolder = true ;
2017-07-10 20:03:46 +00:00
items = await Folder . all ( queryOptions );
2017-07-12 21:39:47 +01:00
modelType = Folder . modelType ();
2017-07-10 20:03:46 +00:00
} else {
if ( ! app (). currentFolder ()) throw new Error ( _ ( 'Please select a notebook first.' ));
items = await Note . previews ( app (). currentFolder (). id , queryOptions );
2017-07-12 21:39:47 +01:00
modelType = Note . modelType ();
2017-07-10 20:03:46 +00:00
}
if ( options . format && options . format == 'json' ) {
2017-10-07 17:30:27 +01:00
this . stdout ( JSON . stringify ( items ));
2017-07-10 20:03:46 +00:00
} else {
2017-07-12 21:39:47 +01:00
let hasTodos = false ;
2017-07-10 20:03:46 +00:00
for ( let i = 0 ; i < items . length ; i ++ ) {
2020-03-13 23:46:14 +00:00
const item = items [ i ];
2017-07-12 21:39:47 +01:00
if ( item . is_todo ) {
hasTodos = true ;
break ;
2017-07-10 20:03:46 +00:00
}
2017-07-12 21:39:47 +01:00
}
2017-07-10 20:47:01 +00:00
2020-03-13 23:46:14 +00:00
const seenTitles = [];
const rows = [];
2017-07-17 18:46:09 +00:00
let shortIdShown = false ;
2017-07-12 21:39:47 +01:00
for ( let i = 0 ; i < items . length ; i ++ ) {
2020-03-13 23:46:14 +00:00
const item = items [ i ];
const row = [];
2017-07-12 21:39:47 +01:00
if ( options . long ) {
2017-07-17 18:46:09 +00:00
row . push ( BaseModel . shortId ( item . id ));
shortIdShown = true ;
2017-07-12 21:39:47 +01:00
if ( modelType == Folder . modelType ()) {
row . push ( await Folder . noteCount ( item . id ));
}
2018-09-04 18:03:21 -04:00
row . push ( time . formatMsToLocal ( item . user_updated_time ));
2017-07-12 21:39:47 +01:00
}
2017-07-30 21:51:18 +02:00
let title = item . title ;
2017-07-17 18:46:09 +00:00
if ( ! shortIdShown && ( seenTitles . indexOf ( item . title ) >= 0 || ! item . title )) {
2019-09-19 22:51:18 +01:00
title += ` ( ${ BaseModel . shortId ( item . id ) } )` ;
2017-07-10 20:47:01 +00:00
} else {
seenTitles . push ( item . title );
}
2017-07-12 21:39:47 +01:00
if ( hasTodos ) {
if ( item . is_todo ) {
2019-07-30 09:35:42 +02:00
row . push ( sprintf ( '[%s]' , item . todo_completed ? 'X' : ' ' ));
2017-07-12 21:39:47 +01:00
} else {
row . push ( ' ' );
}
}
row . push ( title );
rows . push ( row );
2017-07-10 20:03:46 +00:00
}
2017-07-12 21:39:47 +01:00
2017-10-09 21:45:20 +01:00
cliUtils . printArray ( this . stdout . bind ( this ), rows );
2017-07-10 20:03:46 +00:00
}
}
}
2018-09-04 00:42:22 -04:00
module . exports = Command ;