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,66 +43,20 @@ func run_plain_text_loop(opts *Options) (err error) {
if err != nil {
return
}
stdin_chan := make(chan string, 1)
err_chan := make(chan error, 1)
dest := "c"
if opts.UsePrimary {
dest = "p"
}
stdin_is_tty := tty.IsTerminal(os.Stdin.Fd())
var buf [8192]byte
send_to_loop := func(data string) {
select {
case stdin_chan <- data:
lp.WakeupMainThread()
return
default:
lp.WakeupMainThread()
lp.QueueWriteString(data)
}
stdin_chan <- data
}
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
lp.OnWakeup = func() error {
for transmitting {
select {
case p, more := <-stdin_chan:
if more {
lp.QueueWriteString(p)
} else {
after_read_from_stdin := func() {
transmitting = false
if opts.GetClipboard {
lp.QueueWriteString(encode_read_from_clipboard(opts.UsePrimary))
@ -112,17 +66,37 @@ func run_plain_text_loop(opts *Options) (err error) {
lp.Quit(0)
}
}
case err := <-err_chan:
return err
default:
read_from_stdin := func() error {
n, err := os.Stdin.Read(buf[:])
if n > 0 {
enc.Write(buf[:n])
}
if err != nil {
if errors.Is(err, io.EOF) {
enc.Close()
send_to_loop("\x1b\\")
os.Stdin.Close()
after_read_from_stdin()
return nil
}
return fmt.Errorf("Failed to read from STDIN with error: %w", err)
}
lp.WakeupMainThread()
return nil
}
lp.OnWakeup = func() error {
return read_from_stdin()
}
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
}