terminal: Match trait bounds with terminal input (#31441)

The core change here is the following:

```rust
fn write_to_pty(&self, input: impl Into<Vec<u8>>);

// into
fn write_to_pty(&self, input: impl Into<Cow<'static, [u8]>>);
```

This matches the trait bounds that's used by the Alacritty crate. We are
now allowed to effectively pass `&'static str` instead of always needing
a `String`.

The main benefit comes from making the `to_esc_str` function return a
`Cow<'static, str>` instead of `String`. We save an allocation in the
following instances:

- When the user presses any special key that isn't alphanumerical (in
the terminal)
- When the uses presses any key while a modifier is active (in the
terminal)
- When focusing/un-focusing the terminal
- When completing or undoing a terminal transaction
- When starting a terminal assist

This basically saves us an allocation on **every key** press in the
terminal.

NOTE: This same optimization can be done for **nearly all** keypresses
in the entirety of Zed by changing the signature of the `Keystroke`
struct in gpui. If the Zed team is interested in a PR for it, let me
know.

Release Notes:

- N/A
This commit is contained in:
tidely 2025-06-03 06:12:28 +03:00 committed by GitHub
parent 56d4c0af9f
commit 8ab7d44d51
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 162 additions and 196 deletions

View file

@ -266,7 +266,7 @@ impl TerminalView {
pub(crate) fn commit_text(&mut self, text: &str, cx: &mut Context<Self>) {
if !text.is_empty() {
self.terminal.update(cx, |term, _| {
term.input(text.to_string());
term.input(text.to_string().into_bytes());
});
}
}
@ -643,7 +643,7 @@ impl TerminalView {
fn send_text(&mut self, text: &SendText, _: &mut Window, cx: &mut Context<Self>) {
self.clear_bell(cx);
self.terminal.update(cx, |term, _| {
term.input(text.0.to_string());
term.input(text.0.to_string().into_bytes());
});
}