2022-05-03 18:41:33 +02:00
|
|
|
import base
|
|
|
|
import std/[os,strutils,sequtils]
|
|
|
|
|
|
|
|
|
2022-05-04 17:17:02 +02:00
|
|
|
const note_file = getHomeDir() & "Nextcloud/.notes.dmenu" # Putting it in Nextcloud so it can sync :-)
|
|
|
|
const default_bg = white
|
|
|
|
const default_fg = black
|
2022-05-03 18:41:33 +02:00
|
|
|
|
2022-05-04 17:17:02 +02:00
|
|
|
proc startNotes()
|
|
|
|
proc displayOptionMenu(option: string)
|
|
|
|
|
|
|
|
proc readNotes(): seq[string] =
|
2022-05-03 18:41:33 +02:00
|
|
|
try:
|
|
|
|
var notes: seq[string] = @[]
|
|
|
|
for line in note_file.lines:
|
|
|
|
notes.add(line)
|
|
|
|
return notes
|
|
|
|
except:
|
|
|
|
echo "read_notes, Can't open notes file :", getCurrentExceptionMsg()
|
|
|
|
return @["<empty>"]
|
|
|
|
|
2022-05-04 17:17:02 +02:00
|
|
|
proc writeNotes(notes: seq[string]) =
|
2022-05-03 18:41:33 +02:00
|
|
|
try:
|
|
|
|
let f = open(note_file, fmWrite)
|
2022-05-03 22:43:47 +02:00
|
|
|
defer: f.close()
|
2022-05-03 18:41:33 +02:00
|
|
|
for note in notes:
|
|
|
|
f.writeLine(note)
|
|
|
|
except:
|
|
|
|
echo "write_notes, Cannot write notes to file : ", getCurrentExceptionMsg()
|
|
|
|
|
2022-05-04 17:17:02 +02:00
|
|
|
proc removeNote(note: string) =
|
|
|
|
let notes = readNotes()
|
2022-05-03 18:41:33 +02:00
|
|
|
var new_notes: seq[string] = @[]
|
|
|
|
for a_note in notes:
|
|
|
|
if note != a_note:
|
|
|
|
new_notes.add(a_note)
|
2022-05-04 17:17:02 +02:00
|
|
|
writeNotes(new_notes)
|
2022-05-03 18:41:33 +02:00
|
|
|
|
2022-05-04 17:17:02 +02:00
|
|
|
proc writeNote(note: string) =
|
2022-05-03 18:41:33 +02:00
|
|
|
if note == "":
|
|
|
|
return
|
|
|
|
try:
|
|
|
|
let f = open(note_file, fmAppend)
|
|
|
|
defer: f.close()
|
|
|
|
f.writeLine(strip(note))
|
|
|
|
except:
|
|
|
|
echo "write_note, Unable to write note :", getCurrentExceptionMsg()
|
|
|
|
|
2022-05-04 17:17:02 +02:00
|
|
|
proc replaceNote(new_note: string, old_note: string) =
|
|
|
|
let notes = readNotes()
|
2022-05-03 22:43:47 +02:00
|
|
|
var new_notes: seq[string] = @[]
|
|
|
|
for a_note in notes:
|
|
|
|
if old_note == a_note:
|
|
|
|
new_notes.add(new_note)
|
|
|
|
else:
|
|
|
|
new_notes.add(a_note)
|
2022-05-04 17:17:02 +02:00
|
|
|
writeNotes(new_notes)
|
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
|
|
|
|
let notes = readNotes()
|
2022-05-03 18:41:33 +02:00
|
|
|
return (info,notes)
|
|
|
|
|
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":
|
|
|
|
removeNote(note)
|
|
|
|
of "no":
|
|
|
|
displayOptionMenu(note)
|
|
|
|
|
|
|
|
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 17:17:02 +02:00
|
|
|
proc startNotes() =
|
|
|
|
let (info,notes) = getNotes()
|
|
|
|
let all_choices = @["exit"]
|
2022-05-03 18:41:33 +02:00
|
|
|
let args = concat(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
|
|
|
|
if option in notes:
|
2022-05-04 17:17:02 +02:00
|
|
|
displayOptionMenu(option)
|
2022-05-03 22:53:29 +02:00
|
|
|
elif option != "":
|
2022-05-04 17:17:02 +02:00
|
|
|
writeNote(option)
|
|
|
|
startNotes()
|
2022-05-03 18:41:33 +02:00
|
|
|
|
|
|
|
proc main() =
|
|
|
|
echo "Note file : ", note_file
|
|
|
|
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()
|