aboutsummaryrefslogtreecommitdiffstats
path: root/notes_cli.js
diff options
context:
space:
mode:
authorGravatar piotrruss <mail@pruss.it> 2022-05-08 20:51:58 +0100
committerGravatar piotrruss <mail@pruss.it> 2022-05-08 20:53:38 +0100
commiteb38e05f691fe0a4b722b96311fb045d77d31ce9 (patch)
tree5272b7ad4745b428b8fa24788e0255a524d08a08 /notes_cli.js
parent21da1c134cfa8db18a53d6e82a11148c4198dfda (diff)
downloadnotes_cli-eb38e05f691fe0a4b722b96311fb045d77d31ce9.tar.gz
notes_cli-eb38e05f691fe0a4b722b96311fb045d77d31ce9.tar.bz2
notes_cli-eb38e05f691fe0a4b722b96311fb045d77d31ce9.zip
added ability to sort
Diffstat (limited to 'notes_cli.js')
-rwxr-xr-xnotes_cli.js116
1 files changed, 83 insertions, 33 deletions
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()
})
}