From 7885234fbcdd592c2aaf0c97c0da0a89debff8bd Mon Sep 17 00:00:00 2001 From: Mikayla Maki Date: Wed, 13 Jul 2022 13:19:21 -0700 Subject: [PATCH] Added clear screan command --- assets/keymaps/default.json | 3 +- crates/terminal/src/connection.rs | 29 +++++++++------- crates/terminal/src/terminal.rs | 58 +++++++++++++++++-------------- 3 files changed, 50 insertions(+), 40 deletions(-) diff --git a/assets/keymaps/default.json b/assets/keymaps/default.json index e40a5426ddb29160b69046b36ad33872a9ddf040..0e13bae79487d7d972de09fe007567c33b6a358c 100644 --- a/assets/keymaps/default.json +++ b/assets/keymaps/default.json @@ -419,7 +419,8 @@ "down": "terminal::Down", "tab": "terminal::Tab", "cmd-v": "terminal::Paste", - "cmd-c": "terminal::Copy" + "cmd-c": "terminal::Copy", + "ctrl-l": "terminal::Clear" } } ] \ No newline at end of file diff --git a/crates/terminal/src/connection.rs b/crates/terminal/src/connection.rs index 47c631e9e2993898ba63689ab7eeb5ef12a6497f..800791370cdf63efee8be8ae7b5095b87da7a7aa 100644 --- a/crates/terminal/src/connection.rs +++ b/crates/terminal/src/connection.rs @@ -1,4 +1,5 @@ use alacritty_terminal::{ + ansi::{ClearMode, Handler}, config::{Config, PtyConfig}, event::{Event as AlacTermEvent, Notify}, event_loop::{EventLoop, Msg, Notifier}, @@ -120,7 +121,7 @@ impl TerminalConnection { AlacTermEvent::Wakeup => { cx.emit(Event::Wakeup); } - AlacTermEvent::PtyWrite(out) => self.write_to_pty(out, cx), + AlacTermEvent::PtyWrite(out) => self.write_to_pty(out), AlacTermEvent::MouseCursorDirty => { //Calculate new cursor style. //TODO: alacritty/src/input.rs:L922-L939 @@ -138,20 +139,17 @@ impl TerminalConnection { AlacTermEvent::ClipboardStore(_, data) => { cx.write_to_clipboard(ClipboardItem::new(data)) } - AlacTermEvent::ClipboardLoad(_, format) => self.write_to_pty( - format( - &cx.read_from_clipboard() - .map(|ci| ci.text().to_string()) - .unwrap_or("".to_string()), - ), - cx, - ), + AlacTermEvent::ClipboardLoad(_, format) => self.write_to_pty(format( + &cx.read_from_clipboard() + .map(|ci| ci.text().to_string()) + .unwrap_or("".to_string()), + )), AlacTermEvent::ColorRequest(index, format) => { let color = self.term.lock().colors()[index].unwrap_or_else(|| { let term_style = &cx.global::().theme.terminal; to_alac_rgb(get_color_at_index(&index, &term_style.colors)) }); - self.write_to_pty(format(color), cx) + self.write_to_pty(format(color)) } AlacTermEvent::CursorBlinkingChange => { //TODO: Set a timer to blink the cursor on and off @@ -164,12 +162,12 @@ impl TerminalConnection { } ///Write the Input payload to the tty. This locks the terminal so we can scroll it. - pub fn write_to_pty(&mut self, input: String, cx: &mut ModelContext) { - self.write_bytes_to_pty(input.into_bytes(), cx); + pub fn write_to_pty(&mut self, input: String) { + self.write_bytes_to_pty(input.into_bytes()); } ///Write the Input payload to the tty. This locks the terminal so we can scroll it. - fn write_bytes_to_pty(&mut self, input: Vec, _: &mut ModelContext) { + fn write_bytes_to_pty(&mut self, input: Vec) { self.term.lock().scroll_display(Scroll::Bottom); self.pty_tx.notify(input); } @@ -179,6 +177,11 @@ impl TerminalConnection { self.pty_tx.0.send(Msg::Resize(new_size)).ok(); self.term.lock().resize(new_size); } + + pub fn clear(&mut self) { + self.write_to_pty("\x0c".into()); + self.term.lock().clear_screen(ClearMode::Saved); + } } impl Drop for TerminalConnection { diff --git a/crates/terminal/src/terminal.rs b/crates/terminal/src/terminal.rs index 9a811d2a3bb70ed95bda8577e471aad4a9cea552..12c092d6e6ffd78fb326aa1eb5e79f534fb41f38 100644 --- a/crates/terminal/src/terminal.rs +++ b/crates/terminal/src/terminal.rs @@ -88,6 +88,7 @@ pub fn init(cx: &mut MutableAppContext) { cx.add_action(Terminal::paste); cx.add_action(Terminal::scroll_terminal); cx.add_action(Terminal::input); + cx.add_action(Terminal::clear); cx.add_action(deploy_modal); } @@ -177,9 +178,9 @@ impl Terminal { } fn input(&mut self, Input(text): &Input, cx: &mut ViewContext) { - self.connection.update(cx, |connection, cx| { + self.connection.update(cx, |connection, _| { //TODO: This is probably not encoding UTF8 correctly (see alacritty/src/input.rs:L825-837) - connection.write_to_pty(text.clone(), cx); + connection.write_to_pty(text.clone()); }); if self.has_bell { @@ -188,6 +189,11 @@ impl Terminal { } } + fn clear(&mut self, _: &Clear, cx: &mut ViewContext) { + self.connection + .update(cx, |connection, _| connection.clear()); + } + ///Create a new Terminal in the current working directory or the user's home directory fn deploy(workspace: &mut Workspace, _: &Deploy, cx: &mut ViewContext) { let wd = get_wd_for_workspace(workspace, cx); @@ -212,72 +218,72 @@ impl Terminal { ///Attempt to paste the clipboard into the terminal fn paste(&mut self, _: &Paste, cx: &mut ViewContext) { if let Some(item) = cx.read_from_clipboard() { - self.connection.update(cx, |connection, cx| { - connection.write_to_pty(item.text().to_owned(), cx); + self.connection.update(cx, |connection, _| { + connection.write_to_pty(item.text().to_owned()); }) } } ///Send the `up` key fn up(&mut self, _: &Up, cx: &mut ViewContext) { - self.connection.update(cx, |connection, cx| { - connection.write_to_pty(UP_SEQ.to_string(), cx); + self.connection.update(cx, |connection, _| { + connection.write_to_pty(UP_SEQ.to_string()); }); } ///Send the `down` key fn down(&mut self, _: &Down, cx: &mut ViewContext) { - self.connection.update(cx, |connection, cx| { - connection.write_to_pty(DOWN_SEQ.to_string(), cx); + self.connection.update(cx, |connection, _| { + connection.write_to_pty(DOWN_SEQ.to_string()); }); } ///Send the `tab` key fn tab(&mut self, _: &Tab, cx: &mut ViewContext) { - self.connection.update(cx, |connection, cx| { - connection.write_to_pty(TAB_CHAR.to_string(), cx); + self.connection.update(cx, |connection, _| { + connection.write_to_pty(TAB_CHAR.to_string()); }); } ///Send `SIGINT` (`ctrl-c`) fn send_sigint(&mut self, _: &Sigint, cx: &mut ViewContext) { - self.connection.update(cx, |connection, cx| { - connection.write_to_pty(ETX_CHAR.to_string(), cx); + self.connection.update(cx, |connection, _| { + connection.write_to_pty(ETX_CHAR.to_string()); }); } ///Send the `escape` key fn escape(&mut self, _: &Escape, cx: &mut ViewContext) { - self.connection.update(cx, |connection, cx| { - connection.write_to_pty(ESC_CHAR.to_string(), cx); + self.connection.update(cx, |connection, _| { + connection.write_to_pty(ESC_CHAR.to_string()); }); } ///Send the `delete` key. TODO: Difference between this and backspace? fn del(&mut self, _: &Del, cx: &mut ViewContext) { - self.connection.update(cx, |connection, cx| { - connection.write_to_pty(DEL_CHAR.to_string(), cx); + self.connection.update(cx, |connection, _| { + connection.write_to_pty(DEL_CHAR.to_string()); }); } ///Send a carriage return. TODO: May need to check the terminal mode. fn carriage_return(&mut self, _: &Return, cx: &mut ViewContext) { - self.connection.update(cx, |connection, cx| { - connection.write_to_pty(CARRIAGE_RETURN_CHAR.to_string(), cx); + self.connection.update(cx, |connection, _| { + connection.write_to_pty(CARRIAGE_RETURN_CHAR.to_string()); }); } //Send the `left` key fn left(&mut self, _: &Left, cx: &mut ViewContext) { - self.connection.update(cx, |connection, cx| { - connection.write_to_pty(LEFT_SEQ.to_string(), cx); + self.connection.update(cx, |connection, _| { + connection.write_to_pty(LEFT_SEQ.to_string()); }); } //Send the `right` key fn right(&mut self, _: &Right, cx: &mut ViewContext) { - self.connection.update(cx, |connection, cx| { - connection.write_to_pty(RIGHT_SEQ.to_string(), cx); + self.connection.update(cx, |connection, _| { + connection.write_to_pty(RIGHT_SEQ.to_string()); }); } } @@ -467,8 +473,8 @@ mod tests { let terminal = cx.add_view(Default::default(), |cx| Terminal::new(None, false, cx)); terminal.update(cx, |terminal, cx| { - terminal.connection.update(cx, |connection, cx| { - connection.write_to_pty("expr 3 + 4".to_string(), cx); + terminal.connection.update(cx, |connection, _| { + connection.write_to_pty("expr 3 + 4".to_string()); }); terminal.carriage_return(&Return, cx); }); @@ -492,8 +498,8 @@ mod tests { cx.set_condition_duration(Some(Duration::from_secs(2))); terminal.update(cx, |terminal, cx| { - terminal.connection.update(cx, |connection, cx| { - connection.write_to_pty("expr 3 + 4".to_string(), cx); + terminal.connection.update(cx, |connection, _| { + connection.write_to_pty("expr 3 + 4".to_string()); }); terminal.carriage_return(&Return, cx); });