Use terminal titles for buttons

This commit is contained in:
Joseph Lyons 2023-03-07 15:04:12 -05:00
parent c80942ea00
commit 0a3f0c5252
3 changed files with 39 additions and 36 deletions

View File

@ -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 {

View File

@ -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(

View File

@ -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))
}
}
}