furrytime and pingclock working

This commit is contained in:
Paul Wilde 2023-11-22 21:19:37 +00:00
parent 1a478fca9e
commit 83171db72e
9 changed files with 261 additions and 1 deletions

View file

@ -1,6 +1,8 @@
import model/config import model/config
import model/info
export config export config
export info
var myConfig* = newConfig() var myConfig* = newConfig()

13
src/dispatcher.nim Normal file
View file

@ -0,0 +1,13 @@
import common
import util/furrytime
import util/pingclock
proc dispatch*(cfg: Config) =
case cfg.run
of FurryTime:
furrytime.go()
of PingClock:
pingclock.go()
else:
echo "No valid run command given"

View file

@ -4,8 +4,13 @@ import parsetoml
type type
Config* = ref object Config* = ref object
exec*: string exec*: string
run*: Tool
max_lines*: int max_lines*: int
prepend*: bool prepend*: bool
Tool* = enum
None,
FurryTime,
PingClock
let config_dir* = getHomeDir() & ".config/wm_tools/" let config_dir* = getHomeDir() & ".config/wm_tools/"
let config_file* = config_dir & "config.toml" let config_file* = config_dir & "config.toml"

59
src/output.nim Normal file
View file

@ -0,0 +1,59 @@
import osproc
import strutils
import common
proc stripQuotes(str: string): string =
var text = replace(str,"\"",""")
return text
proc quote(str: string): string =
var text = str
# May need to put some further work to escape some special chars here
text = stripQuotes(text)
# Put leading and ending quote marks in
return " \"" & text & "\" "
# ^ Add a spaces ^ so the previous flag isn't touching
proc markup(str: string): string =
# Placeholder proc for future use
var text = stripQuotes(str)
return text
proc genMenuCmd(data: Info, opts: varargs[string]): string =
var cmd = ""
var x_lines = len(opts) + 1
# if the text is empty, we don't want to create a menu item of it
if data.full_text != "":
let text = markup data.full_text
cmd &= text & "\n"
else:
x_lines -= 1
for opt in opts:
let text = markup opt
cmd &= text & "\n"
cmd.removeSuffix("\n")
if x_lines > myConfig.max_lines: x_lines = myConfig.max_lines
if myConfig.prepend:
cmd = "echo -e" & quote(cmd) & "| "
cmd &= myConfig.exec
cmd &= " -i" # set case insensitive
cmd &= " -p" & quote(data.title)
cmd &= "-l " & $x_lines
echo "Sending command:\n" & cmd
return cmd
proc runExec(data: Info, opts: varargs[string]): string =
let cmd = genMenuCmd(data, opts)
var output = execCmdEx(cmd)
echo "Output:\n" & $output
output.output.stripLineEnd()
return output.output
proc outputData*(data: Info, args: varargs[string,`$`]): string {.discardable.} =
var output = runExec(data,args)
return output

31
src/parser.nim Normal file
View file

@ -0,0 +1,31 @@
import os
import argparse
import common
proc parseArgs*() =
let params = commandLineParams()
var p = newParser:
help("WMTools : a set of tools to output option to your program of choice i.e. Rofi")
arg("input")
flag("-l","--loop")
option("-r","--run")
try:
var opts = p.parse(params)
case opts.input
of "furrytime", "fuzzytime", "time":
myConfig.run = FurryTime
of "pingclock", "pingclurrk", "ping":
myConfig.run = PingClock
else:
echo p.help
quit(1)
except ShortCircuit as err:
if err.flag == "argparse_help":
echo err.help
quit(1)
except UsageError:
stderr.writeLine getCurrentExceptionMsg()
quit(1)

91
src/util/furrytime.nim Normal file
View file

@ -0,0 +1,91 @@
import times
import ../model/info
import ../output
proc getHour(hr: int): string =
case hr:
of 1, 13:
return "one"
of 2, 14:
return "two"
of 3, 15:
return "three"
of 4, 16:
return "four"
of 5, 17:
return "five"
of 6, 18:
return "six"
of 7, 19:
return "seven"
of 8, 20:
return "eight"
of 9, 21:
return "nine"
of 10, 22:
return "ten"
of 11, 23:
return "eleven"
of 0, 12, 24:
return "twelve"
else:
return "error"
proc getMinute(min: int): string =
case min:
of 58,59,0,1,2:
return "oclock"
of 3,4,5,6,7,53,54,55,56,57:
return "five"
of 8,9,10,11,12,48,49,50,51,52:
return "ten"
of 13,14,15,16,17,43,44,45,46,47:
return "quarter"
of 18,19,20,21,22,38,39,40,41,42:
return "twenty"
of 23,24,25,26,27,33,34,35,36,37:
return "twenty-five"
of 28,29,30,31,32:
return "half"
else:
return "error"
proc getFuzzyTime(): string =
let tm = now()
var hr = tm.hour()
let min = tm.minute()
var link = "past"
if min > 32 :
link = "to"
case hr:
of 23:
hr = 0
else:
hr = hr + 1
if min >= 58 or min <= 02:
return getHour(hr) & " " & getMinute(min)
else:
return getMinute(min) & " " & link & " " & getHour(hr)
proc getObject(time: string): Info =
var data = newInfo("Furry Time")
data.full_text = time
return data
proc show(time: string, next_fuzzy: bool = false) =
let data = getObject(time)
let x = outputData(data)
if x == time:
case next_fuzzy:
of true:
let t = getFuzzyTime()
show(t, false)
else:
let t = now().format("HH:mm:ss")
show(t, true)
proc go*() =
let time = getFuzzyTime()
show(time)

55
src/util/pingclock.nim Normal file
View file

@ -0,0 +1,55 @@
import osproc
import re
import strutils
import ../common
import ../output
const host: string = "9.9.9.9"
let ping_re = re(r"time=[0-9.]+")
const ping_cmd: string = "ping -4 -c 1 " & host
proc getPing(): float =
var ping: float = -1
let cmdOut = execCmdEx(ping_cmd)
let lines = splitLines(cmdOut.output)
let ping_line = lines[1]
let bounds = findBounds(ping_line, ping_re)
if bounds.first > 0:
let png = ping_line[bounds.first+5..bounds.last]
ping = parseFloat(png)
return ping
proc getObject(ping: float): Info =
let pingstr = split($ping,".")
let niceping = pingstr[0] & "." & pingstr[1][0]
var text = "🏓 " & niceping & " ms"
var state = 0
if ping < 0:
text = "❌ No Pong"
state = 1
else:
case ping:
of 0..100:
state = 0
of 101..400:
state = 1
of 401..1000:
state = 2
else:
state = 9
var data = newInfo("Ping Clurrk")
data.full_text = text
# i3bar stuff
return data
proc go*() =
let ping = get_ping()
let data = getObject(ping)
let output = outputData(data)
if output == data.full_text:
go()

View file

@ -3,6 +3,9 @@
# #
import common import common
import parser
import dispatcher
when isMainModule: when isMainModule:
echo myConfig.exec parseArgs()
dispatch myConfig

View file

@ -12,3 +12,4 @@ bin = @["wmtools"]
requires "nim >= 2.0.0" requires "nim >= 2.0.0"
requires "parsetoml >= 0.7.1" requires "parsetoml >= 0.7.1"
requires "argparse"