diff --git a/helix-view/src/commands/custom.rs b/helix-view/src/commands/custom.rs index a99189622728..994b275b517e 100644 --- a/helix-view/src/commands/custom.rs +++ b/helix-view/src/commands/custom.rs @@ -4,6 +4,8 @@ // TODO: Need to get access to a new table in the config: [commands]. // TODO: Could add an `aliases` to `CustomTypableCommand` and then add those as well? +use std::fmt::Write; + use serde::{Deserialize, Serialize}; // TODO: Might need to manually implement Serialize and Deserialize @@ -15,15 +17,29 @@ pub struct CustomTypeableCommands { impl Default for CustomTypeableCommands { fn default() -> Self { Self { - commands: vec![CustomTypableCommand { - name: String::from(":lg"), - desc: Some(String::from("runs lazygit in a floating pane")), - commands: vec![String::from( - ":sh wezterm cli spawn --floating-pane lazygit", - )], - accepts: None, - completer: None, - }], + commands: vec![ + CustomTypableCommand { + name: String::from(":lg"), + desc: Some(String::from("runs lazygit in a floating pane")), + commands: vec![String::from( + ":sh wezterm cli spawn --floating-pane lazygit", + )], + accepts: None, + completer: None, + }, + CustomTypableCommand { + name: String::from(":wcd!"), + desc: Some(String::from( + "writes buffer forcefully and changes directory", + )), + commands: vec![ + String::from(":write --force %{arg}"), + String::from(":cd %sh{ %{arg} | path dirname }"), + ], + accepts: Some(String::from("")), + completer: Some(String::from(":write")), + }, + ], } } } @@ -61,7 +77,37 @@ impl CustomTypableCommand { // // maps: // :write --force %{arg} -> :cd %sh{ %{arg} | path dirname } - todo!() + let mut prompt = String::new(); + + prompt.push_str(self.name.trim_start_matches(':')); + + if let Some(accepts) = &self.accepts { + write!(prompt, " {accepts}").unwrap(); + } + + prompt.push(':'); + + if let Some(desc) = &self.desc { + write!(prompt, " {desc}").unwrap(); + } + + prompt.push('\n'); + prompt.push('\n'); + + writeln!(prompt, "maps:").unwrap(); + prompt.push_str(" "); + + for (idx, command) in self.commands.iter().enumerate() { + write!(prompt, ":{}", command.trim_start_matches(':')).unwrap(); + + if idx + 1 == self.commands.len() { + break; + } + + write!(prompt, " -> ").unwrap(); + } + + prompt } pub fn iter(&self) -> impl Iterator { @@ -70,3 +116,26 @@ impl CustomTypableCommand { .map(|command| command.trim_start_matches(':')) } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn should_build_correctly_structured_prompt() { + let command = CustomTypableCommand { + name: String::from(":wcd!"), + desc: Some(String::from( + "writes buffer forcefully and changes directory", + )), + commands: vec![ + String::from(":write --force %{arg}"), + String::from(":cd %sh{ %{arg} | path dirname }"), + ], + accepts: Some(String::from("")), + completer: Some(String::from(":write")), + }; + + panic!("{}", command.prompt()); + } +}