Simplify filter mode operation
Dont need channels and goroutines as writing in the loop is already asnychronous
This commit is contained in:
parent
fdd42d5f19
commit
38a7fa73e3
@ -43,66 +43,20 @@ 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
|
|
||||||
}
|
|
||||||
|
|
||||||
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})
|
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 {
|
|
||||||
select {
|
|
||||||
case p, more := <-stdin_chan:
|
|
||||||
if more {
|
|
||||||
lp.QueueWriteString(p)
|
|
||||||
} else {
|
|
||||||
transmitting = false
|
transmitting = false
|
||||||
if opts.GetClipboard {
|
if opts.GetClipboard {
|
||||||
lp.QueueWriteString(encode_read_from_clipboard(opts.UsePrimary))
|
lp.QueueWriteString(encode_read_from_clipboard(opts.UsePrimary))
|
||||||
@ -112,17 +66,37 @@ func run_plain_text_loop(opts *Options) (err error) {
|
|||||||
lp.Quit(0)
|
lp.Quit(0)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
case err := <-err_chan:
|
|
||||||
return err
|
read_from_stdin := func() error {
|
||||||
default:
|
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 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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user