Compare commits
No commits in common. "541635392d1286ef656d52e9111fef416dc7f209" and "b32187db911cd5143d7743efb89c432d0d8c7b91" have entirely different histories.
541635392d
...
b32187db91
3 changed files with 49 additions and 78 deletions
0
fuzzytime.nim
Normal file → Executable file
0
fuzzytime.nim
Normal file → Executable file
123
notes.nim
123
notes.nim
|
@ -1,78 +1,68 @@
|
|||
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_fg = black
|
||||
|
||||
type
|
||||
Note = object
|
||||
id: string
|
||||
text: string
|
||||
|
||||
proc startNotes()
|
||||
|
||||
proc displayOptionMenu(option: string)
|
||||
|
||||
var notes: seq[Note] = @[]
|
||||
|
||||
proc readNotes(): seq[Note] =
|
||||
if len(notes) == 0:
|
||||
if existsOrCreateDir(note_dir):
|
||||
for note_file in walkDir(note_dir):
|
||||
proc readNotes(): seq[string] =
|
||||
try:
|
||||
var note = Note()
|
||||
note.id = note_file[1]
|
||||
note.text = readFile(note_file[1])
|
||||
notes.add(note)
|
||||
except:
|
||||
echo "Unable to read note : ", note_file, " : ", getCurrentExceptionMsg()
|
||||
var notes: seq[string] = @[]
|
||||
for line in note_file.lines:
|
||||
notes.add(line)
|
||||
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:
|
||||
echo "Unable to delete file : ", note.id, " : ", getCurrentExceptionMsg()
|
||||
echo "read_notes, Can't open notes file :", getCurrentExceptionMsg()
|
||||
return @["<empty>"]
|
||||
|
||||
proc getNoteStrings(): seq[string] =
|
||||
var note_list: seq[string] = @[]
|
||||
if len(notes) == 0:
|
||||
discard readNotes()
|
||||
proc writeNotes(notes: seq[string]) =
|
||||
try:
|
||||
let f = open(note_file, fmWrite)
|
||||
defer: f.close()
|
||||
for note in notes:
|
||||
note_list.add(note.text)
|
||||
return note_list
|
||||
f.writeLine(note)
|
||||
except:
|
||||
echo "write_notes, Cannot write notes to file : ", getCurrentExceptionMsg()
|
||||
|
||||
proc writeNote(note: Note, is_new: bool = false) =
|
||||
if note.text == "":
|
||||
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:
|
||||
writeFile(note.id, note.text)
|
||||
if is_new:
|
||||
notes.add(note)
|
||||
else:
|
||||
for idx, a_note in notes:
|
||||
if a_note.id == note.id:
|
||||
notes[idx].text = note.text
|
||||
let f = open(note_file, fmAppend)
|
||||
defer: f.close()
|
||||
f.writeLine(strip(note))
|
||||
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]) =
|
||||
var info = newInfo("Notes")
|
||||
info.selected_bg = default_bg
|
||||
info.selected_fg = default_fg
|
||||
let notes = getNoteStrings()
|
||||
let notes = readNotes()
|
||||
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
|
||||
|
@ -81,22 +71,10 @@ proc displayDeleteConfirmationMenu(note: string) =
|
|||
let choice = outputData(yes_no, args)
|
||||
case choice:
|
||||
of "yes":
|
||||
let rm_note = getNote(note)
|
||||
if rm_note.text == note:
|
||||
removeNote(rm_note)
|
||||
removeNote(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
|
||||
|
@ -117,30 +95,23 @@ 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,all_notes) = getNotes()
|
||||
let (info,notes) = getNotes()
|
||||
let all_choices = @["exit"]
|
||||
let args = concat(all_notes, all_choices)
|
||||
let args = concat(notes, all_choices)
|
||||
let option = outputData(info, args)
|
||||
if option in all_choices:
|
||||
case option:
|
||||
of "exit":
|
||||
return
|
||||
if option in all_notes:
|
||||
if option in notes:
|
||||
displayOptionMenu(option)
|
||||
elif option != "":
|
||||
newNote(option)
|
||||
writeNote(option)
|
||||
startNotes()
|
||||
|
||||
proc main() =
|
||||
echo "Note dir : ", note_dir
|
||||
echo "Note file : ", note_file
|
||||
if not dmenu and not rofi:
|
||||
echo "Can only be run in dmenu or rofi mode. Exiting..."
|
||||
return
|
||||
|
|
0
volume.nim
Normal file → Executable file
0
volume.nim
Normal file → Executable file
Loading…
Reference in a new issue