"echo:Simplified."

This commit is contained in:
jienfak 2020-01-06 12:28:48 +05:00
parent 3d0fc73832
commit 1aa0c5d75a

View file

@ -2,74 +2,20 @@
package echo
import (
"os"
"fmt"
"flag"
"strings"
)
type Echoer struct {
out *os.File
backslashMap map[string] string
}
func (e Echoer)BackslashInterpret(str string) string {
for k, v := range e.backslashMap {
str = strings.ReplaceAll(str, k, v)
}
return str
}
func (e Echoer)Echo(str string) {
e.out.Write( []byte(str[:len(str)]) )
}
func New(out *os.File, backslashMap map[string] string) *Echoer {
e := new(Echoer)
e.out = out
e.backslashMap = backslashMap
return e
}
func Run(args []string) int {
var(
newLineStr string
newLineFlag bool
joinStr string
joinStrsFlag bool
backslashSeqFlag bool
)
seqs := map[string] string {
"\\\\" : "\\",
"\\a" : "\a",
"\\b" : "\b",
/*"\\c" : "\c",
"\\e" : "\e",*/
"\\f" : "\f",
"\\n" : "\n",
"\\r" : "\r",
"\\t" : "\t",
"\\v" : "\v",
}
e := New(os.Stdout, seqs)
flagSet := flag.NewFlagSet(args[0], flag.ExitOnError)
flagSet.BoolVar(&newLineFlag, "n", false,
"Don't add new line character('-N' is lower priority).")
flagSet.StringVar(&newLineStr, "N", "\n", "Use this instead new line character.")
flagSet.BoolVar(&joinStrsFlag, "j", false, "Join strings('-J' is lower priority).")
flagSet.StringVar(&joinStr, "J", " ", "Use instead of space as separator.")
flagSet.BoolVar(&backslashSeqFlag, "e", false, "Interpret backslash special terminal characters(Characters from join options will be interpreted too).")
flagSet.Parse(args[1:])
if newLineFlag {
newLineStr = ""
args = args[1:]
status := 0
l := len(args) - 1
for i, s := range args {
fmt.Print(s)
if i!=l { fmt.Print(" ") }
}
if joinStrsFlag {
joinStr = ""
}
printStr := strings.Join(flagSet.Args(), joinStr) +newLineStr
if backslashSeqFlag {
printStr = e.BackslashInterpret(printStr)
}
e.Echo(printStr)
return 0
fmt.Print("\n")
return status
}