term_command.rs

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