This commit is contained in:
Paul Wilde 2022-03-07 09:37:31 +00:00
parent 976f106d43
commit 3e88ea16fa
3 changed files with 47 additions and 27 deletions

View file

@ -3,6 +3,7 @@ import std/osproc
import std/re
import strutils
import i3bar_base
import std/math
const host: string = "web.wilde.cloud"
const cmd: string = "ping -4 -c 1 " & host
@ -11,14 +12,15 @@ const time_secs: int = 4
let ping_re = re(r"time=[0-9.]+")
proc get_ping(): float =
var ping: float = -1
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 parseFloat(ping)
return -1
let png = ping_line[bounds.first+5..bounds.last]
ping = parseFloat(png)
return round(ping,1)
proc getObject(ping: float): i3barData =
var text = "🏓 " & $ping & " ms"

View file

@ -17,6 +17,7 @@ type
State: string
Time: string
Height: string
Tomorrow: bool
TideList = ref object
Tides: seq[Tide]
LastUpdated: DateTime
@ -26,7 +27,7 @@ proc sortTides(tides: seq[Tide]): seq[Tide] =
var reltides: seq[Tide]
var count = 0
for tide in tides:
if timenow.format("HH:MM") <= tide.Time:
if timenow.format("HH:MM") <= tide.Time or tide.Tomorrow:
reltides.add(tide)
count += 1
if count >= 2:
@ -35,31 +36,37 @@ proc sortTides(tides: seq[Tide]): seq[Tide] =
proc getTideData(gettomorrow: bool = false): seq[Tide] =
var tides: seq[Tide]
let fnd = re">((High|Low)|([0-9]+:[0-9]+)|([0-9]+\.[0-9]+m))"
var client = newHttpClient()
var link = replace(url,re"\%LOC",loc)
if gettomorrow:
let tomdate = now() + initTimeInterval(days = 1)
link &= "-" & tomdate.format("yyyymmdd")
let data = client.getContent(link)
let times = findAll(data,fnd)
var tides: seq[Tide]
var tide: Tide
var count: int = 0
for time in times:
let l = len(time) - 1
if time == ">High" or time == ">Low":
tide = Tide()
tide.State = time[1..l]
count = 1
continue
else:
count += 1
if count == 2:
tide.Time = time[1..l]
elif count == 3:
tide.Height = time[1..l]
tides.add(tide)
link &= "-" & tomdate.format("yyyyMMdd")
try:
let data = client.getContent(link)
let times = findAll(data,fnd)
var tide: Tide
var count: int = 0
for time in times:
let l = len(time) - 1
if time == ">High" or time == ">Low":
tide = Tide()
tide.State = time[1..l]
count = 1
continue
else:
count += 1
if count == 2:
tide.Time = time[1..l]
elif count == 3:
tide.Height = time[1..l]
if gettomorrow:
tide.Tomorrow = true
tides.add(tide)
except:
sleep(5000)
return getTideData(false)
if not gettomorrow:
let tomtides = getTideData(true)
for tide in tomtides:

View file

@ -5,7 +5,18 @@ import std/math
import i3bar_base
import std/threadpool
proc getDesign(vol: string): string =
proc get_current_volume(): string {.gcsafe.}
proc check_volume(volume: string): string =
var vol = volume
if strip(vol) == "Connection error":
discard execCmdEx("pulseaudio -k; sleep 5; pulseaudio -D")
vol = get_current_volume()
vol = check_volume(vol)
return vol
proc getDesign(volume: string): string =
let vol = check_volume(volume)
var icon = ""
if vol == "muted":
return icon & "muted"
@ -21,7 +32,7 @@ proc getDesign(vol: string): string =
icon = ""
else:
icon = ""
let text = icon & $pcnt & "%"
let text = "<span size=\"x-large\">" & icon & "</span>" & $pcnt & "%"
return text
proc get_current_volume(): string =
@ -35,7 +46,7 @@ proc get_volume(run_once: bool = false) =
var last_vol: string = ""
while true:
let vol = get_current_volume()
if vol != last_vol:
if vol != last_vol or true:
let text = getDesign(vol)
let data = i3barData(
full_text: text,