combo/tmpl/tmpl.go

54 lines
800 B
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_utf8_chars] [n_strings]",
)
)
func Run(flags *mtool.Flags) {
delim = '\n'
args := flags.Parse()
if len(args)<2 || len(args[0]) != len(args)-1 {
flags.Usage()
os.Exit(1)
}
chrs := []rune(args[0])
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 !ok {
s = string(c)
}
fmt.Printf("%s", s)
}
fmt.Printf("%s", string(delim))
}
}