Exposes terminal support via ACP and migrates our agent to use it. - N/A --------- Co-authored-by: Bennet Bo Fenner <bennetbo@gmx.de>
169 lines
5.2 KiB
Rust
169 lines
5.2 KiB
Rust
use agent_client_protocol as acp;
|
|
|
|
use futures::{FutureExt as _, future::Shared};
|
|
use gpui::{App, AppContext, Context, Entity, Task};
|
|
use language::LanguageRegistry;
|
|
use markdown::Markdown;
|
|
use std::{path::PathBuf, process::ExitStatus, sync::Arc, time::Instant};
|
|
|
|
pub struct Terminal {
|
|
id: acp::TerminalId,
|
|
command: Entity<Markdown>,
|
|
working_dir: Option<PathBuf>,
|
|
terminal: Entity<terminal::Terminal>,
|
|
started_at: Instant,
|
|
output: Option<TerminalOutput>,
|
|
output_byte_limit: Option<usize>,
|
|
_output_task: Shared<Task<acp::TerminalExitStatus>>,
|
|
}
|
|
|
|
pub struct TerminalOutput {
|
|
pub ended_at: Instant,
|
|
pub exit_status: Option<ExitStatus>,
|
|
pub content: String,
|
|
pub original_content_len: usize,
|
|
pub content_line_count: usize,
|
|
}
|
|
|
|
impl Terminal {
|
|
pub fn new(
|
|
id: acp::TerminalId,
|
|
command: String,
|
|
working_dir: Option<PathBuf>,
|
|
output_byte_limit: Option<usize>,
|
|
terminal: Entity<terminal::Terminal>,
|
|
language_registry: Arc<LanguageRegistry>,
|
|
cx: &mut Context<Self>,
|
|
) -> Self {
|
|
let command_task = terminal.read(cx).wait_for_completed_task(cx);
|
|
Self {
|
|
id,
|
|
command: cx.new(|cx| {
|
|
Markdown::new(
|
|
format!("```\n{}\n```", command).into(),
|
|
Some(language_registry.clone()),
|
|
None,
|
|
cx,
|
|
)
|
|
}),
|
|
working_dir,
|
|
terminal,
|
|
started_at: Instant::now(),
|
|
output: None,
|
|
output_byte_limit,
|
|
_output_task: cx
|
|
.spawn(async move |this, cx| {
|
|
let exit_status = command_task.await;
|
|
|
|
this.update(cx, |this, cx| {
|
|
let (content, original_content_len) = this.truncated_output(cx);
|
|
let content_line_count = this.terminal.read(cx).total_lines();
|
|
|
|
this.output = Some(TerminalOutput {
|
|
ended_at: Instant::now(),
|
|
exit_status,
|
|
content,
|
|
original_content_len,
|
|
content_line_count,
|
|
});
|
|
cx.notify();
|
|
})
|
|
.ok();
|
|
|
|
let exit_status = exit_status.map(portable_pty::ExitStatus::from);
|
|
|
|
acp::TerminalExitStatus {
|
|
exit_code: exit_status.as_ref().map(|e| e.exit_code()),
|
|
signal: exit_status.and_then(|e| e.signal().map(Into::into)),
|
|
}
|
|
})
|
|
.shared(),
|
|
}
|
|
}
|
|
|
|
pub fn id(&self) -> &acp::TerminalId {
|
|
&self.id
|
|
}
|
|
|
|
pub fn wait_for_exit(&self) -> Shared<Task<acp::TerminalExitStatus>> {
|
|
self._output_task.clone()
|
|
}
|
|
|
|
pub fn kill(&mut self, cx: &mut App) {
|
|
self.terminal.update(cx, |terminal, _cx| {
|
|
terminal.kill_active_task();
|
|
});
|
|
}
|
|
|
|
pub fn current_output(&self, cx: &App) -> acp::TerminalOutputResponse {
|
|
if let Some(output) = self.output.as_ref() {
|
|
let exit_status = output.exit_status.map(portable_pty::ExitStatus::from);
|
|
|
|
acp::TerminalOutputResponse {
|
|
output: output.content.clone(),
|
|
truncated: output.original_content_len > output.content.len(),
|
|
exit_status: Some(acp::TerminalExitStatus {
|
|
exit_code: exit_status.as_ref().map(|e| e.exit_code()),
|
|
signal: exit_status.and_then(|e| e.signal().map(Into::into)),
|
|
}),
|
|
}
|
|
} else {
|
|
let (current_content, original_len) = self.truncated_output(cx);
|
|
|
|
acp::TerminalOutputResponse {
|
|
truncated: current_content.len() < original_len,
|
|
output: current_content,
|
|
exit_status: None,
|
|
}
|
|
}
|
|
}
|
|
|
|
fn truncated_output(&self, cx: &App) -> (String, usize) {
|
|
let terminal = self.terminal.read(cx);
|
|
let mut content = terminal.get_content();
|
|
|
|
let original_content_len = content.len();
|
|
|
|
if let Some(limit) = self.output_byte_limit
|
|
&& content.len() > limit
|
|
{
|
|
let mut end_ix = limit.min(content.len());
|
|
while !content.is_char_boundary(end_ix) {
|
|
end_ix -= 1;
|
|
}
|
|
// Don't truncate mid-line, clear the remainder of the last line
|
|
end_ix = content[..end_ix].rfind('\n').unwrap_or(end_ix);
|
|
content.truncate(end_ix);
|
|
}
|
|
|
|
(content, original_content_len)
|
|
}
|
|
|
|
pub fn command(&self) -> &Entity<Markdown> {
|
|
&self.command
|
|
}
|
|
|
|
pub fn working_dir(&self) -> &Option<PathBuf> {
|
|
&self.working_dir
|
|
}
|
|
|
|
pub fn started_at(&self) -> Instant {
|
|
self.started_at
|
|
}
|
|
|
|
pub fn output(&self) -> Option<&TerminalOutput> {
|
|
self.output.as_ref()
|
|
}
|
|
|
|
pub fn inner(&self) -> &Entity<terminal::Terminal> {
|
|
&self.terminal
|
|
}
|
|
|
|
pub fn to_markdown(&self, cx: &App) -> String {
|
|
format!(
|
|
"Terminal:\n```\n{}\n```\n",
|
|
self.terminal.read(cx).get_content()
|
|
)
|
|
}
|
|
}
|