2022-05-03 18:41:33 +02:00
|
|
|
import base
|
2022-05-04 20:03:52 +02:00
|
|
|
import std/[os,strutils,sequtils,times]
|
2022-05-03 18:41:33 +02:00
|
|
|
|
2022-05-04 20:03:52 +02:00
|
|
|
const note_dir = getHomeDir() & "Nextcloud/.notes.dmenu/" # Putting it in Nextcloud so it can sync :-)
|
2022-05-04 17:17:02 +02:00
|
|
|
const default_bg = white
|
|
|
|
const default_fg = black
|
2022-05-03 18:41:33 +02:00
|
|
|
|
2022-05-04 20:03:52 +02:00
|
|
|
type
|
|
|
|
Note = object
|
|
|
|
id: string
|
|
|
|
text: string
|
|
|
|
|
2022-05-04 17:17:02 +02:00
|
|
|
proc startNotes()
|
2022-05-04 20:03:52 +02:00
|
|
|
|
2022-05-04 17:17:02 +02:00
|
|
|
proc displayOptionMenu(option: string)
|
|
|
|
|
2022-05-04 20:03:52 +02:00
|
|
|
var notes: seq[Note] = @[]
|
|
|
|
|
|
|
|
proc readNotes(): seq[Note] =
|
|
|
|
if len(notes) == 0:
|
|
|
|
if existsOrCreateDir(note_dir):
|
|
|
|
for note_file in walkDir(note_dir):
|
|
|
|
try:
|
|
|
|
var note = Note()
|
|
|
|
note.id = note_file[1]
|
|
|
|
note.text = readFile(note_file[1])
|
|
|
|
notes.add(note)
|
|
|
|
except:
|
|
|
|
echo "Unable to read note : ", note_file, " : ", getCurrentExceptionMsg()
|
|
|
|
return notes
|
2022-05-03 18:41:33 +02:00
|
|
|
|
2022-05-04 20:03:52 +02:00
|
|
|
proc removeNote(note: Note) =
|
2022-05-03 18:41:33 +02:00
|
|
|
try:
|
2022-05-04 20:03:52 +02:00
|
|
|
removeFile(note.id)
|
|
|
|
for idx, a_note in notes:
|
|
|
|
if note.id == a_note.id:
|
|
|
|
notes.delete(idx)
|
2022-05-03 18:41:33 +02:00
|
|
|
except:
|
2022-05-04 20:03:52 +02:00
|
|
|
echo "Unable to delete file : ", note.id, " : ", getCurrentExceptionMsg()
|
|
|
|
|
|
|
|
proc getNoteStrings(): seq[string] =
|
|
|
|
var note_list: seq[string] = @[]
|
|
|
|
if len(notes) == 0:
|
|
|
|
discard readNotes()
|
|
|
|
for note in notes:
|
|
|
|
note_list.add(note.text)
|
|
|
|
return note_list
|
|
|
|
|
|
|
|
proc writeNote(note: Note, is_new: bool = false) =
|
|
|
|
if note.text == "":
|
2022-05-03 18:41:33 +02:00
|
|
|
return
|
|
|
|
try:
|
2022-05-04 20:03:52 +02:00
|
|
|
writeFile(note.id, note.text)
|
|
|
|
if is_new:
|
|
|
|
notes.add(note)
|
2022-05-03 22:43:47 +02:00
|
|
|
else:
|
2022-05-04 20:03:52 +02:00
|
|
|
for idx, a_note in notes:
|
|
|
|
if a_note.id == note.id:
|
|
|
|
notes[idx].text = note.text
|
|
|
|
except:
|
|
|
|
echo "write_note, Unable to write note : ", note.id, " : ", getCurrentExceptionMsg()
|
2022-05-03 22:43:47 +02:00
|
|
|
|
2022-05-04 17:17:02 +02:00
|
|
|
proc getNotes(): (Info, seq[string]) =
|
|
|
|
var info = newInfo("Notes")
|
|
|
|
info.selected_bg = default_bg
|
|
|
|
info.selected_fg = default_fg
|
2022-05-04 20:03:52 +02:00
|
|
|
let notes = getNoteStrings()
|
2022-05-03 18:41:33 +02:00
|
|
|
return (info,notes)
|
|
|
|
|
2022-05-04 20:03:52 +02:00
|
|
|
proc getNote(text: string): Note =
|
|
|
|
for note in notes:
|
|
|
|
if note.text == text:
|
|
|
|
return note
|
|
|
|
return Note()
|
|
|
|
|
2022-05-04 17:17:02 +02:00
|
|
|
proc displayDeleteConfirmationMenu(note: string) =
|
|
|
|
var yes_no = newInfo("Confirm Delete note : " & note)
|
|
|
|
yes_no.selected_bg = default_bg
|
|
|
|
yes_no.selected_fg = default_fg
|
|
|
|
let args = @["yes", "no"]
|
|
|
|
let choice = outputData(yes_no, args)
|
|
|
|
case choice:
|
|
|
|
of "yes":
|
2022-05-04 20:03:52 +02:00
|
|
|
let rm_note = getNote(note)
|
|
|
|
if rm_note.text == note:
|
|
|
|
removeNote(rm_note)
|
2022-05-04 17:17:02 +02:00
|
|
|
of "no":
|
|
|
|
displayOptionMenu(note)
|
|
|
|
|
2022-05-04 23:44:41 +02:00
|
|
|
proc replaceNoteConfirmationMenu(note_idx: int, new_text: string): bool =
|
|
|
|
let old_note = notes[note_idx]
|
|
|
|
var diag = newInfo("Replace Note : '" & old_note.text & "', with '" & new_text & "' ?")
|
|
|
|
diag.selected_bg = default_bg
|
|
|
|
diag.selected_fg = default_fg
|
|
|
|
let args = @["yes", "no"]
|
|
|
|
let choice = outputData(diag,args)
|
|
|
|
case choice:
|
|
|
|
of "yes":
|
|
|
|
echo "here"
|
|
|
|
return true
|
|
|
|
else:
|
|
|
|
return false
|
|
|
|
|
2022-05-04 20:03:52 +02:00
|
|
|
proc replaceNote(new_text: string, old_text: string) =
|
|
|
|
for idx, note in notes:
|
|
|
|
if note.text == old_text:
|
2022-05-04 23:44:41 +02:00
|
|
|
if replaceNoteConfirmationMenu(idx,new_text):
|
|
|
|
var new_note = note
|
|
|
|
new_note.text = new_text
|
|
|
|
notes[idx] = new_note
|
|
|
|
writeNote(new_note)
|
2022-05-04 20:03:52 +02:00
|
|
|
return
|
|
|
|
|
2022-05-04 17:17:02 +02:00
|
|
|
proc displayOptionMenu(option: string) =
|
|
|
|
var select = newInfo("Note")
|
|
|
|
select.selected_bg = default_bg
|
|
|
|
select.selected_fg = default_fg
|
|
|
|
let note_choices = @["rm","back"]
|
2022-05-03 22:43:47 +02:00
|
|
|
let args = concat(@[option],note_choices)
|
2022-05-04 17:17:02 +02:00
|
|
|
let chosen = outputData(select,args)
|
2022-05-03 18:41:33 +02:00
|
|
|
if chosen in note_choices:
|
|
|
|
case chosen:
|
|
|
|
of "rm":
|
2022-05-04 17:17:02 +02:00
|
|
|
displayDeleteConfirmationMenu(option)
|
|
|
|
startNotes()
|
2022-05-03 18:41:33 +02:00
|
|
|
of "back":
|
2022-05-04 17:17:02 +02:00
|
|
|
startNotes()
|
2022-05-03 22:43:47 +02:00
|
|
|
elif chosen != "" and chosen != option:
|
2022-05-04 17:17:02 +02:00
|
|
|
replaceNote(chosen, option)
|
|
|
|
displayOptionMenu(chosen)
|
2022-05-03 18:41:33 +02:00
|
|
|
else:
|
2022-05-04 17:17:02 +02:00
|
|
|
displayOptionMenu(option)
|
2022-05-03 18:41:33 +02:00
|
|
|
|
2022-05-04 20:03:52 +02:00
|
|
|
proc newNote(text: string) =
|
|
|
|
var new_note = Note()
|
2022-05-04 23:44:41 +02:00
|
|
|
let note_id = times.now().format("'{'yyyyMMdd-HHmmss-ffffff'}'")
|
2022-05-04 20:03:52 +02:00
|
|
|
new_note.id = note_dir & note_id
|
|
|
|
new_note.text = text
|
|
|
|
writeNote(new_note, is_new = true)
|
|
|
|
|
2022-05-04 17:17:02 +02:00
|
|
|
proc startNotes() =
|
2022-05-04 20:03:52 +02:00
|
|
|
let (info,all_notes) = getNotes()
|
2022-05-04 17:17:02 +02:00
|
|
|
let all_choices = @["exit"]
|
2022-05-04 20:03:52 +02:00
|
|
|
let args = concat(all_notes, all_choices)
|
2022-05-04 17:17:02 +02:00
|
|
|
let option = outputData(info, args)
|
2022-05-03 18:41:33 +02:00
|
|
|
if option in all_choices:
|
|
|
|
case option:
|
|
|
|
of "exit":
|
|
|
|
return
|
2022-05-04 20:03:52 +02:00
|
|
|
if option in all_notes:
|
2022-05-04 17:17:02 +02:00
|
|
|
displayOptionMenu(option)
|
2022-05-03 22:53:29 +02:00
|
|
|
elif option != "":
|
2022-05-04 20:03:52 +02:00
|
|
|
newNote(option)
|
2022-05-04 17:17:02 +02:00
|
|
|
startNotes()
|
2022-05-03 18:41:33 +02:00
|
|
|
|
|
|
|
proc main() =
|
2022-05-04 20:03:52 +02:00
|
|
|
echo "Note dir : ", note_dir
|
2022-05-03 18:41:33 +02:00
|
|
|
if not dmenu and not rofi:
|
|
|
|
echo "Can only be run in dmenu or rofi mode. Exiting..."
|
|
|
|
return
|
2022-05-04 17:17:02 +02:00
|
|
|
startNotes()
|
2022-05-03 18:41:33 +02:00
|
|
|
|
|
|
|
if isMainModule:
|
|
|
|
main()
|