1use std::sync::Arc;
2use std::sync::atomic::AtomicBool;
3
4use anyhow::Result;
5use assistant_slash_command::{
6 ArgumentCompletion, SlashCommand, SlashCommandOutput, SlashCommandOutputSection,
7 SlashCommandResult,
8};
9use chrono::Local;
10use gpui::{Task, WeakEntity};
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<WeakEntity<Workspace>>,
39 _window: &mut Window,
40 _cx: &mut App,
41 ) -> Task<Result<Vec<ArgumentCompletion>>> {
42 Task::ready(Ok(Vec::new()))
43 }
44
45 fn run(
46 self: Arc<Self>,
47 _arguments: &[String],
48 _context_slash_command_output_sections: &[SlashCommandOutputSection<language::Anchor>],
49 _context_buffer: BufferSnapshot,
50 _workspace: WeakEntity<Workspace>,
51 _delegate: Option<Arc<dyn LspAdapterDelegate>>,
52 _window: &mut Window,
53 _cx: &mut App,
54 ) -> Task<SlashCommandResult> {
55 let now = Local::now();
56 let text = format!("Today is {now}.", now = now.to_rfc2822());
57 let range = 0..text.len();
58
59 Task::ready(Ok(SlashCommandOutput {
60 text,
61 sections: vec![SlashCommandOutputSection {
62 range,
63 icon: IconName::CountdownTimer,
64 label: now.to_rfc2822().into(),
65 metadata: None,
66 }],
67 run_commands_in_text: false,
68 }
69 .into_event_stream()))
70 }
71}