From d90d44301790ed784fe825fb52ab97ede05c069c Mon Sep 17 00:00:00 2001 From: surdeus Date: Mon, 23 Sep 2024 01:18:05 +0500 Subject: [PATCH] feat: added custom runner for the tmpl. --- tmpl/tmpl.go | 31 +++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/tmpl/tmpl.go b/tmpl/tmpl.go index 6021136..0beb07c 100644 --- a/tmpl/tmpl.go +++ b/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)) } }