added -a/--all, default to top entry
This commit is contained in:
parent
0da80226c0
commit
6d59e99cd2
38
main.go
38
main.go
@ -2,10 +2,10 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"go/doc"
|
"go/doc"
|
||||||
"log"
|
"log"
|
||||||
"math"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"sort"
|
"sort"
|
||||||
@ -15,6 +15,13 @@ import (
|
|||||||
const url = "http://api.urbandictionary.com/v0/define?term="
|
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"
|
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 {
|
type response struct {
|
||||||
Results []result `json:"list"`
|
Results []result `json:"list"`
|
||||||
}
|
}
|
||||||
@ -43,13 +50,13 @@ func (bv ByVotes) Less(i, j int) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
if len(os.Args) < 2 {
|
flag.Parse()
|
||||||
|
if len(flag.Args()) < 1 {
|
||||||
fmt.Fprintf(os.Stderr, "%s\n", usage)
|
fmt.Fprintf(os.Stderr, "%s\n", usage)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
words := os.Args[1:]
|
u := fmt.Sprintf("%s%s", url, strings.Join(flag.Args(), "+"))
|
||||||
u := fmt.Sprintf("%s%s", url, strings.Join(words, "+"))
|
|
||||||
|
|
||||||
resp, err := http.Get(u)
|
resp, err := http.Get(u)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -64,18 +71,29 @@ func main() {
|
|||||||
|
|
||||||
sort.Sort(ByVotes(data.Results))
|
sort.Sort(ByVotes(data.Results))
|
||||||
|
|
||||||
|
if !all {
|
||||||
|
if len(data.Results) > 0 {
|
||||||
|
data.Results = data.Results[:1]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
for i, res := range data.Results {
|
for i, res := range data.Results {
|
||||||
|
if i > 0 {
|
||||||
|
fmt.Printf("\n\n")
|
||||||
|
}
|
||||||
|
if all {
|
||||||
fmt.Printf(
|
fmt.Printf(
|
||||||
"%d:\n",
|
"Definition %d:\n",
|
||||||
i,
|
i+1,
|
||||||
)
|
)
|
||||||
doc.ToText(os.Stdout, res.Definition, "\t", "", 80)
|
}
|
||||||
|
doc.ToText(os.Stdout, res.Definition, "", "", 80)
|
||||||
fmt.Printf(
|
fmt.Printf(
|
||||||
"\n\n\texample:\n\t%s\n",
|
"\n\n Example:\n %s\n",
|
||||||
strings.Replace(res.Example, "\r\n", "\n\t", -1),
|
strings.Replace(res.Example, "\r\n", "\n ", -1),
|
||||||
)
|
)
|
||||||
fmt.Printf(
|
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.Link,
|
||||||
res.Up,
|
res.Up,
|
||||||
res.Down,
|
res.Down,
|
||||||
|
Loading…
Reference in New Issue
Block a user