mirror of
https://github.com/celogeek/imgcat.git
synced 2025-05-24 15:52:38 +02:00
init project
This commit is contained in:
commit
c399c86070
70
main.go
Normal file
70
main.go
Normal file
@ -0,0 +1,70 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"flag"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Options struct {
|
||||
url string
|
||||
height int
|
||||
}
|
||||
|
||||
func PrintHeader(size int64, height int) {
|
||||
fmt.Printf("\033]1337")
|
||||
fmt.Printf(";File=")
|
||||
fmt.Printf(";inline=1")
|
||||
fmt.Printf(";size=%d;", size)
|
||||
if height > 0 {
|
||||
fmt.Printf(";height=%d", height)
|
||||
}
|
||||
fmt.Printf(":")
|
||||
}
|
||||
|
||||
func PrintFooter() {
|
||||
fmt.Println("\a")
|
||||
}
|
||||
|
||||
func PrintImg(img io.ReadCloser) {
|
||||
encoder := base64.NewEncoder(base64.StdEncoding, os.Stdout)
|
||||
defer img.Close()
|
||||
defer encoder.Close()
|
||||
buf := make([]byte, 8192)
|
||||
for {
|
||||
n, err := img.Read(buf)
|
||||
if err == io.EOF {
|
||||
break
|
||||
}
|
||||
encoder.Write(buf[:n])
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
var opts = &Options{}
|
||||
flag.StringVar(&opts.url, "url", "", "url of the jpg")
|
||||
flag.IntVar(&opts.height, "height", 0, "maximum height in lines")
|
||||
flag.Parse()
|
||||
|
||||
if !(strings.HasPrefix(opts.url, "http://") || strings.HasPrefix(opts.url, "https://")) {
|
||||
log.Fatalln("url doesn't start with http")
|
||||
}
|
||||
|
||||
resp, err := http.Get(opts.url)
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
if resp.StatusCode != 200 {
|
||||
log.Fatalln(fmt.Sprintf("Issue to get img, status = %d", resp.StatusCode))
|
||||
}
|
||||
|
||||
PrintHeader(resp.ContentLength, opts.height)
|
||||
PrintImg(resp.Body)
|
||||
PrintFooter()
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user