added pingclock and clear input

This commit is contained in:
Paul Wilde 2022-02-12 13:20:21 +00:00
parent 117ba68d89
commit 8bf1a1e381
5 changed files with 157 additions and 28 deletions

View file

@ -2,7 +2,6 @@ import std/os
import std/json import std/json
import std/rdstdin import std/rdstdin
import marshal import marshal
import argparse
type type
i3BarData* = object i3BarData* = object
@ -49,6 +48,9 @@ proc parseInput*(): i3BarInput =
return i3input return i3input
except: except:
return i3BarInput() return i3BarInput()
proc clearInput*(count: int = 1) =
for x in countup(0, count):
discard readLineFromStdin("")
proc getArguments*(): seq[string] = proc getArguments*(): seq[string] =
let args = commandLineParams() let args = commandLineParams()

View file

@ -3,6 +3,7 @@ import strutils
import std/osproc import std/osproc
import std/math import std/math
import i3bar_base import i3bar_base
import std/threadpool
proc getLimit(): int proc getLimit(): int
@ -30,23 +31,42 @@ proc getDesign(pcnt: float): string =
let text = icon & " " & $percent & "%" let text = icon & " " & $percent & "%"
return text return text
proc getBrightness() = proc get_brightness(run_once: bool = false) =
let current = parseInt(strip(readFile("/sys/class/backlight/intel_backlight/actual_brightness"))) var last_pcnt: float = 0
let pcnt = (current/limit)*100 while true:
let text = getDesign(pcnt) let current = parseInt(strip(readFile("/sys/class/backlight/intel_backlight/actual_brightness")))
let data = i3barData( let pcnt = (current/limit)*100
full_text: text, if pcnt != last_pcnt:
color: foreground, let text = getDesign(pcnt)
border: yellow let data = i3barData(
) full_text: text,
outputJSON(data) 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() = proc main() =
if len(args) > 0: spawn get_brightness()
if args[0] == "4": spawn await_click_info()
discard execCmd("xbacklight -inc 5") sync()
elif args[0] == "5":
discard execCmd("xbacklight -dec 5")
getBrightness()
main() main()

View file

@ -2,6 +2,7 @@ import std/os
import std/times import std/times
import std/osproc import std/osproc
import std/re import std/re
import std/threadpool
import i3bar_base import i3bar_base
let args = getArguments() let args = getArguments()
@ -14,7 +15,8 @@ proc getObject(date: string): i3barData =
) )
return data return data
proc openCalendar(datestr: string) = #proc openCalendar(datestr: string) =
proc openCalendar() =
var c = """yad --calendar \ var c = """yad --calendar \
--undecorated --fixed --close-on-unfocus --no-buttons \ --undecorated --fixed --close-on-unfocus --no-buttons \
--width="222" --height="193" \ --width="222" --height="193" \
@ -26,18 +28,23 @@ proc openCalendar(datestr: string) =
discard execCmd(c) discard execCmd(c)
proc getDate() = proc getDate() =
let now = now() while true:
let d = now.format("yyyy-MM-dd") let now = now()
let data = getObject(d) let d = now.format("yyyy-MM-dd")
outputJSON(data) let data = getObject(d)
if len(args) > 0 and args[0] == "1": outputJSON(data)
openCalendar(now.format("ddd dd HH:MM")) sleep(30000)
proc await_click_info() =
while true:
let input = parseInput()
if input.button == 1:
openCalendar()
proc main() = proc main() =
getDate() spawn getDate()
#while true: spawn await_click_info()
# let input = parseInput() sync()
# if input.button == 1:
main() main()

40
i3bar_pingclock.nim Normal file
View file

@ -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()

60
i3bar_wlan.nim Executable file
View file

@ -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()