combo/tmpl/tmpl.go

63 lines
870 B
Go
Raw Normal View History

2020-08-23 03:20:12 +03:00
package tmpl
import (
"os"
"fmt"
"bufio"
"io"
)
var(
arg0 string
delim rune
status int
)
func
usage() {
fmt.Fprintf(os.Stderr, "usage: %s [n_utf8_chars] [n_strings]\n", arg0)
os.Exit(1)
}
func
Run(args []string) int {
status = 0
delim = '\n'
arg0 = args[0]
2020-12-07 07:27:39 +03:00
if len(args)<3 || len(args[1]) != len(args)-2 {
2020-08-23 03:20:12 +03:00
usage()
}
chrs := []rune(args[1])
args = args[2:]
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 []rune(s) {
s, ok := tmpl[c]
if !ok {
/*fmt.Fprintf(os.Stderr, "%s: '%s': no such character in template string\n",
arg0, string(c) )*/
s = string(c)
2020-08-23 03:20:12 +03:00
}
fmt.Printf("%s", s)
}
fmt.Printf("%s", string(delim))
}
return status
}