quote: Simple implementation added.

This commit is contained in:
k1574 2020-06-08 00:51:39 +05:00
parent fc36f4db0e
commit 5b80bca4fb
2 changed files with 53 additions and 0 deletions

View file

@ -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" {

51
quote/quote.go Normal file
View file

@ -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
}