feat: added custom runner for the tmpl.
This commit is contained in:
parent
da4713a30e
commit
d90d443017
1 changed files with 19 additions and 12 deletions
31
tmpl/tmpl.go
31
tmpl/tmpl.go
|
@ -1,15 +1,16 @@
|
|||
package tmpl
|
||||
|
||||
import (
|
||||
"os"
|
||||
"fmt"
|
||||
"bufio"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
|
||||
"surdeus.su/core/cli/mtool"
|
||||
)
|
||||
|
||||
var (
|
||||
delim rune
|
||||
delim = '\n'
|
||||
Tool = mtool.T("tmpl").Func(Run).Desc(
|
||||
"substitute the inputted runes with the specified words",
|
||||
).Usage(
|
||||
|
@ -17,17 +18,24 @@ var (
|
|||
)
|
||||
)
|
||||
|
||||
func Run(flags *mtool.Flags){
|
||||
CustomRun(os.Stdin, os.Stdout, flags)
|
||||
}
|
||||
|
||||
func Run(flags *mtool.Flags) {
|
||||
func CustomRun(
|
||||
input io.Reader,
|
||||
output io.Writer,
|
||||
flags *mtool.Flags,
|
||||
) {
|
||||
|
||||
var (
|
||||
chrsString string
|
||||
charsString string
|
||||
chrs []rune
|
||||
skip bool
|
||||
)
|
||||
|
||||
flags.StringVar(
|
||||
&chrsString,
|
||||
&charsString,
|
||||
"c",
|
||||
"0123456789",
|
||||
"character set for substitution",
|
||||
|
@ -40,16 +48,15 @@ func Run(flags *mtool.Flags) {
|
|||
false,
|
||||
"skip and do not substitute not fitting chars",
|
||||
)
|
||||
|
||||
delim = '\n'
|
||||
|
||||
args := flags.Parse()
|
||||
|
||||
|
||||
/*if len(args)<2 || len(args[0]) != len(args)-1 {
|
||||
flags.Usage()
|
||||
os.Exit(1)
|
||||
}*/
|
||||
|
||||
chrs = []rune(chrsString)
|
||||
chrs = []rune(charsString)
|
||||
//args = args[1:]
|
||||
|
||||
tmpl := make(map[rune] string)
|
||||
|
@ -57,7 +64,7 @@ func Run(flags *mtool.Flags) {
|
|||
tmpl[rune(chrs[i])] = s
|
||||
}
|
||||
|
||||
r := bufio.NewReader(os.Stdin)
|
||||
r := bufio.NewReader(input)
|
||||
for{
|
||||
s, e := r.ReadString(byte(delim))
|
||||
if e==io.EOF {
|
||||
|
@ -71,6 +78,6 @@ func Run(flags *mtool.Flags) {
|
|||
}
|
||||
fmt.Printf("%s", s)
|
||||
}
|
||||
fmt.Printf("%s", string(delim))
|
||||
fmt.Fprintf(output, "%s", string(delim))
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue