-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.go
More file actions
164 lines (138 loc) · 3.05 KB
/
utils.go
File metadata and controls
164 lines (138 loc) · 3.05 KB
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package main
import (
"bytes"
"flag"
"io"
"log"
"net/http"
"net/url"
"os"
"os/exec"
"path"
"path/filepath"
"regexp"
"strings"
)
func clipboardHasImage() (Image, bool) {
cmd := exec.Command("wl-paste", "--list-types")
out, err := cmd.Output()
image := Image{}
if err != nil {
return image, false
}
for line := range bytes.SplitSeq(out, []byte("\n")) {
var imageSrc ImageSource
mimeType := ""
if strings.HasPrefix(string(line), "-moz-url") {
imageSrc = ImageSrcBrowser
mimeType = "image/*"
} else if strings.HasPrefix(string(line), "text/uri-list") {
imageSrc = ImageSrcFileSystem
mimeType = "image/*"
}
if strings.HasPrefix(string(line), "image/") {
mimeType = string(line)
}
if mimeType != "" {
return Image{
source: imageSrc,
mimeType: mimeType,
}, true
}
}
return image, false
}
func downloadImage(imgUrl string) (string, error) {
savePath := urlToCachePath(imgUrl)
// return early if the image is already in the cache
if _, err := os.Stat(savePath); err == nil {
return savePath, nil
}
r, err := http.Get(imgUrl)
if err != nil {
log.Fatal(err)
return "", err
}
defer r.Body.Close()
// save image to cache directory
if _, err := os.Stat(savePath); err != nil {
f, err := os.Create(savePath)
if err != nil {
log.Fatal(err)
return "", err
}
defer f.Close()
if _, err = io.Copy(f, r.Body); err != nil {
log.Fatal(err)
return "", err
}
}
return savePath, nil
}
func extractPathFromImgTag(imgTag string) string {
re := regexp.MustCompile(`(?i)<img[^>]+src=["']?([^"' >]+)["']?`)
matches := re.FindStringSubmatch(imgTag)
if len(matches) > 0 {
return matches[1]
}
return ""
}
func fileUrl(path string, img *Image) string {
if strings.HasPrefix(path, "file://") {
return path
}
if img != nil {
switch img.source {
case ImageSrcBrowser:
path = extractPathFromImgTag(path)
img.path = path
path = urlToCachePath(path)
}
}
url := url.URL{
Scheme: "file",
Path: filepath.ToSlash(path),
}
return url.String()
}
func urlToCachePath(imgUrl string) string {
cacheDir, _ := CacheDir()
imgUrl = strings.SplitAfter(imgUrl, "?")[0]
imgUrl = strings.TrimSuffix(imgUrl, "?")
fname := path.Base(imgUrl)
return cacheDir + "/" + fname
}
// Reverse returns a new slice with the elements of the input
// slice in reverse order.
func Reverse[T any](arr []T) []T {
n := len(arr)
reversed := make([]T, n)
for i := range n {
reversed[i] = arr[n-1-i]
}
return reversed
}
// TruncateText shortens the given text to fit within maxWidth.
// If the text exceeds maxWidth, it appends "..." (if possible).
func TruncateText(text string, maxWidth int) string {
runes := []rune(text)
if len(runes) > maxWidth {
if maxWidth > 3 {
return string(runes[:maxWidth-3]) + "..."
}
return string(runes[:maxWidth]) // No space for "..."
}
return text
}
func Clamp(v, lower, upper int) int {
return min(max(v, lower), upper)
}
func IsFlagPassed(name string) bool {
found := false
flag.Visit(func(f *flag.Flag) {
if f.Name == name {
found = true
}
})
return found
}