121 lines
3 KiB
Nim
121 lines
3 KiB
Nim
import base
|
|
import std/[os,strutils,sequtils]
|
|
|
|
|
|
const note_file = getHomeDir() & "Nextcloud/.notes.dmenu" # Putting it in Nextcloud so it can sync :-)
|
|
const default_bg = white
|
|
const default_fg = black
|
|
|
|
proc startNotes()
|
|
proc displayOptionMenu(option: string)
|
|
|
|
proc readNotes(): seq[string] =
|
|
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>"]
|
|
|
|
proc writeNotes(notes: seq[string]) =
|
|
try:
|
|
let f = open(note_file, fmWrite)
|
|
defer: f.close()
|
|
for note in notes:
|
|
f.writeLine(note)
|
|
except:
|
|
echo "write_notes, Cannot write notes to file : ", getCurrentExceptionMsg()
|
|
|
|
proc removeNote(note: string) =
|
|
let notes = readNotes()
|
|
var new_notes: seq[string] = @[]
|
|
for a_note in notes:
|
|
if note != a_note:
|
|
new_notes.add(a_note)
|
|
writeNotes(new_notes)
|
|
|
|
proc writeNote(note: string) =
|
|
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()
|
|
|
|
proc replaceNote(new_note: string, old_note: string) =
|
|
let notes = readNotes()
|
|
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)
|
|
writeNotes(new_notes)
|
|
|
|
proc getNotes(): (Info, seq[string]) =
|
|
var info = newInfo("Notes")
|
|
info.selected_bg = default_bg
|
|
info.selected_fg = default_fg
|
|
let notes = readNotes()
|
|
return (info,notes)
|
|
|
|
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"]
|
|
let args = concat(@[option],note_choices)
|
|
let chosen = outputData(select,args)
|
|
if chosen in note_choices:
|
|
case chosen:
|
|
of "rm":
|
|
displayDeleteConfirmationMenu(option)
|
|
startNotes()
|
|
of "back":
|
|
startNotes()
|
|
elif chosen != "" and chosen != option:
|
|
replaceNote(chosen, option)
|
|
displayOptionMenu(chosen)
|
|
else:
|
|
displayOptionMenu(option)
|
|
|
|
proc startNotes() =
|
|
let (info,notes) = getNotes()
|
|
let all_choices = @["exit"]
|
|
let args = concat(notes, all_choices)
|
|
let option = outputData(info, args)
|
|
if option in all_choices:
|
|
case option:
|
|
of "exit":
|
|
return
|
|
if option in notes:
|
|
displayOptionMenu(option)
|
|
elif option != "":
|
|
writeNote(option)
|
|
startNotes()
|
|
|
|
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
|
|
startNotes()
|
|
|
|
if isMainModule:
|
|
main()
|