Compare commits

..

No commits in common. "541635392d1286ef656d52e9111fef416dc7f209" and "b32187db911cd5143d7743efb89c432d0d8c7b91" have entirely different histories.

3 changed files with 49 additions and 78 deletions

0
fuzzytime.nim Normal file → Executable file
View file

123
notes.nim
View file

@ -1,78 +1,68 @@
import base import base
import std/[os,strutils,sequtils,times] import std/[os,strutils,sequtils]
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)
var notes: seq[Note] = @[] proc readNotes(): seq[string] =
proc readNotes(): seq[Note] =
if len(notes) == 0:
if existsOrCreateDir(note_dir):
for note_file in walkDir(note_dir):
try: try:
var note = Note() var notes: seq[string] = @[]
note.id = note_file[1] for line in note_file.lines:
note.text = readFile(note_file[1]) notes.add(line)
notes.add(note)
except:
echo "Unable to read note : ", note_file, " : ", getCurrentExceptionMsg()
return notes return notes
proc removeNote(note: Note) =
try:
removeFile(note.id)
for idx, a_note in notes:
if note.id == a_note.id:
notes.delete(idx)
except: except:
echo "Unable to delete file : ", note.id, " : ", getCurrentExceptionMsg() echo "read_notes, Can't open notes file :", getCurrentExceptionMsg()
return @["<empty>"]
proc getNoteStrings(): seq[string] = proc writeNotes(notes: seq[string]) =
var note_list: seq[string] = @[] try:
if len(notes) == 0: let f = open(note_file, fmWrite)
discard readNotes() defer: f.close()
for note in notes: for note in notes:
note_list.add(note.text) f.writeLine(note)
return note_list except:
echo "write_notes, Cannot write notes to file : ", getCurrentExceptionMsg()
proc writeNote(note: Note, is_new: bool = false) = proc removeNote(note: string) =
if note.text == "": 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 return
try: try:
writeFile(note.id, note.text) let f = open(note_file, fmAppend)
if is_new: defer: f.close()
notes.add(note) f.writeLine(strip(note))
else:
for idx, a_note in notes:
if a_note.id == note.id:
notes[idx].text = note.text
except: except:
echo "write_note, Unable to write note : ", note.id, " : ", getCurrentExceptionMsg() 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]) = 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 = getNoteStrings() let notes = readNotes()
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
@ -81,22 +71,10 @@ proc displayDeleteConfirmationMenu(note: string) =
let choice = outputData(yes_no, args) let choice = outputData(yes_no, args)
case choice: case choice:
of "yes": of "yes":
let rm_note = getNote(note) removeNote(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
@ -117,30 +95,23 @@ 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,all_notes) = getNotes() let (info,notes) = getNotes()
let all_choices = @["exit"] let all_choices = @["exit"]
let args = concat(all_notes, all_choices) let args = concat(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 all_notes: if option in notes:
displayOptionMenu(option) displayOptionMenu(option)
elif option != "": elif option != "":
newNote(option) writeNote(option)
startNotes() startNotes()
proc main() = proc main() =
echo "Note dir : ", note_dir echo "Note file : ", note_file
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 Normal file → Executable file
View file