Use terminal titles for buttons

Joseph Lyons created

Change summary

crates/terminal/src/terminal.rs           | 33 +++++++++++++++++++++++
crates/terminal_view/src/terminal_view.rs | 35 +-----------------------
crates/workspace/src/terminal_button.rs   |  7 ++--
3 files changed, 39 insertions(+), 36 deletions(-)

Detailed changes

crates/terminal/src/terminal.rs 🔗

@@ -32,6 +32,7 @@ use mappings::mouse::{
 
 use procinfo::LocalProcessInfo;
 use settings::{AlternateScroll, Settings, Shell, TerminalBlink};
+use util::truncate_and_trailoff;
 
 use std::{
     cmp::min,
@@ -1169,6 +1170,38 @@ impl Terminal {
             all_search_matches(&term, &searcher).collect()
         })
     }
+
+    pub fn title(&self) -> String {
+        self.foreground_process_info
+            .as_ref()
+            .map(|fpi| {
+                format!(
+                    "{} — {}",
+                    truncate_and_trailoff(
+                        &fpi.cwd
+                            .file_name()
+                            .map(|name| name.to_string_lossy().to_string())
+                            .unwrap_or_default(),
+                        25
+                    ),
+                    truncate_and_trailoff(
+                        &{
+                            format!(
+                                "{}{}",
+                                fpi.name,
+                                if fpi.argv.len() >= 1 {
+                                    format!(" {}", (&fpi.argv[1..]).join(" "))
+                                } else {
+                                    "".to_string()
+                                }
+                            )
+                        },
+                        25
+                    )
+                )
+            })
+            .unwrap_or_else(|| "Terminal".to_string())
+    }
 }
 
 impl Drop for Terminal {

crates/terminal_view/src/terminal_view.rs 🔗

@@ -30,7 +30,7 @@ use terminal::{
     },
     Event, Terminal,
 };
-use util::{truncate_and_trailoff, ResultExt};
+use util::ResultExt;
 use workspace::{
     item::{Item, ItemEvent},
     notifications::NotifyResultExt,
@@ -547,38 +547,7 @@ impl Item for TerminalView {
         tab_theme: &theme::Tab,
         cx: &gpui::AppContext,
     ) -> ElementBox {
-        let title = self
-            .terminal()
-            .read(cx)
-            .foreground_process_info
-            .as_ref()
-            .map(|fpi| {
-                format!(
-                    "{} — {}",
-                    truncate_and_trailoff(
-                        &fpi.cwd
-                            .file_name()
-                            .map(|name| name.to_string_lossy().to_string())
-                            .unwrap_or_default(),
-                        25
-                    ),
-                    truncate_and_trailoff(
-                        &{
-                            format!(
-                                "{}{}",
-                                fpi.name,
-                                if fpi.argv.len() >= 1 {
-                                    format!(" {}", (&fpi.argv[1..]).join(" "))
-                                } else {
-                                    "".to_string()
-                                }
-                            )
-                        },
-                        25
-                    )
-                )
-            })
-            .unwrap_or_else(|| "Terminal".to_string());
+        let title = self.terminal().read(cx).title();
 
         Flex::row()
             .with_child(

crates/workspace/src/terminal_button.rs 🔗

@@ -120,10 +120,11 @@ impl TerminalButton {
             let local_terminal_handles = project.local_terminal_handles();
 
             for local_terminal_handle in local_terminal_handles {
-                if let Some(_) = local_terminal_handle.upgrade(cx) {
-                    // TODO: Obtain the actual terminal "name" and put it in the menu
+                if let Some(terminal) = local_terminal_handle.upgrade(cx) {
+                    let title = terminal.read(cx).title();
+
                     // TODO: Replace the `NewTerminal` action with an action that instead focuses the selected terminal
-                    menu_options.push(ContextMenuItem::item("Terminal", NewTerminal))
+                    menu_options.push(ContextMenuItem::item(title, NewTerminal))
                 }
             }
         }