1use std::sync::atomic::AtomicBool;
2use std::sync::Arc;
3
4use anyhow::Result;
5use assistant_slash_command::{
6 ArgumentCompletion, SlashCommand, SlashCommandOutput, SlashCommandOutputSection,
7 SlashCommandResult,
8};
9use gpui::{AppContext, Task, View, WeakView};
10use language::{BufferSnapshot, CodeLabel, LspAdapterDelegate};
11use terminal_view::{terminal_panel::TerminalPanel, TerminalView};
12use ui::prelude::*;
13use workspace::{dock::Panel, Workspace};
14
15use crate::DEFAULT_CONTEXT_LINES;
16
17use super::create_label_for_command;
18
19pub(crate) struct TerminalSlashCommand;
20
21const LINE_COUNT_ARG: &str = "--line-count";
22
23impl SlashCommand for TerminalSlashCommand {
24 fn name(&self) -> String {
25 "terminal".into()
26 }
27
28 fn label(&self, cx: &AppContext) -> CodeLabel {
29 create_label_for_command("terminal", &[LINE_COUNT_ARG], cx)
30 }
31
32 fn description(&self) -> String {
33 "Insert terminal output".into()
34 }
35
36 fn menu_text(&self) -> String {
37 self.description()
38 }
39
40 fn requires_argument(&self) -> bool {
41 false
42 }
43
44 fn accepts_arguments(&self) -> bool {
45 true
46 }
47
48 fn complete_argument(
49 self: Arc<Self>,
50 _arguments: &[String],
51 _cancel: Arc<AtomicBool>,
52 _workspace: Option<WeakView<Workspace>>,
53 _cx: &mut WindowContext,
54 ) -> Task<Result<Vec<ArgumentCompletion>>> {
55 Task::ready(Ok(Vec::new()))
56 }
57
58 fn run(
59 self: Arc<Self>,
60 arguments: &[String],
61 _context_slash_command_output_sections: &[SlashCommandOutputSection<language::Anchor>],
62 _context_buffer: BufferSnapshot,
63 workspace: WeakView<Workspace>,
64 _delegate: Option<Arc<dyn LspAdapterDelegate>>,
65 cx: &mut WindowContext,
66 ) -> Task<SlashCommandResult> {
67 let Some(workspace) = workspace.upgrade() else {
68 return Task::ready(Err(anyhow::anyhow!("workspace was dropped")));
69 };
70
71 let Some(active_terminal) = resolve_active_terminal(&workspace, cx) else {
72 return Task::ready(Err(anyhow::anyhow!("no active terminal")));
73 };
74
75 let line_count = arguments
76 .get(0)
77 .and_then(|s| s.parse::<usize>().ok())
78 .unwrap_or(DEFAULT_CONTEXT_LINES);
79
80 let lines = active_terminal
81 .read(cx)
82 .model()
83 .read(cx)
84 .last_n_non_empty_lines(line_count);
85
86 let mut text = String::new();
87 text.push_str("Terminal output:\n");
88 text.push_str(&lines.join("\n"));
89 let range = 0..text.len();
90
91 Task::ready(Ok(SlashCommandOutput {
92 text,
93 sections: vec![SlashCommandOutputSection {
94 range,
95 icon: IconName::Terminal,
96 label: "Terminal".into(),
97 metadata: None,
98 }],
99 run_commands_in_text: false,
100 }
101 .to_event_stream()))
102 }
103}
104
105fn resolve_active_terminal(
106 workspace: &View<Workspace>,
107 cx: &WindowContext,
108) -> Option<View<TerminalView>> {
109 if let Some(terminal_view) = workspace
110 .read(cx)
111 .active_item(cx)
112 .and_then(|item| item.act_as::<TerminalView>(cx))
113 {
114 return Some(terminal_view);
115 }
116
117 let terminal_panel = workspace.read(cx).panel::<TerminalPanel>(cx)?;
118 terminal_panel.read(cx).pane().and_then(|pane| {
119 pane.read(cx)
120 .active_item()
121 .and_then(|t| t.downcast::<TerminalView>())
122 })
123}