feat: added custom runner for the tmpl.

This commit is contained in:
Andrey Parhomenko 2024-09-23 01:18:05 +05:00
parent da4713a30e
commit d90d443017

View file

@ -1,15 +1,16 @@
package tmpl package tmpl
import ( import (
"os"
"fmt"
"bufio" "bufio"
"fmt"
"io" "io"
"os"
"surdeus.su/core/cli/mtool" "surdeus.su/core/cli/mtool"
) )
var ( var (
delim rune delim = '\n'
Tool = mtool.T("tmpl").Func(Run).Desc( Tool = mtool.T("tmpl").Func(Run).Desc(
"substitute the inputted runes with the specified words", "substitute the inputted runes with the specified words",
).Usage( ).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 ( var (
chrsString string charsString string
chrs []rune chrs []rune
skip bool skip bool
) )
flags.StringVar( flags.StringVar(
&chrsString, &charsString,
"c", "c",
"0123456789", "0123456789",
"character set for substitution", "character set for substitution",
@ -41,7 +49,6 @@ func Run(flags *mtool.Flags) {
"skip and do not substitute not fitting chars", "skip and do not substitute not fitting chars",
) )
delim = '\n'
args := flags.Parse() args := flags.Parse()
/*if len(args)<2 || len(args[0]) != len(args)-1 { /*if len(args)<2 || len(args[0]) != len(args)-1 {
@ -49,7 +56,7 @@ func Run(flags *mtool.Flags) {
os.Exit(1) os.Exit(1)
}*/ }*/
chrs = []rune(chrsString) chrs = []rune(charsString)
//args = args[1:] //args = args[1:]
tmpl := make(map[rune] string) tmpl := make(map[rune] string)
@ -57,7 +64,7 @@ func Run(flags *mtool.Flags) {
tmpl[rune(chrs[i])] = s tmpl[rune(chrs[i])] = s
} }
r := bufio.NewReader(os.Stdin) r := bufio.NewReader(input)
for{ for{
s, e := r.ReadString(byte(delim)) s, e := r.ReadString(byte(delim))
if e==io.EOF { if e==io.EOF {
@ -71,6 +78,6 @@ func Run(flags *mtool.Flags) {
} }
fmt.Printf("%s", s) fmt.Printf("%s", s)
} }
fmt.Printf("%s", string(delim)) fmt.Fprintf(output, "%s", string(delim))
} }
} }