wmtools/notes.nim

122 lines
3 KiB
Nim
Raw Normal View History

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)
2022-05-03 22:43:47 +02:00
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()
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)
writeNotes(new_notes)
2022-05-03 22:43:47 +02:00
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"]
2022-05-03 22:43:47 +02:00
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()
2022-05-03 22:43:47 +02:00
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()