diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c799d12 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ + +*.exe diff --git a/go.mod b/go.mod index fde6242..ae6cef0 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,5 @@ -module utf8tmpl +module vultras.su/util/combo go 1.16 +require vultras.su/core/cli v0.0.0-20240104195345-5d79542278a0 // indirect diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..050dbca --- /dev/null +++ b/go.sum @@ -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= diff --git a/install b/install deleted file mode 100755 index f1cf507..0000000 --- a/install +++ /dev/null @@ -1,4 +0,0 @@ -#!/bin/sh - -exec go install - diff --git a/main.go b/main.go index eb559df..38a5ac8 100644 --- a/main.go +++ b/main.go @@ -1,40 +1,17 @@ package main import( - "fmt" "os" - "utf8tmpl/tmpl" - "utf8tmpl/pin" + "vultras.su/util/combo/tmpl" + "vultras.su/util/combo/pin" + "vultras.su/core/cli/mtool" +) + +var root = mtool.T("combo").Subs( + tmpl.Tool, + pin.Tool, ) func main() { - var( - 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) + root.Run(os.Args[1:]) } diff --git a/pin/pin.go b/pin/pin.go index fa2a213..6951592 100644 --- a/pin/pin.go +++ b/pin/pin.go @@ -1,25 +1,27 @@ package pin -import( - "os" +import ( "fmt" - "strconv" - "log" - "flag" + //"strconv" + //"log" + "vultras.su/core/cli/mtool" + "os" ) -var( +var ( Lflag bool lval int - arg0 string delim rune = '\n' - status int = 0 nVal int chrs []rune + Tool = mtool.T("pin").Func(Run).Desc( + "print all the possible PIN combinations made of custom characters", + ).Usage( + "", + ) ) -func -Pow(x, p int) int { +func Pow(x, p int) int { ret := 1 for i:=0 ; i

[len]\n", arg0) - flagSet.PrintDefaults() +func Run(flags *mtool.Flags) { + var ( + length int + rFlag bool + ) + flags.IntVar( + &lval, "min", 0, + "min length of the output pins", + ) + flags.BoolVar( + &Lflag, "m", false, + "set the '-min' flag value to 1 (overrides 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" ) + + args := flags.Parse() + + if len(args) == 0 { + flags.Usage() os.Exit(1) } - var( - err error - n int - ) - - if len(args) == 0 { - flagSet.Usage() - } - 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 { - log.Fatal(err) + if length == 0 { + length = len(chrs) } if Lflag { lval = 1 } + if rFlag { + nVal = len(chrs) + } + if lval != 0 { - if lval > n { - flagSet.Usage() + if lval > length { + flags.Usage() + os.Exit(1) } - for i := lval ; i<=n ; i++ { + for i := lval ; i<=length ; i++ { printPins(chrs, i) } } else { - printPins(chrs, n) + printPins(chrs, length) } - return status } diff --git a/readme b/readme.md similarity index 87% rename from readme rename to readme.md index ac83819..37657ea 100644 --- a/readme +++ b/readme.md @@ -1,4 +1,5 @@ -# utf8tmpl +# combo ## Description + Set of utils to implement templates based on UTF-8 characters. diff --git a/taskfile.yml b/taskfile.yml new file mode 100644 index 0000000..1813fd1 --- /dev/null +++ b/taskfile.yml @@ -0,0 +1,7 @@ +version: 3 + +tasks: + build: + desc: build the binary + cmds: + - go build diff --git a/tmpl/tmpl.go b/tmpl/tmpl.go index 5e48822..1fafb99 100644 --- a/tmpl/tmpl.go +++ b/tmpl/tmpl.go @@ -5,33 +5,30 @@ import ( "fmt" "bufio" "io" + "vultras.su/core/cli/mtool" ) var( - arg0 string 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 -usage() { - fmt.Fprintf(os.Stderr, "usage: %s [n_utf8_chars] [n_strings]\n", arg0) - os.Exit(1) -} - -func -Run(args []string) int { - status = 0 +func Run(flags *mtool.Flags) { delim = '\n' - arg0 = args[0] + args := flags.Parse() - if len(args)<3 || len(args[1]) != len(args)-2 { - usage() + if len(args)<2 || len(args[0]) != len(args)-1 { + flags.Usage() + os.Exit(1) } - chrs := []rune(args[1]) - args = args[2:] + chrs := []rune(args[0]) + args = args[1:] tmpl := make(map[rune] string) for i, s := range args { @@ -45,18 +42,13 @@ Run(args []string) int { break } s = s[:len(s)-1] - for _, c := range []rune(s) { + for _, c := range s { s, ok := tmpl[c] 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", string(delim)) } - - - return status }