Skip to content

Commit

Permalink
feat: build out prompt structure
Browse files Browse the repository at this point in the history
  • Loading branch information
RoloEdits committed Dec 25, 2024
1 parent 430498e commit 58df78c
Showing 1 changed file with 79 additions and 10 deletions.
89 changes: 79 additions & 10 deletions helix-view/src/commands/custom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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("<path>")),
completer: Some(String::from(":write")),
},
],
}
}
}
Expand Down Expand Up @@ -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<Item = &str> {
Expand All @@ -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("<path>")),
completer: Some(String::from(":write")),
};

panic!("{}", command.prompt());
}
}

0 comments on commit 58df78c

Please sign in to comment.