package tmpl import ( "bufio" "fmt" "io" //"log" "os" "surdeus.su/core/cli/mtool" ) var ( delim = '\n' Tool = mtool.T("tmpl").Func(Run).Desc( "substitute the inputted runes with the specified words", ).Usage( "[n_strings]", ) ) func Run(flags *mtool.Flags) { cargs := flags.CustomArgs() if len(cargs) == 0 { CustomRun(os.Stdin, os.Stdout, flags) return } input := cargs[0].(io.Reader) output := cargs[1].(io.Writer) CustomRun(input, output, flags) } func CustomRun( input io.Reader, output io.Writer, flags *mtool.Flags, ) { var ( charsString string chrs []rune skip bool ) flags.StringVar( &charsString, "c", "0123456789", "character set for substitution", "COMBO_CHARS", ) flags.BoolVar( &skip, "skip", false, "skip and do not substitute not fitting chars", ) args := flags.Parse() /*if len(args)<2 || len(args[0]) != len(args)-1 { flags.Usage() os.Exit(1) }*/ chrs = []rune(charsString) //args = args[1:] tmpl := make(map[rune]string) for i, s := range args { tmpl[rune(chrs[i])] = s } r := bufio.NewReader(input) 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.Fprintf(output, "%s", string(delim)) } }