1
0
forked from crony/udict

Compare commits

...

6 Commits

4 changed files with 112 additions and 12 deletions

11
go.mod Normal file
View File

@ -0,0 +1,11 @@
module crony/udict
go 1.23.4
require github.com/fatih/color v1.18.0
require (
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
golang.org/x/sys v0.25.0 // indirect
)

11
go.sum Normal file
View File

@ -0,0 +1,11 @@
github.com/fatih/color v1.18.0 h1:S8gINlzdQ840/4pfAwic/ZE0djQEH3wM94VfqLTZcOM=
github.com/fatih/color v1.18.0/go.mod h1:4FelSpRwEGDpQ12mAdzqdOukCy4u8WUtOY6lkT/6HfU=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.25.0 h1:r+8e+loiHxRqhXVl6ML1nO3l1+oFoWbnlu2Ehimmi34=
golang.org/x/sys v0.25.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=

90
main.go Normal file
View File

@ -0,0 +1,90 @@
package main
import (
"bufio"
"encoding/json"
"flag"
"fmt"
"io"
"net/http"
"os"
"strings"
"time"
"github.com/fatih/color"
)
// Define json structure holder
type Word struct {
List []struct {
Definition string `json:"definition"`
Permalink string `json:"permalink"`
ThumbsUp int `json:"thumbs_up"`
Author string `json:"author"`
Word string `json:"word"`
Defid int `json:"defid"`
CurrentVote string `json:"current_vote"`
WrittenOn time.Time `json:"written_on"`
Example string `json:"example"`
ThumbsDown int `json:"thumbs_down"`
} `json:"list"`
}
func main() {
// get how many result we wan't for the specific word
num := flag.Int("n", 1, "Amount of results to print out")
flag.Parse()
var unknownWord string
// Check if word was provided as cli argument, if not ask user to input it
if flag.NArg() == 0 {
fmt.Print("Enter word the check meaning off: ")
scanner := bufio.NewScanner(os.Stdin)
scanner.Scan()
err := scanner.Err()
if err != nil {
fmt.Println("Error reading user input")
os.Exit(1)
}
unknownWord = strings.ReplaceAll(scanner.Text(), " ", "%20")
fmt.Println()
} else {
unknownWord = strings.Join(flag.Args(), "%20")
}
// Get response from url
res, err := http.Get("https://api.urbandictionary.com/v0/define?term=" + unknownWord)
if err != nil {
fmt.Printf("error making http request %s", err)
os.Exit(1)
}
// Read the body of message
defer res.Body.Close()
body, err := io.ReadAll(res.Body)
if err != nil {
fmt.Printf("error reading the body %s", err)
os.Exit(1)
}
// Read the json and parse it
var word Word
if err := json.Unmarshal(body, &word); err != nil {
fmt.Println("Can not unmarshal json")
os.Exit(1)
}
// Print out the needed amount of results, or exit if not enough results
for i := 0; i < *num; i++ {
if i == len(word.List) {
break
}
color.Cyan(word.List[i].Word)
fmt.Println(word.List[i].Definition)
if *num > 1 {
fmt.Println()
}
}
}

12
udict
View File

@ -1,12 +0,0 @@
#!/bin/sh
[ $# -eq 0 ] && printf '%s\n' "Usage:
udict some term" && exit 1
term="$@"
term="$(printf '%s\n' "${term}" | sed 's/ /\\/g' )"
curl -s "https://api.urbandictionary.com/v0/define?term=${term}" |
jq -r '.list[0].definition' |
sed 's/\[/\o033[1m/g;s/\]/\o033[0m/g'