Added escape characters util.

This commit is contained in:
k1574 2021-11-25 08:14:02 +05:00
parent 2ce694b77a
commit 185833af4c
2 changed files with 54 additions and 0 deletions

52
ec/run.go Normal file
View file

@ -0,0 +1,52 @@
package ec
import(
"fmt"
"io"
"os"
"bufio"
"log"
)
const(
esc = '\\'
)
var(
CharMap = map[string]string{
"n":"\n",
"b":"\b",
"r":"\r",
}
)
func
handleEscChar(rd *bufio.Reader) error {
r, _, e := rd.ReadRune()
if e != nil {
return e
}
fmt.Print(CharMap[string(r)])
return nil
}
func
Run(args []string) int {
rd := bufio.NewReader(os.Stdin)
for {
r, _, e := rd.ReadRune()
if e == io.EOF {
break;
} else if e != nil {
log.Fatal(e)
}
if r == esc {
handleEscChar(rd)
} else {
fmt.Print(string(r))
}
}
return 0
}

View file

@ -20,6 +20,7 @@ import(
"goblin/noext"
"goblin/mergelbl"
"goblin/basename"
"goblin/ec"
)
func main() {
@ -46,6 +47,7 @@ func main() {
"urlprs" : urlprs.Run,
"noext" : noext.Run,
"mergelbl" : mergelbl.Run,
"ec" : ec.Run,
}
if binBase := path.Base(os.Args[0]) ; binBase != "goblin" {