added -a/--all, default to top entry
This commit is contained in:
parent
0da80226c0
commit
6d59e99cd2
42
main.go
42
main.go
@ -2,10 +2,10 @@ package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"flag"
|
||||
"fmt"
|
||||
"go/doc"
|
||||
"log"
|
||||
"math"
|
||||
"net/http"
|
||||
"os"
|
||||
"sort"
|
||||
@ -15,6 +15,13 @@ import (
|
||||
const url = "http://api.urbandictionary.com/v0/define?term="
|
||||
const usage = "ud: confirm that the term you're thinking of has already been added to urban dictionary\n\nusage: ud <term>\n"
|
||||
|
||||
var all bool
|
||||
|
||||
func init() {
|
||||
flag.BoolVar(&all, "all", false, "more than one")
|
||||
flag.BoolVar(&all, "a", false, "more than one")
|
||||
}
|
||||
|
||||
type response struct {
|
||||
Results []result `json:"list"`
|
||||
}
|
||||
@ -43,13 +50,13 @@ func (bv ByVotes) Less(i, j int) bool {
|
||||
}
|
||||
|
||||
func main() {
|
||||
if len(os.Args) < 2 {
|
||||
flag.Parse()
|
||||
if len(flag.Args()) < 1 {
|
||||
fmt.Fprintf(os.Stderr, "%s\n", usage)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
words := os.Args[1:]
|
||||
u := fmt.Sprintf("%s%s", url, strings.Join(words, "+"))
|
||||
u := fmt.Sprintf("%s%s", url, strings.Join(flag.Args(), "+"))
|
||||
|
||||
resp, err := http.Get(u)
|
||||
if err != nil {
|
||||
@ -64,18 +71,29 @@ func main() {
|
||||
|
||||
sort.Sort(ByVotes(data.Results))
|
||||
|
||||
if !all {
|
||||
if len(data.Results) > 0 {
|
||||
data.Results = data.Results[:1]
|
||||
}
|
||||
}
|
||||
|
||||
for i, res := range data.Results {
|
||||
if i > 0 {
|
||||
fmt.Printf("\n\n")
|
||||
}
|
||||
if all {
|
||||
fmt.Printf(
|
||||
"Definition %d:\n",
|
||||
i+1,
|
||||
)
|
||||
}
|
||||
doc.ToText(os.Stdout, res.Definition, "", "", 80)
|
||||
fmt.Printf(
|
||||
"%d:\n",
|
||||
i,
|
||||
)
|
||||
doc.ToText(os.Stdout, res.Definition, "\t", "", 80)
|
||||
fmt.Printf(
|
||||
"\n\n\texample:\n\t%s\n",
|
||||
strings.Replace(res.Example, "\r\n", "\n\t", -1),
|
||||
"\n\n Example:\n %s\n",
|
||||
strings.Replace(res.Example, "\r\n", "\n ", -1),
|
||||
)
|
||||
fmt.Printf(
|
||||
"\n\t%s\n\t^ %d, v %d\n\t%s\n",
|
||||
"\nSource: %s\nUp: %d, Down: %d\nAuthor: %s\n",
|
||||
res.Link,
|
||||
res.Up,
|
||||
res.Down,
|
||||
|
Loading…
Reference in New Issue
Block a user