From 59c12cb89b8ed3ac5bb70d0a55b2bbbe34b24777 Mon Sep 17 00:00:00 2001 From: Brian Buller Date: Thu, 28 Jun 2018 14:59:17 -0500 Subject: [PATCH] Make better for CLI usage --- main.go | 97 ++++++++++++++++++--------------------------------------- 1 file changed, 31 insertions(+), 66 deletions(-) diff --git a/main.go b/main.go index bf501a8..db689ff 100644 --- a/main.go +++ b/main.go @@ -8,80 +8,45 @@ import ( "github.com/kyokomi/emoji" ) -var emoticons map[string]string - func main() { - initEmoticons() - // We default to "shrug" - var done bool + if len(os.Args) < 2 || strings.HasSuffix(os.Args[1], "-help") { + showUsage() + } + + var getApprox bool var which string - if len(os.Args) == 2 { - which = os.Args[1] - if which == "help" { - showHelp() - } else if which == "help-emoji" { - allEmoji := emoji.CodeMap() - fmt.Println("== Supported Emoji ==") - for k, v := range allEmoji { - fmt.Println(k + " " + v) - } - os.Exit(0) - } - // it might be a one-word emoji request - allEmoji := emoji.CodeMap() - if _, done = allEmoji[which]; done { - tst1 := ":" + which + ":" - tst2 := emoji.Sprint(tst1) - if tst1 != tst2 { - fmt.Println(tst2) - done = true - } + var matchWhich string + for _, v := range os.Args { + if v == "-a" { + getApprox = true } else { - // Search for any emoji that contain this string - var cnt int - for k, v := range allEmoji { - if strings.Contains(k, which) { - fmt.Println(k + " " + v) - cnt++ - } - } - if cnt > 0 { - done = true - } else { - fmt.Println("No emoji for " + which + " found.") + which = v + } + } + + allEmoji := emoji.CodeMap() + if !strings.HasPrefix(which, ":") && !strings.HasSuffix(which, ":") { + matchWhich = ":" + which + ":" + } + if v, ok := allEmoji[matchWhich]; ok { + fmt.Println(v) + os.Exit(0) + } else if getApprox { + for k, v := range allEmoji { + if strings.Contains(k, which) { + fmt.Println(v) + os.Exit(0) } } } - if !done { - var i string - if i, done = emoticons[which]; done { - fmt.Println(i) - } - } - if !done { - showHelp() - } + os.Exit(1) } -func showHelp() { - fmt.Println("There are a *ton* of emoji supported, to list them, do \"help-emoji\"") - for k, v := range emoticons { - fmt.Println(k, v) +func showUsage() { + allEmoji := emoji.CodeMap() + fmt.Println("== Supported Emoji ==") + for k, v := range allEmoji { + fmt.Println(k + " " + v) } os.Exit(0) } - -func initEmoticons() { - emoticons = make(map[string]string) - emoticons["ascii-bat"] = "~°·_·°~" - emoticons["ascii-bomb"] = "●~*" - emoticons["ascii-fish"] = ">°)))彡" - emoticons["ascii-flip"] = "(╯°□°)╯︵ ┻━┻" - emoticons["ascii-lenny"] = "( ͡° ͜ʖ ͡°)" - emoticons["ascii-octopus"] = "<コ:彡" - emoticons["ascii-shrug"] = "¯\\_(ツ)_/¯" - emoticons["ascii-smirk"] = "( ͡° ͜ʖ ͡°)" - emoticons["ascii-snake"] = "~>°)~~~" - emoticons["ascii-star"] = "☆彡" - emoticons["ascii-tadpole"] = "(°°)~" -}