tmpl.go 800 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. package tmpl
  2. import (
  3. "os"
  4. "fmt"
  5. "bufio"
  6. "io"
  7. "vultras.su/core/cli/mtool"
  8. )
  9. var(
  10. delim rune
  11. Tool = mtool.T("tmpl").Func(Run).Desc(
  12. "substitute the inputted runes with the specified words",
  13. ).Usage(
  14. "[n_utf8_chars] [n_strings]",
  15. )
  16. )
  17. func Run(flags *mtool.Flags) {
  18. delim = '\n'
  19. args := flags.Parse()
  20. if len(args)<2 || len(args[0]) != len(args)-1 {
  21. flags.Usage()
  22. os.Exit(1)
  23. }
  24. chrs := []rune(args[0])
  25. args = args[1:]
  26. tmpl := make(map[rune] string)
  27. for i, s := range args {
  28. tmpl[rune(chrs[i])] = s
  29. }
  30. r := bufio.NewReader(os.Stdin)
  31. for{
  32. s, e := r.ReadString(byte(delim))
  33. if e==io.EOF {
  34. break
  35. }
  36. s = s[:len(s)-1]
  37. for _, c := range s {
  38. s, ok := tmpl[c]
  39. if !ok {
  40. s = string(c)
  41. }
  42. fmt.Printf("%s", s)
  43. }
  44. fmt.Printf("%s", string(delim))
  45. }
  46. }