Simplify filter mode operation

Dont need channels and goroutines as writing in the loop is already
asnychronous
This commit is contained in:
Kovid Goyal 2022-12-01 19:54:57 +05:30
parent fdd42d5f19
commit 38a7fa73e3
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C

View File

@ -43,86 +43,60 @@ func run_plain_text_loop(opts *Options) (err error) {
if err != nil { if err != nil {
return return
} }
stdin_chan := make(chan string, 1)
err_chan := make(chan error, 1)
dest := "c" dest := "c"
if opts.UsePrimary { if opts.UsePrimary {
dest = "p" dest = "p"
} }
stdin_is_tty := tty.IsTerminal(os.Stdin.Fd())
var buf [8192]byte
send_to_loop := func(data string) { send_to_loop := func(data string) {
select { lp.QueueWriteString(data)
case stdin_chan <- data:
lp.WakeupMainThread()
return
default:
lp.WakeupMainThread()
}
stdin_chan <- data
} }
enc := base64.NewEncoder(base64.StdEncoding, &base64_streaming_enc{send_to_loop})
read_from_stdin := func() {
if !tty.IsTerminal(os.Stdin.Fd()) {
var buf [8192]byte
enc := base64.NewEncoder(base64.StdEncoding, &base64_streaming_enc{send_to_loop})
header_written := false
for {
n, err := os.Stdin.Read(buf[:])
if err != nil {
if errors.Is(err, io.EOF) {
enc.Close()
send_to_loop("\a")
close(stdin_chan)
os.Stdin.Close()
break
}
err_chan <- fmt.Errorf("Failed to read from STDIN with error: %w", err)
lp.WakeupMainThread()
break
}
if n > 0 {
if !header_written {
header_written = true
send_to_loop(fmt.Sprintf("\x1b]52;%s;", dest))
}
enc.Write(buf[:n])
}
}
} else {
close(stdin_chan)
lp.WakeupMainThread()
}
}
transmitting := true transmitting := true
lp.OnWakeup = func() error { after_read_from_stdin := func() {
for transmitting { transmitting = false
select { if opts.GetClipboard {
case p, more := <-stdin_chan: lp.QueueWriteString(encode_read_from_clipboard(opts.UsePrimary))
if more { } else if opts.WaitForCompletion {
lp.QueueWriteString(p) lp.QueueWriteString("\x1bP+q544e\x1b\\")
} else { } else {
transmitting = false lp.Quit(0)
if opts.GetClipboard { }
lp.QueueWriteString(encode_read_from_clipboard(opts.UsePrimary)) }
} else if opts.WaitForCompletion {
lp.QueueWriteString("\x1bP+q544e\x1b\\") read_from_stdin := func() error {
} else { n, err := os.Stdin.Read(buf[:])
lp.Quit(0) if n > 0 {
} enc.Write(buf[:n])
} }
case err := <-err_chan: if err != nil {
return err if errors.Is(err, io.EOF) {
default: enc.Close()
send_to_loop("\x1b\\")
os.Stdin.Close()
after_read_from_stdin()
return nil return nil
} }
return fmt.Errorf("Failed to read from STDIN with error: %w", err)
} }
lp.WakeupMainThread()
return nil return nil
} }
lp.OnWakeup = func() error {
return read_from_stdin()
}
lp.OnInitialize = func() (string, error) { lp.OnInitialize = func() (string, error) {
go read_from_stdin() if !stdin_is_tty {
send_to_loop(fmt.Sprintf("\x1b]52;%s;", dest))
read_from_stdin()
} else {
after_read_from_stdin()
}
return "", nil return "", nil
} }