combo/tmpl/tmpl.go

77 lines
1 KiB
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 (
2020-08-23 03:20:12 +03:00
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_strings]",
2024-01-23 18:20:00 +03:00
)
2020-08-23 03:20:12 +03:00
)
2024-01-23 18:20:00 +03:00
func Run(flags *mtool.Flags) {
var (
chrsString string
chrs []rune
skip bool
)
flags.StringVar(
&chrsString,
"c",
"0123456789",
"character set for substitution",
"COMBO_CHARS",
)
flags.BoolVar(
&skip,
"skip",
false,
"skip and do not substitute not fitting chars",
)
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
/*if len(args)<2 || len(args[0]) != len(args)-1 {
2024-01-23 18:20:00 +03:00
flags.Usage()
os.Exit(1)
}*/
2020-08-23 03:20:12 +03:00
chrs = []rune(chrsString)
//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 !skip && !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))
}
}