aboutsummaryrefslogtreecommitdiffstats
path: root/notes_cli.js
diff options
context:
space:
mode:
authorGravatar piotrruss <mail@pruss.it> 2022-05-08 16:51:43 +0100
committerGravatar piotrruss <mail@pruss.it> 2022-05-08 16:51:43 +0100
commit21da1c134cfa8db18a53d6e82a11148c4198dfda (patch)
tree3954da69cf936869f9ce0a09887a829ce8eb675b /notes_cli.js
parentbc47cbd43a20716032d471292b7c72b4806ed337 (diff)
downloadnotes_cli-21da1c134cfa8db18a53d6e82a11148c4198dfda.tar.gz
notes_cli-21da1c134cfa8db18a53d6e82a11148c4198dfda.tar.bz2
notes_cli-21da1c134cfa8db18a53d6e82a11148c4198dfda.zip
Ability to remove notes
Diffstat (limited to 'notes_cli.js')
-rwxr-xr-xnotes_cli.js82
1 files changed, 77 insertions, 5 deletions
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);