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

View file

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

View file

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

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