added better dmenu cmd generation

This commit is contained in:
Paul Wilde 2022-05-06 11:12:13 +01:00
parent 84ca841406
commit 42a9b61a5b
4 changed files with 82 additions and 21 deletions

View file

@ -14,8 +14,19 @@ type
color*: string color*: string
html_text*: string html_text*: string
short_text*: string short_text*: string
Dmenu = object
type command: string
bottom: string
grab_kb: string
i_case: string
lines_shown: string
monitor: string
prompt: string
font: string
norm_bg: string
norm_fg: string
sel_bg: string
sel_fg: string
i3BarInput* = object i3BarInput* = object
button*: int button*: int
x*: int x*: int
@ -43,6 +54,7 @@ const darkgrey* = "#444"
const primary* = yellow const primary* = yellow
const secondary* = red const secondary* = red
const alert* = "#bd2c40" const alert* = "#bd2c40"
const MAX_LINES = 20
var loop* = false var loop* = false
var stoploop* = true var stoploop* = true
var dmenu* = true var dmenu* = true
@ -60,6 +72,33 @@ proc newInfo*(title: string = "Info"): Info =
background: black, background: black,
color: foreground, color: foreground,
) )
proc newDmenuConfig(): Dmenu =
var dmenu = Dmenu()
dmenu.command = "dmenu"
dmenu.bottom = "-b"
dmenu.grabkb = "-f"
dmenu.i_case = "-i"
dmenu.lines_shown = "-l"
dmenu.monitor = "-m"
dmenu.prompt = "-p"
dmenu.font = "-fn"
dmenu.norm_bg = "-nb"
dmenu.norm_fg = "-nf"
dmenu.sel_bg = "-sb"
dmenu.sel_fg = "-sf"
return dmenu
proc newRofiConfig(): Dmenu =
var rofi = newDmenuConfig()
rofi.command = "rofi -dmenu"
return rofi
proc newDmenu(): Dmenu =
if rofi:
return newRofiConfig()
return newDmenuConfig()
proc debugLog*(str: string) = proc debugLog*(str: string) =
let f = open("/tmp/debug.txt",fmAppend) let f = open("/tmp/debug.txt",fmAppend)
defer: f.close() defer: f.close()
@ -86,29 +125,48 @@ proc clearInput*(count: int = 1) =
proc getArguments*(): seq[string] = proc getArguments*(): seq[string] =
let args = commandLineParams() let args = commandLineParams()
return args return args
proc stripQuotes*(str: string): string =
return replace(str,"\"",""")
proc quote*(str: string): string =
var text = str
# May need to put some further work to escape some special chars here
text = stripQuotes(text)
# Put leading and ending quote marks in
return " \"" & text & "\" "
# ^ Add a spaces ^ so the previous flag isn't touching
proc getLines(num: int): int =
if num > MAX_LINES:
return MAX_LINES
return num + 1
proc runDMenu*(data: Info, opts: varargs[string], rofi: bool = false): string = proc runDMenu*(data: Info, opts: varargs[string], rofi: bool = false): string =
# Build dmenu/rofi command # Build dmenu/rofi command
var cmd = "echo -e \"" var cmd = ""
# if the text is empty, we don't want to create a menu item of it # if the text is empty, we don't want to create a menu item of it
if $data.full_text != "": if data.full_text != "":
cmd &= $data.full_text & "\n" cmd &= data.full_text & "\n"
for opt in opts: for opt in opts:
cmd = cmd & opt & "\n" cmd = cmd & opt & "\n"
cmd.removeSuffix("\n") cmd.removeSuffix("\n")
if not rofi:
cmd = cmd & "\" | dmenu" cmd = "echo -e" & quote(cmd) & " | "
else:
# run rofi in dmenu mode var dmenu = newDmenu()
cmd = cmd & "\" | rofi -dmenu" cmd = cmd & dmenu.command & " "
cmd = cmd & " -l " & $(len(opts) + 1) cmd = cmd & dmenu.lines_shown & " " & $20 & " "
cmd = cmd & " -p \"" & $data.title & "\"" cmd = cmd & dmenu.prompt & quote(data.title)
cmd = cmd & " -nb \"" & $data.unselected_bg & "\"" cmd = cmd & dmenu.norm_bg & quote(data.unselected_bg)
cmd = cmd & " -nf \"" & $data.unselected_fg & "\"" cmd = cmd & dmenu.norm_fg & quote(data.unselected_fg)
cmd = cmd & " -sb \"" & $data.selected_bg & "\"" cmd = cmd & dmenu.sel_bg & quote(data.selected_bg)
cmd = cmd & " -sf \"" & $data.selected_fg & "\"" cmd = cmd & dmenu.sel_fg & quote(data.selected_fg)
cmd = cmd & " -fn " & font cmd = cmd & dmenu.font & quote(font)
#echo "Dmenu :", cmd
#echo cmd
#
# Run command and get output # Run command and get output
let output = execCmdEx(cmd) let output = execCmdEx(cmd)
let option:string = strip(output[0]) let option:string = strip(output[0])

0
fuzzytime.nim Normal file → Executable file
View file

View file

@ -23,7 +23,7 @@ proc readNotes(): seq[Note] =
try: try:
var note = Note() var note = Note()
note.id = note_file[1] note.id = note_file[1]
note.text = readFile(note_file[1]) note.text = stripQuotes(readFile(note_file[1]))
notes.add(note) notes.add(note)
except: except:
echo "Unable to read note : ", note_file, " : ", getCurrentExceptionMsg() echo "Unable to read note : ", note_file, " : ", getCurrentExceptionMsg()
@ -50,13 +50,16 @@ proc writeNote(note: Note, is_new: bool = false) =
if note.text == "": if note.text == "":
return return
try: try:
writeFile(note.id, note.text)
writeFile(note.id, stripQuotes(note.text))
if is_new: if is_new:
var new_note = note
new_note.text = stripQuotes(new_note.text)
notes.add(note) notes.add(note)
else: else:
for idx, a_note in notes: for idx, a_note in notes:
if a_note.id == note.id: if a_note.id == note.id:
notes[idx].text = note.text notes[idx].text = stripQuotes(note.text)
except: except:
echo "write_note, Unable to write note : ", note.id, " : ", getCurrentExceptionMsg() echo "write_note, Unable to write note : ", note.id, " : ", getCurrentExceptionMsg()

0
volume.nim Normal file → Executable file
View file