From 3e88ea16faf215ec49d723486b53cf4173ea2373 Mon Sep 17 00:00:00 2001 From: Paul Wilde Date: Mon, 7 Mar 2022 09:37:31 +0000 Subject: [PATCH] push --- i3bar_pingclock.nim | 8 +++++--- i3bar_tides.nim | 49 ++++++++++++++++++++++++++------------------- i3bar_volume.nim | 17 +++++++++++++--- 3 files changed, 47 insertions(+), 27 deletions(-) diff --git a/i3bar_pingclock.nim b/i3bar_pingclock.nim index 9956ec7..b196b4c 100644 --- a/i3bar_pingclock.nim +++ b/i3bar_pingclock.nim @@ -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" diff --git a/i3bar_tides.nim b/i3bar_tides.nim index f258ba8..ae625db 100644 --- a/i3bar_tides.nim +++ b/i3bar_tides.nim @@ -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: diff --git a/i3bar_volume.nim b/i3bar_volume.nim index bd2706b..b1e3a3d 100644 --- a/i3bar_volume.nim +++ b/i3bar_volume.nim @@ -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 = "" & icon & "" & $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,