diff options
author | 2022-05-08 13:21:34 +0100 | |
---|---|---|
committer | 2022-05-08 13:21:34 +0100 | |
commit | 9d917fae37925506789f64e17901d319e7002039 (patch) | |
tree | a8ecff52eb974ea6847ebb9e5411bd7c839ce17f | |
parent | 119ba1939f4c7d36dbf49347f7ab8322f51bfb18 (diff) | |
download | notes_cli-9d917fae37925506789f64e17901d319e7002039.tar.gz notes_cli-9d917fae37925506789f64e17901d319e7002039.tar.bz2 notes_cli-9d917fae37925506789f64e17901d319e7002039.zip |
Added create note function
-rw-r--r-- | README.md | 3 | ||||
-rwxr-xr-x | notes_cli.js | 152 |
2 files changed, 105 insertions, 50 deletions
@@ -9,10 +9,11 @@ Key bindings (press [h] to view from the script): > [up/k] - Previous note > [down/j] - Next note > [enter/o] - Open note +> [c] - Create note Options that will be added: -> [c] - Create note +> [d] - Delete note > [r] - Rename note > [s] - Sort note diff --git a/notes_cli.js b/notes_cli.js index db146be..675a7cb 100755 --- a/notes_cli.js +++ b/notes_cli.js @@ -20,8 +20,6 @@ const cursor = { show: () => process.stdout.write('\u001B[?25h') } -cursor.hide() - process.on('exit',() => { rl.close() cursor.show() @@ -55,7 +53,7 @@ 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','','Options not yet added:','[c] - Create note','[r] - Rename note','[s] - Sort note']}; + '[enter/o] - Open note','[c] - Create note','','Options not yet added:','[d] - Delete note','[r] - Rename note','[s] - Sort note']}; menu = 'Press any key to exit help' drawApp() } @@ -67,15 +65,35 @@ const showList = (notes, status) => { } notesList = notes - drawApp() - process.stdout.on('resize', drawApp); draw = { t: 'l', v: () => notes.map(formatNote) } drawApp() } +const getNewNoteTitle = () => { + 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} + drawApp() + cursor.show() + rl.question(' Title: ', t => { + cursor.hide() + if (t === '') { + draw = {t: 'p', prev: v, v: () => ['Canceled']} + drawApp() + setTimeout(() => { + draw = {t: 'l', v} + drawApp() + }, 1000) + } else { + createTmpFile(t) + } + }) +} + const saveTmpFile = (note, { content }) => { const file = `${filesPath}/${note._id}.tmp` - fs.writeFile(file, JSON.stringify(content), function(err) { + fs.writeFile(file, content, function(err) { if(err) { exitWithError(['Error creating tmp file']) } @@ -84,15 +102,15 @@ const saveTmpFile = (note, { content }) => { }) } -const readTmpFile = (note) => { - const file =`${filesPath}/${note._id}.tmp` +const readTmpFile = (note, title) => { + const file = `${filesPath}/${title ? 'new' : note._id}.tmp` fs.readFile(file, function(err, f){ if (err) { return getEmail() } try { - const content = JSON.parse(f.toString()) + const content = f.toString() fs.unlink(file, () => {}) @@ -100,18 +118,42 @@ const readTmpFile = (note) => { return getEmail('Error saving note') } - putNote(note, content) + title ? createNote(title, content) : putNote(note, content) } catch (e) { return getEmail('Session error') } }) } -const noteSaved = () => { +const noteSaved = (r, s, h) => { + draw = {t: 'p', v: () => ['Note saved']} drawApp() fetchList() } +const createTmpFile = (title) => { + const file = `${filesPath}/new.tmp` + const child_process = require('child_process') + const editor = process.env.EDITOR || 'vi'; + cursor.show() + const child = child_process.spawn(editor, [file], { stdio: 'inherit' }); + rl.pause() + child.on('exit', function (e) { + rl.resume() + cursor.hide() + if (e === 0) { + draw = { t: 'p', v: () => ['Saving new note...'] } + drawApp() + readTmpFile(null, title) + } else { + draw = { t: 'p', v: () => ['Canceled'] } + fs.unlink(file, () => {}) + drawApp() + fetchList() + } + }); +} + const editTmpFile = (note) => { const file = `${filesPath}/${note._id}.tmp` const child_process = require('child_process') @@ -136,7 +178,7 @@ const editTmpFile = (note) => { // =========== DRAW APP ============= -const drawApp = (lastLine = true) => { +const drawApp = () => { const lines = process.stdout.rows const columns = process.stdout.columns const headers = columns > 72 @@ -198,7 +240,7 @@ const drawApp = (lastLine = true) => { } } - if (lastLine) { + if (!draw.noMenu) { const activeNr = `${active+1}` const notesNr = `${notesList.length}` process.stdout.write(' '+menu+' '.repeat(columns-5-menu.length-activeNr.length-notesNr.length)+`[${activeNr}/${notesNr}]`) @@ -231,13 +273,19 @@ const getKey = () => { break case 'return': case 'o': - fetchNote(notesList[active]) - break + if (draw.t === 'l') { + fetchNote(notesList[active]) + break + } case 'h': if (draw.t === 'l') { showHelp() break } + case 'c': + if (draw.t === 'l') { + getNewNoteTitle() + } case 'q': if (draw.t === 'l') { process.exit() @@ -295,47 +343,47 @@ const getSession = () => { const loginMsg = (text, err) => ['Login to apps.pruss.it', '', `\x1b[31m${err}\x1b[0m`, '', ...text] const getPass = (e) => { - cls() - draw = { t: 'p', v: () => loginMsg(['Please type password for', `your apps.pruss.it account`, 'or press [q] to quit'], '')} - drawApp(false) - rl.stdoutMuted = true - rl.write() - rl.question('', p => { - rl.pause() - rl.stdoutMuted = false - drawApp(false) - if (p === 'q') { - process.exit() - } + cls() + draw = { t: 'p', v: () => loginMsg(['Please type password for', `your apps.pruss.it account`, 'or press [q] to quit'], ''), noMenu: true} + drawApp() + rl.stdoutMuted = true + rl.write() + rl.question('', p => { + rl.pause() + rl.stdoutMuted = false + drawApp() + if (p === 'q') { + process.exit() + } - post( - '/api/login', - {"email": e, "password": p}, - (o, s, h) => { - if (!o || s !== 201) { - return getEmail('Could not log in, try again') - } + post( + '/api/login', + {"email": e, "password": p}, + (o, s, h) => { + if (!o || s !== 201) { + return getEmail('Could not log in, try again') + } - const {_id, email, isVerified, noteList} = o - const session = h['set-cookie'] + const {_id, email, isVerified, noteList} = o + const session = h['set-cookie'] - if (!_id || !email || !noteList) { - exitWithError(['Could not log in']) - } else if (!isVerified) { - exitWithError(['User not verified.', 'Please first verify the user using apps.pruss.it']) - } + if (!_id || !email || !noteList) { + exitWithError(['Could not log in']) + } else if (!isVerified) { + exitWithError(['User not verified.', 'Please first verify the user using apps.pruss.it']) + } - setSession({userId: _id, email, list: noteList, session}) - }) - }) + setSession({userId: _id, email, list: noteList, session}) + }) + }) - rl.history = rl.history.slice(1) + rl.history = rl.history.slice(1) } const getEmail = (error = '') => { cls() - draw = { t: 'p', v: () => loginMsg(['Please input email for your','apps.pruss.it account or' , 'press [q] to quit'], error)} - drawApp(false) + draw = { t: 'p', v: () => loginMsg(['Please input email for your','apps.pruss.it account or' , 'press [q] to quit'], error), noMenu: true} + drawApp() cursor.show() rl.question(' Email: ', e => { cursor.hide() @@ -367,6 +415,10 @@ const putNote = ({ _id, noteId }, content) => { put(`/api/notes/${_id}`, { noteId, content }, noteSaved, conf.session) } +const createNote = (title, content) => { + post('/api/notes', { title, content }, noteSaved, conf.session) +} + /* HTTPS METHODS */ const get = (path, callback, cookie = '') => { @@ -407,11 +459,11 @@ const post = (path, data, callback, cookie = '', put = false) => { headers: put ? { 'Cookie': cookie, } : { - 'Content-Type': 'application/json', + 'Content-Type': 'text/plain', 'Content-Length': dataString.length, 'Cookie': cookie, }, - timeout: 1000 + timeout: 1000, }; const req = https.request(options, (res) => { @@ -434,3 +486,5 @@ const put = (...arg) => post(...arg, true) cls() getSession() +process.stdout.on('resize', drawApp); +cursor.hide() |