From 42a9b61a5ba5ffcf16d97859b8bc81537040f5ea Mon Sep 17 00:00:00 2001 From: Paul Wilde Date: Fri, 6 May 2022 11:12:13 +0100 Subject: [PATCH] added better dmenu cmd generation --- base.nim | 94 +++++++++++++++++++++++++++++++++++++++++---------- fuzzytime.nim | 0 notes.nim | 9 +++-- volume.nim | 0 4 files changed, 82 insertions(+), 21 deletions(-) mode change 100644 => 100755 fuzzytime.nim mode change 100644 => 100755 volume.nim diff --git a/base.nim b/base.nim index f78c4eb..217a162 100644 --- a/base.nim +++ b/base.nim @@ -14,8 +14,19 @@ type color*: string html_text*: string short_text*: string - -type + Dmenu = object + 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 button*: int x*: int @@ -43,6 +54,7 @@ const darkgrey* = "#444" const primary* = yellow const secondary* = red const alert* = "#bd2c40" +const MAX_LINES = 20 var loop* = false var stoploop* = true var dmenu* = true @@ -60,6 +72,33 @@ proc newInfo*(title: string = "Info"): Info = background: black, 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) = let f = open("/tmp/debug.txt",fmAppend) defer: f.close() @@ -86,29 +125,48 @@ proc clearInput*(count: int = 1) = proc getArguments*(): seq[string] = let args = commandLineParams() 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 = # 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 $data.full_text != "": - cmd &= $data.full_text & "\n" + if data.full_text != "": + cmd &= data.full_text & "\n" for opt in opts: cmd = cmd & opt & "\n" cmd.removeSuffix("\n") - if not rofi: - cmd = cmd & "\" | dmenu" - else: - # run rofi in dmenu mode - cmd = cmd & "\" | rofi -dmenu" - cmd = cmd & " -l " & $(len(opts) + 1) - cmd = cmd & " -p \"" & $data.title & "\"" - cmd = cmd & " -nb \"" & $data.unselected_bg & "\"" - cmd = cmd & " -nf \"" & $data.unselected_fg & "\"" - cmd = cmd & " -sb \"" & $data.selected_bg & "\"" - cmd = cmd & " -sf \"" & $data.selected_fg & "\"" - cmd = cmd & " -fn " & font - #echo "Dmenu :", cmd + + cmd = "echo -e" & quote(cmd) & " | " + + var dmenu = newDmenu() + cmd = cmd & dmenu.command & " " + cmd = cmd & dmenu.lines_shown & " " & $20 & " " + cmd = cmd & dmenu.prompt & quote(data.title) + cmd = cmd & dmenu.norm_bg & quote(data.unselected_bg) + cmd = cmd & dmenu.norm_fg & quote(data.unselected_fg) + cmd = cmd & dmenu.sel_bg & quote(data.selected_bg) + cmd = cmd & dmenu.sel_fg & quote(data.selected_fg) + cmd = cmd & dmenu.font & quote(font) + + #echo cmd + # # Run command and get output let output = execCmdEx(cmd) let option:string = strip(output[0]) diff --git a/fuzzytime.nim b/fuzzytime.nim old mode 100644 new mode 100755 diff --git a/notes.nim b/notes.nim index 98a1d6f..c55a9bd 100644 --- a/notes.nim +++ b/notes.nim @@ -23,7 +23,7 @@ proc readNotes(): seq[Note] = try: var note = Note() note.id = note_file[1] - note.text = readFile(note_file[1]) + note.text = stripQuotes(readFile(note_file[1])) notes.add(note) except: echo "Unable to read note : ", note_file, " : ", getCurrentExceptionMsg() @@ -50,13 +50,16 @@ proc writeNote(note: Note, is_new: bool = false) = if note.text == "": return try: - writeFile(note.id, note.text) + + writeFile(note.id, stripQuotes(note.text)) if is_new: + var new_note = note + new_note.text = stripQuotes(new_note.text) notes.add(note) else: for idx, a_note in notes: if a_note.id == note.id: - notes[idx].text = note.text + notes[idx].text = stripQuotes(note.text) except: echo "write_note, Unable to write note : ", note.id, " : ", getCurrentExceptionMsg() diff --git a/volume.nim b/volume.nim old mode 100644 new mode 100755