now_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};
 8use chrono::Local;
 9use gpui::{Task, WeakView};
10use language::{BufferSnapshot, LspAdapterDelegate};
11use ui::prelude::*;
12use workspace::Workspace;
13
14pub(crate) struct NowSlashCommand;
15
16impl SlashCommand for NowSlashCommand {
17    fn name(&self) -> String {
18        "now".into()
19    }
20
21    fn description(&self) -> String {
22        "Insert current date and time".into()
23    }
24
25    fn menu_text(&self) -> String {
26        self.description()
27    }
28
29    fn requires_argument(&self) -> bool {
30        false
31    }
32
33    fn complete_argument(
34        self: Arc<Self>,
35        _arguments: &[String],
36        _cancel: Arc<AtomicBool>,
37        _workspace: Option<WeakView<Workspace>>,
38        _cx: &mut WindowContext,
39    ) -> Task<Result<Vec<ArgumentCompletion>>> {
40        Task::ready(Ok(Vec::new()))
41    }
42
43    fn run(
44        self: Arc<Self>,
45        _arguments: &[String],
46        _context_slash_command_output_sections: &[SlashCommandOutputSection<language::Anchor>],
47        _context_buffer: BufferSnapshot,
48        _workspace: WeakView<Workspace>,
49        _delegate: Option<Arc<dyn LspAdapterDelegate>>,
50        _cx: &mut WindowContext,
51    ) -> Task<Result<SlashCommandOutput>> {
52        let now = Local::now();
53        let text = format!("Today is {now}.", now = now.to_rfc2822());
54        let range = 0..text.len();
55
56        Task::ready(Ok(SlashCommandOutput {
57            text,
58            sections: vec![SlashCommandOutputSection {
59                range,
60                icon: IconName::CountdownTimer,
61                label: now.to_rfc2822().into(),
62                metadata: None,
63            }],
64            run_commands_in_text: false,
65        }))
66    }
67}