feat: moving to VULTRAS

This commit is contained in:
Andrey Parhomenko 2024-01-23 18:20:00 +03:00
parent af7fd31a47
commit 64b1dd2f02
9 changed files with 90 additions and 112 deletions

2
.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
*.exe

3
go.mod
View file

@ -1,4 +1,5 @@
module utf8tmpl module vultras.su/util/combo
go 1.16 go 1.16
require vultras.su/core/cli v0.0.0-20240104195345-5d79542278a0 // indirect

2
go.sum Normal file
View file

@ -0,0 +1,2 @@
vultras.su/core/cli v0.0.0-20240104195345-5d79542278a0 h1:acA77oEg7hDuUchyBvw7scG9gayVLFAC9/CGuqyfLSA=
vultras.su/core/cli v0.0.0-20240104195345-5d79542278a0/go.mod h1:rYu/sFWE3vUkDSSJCZt+K1aVaso0MYcZ+tmxQd4whdQ=

View file

@ -1,4 +0,0 @@
#!/bin/sh
exec go install

41
main.go
View file

@ -1,40 +1,17 @@
package main package main
import( import(
"fmt"
"os" "os"
"utf8tmpl/tmpl" "vultras.su/util/combo/tmpl"
"utf8tmpl/pin" "vultras.su/util/combo/pin"
"vultras.su/core/cli/mtool"
)
var root = mtool.T("combo").Subs(
tmpl.Tool,
pin.Tool,
) )
func main() { func main() {
var( root.Run(os.Args[1:])
utilName string
args []string
)
utilsMap := map[string] interface{} {
"tmpl" : tmpl.Run,
"pin" : pin.Run,
}
if len(os.Args)<2 {
for k, _ := range utilsMap {
fmt.Printf("%s\n", k)
}
os.Exit(0)
} else {
utilName = os.Args[1]
args = os.Args[1:]
}
if _, ok := utilsMap[utilName] ; !ok {
fmt.Printf("%s: %s: no such util\n", os.Args[0], utilName )
os.Exit(1)
}
status := utilsMap[utilName].(func([]string) int )(args)
os.Exit(status)
} }

View file

@ -1,25 +1,27 @@
package pin package pin
import ( import (
"os"
"fmt" "fmt"
"strconv" //"strconv"
"log" //"log"
"flag" "vultras.su/core/cli/mtool"
"os"
) )
var ( var (
Lflag bool Lflag bool
lval int lval int
arg0 string
delim rune = '\n' delim rune = '\n'
status int = 0
nVal int nVal int
chrs []rune chrs []rune
Tool = mtool.T("pin").Func(Run).Desc(
"print all the possible PIN combinations made of custom characters",
).Usage(
"<tmpl_chars>",
)
) )
func func Pow(x, p int) int {
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
@ -27,8 +29,7 @@ Pow(x, p int) int {
return ret return ret
} }
func func GetPin(s []rune, l int, i int) string {
GetPin(s []rune, l int, i int) string {
ret := "" ret := ""
slen := len(s) slen := len(s)
for j:=0 ; j<l ; j++ { for j:=0 ; j<l ; j++ {
@ -37,8 +38,7 @@ GetPin(s []rune, l int, i int) string {
return ret return ret
} }
func func printPins(s []rune, l int) {
printPins(s []rune, l int) {
n := Pow(len(s), l) n := Pow(len(s), l)
for i:=0 ; i<n ; i++ { for i:=0 ; i<n ; i++ {
pin := GetPin(s, l, i) pin := GetPin(s, l, i)
@ -48,8 +48,7 @@ printPins(s []rune, l int) {
} }
} }
func func Fits(s []rune) bool {
Fits(s []rune) 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 {
@ -64,57 +63,58 @@ Fits(s []rune) bool {
return true return true
} }
func func Run(flags *mtool.Flags) {
Run(args []string) int { var (
arg0 = args[0] length int
flagSet := flag.NewFlagSet(args[0], flag.ExitOnError) rFlag bool
flagSet.IntVar(&lval, "l", 0, )
"Add combinations with less number of chars starting with arg.") flags.IntVar(
flagSet.BoolVar(&Lflag, "L", false, &lval, "min", 0,
"Set less number to 1. Overrides l flag.") "min length of the output pins",
flagSet.IntVar(&nVal, "n", 1, "Max repeats of the rune." ) )
flagSet.Parse(args[1:]) flags.BoolVar(
args = flagSet.Args() &Lflag, "m", false,
flagSet.Usage = func() { "set the '-min' flag value to 1 (overrides it)",
fmt.Fprintf(os.Stderr, "Usage of %s: %s [options] <tmpl_chars> [len]\n", arg0) )
flagSet.PrintDefaults() 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" )
args := flags.Parse()
if len(args) == 0 {
flags.Usage()
os.Exit(1) os.Exit(1)
} }
var(
err error
n int
)
if len(args) == 0 {
flagSet.Usage()
}
chrs = []rune(args[0]) chrs = []rune(args[0])
if len(args) == 2 {
n, err = strconv.Atoi(args[1])
} else if len(args) == 1 {
n = len(chrs)
}
if err!=nil { if length == 0 {
log.Fatal(err) length = len(chrs)
} }
if Lflag { if Lflag {
lval = 1 lval = 1
} }
if lval != 0 { if rFlag {
if lval > n { nVal = len(chrs)
flagSet.Usage()
} }
for i := lval ; i<=n ; i++ {
if lval != 0 {
if lval > length {
flags.Usage()
os.Exit(1)
}
for i := lval ; i<=length ; i++ {
printPins(chrs, i) printPins(chrs, i)
} }
} else { } else {
printPins(chrs, n) printPins(chrs, length)
} }
return status
} }

View file

@ -1,4 +1,5 @@
# utf8tmpl # combo
## Description ## Description
Set of utils to implement templates based on UTF-8 characters. Set of utils to implement templates based on UTF-8 characters.

7
taskfile.yml Normal file
View file

@ -0,0 +1,7 @@
version: 3
tasks:
build:
desc: build the binary
cmds:
- go build

View file

@ -5,33 +5,30 @@ import (
"fmt" "fmt"
"bufio" "bufio"
"io" "io"
"vultras.su/core/cli/mtool"
) )
var( var(
arg0 string
delim rune delim rune
status int Tool = mtool.T("tmpl").Func(Run).Desc(
"substitute the inputted runes with the specified words",
).Usage(
"[n_utf8_chars] [n_strings]",
)
) )
func func Run(flags *mtool.Flags) {
usage() { delim = '\n'
fmt.Fprintf(os.Stderr, "usage: %s [n_utf8_chars] [n_strings]\n", arg0) args := flags.Parse()
if len(args)<2 || len(args[0]) != len(args)-1 {
flags.Usage()
os.Exit(1) os.Exit(1)
} }
func chrs := []rune(args[0])
Run(args []string) int { args = args[1:]
status = 0
delim = '\n'
arg0 = args[0]
if len(args)<3 || len(args[1]) != len(args)-2 {
usage()
}
chrs := []rune(args[1])
args = args[2:]
tmpl := make(map[rune] string) tmpl := make(map[rune] string)
for i, s := range args { for i, s := range args {
@ -45,18 +42,13 @@ Run(args []string) int {
break break
} }
s = s[:len(s)-1] s = s[:len(s)-1]
for _, c := range []rune(s) { for _, c := range s {
s, ok := tmpl[c] s, ok := tmpl[c]
if !ok { if !ok {
/*fmt.Fprintf(os.Stderr, "%s: '%s': no such character in template string\n",
arg0, string(c) )*/
s = string(c) s = string(c)
} }
fmt.Printf("%s", s) fmt.Printf("%s", s)
} }
fmt.Printf("%s", string(delim)) fmt.Printf("%s", string(delim))
} }
return status
} }