123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354 |
- package tmpl
- import (
- "os"
- "fmt"
- "bufio"
- "io"
- "vultras.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))
- }
- }
|