feat: added custom runner for the tmpl.

This commit is contained in:
Andrey Parhomenko 2024-09-23 01:18:05 +05:00
parent da4713a30e
commit d90d443017

View file

@ -1,15 +1,16 @@
package tmpl
import (
"os"
"fmt"
"bufio"
"fmt"
"io"
"os"
"surdeus.su/core/cli/mtool"
)
var (
delim rune
delim = '\n'
Tool = mtool.T("tmpl").Func(Run).Desc(
"substitute the inputted runes with the specified words",
).Usage(
@ -17,17 +18,24 @@ var (
)
)
func Run(flags *mtool.Flags){
CustomRun(os.Stdin, os.Stdout, flags)
}
func CustomRun(
input io.Reader,
output io.Writer,
flags *mtool.Flags,
) {
var (
chrsString string
charsString string
chrs []rune
skip bool
)
flags.StringVar(
&chrsString,
&charsString,
"c",
"0123456789",
"character set for substitution",
@ -41,7 +49,6 @@ func Run(flags *mtool.Flags) {
"skip and do not substitute not fitting chars",
)
delim = '\n'
args := flags.Parse()
/*if len(args)<2 || len(args[0]) != len(args)-1 {
@ -49,7 +56,7 @@ func Run(flags *mtool.Flags) {
os.Exit(1)
}*/
chrs = []rune(chrsString)
chrs = []rune(charsString)
//args = args[1:]
tmpl := make(map[rune] string)
@ -57,7 +64,7 @@ func Run(flags *mtool.Flags) {
tmpl[rune(chrs[i])] = s
}
r := bufio.NewReader(os.Stdin)
r := bufio.NewReader(input)
for{
s, e := r.ReadString(byte(delim))
if e==io.EOF {
@ -71,6 +78,6 @@ func Run(flags *mtool.Flags) {
}
fmt.Printf("%s", s)
}
fmt.Printf("%s", string(delim))
fmt.Fprintf(output, "%s", string(delim))
}
}