From deb43144aac5237b46a79ff4bb9984d1a8e0e687 Mon Sep 17 00:00:00 2001 From: Paul Wilde Date: Wed, 22 Nov 2023 22:02:52 +0000 Subject: [PATCH] volurrme now working --- src/common.nim | 2 + src/dispatcher.nim | 3 + src/model/config.nim | 5 +- src/model/extraargs.nim | 4 ++ src/parser.nim | 10 ++++ src/util/batturry.nim | 1 - src/util/volurrme.nim | 118 ++++++++++++++++++++++++++++++++++++++++ 7 files changed, 141 insertions(+), 2 deletions(-) create mode 100644 src/model/extraargs.nim create mode 100644 src/util/volurrme.nim diff --git a/src/common.nim b/src/common.nim index 0924fec..488e05c 100644 --- a/src/common.nim +++ b/src/common.nim @@ -1,8 +1,10 @@ import model/config import model/info +import model/extraargs export config export info +export extraargs var myConfig* = newConfig() diff --git a/src/dispatcher.nim b/src/dispatcher.nim index c2e516d..7cb73d8 100644 --- a/src/dispatcher.nim +++ b/src/dispatcher.nim @@ -3,6 +3,7 @@ import common import util/furrytime import util/pingclock import util/batturry +import util/volurrme proc dispatch*(cfg: Config) = case cfg.run @@ -12,5 +13,7 @@ proc dispatch*(cfg: Config) = pingclock.go() of Batturry: batturry.go() + of Volurrme: + volurrme.go() else: echo "No valid run command given" diff --git a/src/model/config.nim b/src/model/config.nim index 00bbc91..136de32 100644 --- a/src/model/config.nim +++ b/src/model/config.nim @@ -1,5 +1,6 @@ import os import parsetoml +import extraargs type Config* = ref object @@ -7,11 +8,13 @@ type run*: Tool max_lines*: int prepend*: bool + extra_args*: seq[ExtraArg] Tool* = enum None, FurryTime, PingClock, - Batturry + Batturry, + Volurrme let config_dir* = getHomeDir() & ".config/wm_tools/" let config_file* = config_dir & "config.toml" diff --git a/src/model/extraargs.nim b/src/model/extraargs.nim new file mode 100644 index 0000000..f70daf6 --- /dev/null +++ b/src/model/extraargs.nim @@ -0,0 +1,4 @@ + +type + ExtraArg* = enum + None, VolUp, VolDown, VolMute diff --git a/src/parser.nim b/src/parser.nim index 2a70e43..7a92ace 100644 --- a/src/parser.nim +++ b/src/parser.nim @@ -9,6 +9,7 @@ proc parseArgs*() = var p = newParser: help("WMTools : a set of tools to output option to your program of choice i.e. Rofi") arg("input",help="the tool to run, i.e. furrytime, pingclock, volurrme, etc.") + arg("adjust", help="(up, down, mute) allows to send extra commands. i.e. when run with volurrme, allows to adjust the volume") try: var opts = p.parse(params) case opts.input @@ -18,9 +19,18 @@ proc parseArgs*() = myConfig.run = PingClock of "batturry", "battery", "bat": myConfig.run = Batturry + of "volurrme", "volume", "vol": + myConfig.run = Volurrme else: echo p.help quit(1) + case opts.adjust + of "volup": + myConfig.extra_args.add(VolUp) + of "voldown": + myConfig.extra_args.add(VolDown) + of "mute","volmute": + myConfig.extra_args.add(VolMute) except ShortCircuit as err: if err.flag == "argparse_help": echo err.help diff --git a/src/util/batturry.nim b/src/util/batturry.nim index 3962f76..94d94f5 100644 --- a/src/util/batturry.nim +++ b/src/util/batturry.nim @@ -1,5 +1,4 @@ import strutils -import os import ../common import ../common/colours diff --git a/src/util/volurrme.nim b/src/util/volurrme.nim new file mode 100644 index 0000000..afa2671 --- /dev/null +++ b/src/util/volurrme.nim @@ -0,0 +1,118 @@ +import os +import strutils +import sequtils +import osproc + +import ../common +import ../output + +const audio_tools = @["ncpamixer", "pavucontrol"] +const vol_cmd = "pamixer" +const vol_up = vol_cmd & " -i %v" # where %v is amount by +const vol_down = vol_cmd & " -d %v" # where %v is amount by +const vol_set = vol_cmd & " --set-volume %v" # where %v is amount by +const vol_mute = vol_cmd & " -t" +const vol_default_by = "5" +const vol_get = vol_cmd & " --get-volume" +const vol_get_mute = vol_cmd & " --get-mute" + + +proc getCurrentVolume(): string = + let mute = execCmdEx(vol_get_mute) + if strip(mute.output) == "true": + return "muted" + let vol = execCmdEx(vol_get) + return vol.output + +proc checkVolume(volume: string): string = + var vol = volume + if strip(vol) == "Connection error": + sleep(1000) + vol = getCurrentVolume() + vol = checkVolume(vol) + return vol + +proc getDesign(volume: string): string = + let vol = checkVolume(volume) + var icon = " " + if vol == "muted": + return icon & "muted" + let pcnt = parseInt(strip(vol)) + case pcnt: + of 85..100: + icon = " " + of 55..84: + icon = " " + of 35..54: + icon = " " + of 10..34: + icon = " " + else: + icon = " " + let main_text = icon & $pcnt & "%" + return main_text + +proc volumeUp() = + let cmd = replace(vol_up, "%v", vol_default_by) + discard execCmd(cmd) + +proc volumeDown() = + let cmd = replace(vol_down, "%v", vol_default_by) + discard execCmd(cmd) + +proc volumeMute() = + discard execCmd(vol_mute) + +proc getVolume*() = + let vol = getCurrentVolume() + let main_text = getDesign(vol) + var data = newInfo("Volurrme") + data.full_text = main_text + let args = concat(@["up", "down", "mute", "---", "exit", "---"],audio_tools) + let option = outputData(data,args) + if option == "": + return + elif option in args: + if option in audio_tools: + discard(execCmd(option)) + return + case option: + of "up": + volumeUp() + getVolume() + of "down": + volumeDown() + getVolume() + of "mute": + volumeMute() + getVolume() + of "---": + getVolume() + of "exit": + return + else: + try: + let vol = parseInt(option) + let cmd = replace(vol_set, "%v", $vol) + discard execCmd(cmd) + getVolume() + except: + echo getCurrentExceptionMsg() + getVolume() + +proc go*() = + block start: + for arg in myConfig.extra_args: + case arg + of VolUp: + volumeUp() + break start + of VolDown: + volumeDown() + break start + of VolMute: + volumeMute() + break start + else: + continue + getVolume()