-
Notifications
You must be signed in to change notification settings - Fork 6
/
imgls.go
101 lines (87 loc) · 1.89 KB
/
imgls.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package main
import (
"encoding/base64"
"flag"
"fmt"
"io"
"io/ioutil"
"os"
"strings"
)
var (
width = flag.String("width", "", "width (e.g. 100px, 10%, or auto)")
height = flag.String("height", "", "height (e.g. 100px, 10%, or auto)")
size = flag.String("size", "", "width,height in pixels (e.g. 1024px,768px or 3,3)")
)
// func main() {
// flag.Usage = usage
// flag.Parse()
// for _, pattern := range flag.Args() {
// matches, err := filepath.Glob(pattern)
// if err != nil {
// log.Fatal(err)
// }
// for _, filename := range matches {
// // Skip errors and directories
// if fi, err := os.Stat(filename); err != nil || fi.IsDir() {
// continue
// }
// f, err := os.Open(filename)
// if err != nil {
// log.Fatal(err)
// }
// defer f.Close()
// if err := display(filename, f); err != nil {
// log.Fatal(err)
// }
// }
// }
// }
func display(filename string, r io.Reader) error {
data, err := ioutil.ReadAll(r)
if err != nil {
return err
}
b64filename := base64.StdEncoding.EncodeToString([]byte(filename))
width, height := widthAndHeight()
if width == "" && height == "" {
width = "3"
height = "3"
}
fmt.Print("\033]1337;")
fmt.Printf("File=inline=1;preserveAspectRatio=1;name='%s'", b64filename)
if width != "" || height != "" {
if width != "" {
fmt.Printf(";width=%s", width)
}
if height != "" {
fmt.Printf(";height=%s", height)
}
}
fmt.Print(":")
fmt.Printf("%s", base64.StdEncoding.EncodeToString(data))
fmt.Print("\a")
fmt.Printf("\033[A%s\n", filename)
return nil
}
func widthAndHeight() (w, h string) {
if *width != "" {
w = *width
}
if *height != "" {
h = *height
}
if *size != "" {
sp := strings.SplitN(*size, ",", -1)
if len(sp) == 2 {
w = sp[0]
h = sp[1]
}
}
return
}
func usage() {
fmt.Fprint(os.Stderr, "usage: imgls [flags] filename\n")
flag.PrintDefaults()
os.Exit(2)
}