More work on porting icat

This commit is contained in:
Kovid Goyal 2022-12-31 13:24:43 +05:30
parent b520882b62
commit d76e0850ae
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
3 changed files with 70 additions and 4 deletions

View File

@ -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
}

View File

@ -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

View File

@ -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