From eb38e05f691fe0a4b722b96311fb045d77d31ce9 Mon Sep 17 00:00:00 2001 From: piotrruss Date: Sun, 8 May 2022 20:51:58 +0100 Subject: added ability to sort --- README.md | 5 +-- notes_cli.js | 116 ++++++++++++++++++++++++++++++++++++++++++----------------- 2 files changed, 86 insertions(+), 35 deletions(-) diff --git a/README.md b/README.md index 714cdb7..7311d97 100644 --- a/README.md +++ b/README.md @@ -15,9 +15,10 @@ Key bindings (press [h] to view from the script): > [t] - Change note's title > [d] - Delete note -Options that will be added: +> [1] - Sort by title +> [2] - Sort by creation date +> [3] - Sort by modification date -> [s] - Sort note Screenshot: diff --git a/notes_cli.js b/notes_cli.js index 06e2bd9..d40df6f 100755 --- a/notes_cli.js +++ b/notes_cli.js @@ -34,7 +34,7 @@ const exitWithError = (e) => { setTimeout(() => process.exit(1), 2000) } -let conf, notesList = [], active = 0; scroll = 0; draw = { t: 'p', v: () => ['Loading NOTES CLI...'] }; menu = 'Press [h] for help' +let conf, notesList = [], active = 0; scroll = 0; draw = { t: 'p', v: () => ['Loading NOTES CLI...'] }; menu = 'Press [h] for help'; sort = 3 const cl = (m, c) => console.log(c ? `\x1b[${c}m${m}\x1b[0m` : m) const cls = () => console.clear() const filesPath = `${homedir}/.local/share/notes_cli` @@ -53,11 +53,27 @@ const formatNote = note => { const showHelp = () => { const prev = draw.v draw = { t: 'h', prev, v: () => ['[q] - Quit','[h] - Help','','[up/k] - Previous note','[down/j] - Next note', - '[enter/o] - Open note','','[c] - Create note','[d] - Delete note',"[t] - Change note's title",'','Options not yet added:','[s] - Sort note']}; + '[enter/o] - Open note','','[c] - Create note','[d] - Delete note',"[t] - Change note's title",'','[1] - Sort by title','[2] - Sort by creation date','[3] - Sort by modification date']}; menu = 'Press any key to exit help' drawApp() } +const setSort = (s) => { + rl.clearLine(-1) + sort = s === Math.abs(sort) ? sort * (-1) : parseInt(s) + draw = { t: 'l', v: () => notesList.sort(sortFn).map(formatNote) } + drawApp() +} + +const sortFn = (a, b) => { + const d = sort > 0 ? 1 : -1 + switch (Math.abs(sort)) { + case 1: return d * a.title.localeCompare(b.title) + case 2: return d * (new Date(b.created_at) - new Date(a.created_at)) + case 3: return d * (new Date(b.updated_at) - new Date(a.updated_at)) + } +} + const showList = (notes, status) => { if (status !== 200) { cls() @@ -65,7 +81,7 @@ const showList = (notes, status) => { } notesList = notes - draw = { t: 'l', v: () => notes.map(formatNote) } + draw = { t: 'l', v: () => notes.sort(sortFn).map(formatNote) } drawApp() } @@ -94,7 +110,7 @@ const getNewNoteTitle = (existing = false) => { const v = draw.v rl.clearLine(-1) cls() - draw = {t: 'p', prev: v, v: () => ['Please type the new note title', 'or leave empty to cancel'], noMenu: true} + draw = {t: 'i', prev: v, v: () => ['Please type the new note title', 'or leave empty to cancel'], noMenu: true} drawApp() cursor.show() rl.question(' Title: ', t => { @@ -207,11 +223,19 @@ const editTmpFile = (note) => { // =========== DRAW APP ============= +const sortIco = c => { + if (c === Math.abs(sort)){ + return sort > 0 ? '▼' : '▲' + } else { + return ' ' + } +} + const drawApp = () => { const lines = process.stdout.rows const columns = process.stdout.columns const headers = columns > 72 - ? `${fixedStr(' Title', columns - 44)} Created at${' '.repeat(10)} Updated at ${' '.repeat(9)}` + ? `${fixedStr(' Title '+sortIco(1), columns - 44)} Created at ${sortIco(2)}${' '.repeat(8)} Updated at ${sortIco(3)}${' '.repeat(8)}` : ` Title ${' '.repeat(columns - 9)}` cls() @@ -240,7 +264,7 @@ const drawApp = () => { break default: const max = Math.max(...(draw.v().map(el => el.length))) - if (['p','h','c'].includes(draw.t)) { + if (['p','h','c','i'].includes(draw.t)) { if (i === (Math.floor((lines - draw.v().length) / 2) - 2)){ cl('║'+' '.repeat(dist(max+4)[0])+'┌'+'─'.repeat(max+2)+'┐'+' '.repeat(dist(max+4)[1])+'║') } else if (i === (Math.floor((lines + draw.v().length) / 2) + 1)) { @@ -283,68 +307,95 @@ const getKey = () => { process.stdin.setRawMode(true); process.stdin.on('keypress', (_, key) => { const lines = process.stdout.rows + const run = (mode, fn) => { + if (draw.t === mode) { + fn() + } else if (draw.t === 'h') { + const v = draw.prev + draw = { t: 'l', v } + menu = 'Press [h] for help' + drawApp() + } else if (draw.t === 'i') { + } else { + rl.clearLine(-1) + drawApp() + } + } switch(key.name) { case 'up': case 'k': - if (draw.t === 'l') { + run('l', () => { active > 0 && active-- active < scroll && scroll-- drawApp() - } + }) break case 'down': case 'j': - if (draw.t === 'l') { + run('l', () => { active < draw.v().length - 1 && active++ active - scroll > lines - 7 && scroll++ drawApp() - } + }) break case 'return': case 'o': - if (draw.t === 'l') { + run('l', () => { fetchNote(notesList[active]) - break - } + }) + break case 'h': - if (draw.t === 'l') { + run('l', () => { showHelp() - break - } + }) + break case 'c': - if (draw.t === 'l') { + run('l', () => { getNewNoteTitle() - } + }) + break case 't': - if (draw.t === 'l') { + run('l', () => { getNewNoteTitle(true) - break - } + }) + break case 'd': - if (draw.t === 'l') { + run('l', () => { confirmRemoval() - break - } + }) + break case 'y': - if (draw.t === 'c') { + run('c', () => { removeNote(notesList[active]) - break - } + }) + break case 'n': - if (draw.t === 'c') { + run('c', () => { closePopup() - break - } + }) + break + case '1': + case '2': + case '3': + run('l', () => { + setSort(parseInt(key.name)) + }) + break case 'q': - if (draw.t === 'l') { + run('l', () => { process.exit() - } + }) + break default: if (draw.t === 'h') { const v = draw.prev draw = { t: 'l', v } menu = 'Press [h] for help' drawApp() + } if (draw.t === 'i') { + } else { + rl.clearLine(-1) + drawApp() } } }) @@ -362,7 +413,6 @@ const setSession = (session) => { exitWithError(['Error writting session file']) } - rl.resume() getSession() }) } -- cgit v1.2.3