From d76e0850ae446a4726e4ed5038371db57fc72649 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sat, 31 Dec 2022 13:24:43 +0530 Subject: [PATCH] More work on porting icat --- tools/cmd/icat/main.go | 34 ++++++++++++++++++++++++++++++-- tools/cmd/icat/process_images.go | 29 +++++++++++++++++++++++++-- tools/tui/loop/api.go | 11 +++++++++++ 3 files changed, 70 insertions(+), 4 deletions(-) diff --git a/tools/cmd/icat/main.go b/tools/cmd/icat/main.go index 05ccaa91c..98110a076 100644 --- a/tools/cmd/icat/main.go +++ b/tools/cmd/icat/main.go @@ -64,7 +64,7 @@ func parse_mirror() (err error) { } func parse_background() (err error) { - if opts.Background == "" { + if opts.Background == "" || opts.Background == "none" { return nil } col, err := style.ParseColor(opts.Background) @@ -219,9 +219,12 @@ func on_query_finished() (err error) { default: print_error("stream") } - lp.Quit(0) + quit_loop() return } + if num_of_items <= 0 { + quit_loop() + } return } @@ -274,7 +277,34 @@ func on_finalize() string { return "" } +var errors_occurred bool = false + +func quit_loop() { + if errors_occurred { + lp.Quit(1) + } else { + lp.Quit(0) + } +} + func on_wakeup() error { + have_more := true + for have_more { + select { + case imgd := <-output_channel: + num_of_items-- + if imgd.err != nil { + print_error("Failed to process \x1b[31m%s\x1b[39m: %v\r\n", imgd.source_name, imgd.err) + continue + } + lp.QueueWriteString("Processed " + imgd.source_name + "\r\n") + default: + have_more = false + } + } + if num_of_items <= 0 && !query_in_flight { + quit_loop() + } return nil } diff --git a/tools/cmd/icat/process_images.go b/tools/cmd/icat/process_images.go index 5032f0a1a..ad2c9e82d 100644 --- a/tools/cmd/icat/process_images.go +++ b/tools/cmd/icat/process_images.go @@ -69,7 +69,7 @@ func is_http_url(arg string) bool { func process_dirs(args ...string) (results []input_arg, err error) { results = make([]input_arg, 0, 64) - if opts.Stdin != "no" && (opts.Stdin == "yes" || tty.IsTerminal(os.Stdin.Fd())) { + if opts.Stdin != "no" && (opts.Stdin == "yes" || !tty.IsTerminal(os.Stdin.Fd())) { results = append(results, input_arg{arg: "/dev/stdin"}) } for _, arg := range args { @@ -140,6 +140,13 @@ type image_data struct { format_uppercase string available_width, available_height int needs_scaling, needs_conversion bool + filename string + in_memory_bytes []byte + filename_is_temporary bool + + // for error reporting + err error + source_name string } func set_basic_metadata(imgd *image_data) { @@ -159,6 +166,24 @@ func send_output(imgd *image_data) { lp.WakeupMainThread() } +func report_error(source_name, msg string, err error) { + imgd := image_data{source_name: source_name, err: fmt.Errorf("%s: %w", msg, err)} + send_output(&imgd) +} + +func make_output_from_input(imgd *image_data, f *opened_input) { + bb, ok := f.file.(*BytesBuf) + if ok { + imgd.in_memory_bytes = bb.data + } else { + imgd.filename = f.file.(*os.File).Name() + if f.name_to_unlink != "" { + imgd.filename_is_temporary = true + f.name_to_unlink = "" + } + } +} + func process_arg(arg input_arg) { var f opened_input if arg.is_http_url { @@ -198,7 +223,7 @@ func process_arg(arg input_arg) { defer f.Release() c, format, err := image.DecodeConfig(f.file) f.Rewind() - imgd := image_data{} + imgd := image_data{source_name: arg.value} if err != nil { report_error(arg.value, "Unknown image format", err) return diff --git a/tools/tui/loop/api.go b/tools/tui/loop/api.go index 401cca373..9f6bd3faf 100644 --- a/tools/tui/loop/api.go +++ b/tools/tui/loop/api.go @@ -6,6 +6,7 @@ import ( "encoding/base64" "fmt" "kitty/tools/tty" + "strings" "time" "golang.org/x/sys/unix" @@ -171,6 +172,16 @@ func (self *Loop) KillIfSignalled() { } } +func (self *Loop) Println(args ...any) { + self.QueueWriteString(fmt.Sprint(args...)) + self.QueueWriteString("\r\n") +} + +func (self *Loop) Printf(format string, args ...any) { + format = strings.ReplaceAll(format, "\n", "\r\n") + self.QueueWriteString(fmt.Sprintf(format, args...)) +} + func (self *Loop) DebugPrintln(args ...any) { if self.controlling_term != nil { const limit = 2048