added pw_generaturr
This commit is contained in:
parent
3074f55973
commit
e018c8f985
13 changed files with 152 additions and 110 deletions
|
@ -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()
|
||||
|
||||
|
|
|
@ -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:
|
||||
|
|
|
@ -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"
|
||||
|
|
14
src/model/pwgen.nim
Normal file
14
src/model/pwgen.nim
Normal file
|
@ -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
|
|
@ -11,4 +11,5 @@ type
|
|||
Calendurr,
|
||||
Remminurr,
|
||||
Passwurrd,
|
||||
PasswurrdGeneraturr,
|
||||
Temperaturr
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
|
||||
type
|
||||
ExtraArg* = enum
|
||||
VolArg* = enum
|
||||
None, VolUp, VolDown, VolMute
|
|
@ -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
|
||||
|
|
65
src/util/pw_generaturr.nim
Normal file
65
src/util/pw_generaturr.nim
Normal file
|
@ -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()
|
|
@ -1 +0,0 @@
|
|||
-d:ssl
|
|
@ -1,4 +0,0 @@
|
|||
{
|
||||
"version": 1,
|
||||
"reverseDeps": {}
|
||||
}
|
|
@ -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"
|
|
@ -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()
|
|
@ -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()
|
||||
|
|
Loading…
Reference in a new issue