From 9639b17cbe5c5a54721165952dfdc9882b47fd56 Mon Sep 17 00:00:00 2001 From: Paul Wilde Date: Fri, 6 May 2022 12:07:22 +0100 Subject: [PATCH] added command_wrapper for various other dmenu styled tools --- README.MD | 22 ++++++++++++---------- base.nim | 32 +++++++++++++++++++++----------- command_wrapper.nim | 22 ++++++++++++++++++++++ passmenu_wrapper.nim | 13 +++++++++++++ 4 files changed, 68 insertions(+), 21 deletions(-) create mode 100644 command_wrapper.nim create mode 100644 passmenu_wrapper.nim diff --git a/README.MD b/README.MD index 787d288..3334702 100644 --- a/README.MD +++ b/README.MD @@ -5,16 +5,18 @@ about system status in dmenu. Some of them i.e. `volume` have options (up, down, which are selectable options in dmenu. ## Tools -- `pingclock` performs a single `ping` to a server and returns the response time -- `battery` shows the current battery level -- `brightness` shows the current backlight level and gives options to adjust it -- `volume` shows the current volume level and gives options to adjust and manage it -- `date` shows the date -- `fuzzytime` shows the fuzzytime clock -- `wlan` shows the state of the wireless network interface. SSID connected to and signal level. -- `nic` shows the status and/or the ip address of the network interface card -- `temperature` shows the current CPU temperature -- `notes` a simple one liner note taking tool, displaying notes in dmenu/rofi +- `pingclock` performs a single `ping` to a server and returns the response time +- `battery` shows the current battery level +- `brightness` shows the current backlight level and gives options to adjust it +- `volume` shows the current volume level and gives options to adjust and manage it +- `date` shows the date +- `fuzzytime` shows the fuzzytime clock +- `wlan` shows the state of the wireless network interface. SSID connected to and signal level. +- `nic` shows the status and/or the ip address of the network interface card +- `temperature` shows the current CPU temperature +- `notes` a simple one liner note taking tool, displaying notes in dmenu/rofi +- `passmenu_wrapper` a wrapper for passmenu. It basically just styles passmenu with no other features +- `command_wrapper` inspired by passmenu_wrapper, a basic tool to run other dmenu related tools with uniform styling ### Example in dmenu: diff --git a/base.nim b/base.nim index 217a162..686d368 100644 --- a/base.nim +++ b/base.nim @@ -59,6 +59,9 @@ var loop* = false var stoploop* = true var dmenu* = true var rofi* = false +var passmenu* = false +var command_wrapper* = false +var run_command* = "" proc newInfo*(title: string = "Info"): Info = return Info( @@ -73,9 +76,9 @@ proc newInfo*(title: string = "Info"): Info = color: foreground, ) -proc newDmenuConfig(): Dmenu = +proc newDmenuConfig(cmd: string = "dmenu"): Dmenu = var dmenu = Dmenu() - dmenu.command = "dmenu" + dmenu.command = cmd dmenu.bottom = "-b" dmenu.grabkb = "-f" dmenu.i_case = "-i" @@ -97,6 +100,10 @@ proc newRofiConfig(): Dmenu = proc newDmenu(): Dmenu = if rofi: return newRofiConfig() + elif passmenu: + return newDmenuConfig("passmenu") + elif command_wrapper: + return newDmenuConfig(run_command) return newDmenuConfig() proc debugLog*(str: string) = @@ -137,13 +144,7 @@ proc quote*(str: string): string = 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 genDmenuCmd*(data: Info, opts: varargs[string], rofi: bool = false): string = # Build dmenu/rofi command var cmd = "" # if the text is empty, we don't want to create a menu item of it @@ -157,14 +158,19 @@ proc runDMenu*(data: Info, opts: varargs[string], rofi: bool = false): string = var dmenu = newDmenu() cmd = cmd & dmenu.command & " " - cmd = cmd & dmenu.lines_shown & " " & $20 & " " + cmd = cmd & dmenu.lines_shown & " " & $MAX_LINES & " " 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) + return cmd + + +proc runDMenu*(data: Info, opts: varargs[string], rofi: bool = false): string = + let cmd = genDmenuCmd(data, opts, rofi) #echo cmd # # Run command and get output @@ -194,7 +200,8 @@ proc outputData*(data: Info, args: varargs[string]): string {.discardable.} = # Switch bindsym mode back to default as it could be being used. switchTwmMode() -for arg in getArguments(): +let args* = getArguments() +for idx, arg in args: case arg: of "noloop": stoploop = true @@ -209,5 +216,8 @@ for arg in getArguments(): of "rofi": stoploop = true rofi = true + of ["pass","passmenu"]: + passmenu = true + break diff --git a/command_wrapper.nim b/command_wrapper.nim new file mode 100644 index 0000000..09da560 --- /dev/null +++ b/command_wrapper.nim @@ -0,0 +1,22 @@ +import base +import std/[strutils,osproc] + +# Basically just a wrapper to style passmenu nicely +proc main() = + var info = newInfo(capitalizeAscii(run_command)) + let cmd = genDmenuCmd(info) + discard execCmd(cmd) + return + +if isMainModule: + base.command_wrapper = true + for idx, arg in args: + case arg: + of "-r", "--run": + run_command = args[idx + 1] + else: + echo "No run tag given. Please run again with '--run _cmd_'" + break + + if run_command != "": + main() diff --git a/passmenu_wrapper.nim b/passmenu_wrapper.nim new file mode 100644 index 0000000..e6e543b --- /dev/null +++ b/passmenu_wrapper.nim @@ -0,0 +1,13 @@ +import base +import std/osproc + +# Basically just a wrapper to style passmenu nicely +proc main() = + var info = newInfo("Passmenu") + let cmd = genDmenuCmd(info) + discard execCmd(cmd) + return + +if isMainModule: + passmenu = true + main()