From 5b80bca4fb07e4765cebb6d189c0f28b1df771e5 Mon Sep 17 00:00:00 2001 From: k1574 Date: Mon, 8 Jun 2020 00:51:39 +0500 Subject: [PATCH] quote: Simple implementation added. --- goblin.go | 2 ++ quote/quote.go | 51 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 quote/quote.go diff --git a/goblin.go b/goblin.go index def0048..42f9b70 100644 --- a/goblin.go +++ b/goblin.go @@ -15,6 +15,7 @@ import( "github.com/k1574/goblin/yes" "github.com/k1574/goblin/date" "github.com/k1574/goblin/uniq" + "github.com/k1574/goblin/quote" ) func main() { @@ -36,6 +37,7 @@ func main() { "yes" : yes.Run, "date" : date.Run, "uniq" : uniq.Run, + "quote" : quote.Run, } if binBase := path.Base(os.Args[0]) ; binBase != "goblin" { diff --git a/quote/quote.go b/quote/quote.go new file mode 100644 index 0000000..2ed19fe --- /dev/null +++ b/quote/quote.go @@ -0,0 +1,51 @@ +package quote +/* Quote quotes string if it contains white space character. */ + +import( + "os" + "io" + "flag" + "fmt" + "unicode" + "bufio" +) + +func HasWhiteSpace(s string) bool { + for _, r := range s { + if(unicode.IsSpace(r)){ + return true + } + } + return false +} + +func Run(args []string) int { + status := 0 + arg0 := args[0] + args = args[1:] + flagSet := flag.NewFlagSet(arg0, flag.ExitOnError) + flagSet.Usage = func() { + fmt.Fprintf(os.Stderr, "Usage of %s: %s\n", arg0, arg0) + flagSet.PrintDefaults() + } + flagSet.Parse(args) + args = flagSet.Args() + + r := bufio.NewReader(os.Stdin) + for{ + l, e := r.ReadString('\n') + if e==io.EOF { + break + } + last := len(l) - 1 + if l[last] == '\n' { + l = l[:last] + } + if HasWhiteSpace(l) { + fmt.Printf("'%s'\n", l) + }else { + fmt.Println(l) + } + } + return status +}