Skip to content

Commit 5526328

Browse files
authored
feat(chat): expand support for /prompts command (#2799)
* feat: expand support for /prompts command * fix: prompts spec * fix: add /prompts delete cmd * fix(prompts): improve validation and user input handling * fix(prompts): manage prompt using structs
1 parent 5acd7e3 commit 5526328

File tree

5 files changed

+1071
-29
lines changed

5 files changed

+1071
-29
lines changed

crates/chat-cli/src/cli/chat/cli/editor.rs

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,8 @@ impl EditorArgs {
8484
}
8585
}
8686

87-
/// Opens the user's preferred editor to compose a prompt
88-
pub fn open_editor(initial_text: Option<String>) -> Result<String, ChatError> {
89-
// Create a temporary file with a unique name
90-
let temp_dir = std::env::temp_dir();
91-
let file_name = format!("q_prompt_{}.md", Uuid::new_v4());
92-
let temp_file_path = temp_dir.join(file_name);
93-
87+
/// Launch the user's preferred editor with the given file path
88+
fn launch_editor(file_path: &std::path::Path) -> Result<(), ChatError> {
9489
// Get the editor from environment variable or use a default
9590
let editor_cmd = std::env::var("EDITOR").unwrap_or_else(|_| "vi".to_string());
9691

@@ -104,11 +99,6 @@ pub fn open_editor(initial_text: Option<String>) -> Result<String, ChatError> {
10499

105100
let editor_bin = parts.remove(0);
106101

107-
// Write initial content to the file if provided
108-
let initial_content = initial_text.unwrap_or_default();
109-
std::fs::write(&temp_file_path, &initial_content)
110-
.map_err(|e| ChatError::Custom(format!("Failed to create temporary file: {}", e).into()))?;
111-
112102
// Open the editor with the parsed command and arguments
113103
let mut cmd = std::process::Command::new(editor_bin);
114104
// Add any arguments that were part of the EDITOR variable
@@ -117,14 +107,37 @@ pub fn open_editor(initial_text: Option<String>) -> Result<String, ChatError> {
117107
}
118108
// Add the file path as the last argument
119109
let status = cmd
120-
.arg(&temp_file_path)
110+
.arg(file_path)
121111
.status()
122112
.map_err(|e| ChatError::Custom(format!("Failed to open editor: {}", e).into()))?;
123113

124114
if !status.success() {
125115
return Err(ChatError::Custom("Editor exited with non-zero status".into()));
126116
}
127117

118+
Ok(())
119+
}
120+
121+
/// Opens the user's preferred editor to edit an existing file
122+
pub fn open_editor_file(file_path: &std::path::Path) -> Result<(), ChatError> {
123+
launch_editor(file_path)
124+
}
125+
126+
/// Opens the user's preferred editor to compose a prompt
127+
pub fn open_editor(initial_text: Option<String>) -> Result<String, ChatError> {
128+
// Create a temporary file with a unique name
129+
let temp_dir = std::env::temp_dir();
130+
let file_name = format!("q_prompt_{}.md", Uuid::new_v4());
131+
let temp_file_path = temp_dir.join(file_name);
132+
133+
// Write initial content to the file if provided
134+
let initial_content = initial_text.unwrap_or_default();
135+
std::fs::write(&temp_file_path, &initial_content)
136+
.map_err(|e| ChatError::Custom(format!("Failed to create temporary file: {}", e).into()))?;
137+
138+
// Launch the editor
139+
launch_editor(&temp_file_path)?;
140+
128141
// Read the content back
129142
let content = std::fs::read_to_string(&temp_file_path)
130143
.map_err(|e| ChatError::Custom(format!("Failed to read temporary file: {}", e).into()))?;

crates/chat-cli/src/cli/chat/cli/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ impl SlashCommand {
151151
})
152152
},
153153
Self::Changelog(args) => args.execute(session).await,
154-
Self::Prompts(args) => args.execute(session).await,
154+
Self::Prompts(args) => args.execute(os, session).await,
155155
Self::Hooks(args) => args.execute(session).await,
156156
Self::Usage(args) => args.execute(os, session).await,
157157
Self::Mcp(args) => args.execute(session).await,

0 commit comments

Comments
 (0)