summaryrefslogtreecommitdiff
path: root/sixkcd.go
diff options
context:
space:
mode:
authorBenjamin Chausse <benjamin@chausse.xyz>2024-06-06 01:25:11 -0400
committerBenjamin Chausse <benjamin@chausse.xyz>2024-06-06 01:25:11 -0400
commit106597566c8d9b901aa5cad75767340386e00cd8 (patch)
treeeaa607da823bc4c5c4a4ff0610c7b17ca3811c06 /sixkcd.go
parentf2aa28deb420987e6183086d66b2c2c43a98a256 (diff)
Build with makefile + raw mode
Diffstat (limited to 'sixkcd.go')
-rw-r--r--sixkcd.go37
1 files changed, 27 insertions, 10 deletions
diff --git a/sixkcd.go b/sixkcd.go
index efb65a7..b9c13d7 100644
--- a/sixkcd.go
+++ b/sixkcd.go
@@ -22,7 +22,7 @@ const (
target = "info.0.json"
)
-type Xkcd struct {
+type Comic struct {
Month string
Num int
Link string
@@ -40,12 +40,23 @@ func main() {
app := &cli.App{
Name: "siXKCD",
Usage: "Sixel viewer/fetcher for XKCD Comics",
+ Authors: []*cli.Author{
+ {
+ Name: "Benjamin Chausse",
+ Email: "benjamin@chausse.xyz",
+ },
+ },
Flags: []cli.Flag{
&cli.StringFlag{
Name: "id",
Aliases: []string{"i"},
Usage: "Specify which comic `ID` to fetch.\nA value of 0 will fetch the latest comic (default: random)",
},
+ &cli.BoolFlag{
+ Name: "raw",
+ Aliases: []string{"r"},
+ Usage: "Output only the sixel image without the title or the alternate caption.",
+ },
},
Action: func(ctx *cli.Context) error {
comic := fetchComic("latest")
@@ -62,11 +73,17 @@ func main() {
}
var img image.Image = fetchImg(comic.Img)
- fmt.Println("Title: ", comic.Title)
encoder := sixel.NewEncoder(os.Stdout)
- _ = encoder.Encode(img)
- if comic.Alt != "" {
- fmt.Println(comic.Alt)
+
+ switch ctx.Bool("raw") {
+ case true:
+ _ = encoder.Encode(img)
+ default:
+ fmt.Println("Title: ", comic.Title)
+ _ = encoder.Encode(img)
+ if comic.Alt != "" {
+ fmt.Println(comic.Alt)
+ }
}
@@ -79,7 +96,7 @@ func main() {
}
}
-func fetchComic(id string) Xkcd {
+func fetchComic(id string) Comic {
var res *http.Response
var err error
@@ -89,23 +106,23 @@ func fetchComic(id string) Xkcd {
} else {
res, err = http.Get(hostname+"/"+id+"/"+target)
}
+ defer res.Body.Close()
if err != nil {
log.Fatal(err)
}
content, err := io.ReadAll(res.Body)
- res.Body.Close()
if err != nil {
log.Fatal(err)
}
- today := Xkcd{}
- err = json.Unmarshal(content, &today)
+ comic := Comic{}
+ err = json.Unmarshal(content, &comic)
if err != nil {
log.Fatal(err)
}
- return today
+ return comic
}
func fetchImg(url string) image.Image {