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