This commit is contained in:
k1574 2020-08-23 05:20:12 +05:00
commit 71adfdbf45
3 changed files with 104 additions and 0 deletions

38
main.go Normal file
View file

@ -0,0 +1,38 @@
package main
import(
"fmt"
"os"
"github.com/k1574/utf8tmpl/tmpl"
)
func main() {
var(
utilName string
args []string
)
utilsMap := map[string] interface{} {
"tmpl" : tmpl.Run,
}
if len(os.Args)<2 {
for k, _ := range utilsMap {
fmt.Printf("%s\n", k)
}
os.Exit(0)
} else {
utilName = os.Args[1]
args = os.Args[1:]
}
if _, ok := utilsMap[utilName] ; !ok {
fmt.Printf("%s: %s: no such util\n", os.Args[0], utilName )
os.Exit(1)
}
status := utilsMap[utilName].(func([]string) int )(args)
os.Exit(status)
}

4
readme Normal file
View file

@ -0,0 +1,4 @@
# utf8tmpl
## Description
Set of utils to implement templates based on UTF-8 characters.

62
tmpl/tmpl.go Normal file
View file

@ -0,0 +1,62 @@
package tmpl
import (
"os"
"fmt"
"bufio"
"io"
)
var(
arg0 string
delim rune
status int
)
func
usage() {
fmt.Fprintf(os.Stderr, "usage: %s [n_utf8_chars] [n_strings]\n", arg0)
os.Exit(1)
}
func
Run(args []string) int {
status = 0
delim = '\n'
arg0 = args[0]
if len(args[1]) != len(args)-2 {
usage()
}
chrs := []rune(args[1])
args = args[2:]
tmpl := make(map[rune] string)
for i, s := range args {
tmpl[rune(chrs[i])] = s
}
r := bufio.NewReader(os.Stdin)
for{
s, e := r.ReadString(byte(delim))
if e==io.EOF {
break
}
s = s[:len(s)-1]
for _, c := range []rune(s) {
s, ok := tmpl[c]
if !ok {
fmt.Fprintf(os.Stderr, "%s: '%s': no such character in template string\n",
arg0, string(c) )
continue
}
fmt.Printf("%s", s)
}
fmt.Printf("%s", string(delim))
}
return status
}