Compare commits

...

2 commits

3 changed files with 78 additions and 49 deletions

0
fuzzytime.nim Executable file → Normal file
View file

121
notes.nim
View file

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

0
volume.nim Executable file → Normal file
View file