From 89b73b613c5b536bfc27d09c173859f6243ce5b7 Mon Sep 17 00:00:00 2001 From: Paul Wilde Date: Thu, 28 Apr 2022 15:52:45 +0100 Subject: [PATCH] loads of changes - will now give options in dmenu entries to change configs etc, volume, brights.... --- i3bar_base.nim | 42 +++++++++++++++++++---- i3bar_battery.nim | 13 ++++--- i3bar_brightness.nim | 39 ++++++++++----------- i3bar_brightness_threads.nim | 27 +++++++++++++++ i3bar_date.nim | 49 +++++++++++++++----------- i3bar_date_threads.nim | 18 ++++++++++ i3bar_fuzzytime.nim | 10 +++--- i3bar_nic.nim | 23 ++++++------- i3bar_nic_threads.nim | 25 ++++++++++++++ i3bar_pingclock.nim | 10 +++--- i3bar_temperature.nim | 14 +++++--- i3bar_tides.nim | 25 ++++---------- i3bar_tides_threads.nim | 21 ++++++++++++ i3bar_volume.nim | 66 +++++++++++++++++------------------- i3bar_volume_threads.nim | 34 +++++++++++++++++++ i3bar_wlan.nim | 22 +++++++----- i3bar_wlan_threads.nim | 22 ++++++++++++ 17 files changed, 318 insertions(+), 142 deletions(-) create mode 100644 i3bar_brightness_threads.nim create mode 100644 i3bar_date_threads.nim create mode 100644 i3bar_nic_threads.nim create mode 100644 i3bar_tides_threads.nim create mode 100644 i3bar_volume_threads.nim create mode 100755 i3bar_wlan_threads.nim diff --git a/i3bar_base.nim b/i3bar_base.nim index e82451b..245f1d6 100644 --- a/i3bar_base.nim +++ b/i3bar_base.nim @@ -1,4 +1,4 @@ -import std/[os,osproc] +import std/[os,osproc,strutils] import std/json import std/rdstdin import marshal @@ -7,6 +7,7 @@ type i3BarData* = object title*: string full_text*: string + html_text*: string short_text*: string color*: string border*: string @@ -41,7 +42,8 @@ const secondary* = red const alert* = "#bd2c40" var loop* = true var stoploop* = false -var dmenu* = false +var dmenu = false +var rofi = false proc newi3BarData*(): i3BarData = return i3BarData( @@ -74,20 +76,43 @@ proc getArguments*(): seq[string] = let args = commandLineParams() return args -proc outputJSON*(data: i3barData) = - echo $$data - -proc runDMenu*(data: i3BarData) = - var cmd = "echo " & $data.full_text & " | dmenu " +proc runDMenu*(data: i3BarData, opts: varargs[string]): string = + var cmd = "echo -e \"" & $data.full_text & "\n" + for opt in opts: + cmd = cmd & opt & "\n" + cmd = cmd & "\" | dmenu" #cmd = cmd & " -l 2 " cmd = cmd & " -p \"" & $data.title & "\"" cmd = cmd & " -nb \"" & $data.background & "\"" cmd = cmd & " -nf \"" & $data.color & "\"" cmd = cmd & " -sb \"" & $data.border & "\"" cmd = cmd & " -sf \"" & $data.color & "\"" + #echo "Dmenu :", cmd + let output = execCmdEx(cmd) + let option:string = strip(output[0]) + discard execCmd("i3-msg mode \"default\"") + return option + +proc runRofi*(data: i3BarData) = + var cmd = "rofi" + cmd = cmd & " -e \"" & $data.full_text & "\"" + #echo "Rofi : ", cmd discard execCmd(cmd) discard execCmd("i3-msg mode \"default\"") +proc outputJSON*(data: i3barData, args: seq[string]): string {.discardable.} = + var output = "" + if dmenu: + output = runDMenu(data, args) + elif rofi: + runRofi(data) + else: + var j_data = data + if j_data.html_text != "": + j_data.full_text = j_data.html_text + echo $$j_data + return output + let args = getArguments() for arg in args: if arg == "noloop": @@ -95,5 +120,8 @@ for arg in args: if arg == "dmenu": stoploop = true dmenu = true + if arg == "rofi": + stoploop = true + rofi = true diff --git a/i3bar_battery.nim b/i3bar_battery.nim index a0ce348..e693367 100755 --- a/i3bar_battery.nim +++ b/i3bar_battery.nim @@ -24,7 +24,7 @@ proc get_charge(): int = charge = parseInt(chg) return charge -proc get_design(charge: int, state: bool): (string, string, string, string) = +proc get_design(charge: int, state: bool): (string, string, string, string, string) = var icon = " " var icon_colour = lightgreen var col = foreground @@ -60,13 +60,16 @@ proc get_design(charge: int, state: bool): (string, string, string, string) = else: icon = "x " + let main_text = icon & " " & $charge & "%" let text = "" & icon & "" & $charge & "%" - return (text, col, bg, border) + return (text,main_text, col, bg, border) proc get_output(charge: int, state: bool): i3barData = - let (text,col,bg_col,bord_col) = get_design(charge, state) + let (text,main_text,col,bg_col,bord_col) = get_design(charge, state) let data = i3barData( - full_text: text, + title: "Battery : ", + full_text: main_text, + html_text: text, color: col, border: bord_col, background: bg_col @@ -85,6 +88,8 @@ proc get_battery_info() = outputJSON(data) last_charge = charge last_state = state + if stoploop: + break sleep(1000) proc main() = diff --git a/i3bar_brightness.nim b/i3bar_brightness.nim index ba0d8cb..dd6e60b 100644 --- a/i3bar_brightness.nim +++ b/i3bar_brightness.nim @@ -3,7 +3,6 @@ import strutils import std/osproc import std/math import i3bar_base -import std/threadpool proc getLimit(): int @@ -31,7 +30,7 @@ proc getDesign(pcnt: float): string = let text = icon & " " & $percent & "%" return text -proc get_brightness(run_once: bool = false) = +proc get_brightness*(run_once: bool = false) = var last_pcnt: float = 0 while true: let current = parseInt(strip(readFile("/sys/class/backlight/intel_backlight/actual_brightness"))) @@ -39,32 +38,28 @@ proc get_brightness(run_once: bool = false) = if pcnt != last_pcnt: let text = getDesign(pcnt) var data = newi3BarData() + data.title = "Brightness : " data.full_text = text data.border = yellow - outputJSON(data) + let args = @["up", "down"] + let option = outputJSON(data,args) + case option: + of "up": + discard execCmd("xbacklight -inc 5") + get_brightness(true) + of "down": + discard execCmd("xbacklight -dec 5") + get_brightness(true) + if run_once: break + if stoploop: + break last_pcnt = pcnt sleep(1000) -proc await_click_info() = - while true: - let input = parseInput() - case input.button: - of 1,4: - let state = execCmd("xbacklight -inc 5") - get_brightness(true) - of 3,5: - let state = execCmd("xbacklight -dec 5") - get_brightness(true) - else: - let no = false - - clearInput(2) - proc main() = - spawn get_brightness() - spawn await_click_info() - sync() + get_brightness() -main() +if isMainModule: + main() diff --git a/i3bar_brightness_threads.nim b/i3bar_brightness_threads.nim new file mode 100644 index 0000000..1f7679a --- /dev/null +++ b/i3bar_brightness_threads.nim @@ -0,0 +1,27 @@ +import i3bar_base +import i3bar_brightness +import std/threadpool +import std/osproc + +proc await_click_info() = + while true: + let input = parseInput() + case input.button: + of 1,4: + let state = execCmd("xbacklight -inc 5") + get_brightness(true) + of 3,5: + let state = execCmd("xbacklight -dec 5") + get_brightness(true) + else: + let no = false + + clearInput(2) + +proc main() = + spawn get_brightness() + spawn await_click_info() + sync() + +if isMainModule: + main() diff --git a/i3bar_date.nim b/i3bar_date.nim index 83d3d67..5541a5e 100644 --- a/i3bar_date.nim +++ b/i3bar_date.nim @@ -2,56 +2,65 @@ import std/os import std/times import std/osproc import std/re -import std/threadpool import i3bar_base let args = getArguments() proc getObject(date: string): i3barData = - let data = i3barData( - full_text: date, - color: foreground, - border: blue - ) + var data = newi3barData() + data.title = "Date : " + data.full_text = date + data.border = blue return data #proc openCalendar(datestr: string) = -proc openCalendar(input: i3barInput) = - +proc newCalendar(): string = var c = """yad --calendar \ --undecorated --fixed --close-on-unfocus --no-buttons \ --width="222" --height="193" \ --posx="%pos_x" --posy="%pos_y" \ --title="yad-calendar" --borders 0 > /dev/null """ + return c +proc openCalendar*(input: i3barInput) = + var c = newCalendar() + c = replace(c,re"%pos_x", $(input.x - 111)) c = replace(c,re"%pos_y", $input.y) discard execCmd(c) -proc getDate() = +proc dmenuCalendar() = + var c = newCalendar() + + c = replace(c,re"%pos_x", "100") + c = replace(c,re"%pos_y", "0") + + discard execCmd(c) + + +proc getDate*() = var last_date = "" while true: let now = now() let d = now.format("yyyy-MM-dd") if d != last_date: let data = getObject(d) - outputJSON(data) + let args = @["open calendar"] + case outputJSON(data, args): + of "open calendar": + dmenuCalendar() last_date = d + if stoploop: + break sleep(5000) -proc await_click_info() = - while true: - let input = parseInput() - if input.button == 1: - openCalendar(input) - proc main() = - spawn getDate() - spawn await_click_info() - sync() + getDate() + -main() +if isMainModule: + main() #DATE="$(date +"%a %d %H:%M")" #if [ "$(xdotool getwindowfocus getwindowname)" = "yad-calendar" ]; then diff --git a/i3bar_date_threads.nim b/i3bar_date_threads.nim new file mode 100644 index 0000000..aacd71d --- /dev/null +++ b/i3bar_date_threads.nim @@ -0,0 +1,18 @@ +import std/threadpool +import i3bar_date +import i3bar_base + +proc await_click_info() = + while true: + let input = parseInput() + if input.button == 1: + openCalendar(input) + +proc main() = + spawn getDate() + spawn await_click_info() + sync() + + +if isMainModule: + main() diff --git a/i3bar_fuzzytime.nim b/i3bar_fuzzytime.nim index b910650..8ca2b6d 100644 --- a/i3bar_fuzzytime.nim +++ b/i3bar_fuzzytime.nim @@ -74,7 +74,7 @@ proc getObject(time: string): i3barData = var data = newi3barData() data.title = "Fuzzy Time :" data.full_text = time - #data.color = foreground + data.color = foreground data.border = lightblue return data @@ -85,12 +85,12 @@ proc main() = let time = get_fuzzytime() if time != last_time: let data = getObject(time) - if dmenu: - runDMenu(data) - break outputJSON(data) + if stoploop: + break last_time = time sleep(2000) -main() +if isMainModule: + main() diff --git a/i3bar_nic.nim b/i3bar_nic.nim index f5c33f6..36fdca1 100644 --- a/i3bar_nic.nim +++ b/i3bar_nic.nim @@ -1,7 +1,6 @@ import i3bar_base import std/os import std/osproc -import std/threadpool import strutils const nics: seq[string] = @["wlan0", "enp3s0","wlp2s0","enp0s20f0u3"] @@ -25,29 +24,31 @@ proc get_net(nic: string): (string, string) = proc getObject(conn: string, nic: string): i3barData = var data = newi3barData() + data.title = "IP :" data.full_text = conn data.border = purple return data -proc get_net_info(nic: string) = +proc get_net_info*(nic: string) = var last_ip = "" var last_state = "" while true: let (ip, state) = get_net(nic) if ip != last_ip or state != last_state: let data = getObject(state & " " & ip, nic) - outputJSON(data) + let args = @["nmtui-connect"] + let option = outputJSON(data, args) + case option: + of "nmtui-connect": + discard execCmd("alacritty -e nmtui-connect") last_ip = ip last_state = state + if stoploop: + break sleep(1000) -proc await_click_info() = - while true: - let input = parseInput() - if input.button == 1: - discard execCmd("alacritty -e nmtui-connect") -proc get_nic(): string = +proc get_nic*(): string = for nic in nics: if dirExists("/sys/class/net/" & nic): return nic @@ -56,9 +57,7 @@ proc get_nic(): string = proc main() = let mynic = get_nic() if dirExists("/sys/class/net/" & mynic): - spawn get_net_info(mynic) - spawn await_click_info() - sync() + get_net_info(mynic) else: echo "No NIC" diff --git a/i3bar_nic_threads.nim b/i3bar_nic_threads.nim new file mode 100644 index 0000000..8cbb2b8 --- /dev/null +++ b/i3bar_nic_threads.nim @@ -0,0 +1,25 @@ +import i3bar_base +import i3bar_nic +import std/os +import std/osproc +import std/threadpool +import strutils + +proc await_click_info() = + while true: + let input = parseInput() + if input.button == 1: + discard execCmd("alacritty -e nmtui-connect") + + +proc main() = + let mynic = get_nic() + if dirExists("/sys/class/net/" & mynic): + spawn get_net_info(mynic) + spawn await_click_info() + sync() + else: + echo "No NIC" + +if isMainModule: + main() diff --git a/i3bar_pingclock.nim b/i3bar_pingclock.nim index 6fa8bbf..88c8942 100644 --- a/i3bar_pingclock.nim +++ b/i3bar_pingclock.nim @@ -68,14 +68,12 @@ proc main() = let ping = get_ping() if ping != last_ping: let data = getObject(ping) - if dmenu: - runDMenu(data) - else: - outputJSON(data) - last_ping = ping + outputJSON(data) + last_ping = ping loop = not stoploop if loop: sleep(time_secs * 1000) -main() +if isMainModule: + main() diff --git a/i3bar_temperature.nim b/i3bar_temperature.nim index 511b38d..cfc0f4c 100644 --- a/i3bar_temperature.nim +++ b/i3bar_temperature.nim @@ -41,11 +41,13 @@ proc getObject(temp: int): i3barData = colour = green icon = "" let text = "" & icon & " " & $temp & "°C" - let data = i3barData( - full_text: text, - color: foreground, - border: colour - ) + let main_text = icon & " " & $temp & "°C" + var data = newi3barData() + data.title = "Temp : " + data.full_text = main_text + data.html_text = text + data.color = foreground + data.border = colour return data proc main() = @@ -57,6 +59,8 @@ proc main() = let data = getObject(temp) outputJSON(data) last_temp = temp + if stoploop: + break sleep(10000) main() diff --git a/i3bar_tides.nim b/i3bar_tides.nim index ae625db..99412b5 100644 --- a/i3bar_tides.nim +++ b/i3bar_tides.nim @@ -3,13 +3,11 @@ import i3bar_base import std/re import std/httpclient import std/os -import std/json import std/times -import std/threadpool import std/osproc -const url = "https://www.tidetimes.org.uk/%LOC-tide-times" -const loc = "exmouth-dock" +const url* = "https://www.tidetimes.org.uk/%LOC-tide-times" +const loc* = "exmouth-dock" const icon: string = "🌊" type @@ -86,9 +84,7 @@ proc getDesign(tides: seq[Tide]): i3barData = return data - - -proc getTides() {.gcsafe.}= +proc getTides*() {.gcsafe.}= var mytides = TideList() var last_data = "" while true: @@ -99,20 +95,11 @@ proc getTides() {.gcsafe.}= if $data != last_data: outputJSON(data) last_data = $data + if stoploop: + break sleep(10000) -proc await_click_info() = - while true: - let input = parseInput() - case input.button: - of 1: - let state = execCmd("xdg-open " & replace(url,re"\%LOC",loc)) - else: - let no = false - proc main() = - spawn getTides() - spawn await_click_info() - sync() + getTides() main() diff --git a/i3bar_tides_threads.nim b/i3bar_tides_threads.nim new file mode 100644 index 0000000..4839cfc --- /dev/null +++ b/i3bar_tides_threads.nim @@ -0,0 +1,21 @@ +import i3bar_tides +import i3bar_base +import std/re +import std/threadpool +import std/osproc + +proc await_click_info() = + while true: + let input = parseInput() + case input.button: + of 1: + let state = execCmd("xdg-open " & replace(url,re"\%LOC",loc)) + else: + let no = false + +proc main() = + spawn getTides() + spawn await_click_info() + sync() + +main() diff --git a/i3bar_volume.nim b/i3bar_volume.nim index b1e3a3d..dfc9254 100644 --- a/i3bar_volume.nim +++ b/i3bar_volume.nim @@ -3,7 +3,6 @@ import strutils import std/osproc import std/math import i3bar_base -import std/threadpool proc get_current_volume(): string {.gcsafe.} @@ -15,11 +14,11 @@ proc check_volume(volume: string): string = vol = check_volume(vol) return vol -proc getDesign(volume: string): string = +proc getDesign(volume: string): (string,string) = let vol = check_volume(volume) var icon = " " if vol == "muted": - return icon & "muted" + return (icon & "muted", "") let pcnt = parseInt(strip(vol)) case pcnt: of 85..100: @@ -32,8 +31,9 @@ proc getDesign(volume: string): string = icon = " " else: icon = " " + let main_text = icon & $pcnt & "%" let text = "" & icon & "" & $pcnt & "%" - return text + return (text, main_text) proc get_current_volume(): string = let mute = execCmdEx("pamixer --get-mute") @@ -42,49 +42,47 @@ proc get_current_volume(): string = let vol = execCmdEx("pamixer --get-volume") return vol.output -proc get_volume(run_once: bool = false) = +proc get_volume*(run_once: bool = false) = var last_vol: string = "" while true: let vol = get_current_volume() if vol != last_vol or true: - let text = getDesign(vol) + let (text, main_text) = getDesign(vol) let data = i3barData( - full_text: text, + title: "Volume : ", + html_text: text, + full_text: main_text, color: foreground, border: green, background: black ) - outputJSON(data) + let args = @["up", "down", "mute", "ncpamixer", "pavucontrol"] + let option = outputJSON(data,args) + case option: + of "up": + discard execCmd("pamixer -i 5") + get_volume() + of "down": + discard execCmd("pamixer -d 5") + get_volume() + of "mute": + discard execCmd("pamixer -t") + get_volume() + of "ncpamixer": + discard execCmd("ncpamixer") + of "pavucontrol": + discard execCmd("pavucontrol") + + if run_once: break last_vol = vol sleep(1000) - -proc await_click_info() = - while true: - let input = parseInput() - case input.button: - of 4: - discard execCmd("pamixer -i 5") - get_volume(true) - clearInput(2) - of 5: - discard execCmd("pamixer -d 5") - get_volume(true) - clearInput(2) - of 1: - discard execCmd("pamixer -t") - get_volume(true) - of 3: - discard execCmd("alacritty -e ncpamixer") - get_volume(true) - else: - let no = false - + if stoploop: + break proc main() = - spawn get_volume() - spawn await_click_info() - sync() + get_volume() -main() +if isMainModule: + main() diff --git a/i3bar_volume_threads.nim b/i3bar_volume_threads.nim new file mode 100644 index 0000000..9863a19 --- /dev/null +++ b/i3bar_volume_threads.nim @@ -0,0 +1,34 @@ +import i3bar_base +import i3bar_volume +import std/threadpool +import std/osproc + +proc await_click_info() = + while true: + let input = parseInput() + case input.button: + of 4: + discard execCmd("pamixer -i 5") + get_volume(true) + clearInput(2) + of 5: + discard execCmd("pamixer -d 5") + get_volume(true) + clearInput(2) + of 1: + discard execCmd("pamixer -t") + get_volume(true) + of 3: + discard execCmd("alacritty -e ncpamixer") + get_volume(true) + else: + let no = false + + +proc main() = + spawn get_volume() + spawn await_click_info() + sync() + +if isMainModule: + main() diff --git a/i3bar_wlan.nim b/i3bar_wlan.nim index d799b7c..5bf2c7c 100755 --- a/i3bar_wlan.nim +++ b/i3bar_wlan.nim @@ -1,10 +1,9 @@ import i3bar_base import std/os import std/osproc -import std/threadpool import strutils -const wlan_nic: string ="wlan0" +const wlan_nic*: string ="wlan0" # /sys/class/net/wlp2s0/operstate up or down if connected @@ -29,18 +28,26 @@ proc get_wifi(): (string, string) = proc getObject(conn: string): i3barData = var data = newi3barData() + data.title = "WiFi : " data.full_text = conn data.border = purple return data -proc get_wifi_info() = +proc get_wifi_info*() = var last_qual = "" while true: let (essid, quality) = get_wifi() if quality != last_qual: let data = getObject(quality & " " & essid) - outputJSON(data) + let args = @["nmtui-connect"] + let output = outputJSON(data, args) + echo output + case output: + of "nmtui-connect": + discard execCmd("alacritty -e nmtui-connect") last_qual = quality + if stoploop: + break sleep(1000) proc await_click_info() = @@ -51,10 +58,9 @@ proc await_click_info() = proc main() = if dirExists("/sys/class/net/" & wlan_nic): - spawn get_wifi_info() - spawn await_click_info() - sync() + get_wifi_info() else: echo "No WLAN" -main() +if isMainModule: + main() diff --git a/i3bar_wlan_threads.nim b/i3bar_wlan_threads.nim new file mode 100755 index 0000000..6034b80 --- /dev/null +++ b/i3bar_wlan_threads.nim @@ -0,0 +1,22 @@ +import i3bar_base +import i3bar_wlan +import std/[os,osproc] +import std/threadpool + + +proc await_click_info() = + while true: + let input = parseInput() + if input.button == 1: + discard execCmd("alacritty -e nmtui-connect") + +proc main() = + if dirExists("/sys/class/net/" & wlan_nic): + spawn get_wifi_info() + spawn await_click_info() + sync() + else: + echo "No WLAN" + +if isMainModule: + main()