2022-05-03 18:41:33 +02:00
|
|
|
import base
|
|
|
|
import std/[os,strutils,sequtils]
|
|
|
|
|
|
|
|
proc start_notes()
|
|
|
|
|
|
|
|
const all_choices = @["exit"]
|
|
|
|
const note_choices = @["rm","back"]
|
2022-05-03 22:53:29 +02:00
|
|
|
const note_file = getHomeDir() & "Nextcloud/.notes.dmenu"
|
2022-05-03 18:41:33 +02:00
|
|
|
|
|
|
|
proc read_notes(): 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 write_notes(notes: seq[string]) =
|
|
|
|
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()
|
|
|
|
|
|
|
|
proc remove_note(note: string) =
|
|
|
|
let notes = read_notes()
|
|
|
|
var new_notes: seq[string] = @[]
|
|
|
|
for a_note in notes:
|
|
|
|
if note != a_note:
|
|
|
|
new_notes.add(a_note)
|
|
|
|
write_notes(new_notes)
|
|
|
|
|
|
|
|
proc write_note(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()
|
|
|
|
|
2022-05-03 22:43:47 +02:00
|
|
|
proc replace_note(new_note: string, old_note: string) =
|
|
|
|
let notes = read_notes()
|
|
|
|
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)
|
|
|
|
write_notes(new_notes)
|
|
|
|
|
2022-05-03 18:41:33 +02:00
|
|
|
proc get_notes(): (Info, seq[string]) =
|
|
|
|
var info = newInfo()
|
|
|
|
info.title = "Notes :"
|
|
|
|
let notes = read_notes()
|
|
|
|
return (info,notes)
|
|
|
|
|
|
|
|
proc display_option_menu(option: string) =
|
|
|
|
var select = newInfo()
|
|
|
|
select.title = "Note :"
|
2022-05-03 22:43:47 +02:00
|
|
|
let args = concat(@[option],note_choices)
|
2022-05-03 18:41:33 +02:00
|
|
|
let chosen = outputJSON(select,args)
|
|
|
|
if chosen in note_choices:
|
|
|
|
case chosen:
|
|
|
|
of "rm":
|
|
|
|
remove_note(option)
|
|
|
|
start_notes()
|
|
|
|
of "back":
|
|
|
|
start_notes()
|
2022-05-03 22:43:47 +02:00
|
|
|
elif chosen != "" and chosen != option:
|
|
|
|
replace_note(chosen, option)
|
|
|
|
display_option_menu(chosen)
|
2022-05-03 18:41:33 +02:00
|
|
|
else:
|
2022-05-03 22:43:47 +02:00
|
|
|
display_option_menu(option)
|
2022-05-03 18:41:33 +02:00
|
|
|
|
|
|
|
proc start_notes() =
|
|
|
|
let (info,notes) = get_notes()
|
|
|
|
let args = concat(notes, all_choices)
|
|
|
|
let option = outputJSON(info, args)
|
|
|
|
if option in all_choices:
|
|
|
|
case option:
|
|
|
|
of "exit":
|
|
|
|
return
|
|
|
|
if option in notes:
|
|
|
|
display_option_menu(option)
|
2022-05-03 22:53:29 +02:00
|
|
|
elif option != "":
|
2022-05-03 18:41:33 +02:00
|
|
|
write_note(option)
|
|
|
|
start_notes()
|
|
|
|
|
|
|
|
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
|
|
|
|
start_notes()
|
|
|
|
|
|
|
|
if isMainModule:
|
|
|
|
main()
|