feat: added way for pin custom run.

This commit is contained in:
Andrey Parhomenko 2024-09-22 18:40:58 +05:00
parent bf6241ea61
commit da4713a30e

View file

@ -4,16 +4,14 @@ import (
"fmt" "fmt"
//"strconv" //"strconv"
//"log" //"log"
"surdeus.su/core/cli/mtool" "io"
"os" "os"
"surdeus.su/core/cli/mtool"
) )
var ( var (
Lflag bool
lval int
delim rune = '\n' delim rune = '\n'
nVal int
chrs []rune
Tool = mtool.T("pin").Func(Run).Desc( Tool = mtool.T("pin").Func(Run).Desc(
"print all the possible PIN combinations made of custom characters", "print all the possible PIN combinations made of custom characters",
).Usage( ).Usage(
@ -22,39 +20,46 @@ var (
) )
func Pow(x, p int) int { func Pow(x, p int) int {
ret := 1 ret := 1
for i:=0 ; i<p ; i++ { for i:=0 ; i<p ; i++ {
ret *= x ret *= x
} }
return ret return ret
} }
func GetPin(s []rune, l int, i int) string { func GetPin(chars []rune, length int, i int) string {
ret := "" ret := ""
slen := len(s) slen := len(chars)
for j:=0 ; j<l ; j++ { for j:=0 ; j<length ; j++ {
ret = string(s[ ( i/Pow(slen, j) ) % slen] ) + ret ret = string(chars[ ( i/Pow(slen, j) ) % slen] ) + ret
} }
return ret return ret
} }
func printPins(s []rune, l int) { func FprintPins(
n := Pow(len(s), l) output io.Writer,
chars []rune,
length int,
) {
n := Pow(len(chars), length)
for i:=0 ; i<n ; i++ { for i:=0 ; i<n ; i++ {
pin := GetPin(s, l, i) pin := GetPin(chars, length, i)
if Fits([]rune(pin)) { if Fits([]rune(pin), chars, length) {
fmt.Println(GetPin(s, l, i)) fmt.Fprintln(
output,
GetPin(chars, length, i),
)
} }
} }
} }
func Fits(s []rune) bool { func Fits(s []rune, chrs []rune, maxReps int) bool {
a := make([]int, len(chrs)) a := make([]int, len(chrs))
for i, v1 := range chrs { for i, v1 := range chrs {
for _, v2 := range s { for _, v2 := range s {
if v1 == v2 { if v1 == v2 {
a[i]++ a[i]++
if a[i] > nVal { if a[i] > maxReps {
return false return false
} }
} }
@ -63,38 +68,55 @@ func Fits(s []rune) bool {
return true return true
} }
func Run(flags *mtool.Flags) { func Run(
flags *mtool.Flags,
) {
CustomRun(
os.Stdin,
os.Stdout,
flags,
)
}
func CustomRun(
input io.Reader,
output io.Writer,
flags *mtool.Flags,
) {
var ( var (
length int length int
rFlag bool rFlag bool
mFlag bool
minLength int
maxReps int
) )
var ( var (
chrsString string charsString string
chrs []rune chrs []rune
) )
flags.StringVar( flags.StringVar(
&chrsString, &charsString,
"c", "c",
"0123456789", "0123456789",
"character set for substitution", "character set for substitution",
"COMBO_CHARS", "COMBO_CHARS",
) )
flags.IntVar( flags.IntVar(
&lval, "min", 1, &minLength, "min-len", 1,
"min length of the output pins", "min length of the output pins",
) )
flags.BoolVar( flags.BoolVar(
&Lflag, "m", false, &mFlag, "m", false,
"set the '-min' flag value to 1 (overrides the '-min' it)", "set the '-min-len' flag value to 1 (overrides the '-min' it)",
) )
flags.IntVar( flags.IntVar(
&length, "max", 0, &length, "max", 0,
"max length of the output pins", "max length of the output pins",
) )
flags.IntVar(&nVal, "rep", 1, "max repeats of the rune." ) flags.IntVar(&maxReps, "max-reps", 1, "max repeats of the rune." )
flags.BoolVar(&rFlag, "R", false, "make the maximum repeat equal to the length of input chars" ) flags.BoolVar(&rFlag, "r", false, "make the -max-reps value equal to the length of input chars" )
_ = flags.Parse() _ = flags.Parse()
@ -103,30 +125,30 @@ func Run(flags *mtool.Flags) {
os.Exit(1) os.Exit(1)
}*/ }*/
chrs = []rune(chrsString) chrs = []rune(charsString)
if length == 0 { if length == 0 {
length = 4 length = 4
} }
if Lflag { if mFlag {
lval = 1 minLength = 1
} }
if rFlag { if rFlag {
nVal = len(chrs) maxReps = len(chrs)
} }
if lval != 0 { if minLength != 0 {
if lval > length { if minLength > length {
flags.Usage() flags.Usage()
os.Exit(1) os.Exit(1)
} }
for i := lval ; i<=length ; i++ { for i := minLength ; i<=length ; i++ {
printPins(chrs, i) FprintPins(output, chrs, i)
} }
} else { } else {
printPins(chrs, length) FprintPins(output, chrs, length)
} }
} }