mirror of
https://github.com/kovidgoyal/kitty
synced 2026-07-07 17:43:53 +02:00
Fix @ send-text --stdin always assuming STDIN is a tty
This commit is contained in:
@@ -6,6 +6,7 @@ import (
|
||||
"encoding/base64"
|
||||
"errors"
|
||||
"io"
|
||||
"kitty/tools/tty"
|
||||
"kitty/tools/tui/loop"
|
||||
"kitty/tools/utils"
|
||||
"kitty/tools/utils/shlex"
|
||||
@@ -16,6 +17,19 @@ import (
|
||||
var end_reading_from_stdin = errors.New("end reading from STDIN")
|
||||
var waiting_on_stdin = errors.New("wait for key events from STDIN")
|
||||
|
||||
func make_file_gen(f *os.File) func(*rc_io_data) (bool, error) {
|
||||
chunk := make([]byte, 2048)
|
||||
file_gen := func(io_data *rc_io_data) (bool, error) {
|
||||
n, err := f.Read(chunk)
|
||||
if err != nil && !errors.Is(err, io.EOF) {
|
||||
return false, err
|
||||
}
|
||||
set_payload_data(io_data, "base64:"+base64.StdEncoding.EncodeToString(chunk[:n]))
|
||||
return n == 0 || errors.Is(err, io.EOF), nil
|
||||
}
|
||||
return file_gen
|
||||
|
||||
}
|
||||
func parse_send_text(io_data *rc_io_data, args []string) error {
|
||||
io_data.rc.NoResponse = true
|
||||
generators := make([]func(io_data *rc_io_data) (bool, error), 0, 1)
|
||||
@@ -42,49 +56,43 @@ func parse_send_text(io_data *rc_io_data, args []string) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
chunk := make([]byte, 2048)
|
||||
file_gen := func(io_data *rc_io_data) (bool, error) {
|
||||
n, err := f.Read(chunk)
|
||||
if err != nil && !errors.Is(err, io.EOF) {
|
||||
return false, err
|
||||
}
|
||||
set_payload_data(io_data, "base64:"+base64.StdEncoding.EncodeToString(chunk[:n]))
|
||||
return n == 0, nil
|
||||
}
|
||||
generators = append(generators, file_gen)
|
||||
generators = append(generators, make_file_gen(f))
|
||||
}
|
||||
|
||||
if options_send_text.Stdin {
|
||||
pending_key_events := make([]string, 0, 1)
|
||||
if tty.IsTerminal(os.Stdin.Fd()) {
|
||||
pending_key_events := make([]string, 0, 1)
|
||||
|
||||
io_data.on_key_event = func(lp *loop.Loop, ke *loop.KeyEvent) error {
|
||||
ke.Handled = true
|
||||
if ke.MatchesPressOrRepeat("ctrl+d") {
|
||||
return end_reading_from_stdin
|
||||
io_data.on_key_event = func(lp *loop.Loop, ke *loop.KeyEvent) error {
|
||||
ke.Handled = true
|
||||
if ke.MatchesPressOrRepeat("ctrl+d") {
|
||||
return end_reading_from_stdin
|
||||
}
|
||||
bs := "kitty-key:" + base64.StdEncoding.EncodeToString([]byte(ke.AsCSI()))
|
||||
pending_key_events = append(pending_key_events, bs)
|
||||
if ke.Text != "" {
|
||||
lp.QueueWriteString(ke.Text)
|
||||
} else if ke.MatchesPressOrRepeat("backspace") {
|
||||
lp.QueueWriteString("\x08\x1b[P")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
bs := "kitty-key:" + base64.StdEncoding.EncodeToString([]byte(ke.AsCSI()))
|
||||
pending_key_events = append(pending_key_events, bs)
|
||||
if ke.Text != "" {
|
||||
lp.QueueWriteString(ke.Text)
|
||||
} else if ke.MatchesPressOrRepeat("backspace") {
|
||||
lp.QueueWriteString("\x08\x1b[P")
|
||||
|
||||
key_gen := func(io_data *rc_io_data) (bool, error) {
|
||||
if len(pending_key_events) > 0 {
|
||||
payload := io_data.rc.Payload.(send_text_json_type)
|
||||
payload.Exclude_active = true
|
||||
io_data.rc.Payload = payload
|
||||
set_payload_data(io_data, pending_key_events[0])
|
||||
pending_key_events = pending_key_events[1:]
|
||||
return false, nil
|
||||
}
|
||||
return false, waiting_on_stdin
|
||||
}
|
||||
return nil
|
||||
generators = append(generators, key_gen)
|
||||
} else {
|
||||
generators = append(generators, make_file_gen(os.Stdin))
|
||||
}
|
||||
|
||||
key_gen := func(io_data *rc_io_data) (bool, error) {
|
||||
if len(pending_key_events) > 0 {
|
||||
payload := io_data.rc.Payload.(send_text_json_type)
|
||||
payload.Exclude_active = true
|
||||
io_data.rc.Payload = payload
|
||||
set_payload_data(io_data, pending_key_events[0])
|
||||
pending_key_events = pending_key_events[1:]
|
||||
return false, nil
|
||||
}
|
||||
return false, waiting_on_stdin
|
||||
}
|
||||
generators = append(generators, key_gen)
|
||||
|
||||
}
|
||||
|
||||
io_data.multiple_payload_generator = func(io_data *rc_io_data) (bool, error) {
|
||||
|
||||
Reference in New Issue
Block a user