From 8bf1a1e381ac5824dc3cd05153b7f4624c85a2f4 Mon Sep 17 00:00:00 2001 From: Paul Wilde Date: Sat, 12 Feb 2022 13:20:21 +0000 Subject: [PATCH] added pingclock and clear input --- i3bar_base.nim | 4 ++- i3bar_brightness.nim | 52 ++++++++++++++++++++++++++------------ i3bar_date.nim | 29 +++++++++++++-------- i3bar_pingclock.nim | 40 +++++++++++++++++++++++++++++ i3bar_wlan.nim | 60 ++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 157 insertions(+), 28 deletions(-) create mode 100644 i3bar_pingclock.nim create mode 100755 i3bar_wlan.nim diff --git a/i3bar_base.nim b/i3bar_base.nim index 484620d..3f04f13 100644 --- a/i3bar_base.nim +++ b/i3bar_base.nim @@ -2,7 +2,6 @@ import std/os import std/json import std/rdstdin import marshal -import argparse type i3BarData* = object @@ -49,6 +48,9 @@ proc parseInput*(): i3BarInput = return i3input except: return i3BarInput() +proc clearInput*(count: int = 1) = + for x in countup(0, count): + discard readLineFromStdin("") proc getArguments*(): seq[string] = let args = commandLineParams() diff --git a/i3bar_brightness.nim b/i3bar_brightness.nim index e61f6ab..936d3b7 100644 --- a/i3bar_brightness.nim +++ b/i3bar_brightness.nim @@ -3,6 +3,7 @@ import strutils import std/osproc import std/math import i3bar_base +import std/threadpool proc getLimit(): int @@ -30,23 +31,42 @@ proc getDesign(pcnt: float): string = let text = icon & " " & $percent & "%" return text -proc getBrightness() = - let current = parseInt(strip(readFile("/sys/class/backlight/intel_backlight/actual_brightness"))) - let pcnt = (current/limit)*100 - let text = getDesign(pcnt) - let data = i3barData( - full_text: text, - color: foreground, - border: yellow - ) - outputJSON(data) +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"))) + let pcnt = (current/limit)*100 + if pcnt != last_pcnt: + let text = getDesign(pcnt) + let data = i3barData( + full_text: text, + color: foreground, + border: yellow + ) + outputJSON(data) + if run_once: + 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() = - if len(args) > 0: - if args[0] == "4": - discard execCmd("xbacklight -inc 5") - elif args[0] == "5": - discard execCmd("xbacklight -dec 5") - getBrightness() + spawn get_brightness() + spawn await_click_info() + sync() main() diff --git a/i3bar_date.nim b/i3bar_date.nim index 5a008d8..3893b57 100644 --- a/i3bar_date.nim +++ b/i3bar_date.nim @@ -2,6 +2,7 @@ import std/os import std/times import std/osproc import std/re +import std/threadpool import i3bar_base let args = getArguments() @@ -14,7 +15,8 @@ proc getObject(date: string): i3barData = ) return data -proc openCalendar(datestr: string) = +#proc openCalendar(datestr: string) = +proc openCalendar() = var c = """yad --calendar \ --undecorated --fixed --close-on-unfocus --no-buttons \ --width="222" --height="193" \ @@ -26,18 +28,23 @@ proc openCalendar(datestr: string) = discard execCmd(c) proc getDate() = - let now = now() - let d = now.format("yyyy-MM-dd") - let data = getObject(d) - outputJSON(data) - if len(args) > 0 and args[0] == "1": - openCalendar(now.format("ddd dd HH:MM")) + while true: + let now = now() + let d = now.format("yyyy-MM-dd") + let data = getObject(d) + outputJSON(data) + sleep(30000) + +proc await_click_info() = + while true: + let input = parseInput() + if input.button == 1: + openCalendar() proc main() = - getDate() - #while true: - # let input = parseInput() - # if input.button == 1: + spawn getDate() + spawn await_click_info() + sync() main() diff --git a/i3bar_pingclock.nim b/i3bar_pingclock.nim new file mode 100644 index 0000000..7648df6 --- /dev/null +++ b/i3bar_pingclock.nim @@ -0,0 +1,40 @@ +import std/os +import std/osproc +import std/re +import strutils +import i3bar_base + +const host: string = "8.8.8.8" +const cmd: string = "ping -c 1 " & host +const time_secs: int = 4 + +let ping_re = re(r"time=[0-9.]+\sms") + +proc get_pingms(): string = + let cmdOut = execCmdEx(cmd) + let lines = splitLines(cmdOut.output) + let ping_line = lines[1] + let bounds = findBounds(ping_line, ping_re) + if bounds.first > 0: + let ping = ping_line[bounds.first+5..bounds.last] + return "🏓 " & ping + return "❌ No Pong" + +proc getObject(ping: string): i3barData = + let data = i3barData( + full_text: ping, + color: foreground, + border: lightblue, + ) + return data + + +proc main() = + while true: + let ping = get_pingms() + let data = getObject(ping) + outputJSON(data) + + sleep(time_secs * 1000) + +main() diff --git a/i3bar_wlan.nim b/i3bar_wlan.nim new file mode 100755 index 0000000..3411484 --- /dev/null +++ b/i3bar_wlan.nim @@ -0,0 +1,60 @@ +import i3bar_base +import std/os +import std/osproc +import std/threadpool +import strutils + +const wlan_nic: string ="wlp2s0" + +# /sys/class/net/wlp2s0/operstate up or down if connected + +proc get_essid(): string = + let essid = execCmdEx("iwgetid -r") + return strip(essid.output) + +proc get_signal_quality(): string = + let wl = readFile("/proc/net/wireless") + let ln = splitLines(wl)[2] + let links = split(ln," ") + var qual = strip(links[1]) + qual = replace(qual,".","") + return "[" & qual & "]" + +proc get_wifi(): (string, string) = + let essid = get_essid() + if essid == "": + return ("disconnected", "") + let quality = get_signal_quality() + return (essid, quality) + +proc getObject(conn: string): i3barData = + let data = i3barData( + full_text: conn, + color: foreground, + border: purple, + ) + return data + +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) + last_qual = quality + sleep(1000) + +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() + +main()