Added clear screan command

Mikayla Maki created

Change summary

assets/keymaps/default.json       |  3 +
crates/terminal/src/connection.rs | 29 +++++++++-------
crates/terminal/src/terminal.rs   | 58 ++++++++++++++++++--------------
3 files changed, 50 insertions(+), 40 deletions(-)

Detailed changes

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"
         }
     }
 ]

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::<Settings>().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>) {
-        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<u8>, _: &mut ModelContext<Self>) {
+    fn write_bytes_to_pty(&mut self, input: Vec<u8>) {
         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 {

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>) {
-        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>) {
+        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<Workspace>) {
         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<Self>) {
         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>) {
-        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>) {
-        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>) {
-        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>) {
-        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>) {
-        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>) {
-        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>) {
-        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>) {
-        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>) {
-        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);
         });