From 83171db72e94a95aa3bf23faab201cbc05d804b9 Mon Sep 17 00:00:00 2001 From: Paul Wilde Date: Wed, 22 Nov 2023 21:19:37 +0000 Subject: [PATCH] furrytime and pingclock working --- src/common.nim | 2 + src/dispatcher.nim | 13 ++++++ src/model/config.nim | 5 +++ src/output.nim | 59 +++++++++++++++++++++++++++ src/parser.nim | 31 ++++++++++++++ src/util/furrytime.nim | 91 ++++++++++++++++++++++++++++++++++++++++++ src/util/pingclock.nim | 55 +++++++++++++++++++++++++ src/wmtools.nim | 5 ++- wm_tools.nimble | 1 + 9 files changed, 261 insertions(+), 1 deletion(-) create mode 100644 src/dispatcher.nim create mode 100644 src/output.nim create mode 100644 src/parser.nim create mode 100644 src/util/furrytime.nim create mode 100644 src/util/pingclock.nim diff --git a/src/common.nim b/src/common.nim index f55a6f9..0924fec 100644 --- a/src/common.nim +++ b/src/common.nim @@ -1,6 +1,8 @@ import model/config +import model/info export config +export info var myConfig* = newConfig() diff --git a/src/dispatcher.nim b/src/dispatcher.nim new file mode 100644 index 0000000..1b2f2d5 --- /dev/null +++ b/src/dispatcher.nim @@ -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" diff --git a/src/model/config.nim b/src/model/config.nim index d2fb563..2cac241 100644 --- a/src/model/config.nim +++ b/src/model/config.nim @@ -4,8 +4,13 @@ import parsetoml type Config* = ref object exec*: string + run*: Tool max_lines*: int prepend*: bool + Tool* = enum + None, + FurryTime, + PingClock let config_dir* = getHomeDir() & ".config/wm_tools/" let config_file* = config_dir & "config.toml" diff --git a/src/output.nim b/src/output.nim new file mode 100644 index 0000000..6941d4f --- /dev/null +++ b/src/output.nim @@ -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 diff --git a/src/parser.nim b/src/parser.nim new file mode 100644 index 0000000..ed2602e --- /dev/null +++ b/src/parser.nim @@ -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) + diff --git a/src/util/furrytime.nim b/src/util/furrytime.nim new file mode 100644 index 0000000..9765923 --- /dev/null +++ b/src/util/furrytime.nim @@ -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) + diff --git a/src/util/pingclock.nim b/src/util/pingclock.nim new file mode 100644 index 0000000..f3baa8e --- /dev/null +++ b/src/util/pingclock.nim @@ -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() + diff --git a/src/wmtools.nim b/src/wmtools.nim index 4b33b59..146e029 100644 --- a/src/wmtools.nim +++ b/src/wmtools.nim @@ -3,6 +3,9 @@ # import common +import parser +import dispatcher when isMainModule: - echo myConfig.exec + parseArgs() + dispatch myConfig diff --git a/wm_tools.nimble b/wm_tools.nimble index 64aad7d..9c31683 100644 --- a/wm_tools.nimble +++ b/wm_tools.nimble @@ -12,3 +12,4 @@ bin = @["wmtools"] requires "nim >= 2.0.0" requires "parsetoml >= 0.7.1" +requires "argparse"