combo/tmpl/tmpl.go

76 lines
1 KiB
Go

package tmpl
import (
"os"
"fmt"
"bufio"
"io"
"surdeus.su/core/cli/mtool"
)
var (
delim rune
Tool = mtool.T("tmpl").Func(Run).Desc(
"substitute the inputted runes with the specified words",
).Usage(
"[n_strings]",
)
)
func Run(flags *mtool.Flags) {
var (
chrsString string
chrs []rune
skip bool
)
flags.StringVar(
&chrsString,
"c",
"0123456789",
"character set for substitution",
"COMBO_CHARS",
)
flags.BoolVar(
&skip,
"skip",
false,
"skip and do not substitute not fitting chars",
)
delim = '\n'
args := flags.Parse()
/*if len(args)<2 || len(args[0]) != len(args)-1 {
flags.Usage()
os.Exit(1)
}*/
chrs = []rune(chrsString)
//args = args[1:]
tmpl := make(map[rune] string)
for i, s := range args {
tmpl[rune(chrs[i])] = s
}
r := bufio.NewReader(os.Stdin)
for{
s, e := r.ReadString(byte(delim))
if e==io.EOF {
break
}
s = s[:len(s)-1]
for _, c := range s {
s, ok := tmpl[c]
if !skip && !ok {
s = string(c)
}
fmt.Printf("%s", s)
}
fmt.Printf("%s", string(delim))
}
}