-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathmain.go
314 lines (291 loc) · 8.1 KB
/
main.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
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
// License: GPLv3 Copyright: 2022, Kovid Goyal, <kovid at kovidgoyal.net>
package icat
import (
"fmt"
"os"
"runtime"
"strconv"
"strings"
"sync/atomic"
"time"
"kitty/tools/cli"
"kitty/tools/tty"
"kitty/tools/tui"
"kitty/tools/tui/graphics"
"kitty/tools/utils"
"kitty/tools/utils/images"
"kitty/tools/utils/style"
"golang.org/x/sys/unix"
)
var _ = fmt.Print
type Place struct {
width, height, left, top int
}
var opts *Options
var place *Place
var z_index int32
var remove_alpha *images.NRGBColor
var flip, flop bool
type transfer_mode int
const (
unknown transfer_mode = iota
unsupported
supported
)
var transfer_by_file, transfer_by_memory transfer_mode
var files_channel chan input_arg
var output_channel chan *image_data
var num_of_items int
var keep_going *atomic.Bool
var screen_size *unix.Winsize
func send_output(imgd *image_data) {
output_channel <- imgd
}
func parse_mirror() (err error) {
flip = opts.Mirror == "both" || opts.Mirror == "vertical"
flop = opts.Mirror == "both" || opts.Mirror == "horizontal"
return
}
func parse_background() (err error) {
if opts.Background == "" || opts.Background == "none" {
return nil
}
col, err := style.ParseColor(opts.Background)
if err != nil {
return fmt.Errorf("Invalid value for --background: %w", err)
}
remove_alpha = &images.NRGBColor{R: col.Red, G: col.Green, B: col.Blue}
return
}
func parse_z_index() (err error) {
val := opts.ZIndex
var origin int32
if strings.HasPrefix(val, "--") {
origin = -1073741824
val = val[1:]
}
i, err := strconv.ParseInt(val, 10, 32)
if err != nil {
return fmt.Errorf("Invalid value for --z-index with error: %w", err)
}
z_index = int32(i) + origin
return
}
func parse_place() (err error) {
if opts.Place == "" {
return nil
}
area, pos, found := strings.Cut(opts.Place, "@")
if !found {
return fmt.Errorf("Invalid --place specification: %s", opts.Place)
}
w, h, found := strings.Cut(area, "x")
if !found {
return fmt.Errorf("Invalid --place specification: %s", opts.Place)
}
l, t, found := strings.Cut(pos, "x")
if !found {
return fmt.Errorf("Invalid --place specification: %s", opts.Place)
}
place = &Place{}
place.width, err = strconv.Atoi(w)
if err != nil {
return err
}
place.height, err = strconv.Atoi(h)
if err != nil {
return err
}
place.left, err = strconv.Atoi(l)
if err != nil {
return err
}
place.top, err = strconv.Atoi(t)
if err != nil {
return err
}
return nil
}
func print_error(format string, args ...any) {
fmt.Fprintf(os.Stderr, format, args...)
fmt.Fprintln(os.Stderr)
}
func main(cmd *cli.Command, o *Options, args []string) (rc int, err error) {
opts = o
err = parse_place()
if err != nil {
return 1, err
}
err = parse_z_index()
if err != nil {
return 1, err
}
err = parse_background()
if err != nil {
return 1, err
}
err = parse_mirror()
if err != nil {
return 1, err
}
if opts.UseWindowSize == "" {
if tty.IsTerminal(os.Stdout.Fd()) {
screen_size, err = tty.GetSize(int(os.Stdout.Fd()))
} else {
t, oerr := tty.OpenControllingTerm()
if oerr != nil {
return 1, fmt.Errorf("Failed to open controlling terminal with error: %w", oerr)
}
screen_size, err = t.GetSize()
}
if err != nil {
return 1, fmt.Errorf("Failed to query terminal using TIOCGWINSZ with error: %w", err)
}
} else {
parts := strings.SplitN(opts.UseWindowSize, ",", 4)
if len(parts) != 4 {
return 1, fmt.Errorf("Invalid size specification: " + opts.UseWindowSize)
}
screen_size = &unix.Winsize{}
var t uint64
if t, err = strconv.ParseUint(parts[0], 10, 16); err != nil || t < 1 {
return 1, fmt.Errorf("Invalid size specification: %s with error: %w", opts.UseWindowSize, err)
}
screen_size.Col = uint16(t)
if t, err = strconv.ParseUint(parts[1], 10, 16); err != nil || t < 1 {
return 1, fmt.Errorf("Invalid size specification: %s with error: %w", opts.UseWindowSize, err)
}
screen_size.Row = uint16(t)
if t, err = strconv.ParseUint(parts[2], 10, 16); err != nil || t < 1 {
return 1, fmt.Errorf("Invalid size specification: %s with error: %w", opts.UseWindowSize, err)
}
screen_size.Xpixel = uint16(t)
if t, err = strconv.ParseUint(parts[3], 10, 16); err != nil || t < 1 {
return 1, fmt.Errorf("Invalid size specification: %s with error: %w", opts.UseWindowSize, err)
}
screen_size.Ypixel = uint16(t)
if screen_size.Xpixel < screen_size.Col {
return 1, fmt.Errorf("Invalid size specification: %s with error: The pixel width is smaller than the number of columns", opts.UseWindowSize)
}
if screen_size.Ypixel < screen_size.Row {
return 1, fmt.Errorf("Invalid size specification: %s with error: The pixel height is smaller than the number of rows", opts.UseWindowSize)
}
}
if opts.PrintWindowSize {
fmt.Printf("%dx%d", screen_size.Xpixel, screen_size.Ypixel)
return 0, nil
}
if opts.Clear {
cc := &graphics.GraphicsCommand{}
cc.SetAction(graphics.GRT_action_delete).SetDelete(graphics.GRT_free_visible)
if err = cc.WriteWithPayloadTo(os.Stdout, nil); err != nil {
return 1, err
}
}
if screen_size.Xpixel == 0 || screen_size.Ypixel == 0 {
return 1, fmt.Errorf("Terminal does not support reporting screen sizes in pixels, use a terminal such as kitty, WezTerm, Konsole, etc. that does.")
}
items, err := process_dirs(args...)
if err != nil {
return 1, err
}
if opts.Place != "" && len(items) > 1 {
return 1, fmt.Errorf("The --place option can only be used with a single image, not %d", len(items))
}
files_channel = make(chan input_arg, len(items))
for _, ia := range items {
files_channel <- ia
}
num_of_items = len(items)
output_channel = make(chan *image_data, 1)
keep_going = &atomic.Bool{}
keep_going.Store(true)
if !opts.DetectSupport && num_of_items > 0 {
num_workers := utils.Max(1, utils.Min(num_of_items, runtime.NumCPU()))
for i := 0; i < num_workers; i++ {
go run_worker()
}
}
passthrough_mode := no_passthrough
switch opts.Passthrough {
case "tmux":
passthrough_mode = tmux_passthrough
case "detect":
if tui.TmuxSocketAddress() != "" {
passthrough_mode = tmux_passthrough
}
}
if passthrough_mode == no_passthrough && (opts.TransferMode == "detect" || opts.DetectSupport) {
memory, files, direct, err := DetectSupport(time.Duration(opts.DetectionTimeout * float64(time.Second)))
if err != nil {
return 1, err
}
if !direct {
keep_going.Store(false)
return 1, fmt.Errorf("This terminal does not support the graphics protocol use a terminal such as kitty, WezTerm or Konsole that does. If you are running inside a terminal multiplexer such as tmux or screen that might be interfering as well.")
}
if memory {
transfer_by_memory = supported
} else {
transfer_by_memory = unsupported
}
if files {
transfer_by_file = supported
} else {
transfer_by_file = unsupported
}
}
if passthrough_mode != no_passthrough {
// tmux doesn't allow responses from the terminal so we can't detect if memory or file based transferring is supported
transfer_by_memory = unsupported
transfer_by_file = unsupported
}
if opts.DetectSupport {
if transfer_by_memory == supported {
print_error("memory")
} else if transfer_by_file == supported {
print_error("files")
} else {
print_error("stream")
}
return 0, nil
}
use_unicode_placeholder := opts.UnicodePlaceholder
if passthrough_mode != no_passthrough {
use_unicode_placeholder = true
}
base_id := uint32(opts.ImageId)
for num_of_items > 0 {
imgd := <-output_channel
if base_id != 0 {
imgd.image_id = base_id
base_id++
if base_id == 0 {
base_id++
}
}
imgd.use_unicode_placeholder = use_unicode_placeholder
imgd.passthrough_mode = passthrough_mode
num_of_items--
if imgd.err != nil {
print_error("Failed to process \x1b[31m%s\x1b[39m: %s\r\n", imgd.source_name, imgd.err)
} else {
transmit_image(imgd, opts.NoTrailingNewline)
if imgd.err != nil {
print_error("Failed to transmit \x1b[31m%s\x1b[39m: %s\r\n", imgd.source_name, imgd.err)
}
}
}
keep_going.Store(false)
if opts.Hold {
fmt.Print("\r")
if opts.Place != "" {
fmt.Println()
}
tui.HoldTillEnter(false)
}
return 0, nil
}
func EntryPoint(parent *cli.Command) {
create_cmd(parent, main)
}