From 71adfdbf4562e5ff0ead948f4ea5c588a88c0ecb Mon Sep 17 00:00:00 2001 From: k1574 Date: Sun, 23 Aug 2020 05:20:12 +0500 Subject: [PATCH] Init. --- main.go | 38 ++++++++++++++++++++++++++++++++ readme | 4 ++++ tmpl/tmpl.go | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 104 insertions(+) create mode 100644 main.go create mode 100644 readme create mode 100644 tmpl/tmpl.go diff --git a/main.go b/main.go new file mode 100644 index 0000000..9a4885f --- /dev/null +++ b/main.go @@ -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) +} diff --git a/readme b/readme new file mode 100644 index 0000000..ac83819 --- /dev/null +++ b/readme @@ -0,0 +1,4 @@ +# utf8tmpl + +## Description +Set of utils to implement templates based on UTF-8 characters. diff --git a/tmpl/tmpl.go b/tmpl/tmpl.go new file mode 100644 index 0000000..3eead9f --- /dev/null +++ b/tmpl/tmpl.go @@ -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 +}