combo/tmpl/tmpl.go

55 lines
800 B
Go
Raw Normal View History

2020-08-23 03:20:12 +03:00
package tmpl
import (
"os"
"fmt"
"bufio"
"io"
2024-09-20 22:52:53 +03:00
"surdeus.su/core/cli/mtool"
2020-08-23 03:20:12 +03:00
)
var(
delim rune
2024-01-23 18:20:00 +03:00
Tool = mtool.T("tmpl").Func(Run).Desc(
"substitute the inputted runes with the specified words",
).Usage(
"[n_utf8_chars] [n_strings]",
)
2020-08-23 03:20:12 +03:00
)
2024-01-23 18:20:00 +03:00
func Run(flags *mtool.Flags) {
2020-08-23 03:20:12 +03:00
delim = '\n'
2024-01-23 18:20:00 +03:00
args := flags.Parse()
2020-12-07 07:27:39 +03:00
2024-01-23 18:20:00 +03:00
if len(args)<2 || len(args[0]) != len(args)-1 {
flags.Usage()
os.Exit(1)
2020-08-23 03:20:12 +03:00
}
2024-01-23 18:20:00 +03:00
chrs := []rune(args[0])
args = args[1:]
2020-08-23 03:20:12 +03:00
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]
2024-01-23 18:20:00 +03:00
for _, c := range s {
2020-08-23 03:20:12 +03:00
s, ok := tmpl[c]
if !ok {
2024-01-23 18:20:00 +03:00
s = string(c)
2020-08-23 03:20:12 +03:00
}
fmt.Printf("%s", s)
}
fmt.Printf("%s", string(delim))
}
}