From ec9c5742b12a10fffb15ad868c5c5b59c581ff7b Mon Sep 17 00:00:00 2001 From: Paul Wilde Date: Sat, 12 Feb 2022 15:19:43 +0000 Subject: [PATCH] added wlan, battery and volume --- i3bar_base.nim | 6 ++- i3bar_battery.nim | 95 +++++++++++++++++++++++++++++++++++++++++++ i3bar_date.nim | 19 +++++---- i3bar_fuzzytime.nim | 9 ++-- i3bar_pingclock.nim | 7 +++- i3bar_temperature.nim | 7 +++- i3bar_volume.nim | 78 +++++++++++++++++++++++++++++++++++ 7 files changed, 206 insertions(+), 15 deletions(-) create mode 100755 i3bar_battery.nim create mode 100644 i3bar_volume.nim diff --git a/i3bar_base.nim b/i3bar_base.nim index 3f04f13..b24d891 100644 --- a/i3bar_base.nim +++ b/i3bar_base.nim @@ -9,10 +9,13 @@ type short_text*: string color*: string border*: string + background*: string type i3BarInput* = object button*: int + x*: int + y*: int const background* = "#000000" const backgroundalt* = "#bb222222" @@ -20,6 +23,7 @@ const backgroundalt2* = "#bb333333" const foreground* = "#dfdfdf" const foregroundalt* = "#777" const foregroundalt2* = "#ccc" +const black* = "#000000" const yellow* = "#ffb52a" const red* = "#e60053" const purple* = "#9f78e1" @@ -49,7 +53,7 @@ proc parseInput*(): i3BarInput = except: return i3BarInput() proc clearInput*(count: int = 1) = - for x in countup(0, count): + for x in countup(1, count): discard readLineFromStdin("") proc getArguments*(): seq[string] = diff --git a/i3bar_battery.nim b/i3bar_battery.nim new file mode 100755 index 0000000..3946bb0 --- /dev/null +++ b/i3bar_battery.nim @@ -0,0 +1,95 @@ +import i3bar_base +import strutils +import std/times +import std/os + +proc battery_exists(): bool = + let state = strip(readFile("/sys/class/power_supply/BAT0/present")) + if state == "1": + return true + return false + +proc is_charging(): bool = + let state = strip(readFile("/sys/class/power_supply/BAT0/status")) + case state: + of "Charging": + return true + else: + return false + +proc get_charge(): int = + var charge = 0 + let chg = strip(readFile("/sys/class/power_supply/BAT0/capacity")) + if chg != "": + charge = parseInt(chg) + return charge + +proc get_design(charge: int, state: bool): (string, string, string, string) = + var icon = " " + var icon_colour = lightgreen + var col = foreground + var bg = black + var border = lightgreen + if is_charging(): + icon = " " + else: + case charge: + of 0..5: + icon_colour = black + col = black + bg = red + of 6..19: + icon_colour = alert + border = alert + of 20..39: + icon_colour = yellow + border = yellow + icon = " " + of 40..59: + icon_colour = green + border= green + icon = " " + of 60..79: + icon_colour = green + border= green + icon = " " + of 80..100: + icon_colour = lightgreen + border = lightgreen + icon = " " + else: + icon = "x " + + let text = "" & icon & "" & $charge & "%" + return (text, col, bg, border) + +proc get_output(charge: int, state: bool): i3barData = + let (text,col,bg_col,bord_col) = get_design(charge, state) + let data = i3barData( + full_text: text, + color: col, + border: bord_col, + background: bg_col + ) + return data + + +proc get_battery_info() = + var last_charge = 0 + var last_state = false + while true: + let charge = get_charge() + let state = is_charging() + if charge != last_charge or state != last_state: + let data = get_output(charge, state) + outputJSON(data) + last_charge = charge + last_state = state + sleep(1000) + +proc main() = + if battery_exists(): + get_battery_info() + +main() + diff --git a/i3bar_date.nim b/i3bar_date.nim index 3893b57..83d3d67 100644 --- a/i3bar_date.nim +++ b/i3bar_date.nim @@ -16,30 +16,35 @@ proc getObject(date: string): i3barData = return data #proc openCalendar(datestr: string) = -proc openCalendar() = +proc openCalendar(input: i3barInput) = + var c = """yad --calendar \ --undecorated --fixed --close-on-unfocus --no-buttons \ --width="222" --height="193" \ - --posx="1650" --posy="25" \ + --posx="%pos_x" --posy="%pos_y" \ --title="yad-calendar" --borders 0 > /dev/null """ - + c = replace(c,re"%pos_x", $(input.x - 111)) + c = replace(c,re"%pos_y", $input.y) discard execCmd(c) proc getDate() = + var last_date = "" while true: let now = now() let d = now.format("yyyy-MM-dd") - let data = getObject(d) - outputJSON(data) - sleep(30000) + if d != last_date: + let data = getObject(d) + outputJSON(data) + last_date = d + sleep(5000) proc await_click_info() = while true: let input = parseInput() if input.button == 1: - openCalendar() + openCalendar(input) proc main() = spawn getDate() diff --git a/i3bar_fuzzytime.nim b/i3bar_fuzzytime.nim index 1bfdac5..6a0ea81 100644 --- a/i3bar_fuzzytime.nim +++ b/i3bar_fuzzytime.nim @@ -77,11 +77,14 @@ proc getObject(time: string): i3barData = proc main() = + var last_time = "" while true: let time = get_fuzzytime() - let data = getObject(time) - outputJSON(data) + if time != last_time: + let data = getObject(time) + outputJSON(data) + last_time = time - sleep(10000) + sleep(2000) main() diff --git a/i3bar_pingclock.nim b/i3bar_pingclock.nim index 7648df6..440c34c 100644 --- a/i3bar_pingclock.nim +++ b/i3bar_pingclock.nim @@ -30,10 +30,13 @@ proc getObject(ping: string): i3barData = proc main() = + var last_ping = "" while true: let ping = get_pingms() - let data = getObject(ping) - outputJSON(data) + if ping != last_ping: + let data = getObject(ping) + outputJSON(data) + last_ping = ping sleep(time_secs * 1000) diff --git a/i3bar_temperature.nim b/i3bar_temperature.nim index e43d734..511b38d 100644 --- a/i3bar_temperature.nim +++ b/i3bar_temperature.nim @@ -50,10 +50,13 @@ proc getObject(temp: int): i3barData = proc main() = let zones = getThermalZones() + var last_temp = 0 while true: let temp = getAverageTemp(zones) - let data = getObject(temp) - outputJSON(data) + if temp != last_temp: + let data = getObject(temp) + outputJSON(data) + last_temp = temp sleep(10000) main() diff --git a/i3bar_volume.nim b/i3bar_volume.nim new file mode 100644 index 0000000..267d01b --- /dev/null +++ b/i3bar_volume.nim @@ -0,0 +1,78 @@ +import std/os +import strutils +import std/osproc +import std/math +import i3bar_base +import std/threadpool + +proc getDesign(vol: string): string = + 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 text = icon & $pcnt & "%" + return text + +proc get_current_volume(): string = + let mute = execCmdEx("pamixer --get-mute") + if strip(mute.output) == "true": + return "muted" + let vol = execCmdEx("pamixer --get-volume") + return vol.output + +proc get_volume(run_once: bool = false) = + var last_vol: string = "" + while true: + let vol = get_current_volume() + if vol != last_vol: + let text = getDesign(vol) + let data = i3barData( + full_text: text, + color: foreground, + border: green + ) + outputJSON(data) + 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 + + +proc main() = + spawn get_volume() + spawn await_click_info() + sync() + +main()