From e018c8f9854c4e2ff0584a0af26d0cb94484bbb0 Mon Sep 17 00:00:00 2001 From: Paul Wilde Date: Thu, 23 Nov 2023 11:09:41 +0000 Subject: [PATCH] added pw_generaturr --- src/common.nim | 2 - src/dispatcher.nim | 3 + src/model/config.nim | 2 - src/model/pwgen.nim | 14 ++++ src/model/tool.nim | 1 + src/model/{extraargs.nim => volume.nim} | 2 +- src/parser.nim | 65 ++++++++++++++++--- src/util/pw_generaturr.nim | 65 +++++++++++++++++++ src/util/pw_generaturr/nim.cfg | 1 - .../pw_generaturr/nimbledeps/nimbledata2.json | 4 -- src/util/pw_generaturr/pw_generaturr.nimble | 13 ---- src/util/pw_generaturr/src/pw_generaturr.nim | 65 ------------------- src/util/volurrme.nim | 25 ++++--- 13 files changed, 152 insertions(+), 110 deletions(-) create mode 100644 src/model/pwgen.nim rename src/model/{extraargs.nim => volume.nim} (67%) create mode 100644 src/util/pw_generaturr.nim delete mode 100644 src/util/pw_generaturr/nim.cfg delete mode 100644 src/util/pw_generaturr/nimbledeps/nimbledata2.json delete mode 100644 src/util/pw_generaturr/pw_generaturr.nimble delete mode 100644 src/util/pw_generaturr/src/pw_generaturr.nim diff --git a/src/common.nim b/src/common.nim index 58e7a4c..51af888 100644 --- a/src/common.nim +++ b/src/common.nim @@ -1,12 +1,10 @@ import model/config import model/tool import model/info -import model/extraargs export config export tool export info -export extraargs var myConfig* = newConfig() diff --git a/src/dispatcher.nim b/src/dispatcher.nim index 7254f4e..e7fc768 100644 --- a/src/dispatcher.nim +++ b/src/dispatcher.nim @@ -9,6 +9,7 @@ import util/emurrji import util/calendurr import util/remminurr import util/passwurrd +import util/pw_generaturr import util/temperaturr proc dispatch*(cfg: Config) = @@ -31,6 +32,8 @@ proc dispatch*(cfg: Config) = remminurr.go() of Passwurrd: passwurrd.go() + of PasswurrdGeneraturr: + pw_generaturr.go() of Temperaturr: temperaturr.go() else: diff --git a/src/model/config.nim b/src/model/config.nim index c74997c..eff2278 100644 --- a/src/model/config.nim +++ b/src/model/config.nim @@ -1,7 +1,6 @@ import os import parsetoml -import extraargs import tool type @@ -10,7 +9,6 @@ type run*: Tool max_lines*: int prepend*: bool - extra_args*: seq[ExtraArg] let config_dir* = getHomeDir() & ".config/wm_tools/" let config_file* = config_dir & "config.toml" diff --git a/src/model/pwgen.nim b/src/model/pwgen.nim new file mode 100644 index 0000000..a0238e9 --- /dev/null +++ b/src/model/pwgen.nim @@ -0,0 +1,14 @@ + +type + PWGen* = object + to_terminal*: bool + word_len*: int + digits*: int + number*: int + +proc newPWGen*(): PWGen = + var pw = PWGen() + pw.word_len = 5 + pw.digits = 4 + pw.number = 10 + return pw diff --git a/src/model/tool.nim b/src/model/tool.nim index 0acb749..661b292 100644 --- a/src/model/tool.nim +++ b/src/model/tool.nim @@ -11,4 +11,5 @@ type Calendurr, Remminurr, Passwurrd, + PasswurrdGeneraturr, Temperaturr diff --git a/src/model/extraargs.nim b/src/model/volume.nim similarity index 67% rename from src/model/extraargs.nim rename to src/model/volume.nim index f70daf6..0a08d69 100644 --- a/src/model/extraargs.nim +++ b/src/model/volume.nim @@ -1,4 +1,4 @@ type - ExtraArg* = enum + VolArg* = enum None, VolUp, VolDown, VolMute diff --git a/src/parser.nim b/src/parser.nim index 0da2994..8382288 100644 --- a/src/parser.nim +++ b/src/parser.nim @@ -3,13 +3,15 @@ import os import argparse import common +import model/pwgen +import model/volume 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",help="the tool to run, i.e. furrytime, pingclock, volurrme, etc.") - arg("adjust", help="(up, down, mute) allows to send extra commands. i.e. when run with volurrme, allows to adjust the volume",default=some("None")) + arg("others", nargs = -1) try: var opts = p.parse(params) case opts.input @@ -31,18 +33,13 @@ proc parseArgs*() = myConfig.run = Remminurr of "passwurrd", "password", "pw": myConfig.run = Passwurrd + of "passwurrdgeneraturr", "passwordgenerator", "pwgen": + myConfig.run = PasswurrdGeneraturr of "temperaturr", "temperature", "temp": myConfig.run = Temperaturr else: echo p.help quit(1) - case opts.adjust - of "volup": - myConfig.extra_args.add(VolUp) - of "voldown": - myConfig.extra_args.add(VolDown) - of "mute","volmute": - myConfig.extra_args.add(VolMute) except ShortCircuit as err: if err.flag == "argparse_help": echo err.help @@ -51,3 +48,55 @@ proc parseArgs*() = stderr.writeLine getCurrentExceptionMsg() quit(1) +proc parseVolArgs*(): VolArg = + var arg: VolArg + let params = commandLineParams() + var p = newParser: + help("Args for volurrme") + arg("volurrme",help="can only ever be 'volurrme' as you won't have gotten this far otherwise") + arg("adjust",help="up, down, or mute",default=some("")) + try: + var opts = p.parse(params) + case opts.adjust + of "volup", "up": + arg = VolUp + of "voldown", "down": + arg = VolDown + of "mute","volmute": + arg = VolMute + except ShortCircuit as err: + if err.flag == "argparse_help": + echo err.help + quit(1) + except UsageError: + stderr.writeLine getCurrentExceptionMsg() + quit(1) + return arg + +proc parsePWGenArgs*(): PWGen = + var gen = newPWGen() + let params = commandLineParams() + var p = newParser: + help("Args for pw_generaturr") + arg("pwgen",help="can only ever be 'pwgen' as you won't have gotten this far otherwise") + flag("-o","--output",help="outputs to terminal instead of something else") + option("-l","--length",help="Length of the word part of the password") + option("-d","--digits",help="Length of the number part of the password",default=some("")) + option("-n","--number",help="Number of passwords to return") + try: + var opts = p.parse(params) + if opts.length != "": + gen.word_len = parseInt(opts.length) + if opts.digits != "": + gen.digits = parseInt(opts.digits) + if opts.number != "": + gen.number = parseInt(opts.number) + gen.to_terminal = not opts.output + except ShortCircuit as err: + if err.flag == "argparse_help": + echo err.help + quit(1) + except UsageError: + stderr.writeLine getCurrentExceptionMsg() + quit(1) + return gen diff --git a/src/util/pw_generaturr.nim b/src/util/pw_generaturr.nim new file mode 100644 index 0000000..c3370df --- /dev/null +++ b/src/util/pw_generaturr.nim @@ -0,0 +1,65 @@ + +import httpclient +import json +import strutils +import random + +import ../parser +import ../model/pwgen +import ../common +import ../output + +var passwords: seq[string] +proc getNumber(size: int = 4): string = + var num = "" + for _ in countup(1,size): + randomize() + let roll = rand(0..9) + num &= $roll + return num + +proc parsePasswords(body: string, digits: int) = + passwords = @[] + let j = body.parseJson + for pass in j.getElems: + var p = pass.getStr.capitalizeAscii + p &= getNumber(digits) + passwords.add(p) + +proc getPasswords(pwgen: PWGen) = + var c = newHttpClient() + try: + let url = "https://random-word-api.herokuapp.com/word?number=" & $pwgen.number & "&length=" & $pwgen.word_len + let resp = c.get(url) + if resp.status == $Http200: + parsePasswords(resp.body, pwgen.digits) + except: + stderr.writeLine getCurrentExceptionMsg() + +proc getOutput(): Info = + var data = newInfo("PW Generaturr") + data.full_text = "Refresh" + return data + +proc goOutput(args: PWGen) = + let data = getoutput() + let selected = outputData(data,passwords) + if selected in passwords: + copyToClipboard(selected) + elif selected == "Refresh": + getPasswords(args) + goOutput(args) + +proc go*() = + echo "Getting passwords..." + let args = parsePWGenArgs() + getPasswords(args) + if args.to_terminal: + for pw in passwords: + echo pw + else: + echo "Outputting to app" + goOutput(args) + +if isMainModule: + go() diff --git a/src/util/pw_generaturr/nim.cfg b/src/util/pw_generaturr/nim.cfg deleted file mode 100644 index 521e21d..0000000 --- a/src/util/pw_generaturr/nim.cfg +++ /dev/null @@ -1 +0,0 @@ --d:ssl diff --git a/src/util/pw_generaturr/nimbledeps/nimbledata2.json b/src/util/pw_generaturr/nimbledeps/nimbledata2.json deleted file mode 100644 index 6ffe1cc..0000000 --- a/src/util/pw_generaturr/nimbledeps/nimbledata2.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "version": 1, - "reverseDeps": {} -} \ No newline at end of file diff --git a/src/util/pw_generaturr/pw_generaturr.nimble b/src/util/pw_generaturr/pw_generaturr.nimble deleted file mode 100644 index 9fd4a3c..0000000 --- a/src/util/pw_generaturr/pw_generaturr.nimble +++ /dev/null @@ -1,13 +0,0 @@ -# Package - -version = "0.1.0" -author = "Paul Wilde" -description = "A new awesome nimble package" -license = "AGPL-3.0-or-later" -srcDir = "src" -bin = @["pw_generaturr"] - - -# Dependencies - -requires "nim >= 2.0.0" diff --git a/src/util/pw_generaturr/src/pw_generaturr.nim b/src/util/pw_generaturr/src/pw_generaturr.nim deleted file mode 100644 index 82e2cc8..0000000 --- a/src/util/pw_generaturr/src/pw_generaturr.nim +++ /dev/null @@ -1,65 +0,0 @@ - -#import ../../globurrl - -import httpclient -import json -import strutils -import random -import parseopt -import os - -var length = 4 -var number = 10 -var word_len = 5 - -proc getNumber(size: int = 4): string = - var num = "" - for _ in countup(1,size): - randomize() - let roll = rand(0..9) - num &= $roll - return num - -proc parsePw(body: string) = - let j = body.parseJson - for pass in j.getElems: - var p = pass.getStr.capitalizeAscii - p &= getNumber(length) - echo p - -proc getPW() = - var c = newHttpClient() - try: - let url = "https://random-word-api.herokuapp.com/word?number=" & $number & "&length=" & $word_len - echo url - let resp = c.get(url) - if resp.status == $Http200: - parsePw(resp.body) - - except: - echo getCurrentExceptionMsg() - -proc parseArgs() = - var p = initOptParser(commandLineParams()) - while true: - p.next() - case p.kind - of cmdEnd: break - of cmdShortOption, cmdLongOption: - if p.val == "": - #echo "Option: ", p.key - discard - else: - #echo "Option and value: ", p.key, ", ", p.val - case p.key - of "length": - word_len = parseInt(p.val) - of "number": - number = parseInt(p.val) - of cmdArgument: - #echo "Argument: ", p.key - discard - -if isMainModule: - parseArgs() - getPW() diff --git a/src/util/volurrme.nim b/src/util/volurrme.nim index afa2671..80eed8b 100644 --- a/src/util/volurrme.nim +++ b/src/util/volurrme.nim @@ -5,6 +5,8 @@ import osproc import ../common import ../output +import ../parser +import ../model/volume const audio_tools = @["ncpamixer", "pavucontrol"] const vol_cmd = "pamixer" @@ -101,18 +103,13 @@ proc getVolume*() = getVolume() proc go*() = - block start: - for arg in myConfig.extra_args: - case arg - of VolUp: - volumeUp() - break start - of VolDown: - volumeDown() - break start - of VolMute: - volumeMute() - break start - else: - continue + let arg = parseVolArgs() + case arg + of VolUp: + volumeUp() + of VolDown: + volumeDown() + of VolMute: + volumeMute() + else: getVolume()