Compare commits
2 commits
b32187db91
...
541635392d
Author | SHA1 | Date | |
---|---|---|---|
541635392d | |||
8762d8b465 |
3 changed files with 78 additions and 49 deletions
0
fuzzytime.nim
Executable file → Normal file
0
fuzzytime.nim
Executable file → Normal file
127
notes.nim
127
notes.nim
|
@ -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:
|
||||||
|
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()
|
||||||
|
return notes
|
||||||
|
|
||||||
|
proc removeNote(note: Note) =
|
||||||
try:
|
try:
|
||||||
var notes: seq[string] = @[]
|
removeFile(note.id)
|
||||||
for line in note_file.lines:
|
for idx, a_note in notes:
|
||||||
notes.add(line)
|
if note.id == a_note.id:
|
||||||
return notes
|
notes.delete(idx)
|
||||||
except:
|
except:
|
||||||
echo "read_notes, Can't open notes file :", getCurrentExceptionMsg()
|
echo "Unable to delete file : ", note.id, " : ", getCurrentExceptionMsg()
|
||||||
return @["<empty>"]
|
|
||||||
|
|
||||||
proc writeNotes(notes: seq[string]) =
|
proc getNoteStrings(): seq[string] =
|
||||||
try:
|
var note_list: seq[string] = @[]
|
||||||
let f = open(note_file, fmWrite)
|
if len(notes) == 0:
|
||||||
defer: f.close()
|
discard readNotes()
|
||||||
for note in notes:
|
for note in notes:
|
||||||
f.writeLine(note)
|
note_list.add(note.text)
|
||||||
except:
|
return note_list
|
||||||
echo "write_notes, Cannot write notes to file : ", getCurrentExceptionMsg()
|
|
||||||
|
|
||||||
proc removeNote(note: string) =
|
proc writeNote(note: Note, is_new: bool = false) =
|
||||||
let notes = readNotes()
|
if note.text == "":
|
||||||
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:
|
||||||
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
0
volume.nim
Executable file → Normal file
Loading…
Reference in a new issue