From 21da1c134cfa8db18a53d6e82a11148c4198dfda Mon Sep 17 00:00:00 2001 From: piotrruss Date: Sun, 8 May 2022 16:51:43 +0100 Subject: Ability to remove notes --- README.md | 4 ++- notes_cli.js | 82 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 80 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 8b74303..714cdb7 100644 --- a/README.md +++ b/README.md @@ -6,15 +6,17 @@ Key bindings (press [h] to view from the script): > [q] - Quit > [h] - Help + > [up/k] - Previous note > [down/j] - Next note > [enter/o] - Open note + > [c] - Create note > [t] - Change note's title +> [d] - Delete note Options that will be added: -> [d] - Delete note > [s] - Sort note Screenshot: diff --git a/notes_cli.js b/notes_cli.js index c1d4740..06e2bd9 100755 --- a/notes_cli.js +++ b/notes_cli.js @@ -52,8 +52,8 @@ 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',"[t] - Change note's title",'','Options not yet added:','[d] - Delete note','[s] - Sort note']}; + 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']}; menu = 'Press any key to exit help' drawApp() } @@ -76,6 +76,20 @@ const changeTitle = (title) => { putTitle(notesList[active], title) } +const confirmRemoval = () => { + const v = draw.v + menu = '[y]es [n]o' + draw = {t: 'c', prev: v, v: () => ['Do you want to remove note?']} + drawApp() +} + +const closePopup = () => { + const v = draw.prev + menu = 'Press [h] for help' + draw = {t: 'l', v} + drawApp() +} + const getNewNoteTitle = (existing = false) => { const v = draw.v rl.clearLine(-1) @@ -132,8 +146,16 @@ const readTmpFile = (note, title) => { }) } -const noteSaved = () => { - draw = {t: 'p', v: () => ['Note saved']} +const noteSaved = (list) => { + draw = {t: 'p', v: () => list ? ['Note saved'] : ['Could not save note']} + drawApp() + fetchList() +} + +const noteRemoved = (list) => { + menu = 'Press [h] for help' + draw = {t: 'p', v: () => list ? ['Note removed'] : ['Could not remove note']} + if (list) { active-- } drawApp() fetchList() } @@ -218,7 +240,7 @@ const drawApp = () => { break default: const max = Math.max(...(draw.v().map(el => el.length))) - if (['p','h'].includes(draw.t)) { + if (['p','h','c'].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)) { @@ -296,6 +318,22 @@ const getKey = () => { case 't': if (draw.t === 'l') { getNewNoteTitle(true) + break + } + case 'd': + if (draw.t === 'l') { + confirmRemoval() + break + } + case 'y': + if (draw.t === 'c') { + removeNote(notesList[active]) + break + } + case 'n': + if (draw.t === 'c') { + closePopup() + break } case 'q': if (draw.t === 'l') { @@ -422,6 +460,12 @@ const fetchNote = note => { get(`/api/notes/${note.noteId}`, (content) => saveTmpFile(note, content), conf.session) } +const removeNote = note => { + draw = { t: 'p', v: () => ['Removing note...'] } + drawApp() + remove(`/api/notes/${note._id}`, noteRemoved, conf.session) +} + const putNote = ({ _id, noteId }, content) => { put(`/api/notes/${_id}`, { noteId, content }, noteSaved, conf.session) } @@ -499,6 +543,34 @@ const post = (path, data, callback, cookie = '', put = false) => { const put = (...arg) => post(...arg, true) +const remove = (path, callback, cookie = '') => { + const options = { + hostname: 'apps.pruss.it', + port: 443, + path, + method: 'DELETE', + headers: { + 'Cookie': cookie, + }, + }; + + const req = https.request(options, res => { + let data = ''; + + res.on('data', chunk => { + data += chunk + }); + + res.on('end', () => callback(JSON.parse(data), res.statusCode, res.headers)); + }); + + req.on('error', () => { + callback(null) + }); + + req.end(); +} + cls() getSession() process.stdout.on('resize', drawApp); -- cgit v1.2.3