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