terminal_command.rs

  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 icon(&self) -> IconName {
 37        IconName::Terminal
 38    }
 39
 40    fn menu_text(&self) -> String {
 41        self.description()
 42    }
 43
 44    fn requires_argument(&self) -> bool {
 45        false
 46    }
 47
 48    fn accepts_arguments(&self) -> bool {
 49        true
 50    }
 51
 52    fn complete_argument(
 53        self: Arc<Self>,
 54        _arguments: &[String],
 55        _cancel: Arc<AtomicBool>,
 56        _workspace: Option<WeakView<Workspace>>,
 57        _cx: &mut WindowContext,
 58    ) -> Task<Result<Vec<ArgumentCompletion>>> {
 59        Task::ready(Ok(Vec::new()))
 60    }
 61
 62    fn run(
 63        self: Arc<Self>,
 64        arguments: &[String],
 65        _context_slash_command_output_sections: &[SlashCommandOutputSection<language::Anchor>],
 66        _context_buffer: BufferSnapshot,
 67        workspace: WeakView<Workspace>,
 68        _delegate: Option<Arc<dyn LspAdapterDelegate>>,
 69        cx: &mut WindowContext,
 70    ) -> Task<SlashCommandResult> {
 71        let Some(workspace) = workspace.upgrade() else {
 72            return Task::ready(Err(anyhow::anyhow!("workspace was dropped")));
 73        };
 74
 75        let Some(active_terminal) = resolve_active_terminal(&workspace, cx) else {
 76            return Task::ready(Err(anyhow::anyhow!("no active terminal")));
 77        };
 78
 79        let line_count = arguments
 80            .get(0)
 81            .and_then(|s| s.parse::<usize>().ok())
 82            .unwrap_or(DEFAULT_CONTEXT_LINES);
 83
 84        let lines = active_terminal
 85            .read(cx)
 86            .model()
 87            .read(cx)
 88            .last_n_non_empty_lines(line_count);
 89
 90        let mut text = String::new();
 91        text.push_str("Terminal output:\n");
 92        text.push_str(&lines.join("\n"));
 93        let range = 0..text.len();
 94
 95        Task::ready(Ok(SlashCommandOutput {
 96            text,
 97            sections: vec![SlashCommandOutputSection {
 98                range,
 99                icon: IconName::Terminal,
100                label: "Terminal".into(),
101                metadata: None,
102            }],
103            run_commands_in_text: false,
104        }
105        .to_event_stream()))
106    }
107}
108
109fn resolve_active_terminal(
110    workspace: &View<Workspace>,
111    cx: &WindowContext,
112) -> Option<View<TerminalView>> {
113    if let Some(terminal_view) = workspace
114        .read(cx)
115        .active_item(cx)
116        .and_then(|item| item.act_as::<TerminalView>(cx))
117    {
118        return Some(terminal_view);
119    }
120
121    let terminal_panel = workspace.read(cx).panel::<TerminalPanel>(cx)?;
122    terminal_panel.read(cx).pane().and_then(|pane| {
123        pane.read(cx)
124            .active_item()
125            .and_then(|t| t.downcast::<TerminalView>())
126    })
127}