From 106597566c8d9b901aa5cad75767340386e00cd8 Mon Sep 17 00:00:00 2001 From: Benjamin Chausse Date: Thu, 6 Jun 2024 01:25:11 -0400 Subject: Build with makefile + raw mode --- Makefile | 24 ++++++++++++++++++++++++ README.org | 20 ++++---------------- demo.png | Bin 144680 -> 845458 bytes sixkcd.1 | 27 +++++++++++++++++++++++++++ sixkcd.go | 37 +++++++++++++++++++++++++++---------- 5 files changed, 82 insertions(+), 26 deletions(-) create mode 100644 Makefile create mode 100644 sixkcd.1 diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..ff38c26 --- /dev/null +++ b/Makefile @@ -0,0 +1,24 @@ +# sixkcd - Fetch XKCD comics from the command line +# See LICENSE file for copyright and license details. + +PREFIX=/usr/local +MANPREFIX=$(PREFIX)/share/man + +all: + go mod download + go build -o sixkcd ./sixkcd.go + +install: all + mkdir -p $(PREFIX)/bin + mkdir -p $(MANPREFIX)/man1 + cp -f sixkcd $(PREFIX)/bin + cp -f sixkcd.1 $(MANPREFIX)/man1 + +clean: + rm -f ./sixkcd + +uninstall: + rm -f $(PREFIX)/bin/sixkcd + rm -f $(MANPREFIX)/man1/sixkcd.1 + +.PHONY: all install clean uninstall help diff --git a/README.org b/README.org index 152bfcb..ddf4b37 100644 --- a/README.org +++ b/README.org @@ -17,21 +17,9 @@ already does: ** Installation -I'll probably create a =Makefile= in the future -to streamline the process. In the meantime, you -can build the binary in the following way. - -#+begin_src sh -# Creating the binary -go get download -go build -o sixkcd ./sixkcd.go -# Installing it on your system -sudo cp sixkcd /usr/local/bin -#+end_src - -Uninstalling is as simple as: - #+begin_src sh -sudo rm /usr/local/bin/sixkcd +make +sudo make install +# To uninstall: +sudo make uninstall #+end_src - diff --git a/demo.png b/demo.png index 38bff5c..98e6f02 100644 Binary files a/demo.png and b/demo.png differ diff --git a/sixkcd.1 b/sixkcd.1 new file mode 100644 index 0000000..759f085 --- /dev/null +++ b/sixkcd.1 @@ -0,0 +1,27 @@ +.TH siXKCD 1 "June 2024" "1.0" "siXKCD Manual" +.SH NAME +siXKCD \- Sixel viewer/fetcher for XKCD Comics +.SH SYNOPSIS +.B siXKCD +[\fBglobal options\fR] \fBcommand\fR [\fBcommand options\fR] +.SH DESCRIPTION +.B siXKCD +is a tool to view and fetch XKCD comics in sixel format. +.SH AUTHOR +.B Benjamin Chausse +.br + +.SH COMMANDS +.TP +.B help, h +Shows a list of commands or help for one command. +.SH GLOBAL OPTIONS +.TP +.BR \-\-id\ ID ,\ \-i\ ID +Specify which comic ID to fetch. A value of 0 will fetch the latest comic (default: random). +.TP +.BR \-\-raw ,\ \-r +Output only the sixel image without the title or the alternate caption. (default: false) +.TP +.BR \-\-help ,\ \-h +Show help. 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 { -- cgit v1.2.3