changed notes process to have separate files per note

This commit is contained in:
Paul Wilde 2022-05-04 19:03:52 +01:00
parent b32187db91
commit 8762d8b465
3 changed files with 80 additions and 49 deletions

0
fuzzytime.nim Executable file → Normal file
View file

129
notes.nim
View file

@ -1,68 +1,80 @@
import base
import std/[os,strutils,sequtils]
import std/[os,strutils,sequtils,times]
const note_file = getHomeDir() & "Nextcloud/.notes.dmenu" # Putting it in Nextcloud so it can sync :-)
const note_dir = getHomeDir() & "Nextcloud/.notes.dmenu/" # Putting it in Nextcloud so it can sync :-)
const default_bg = white
const default_fg = black
type
Note = object
id: string
text: string
proc startNotes()
proc displayOptionMenu(option: string)
proc readNotes(): seq[string] =
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()
echo note_file[0]
echo note_file[1]
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
proc removeNote(note: Note) =
try:
var notes: seq[string] = @[]
for line in note_file.lines:
notes.add(line)
return notes
removeFile(note.id)
for idx, a_note in notes:
if note.id == a_note.id:
notes.delete(idx)
except:
echo "read_notes, Can't open notes file :", getCurrentExceptionMsg()
return @["<empty>"]
echo "Unable to delete file : ", note.id, " : ", getCurrentExceptionMsg()
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 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 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 == "":
proc writeNote(note: Note, is_new: bool = false) =
if note.text == "":
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)
writeFile(note.id, note.text)
if is_new:
notes.add(note)
else:
new_notes.add(a_note)
writeNotes(new_notes)
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()
proc getNotes(): (Info, seq[string]) =
var info = newInfo("Notes")
info.selected_bg = default_bg
info.selected_fg = default_fg
let notes = readNotes()
let notes = getNoteStrings()
return (info,notes)
proc getNote(text: string): Note =
for note in notes:
if note.text == text:
return note
return Note()
proc displayDeleteConfirmationMenu(note: string) =
var yes_no = newInfo("Confirm Delete note : " & note)
yes_no.selected_bg = default_bg
@ -71,10 +83,22 @@ proc displayDeleteConfirmationMenu(note: string) =
let choice = outputData(yes_no, args)
case choice:
of "yes":
removeNote(note)
let rm_note = getNote(note)
if rm_note.text == note:
removeNote(rm_note)
of "no":
displayOptionMenu(note)
proc replaceNote(new_text: string, old_text: string) =
for idx, note in notes:
if note.text == old_text:
var new_note = note
new_note.text = new_text
notes[idx] = new_note
writeNote(new_note)
return
proc displayOptionMenu(option: string) =
var select = newInfo("Note")
select.selected_bg = default_bg
@ -95,23 +119,30 @@ proc displayOptionMenu(option: string) =
else:
displayOptionMenu(option)
proc newNote(text: string) =
var new_note = Note()
let note_id = times.now().format("yyyyMMddHHmmssffffff")
new_note.id = note_dir & note_id
new_note.text = text
writeNote(new_note, is_new = true)
proc startNotes() =
let (info,notes) = getNotes()
let (info,all_notes) = getNotes()
let all_choices = @["exit"]
let args = concat(notes, all_choices)
let args = concat(all_notes, all_choices)
let option = outputData(info, args)
if option in all_choices:
case option:
of "exit":
return
if option in notes:
if option in all_notes:
displayOptionMenu(option)
elif option != "":
writeNote(option)
newNote(option)
startNotes()
proc main() =
echo "Note file : ", note_file
echo "Note dir : ", note_dir
if not dmenu and not rofi:
echo "Can only be run in dmenu or rofi mode. Exiting..."
return

0
volume.nim Executable file → Normal file
View file