vibe_ticket/interactive/
mod.rs1use crate::error::Result;
7use crate::templates::{FieldType, TemplateManager};
8use dialoguer::{Confirm, Input, MultiSelect, Select, theme::ColorfulTheme};
9use std::collections::HashMap;
10
11pub struct InteractiveMode {
13 theme: ColorfulTheme,
14 template_manager: TemplateManager,
15}
16
17impl Default for InteractiveMode {
18 fn default() -> Self {
19 Self {
20 theme: ColorfulTheme::default(),
21 template_manager: TemplateManager::new(),
22 }
23 }
24}
25
26impl InteractiveMode {
27 #[must_use]
29 pub fn new() -> Self {
30 Self::default()
31 }
32
33 pub fn create_ticket(&self) -> Result<InteractiveTicketData> {
35 println!("š« Welcome to vibe-ticket interactive mode!\n");
36
37 let ticket_types = vec![
39 "Feature - New functionality",
40 "Bug - Something isn't working",
41 "Task - General work item",
42 "Refactor - Code improvement",
43 "Custom - No template",
44 ];
45
46 let selection = Select::with_theme(&self.theme)
47 .with_prompt("What would you like to create?")
48 .items(&ticket_types)
49 .default(0)
50 .interact()?;
51
52 let template_name = match selection {
53 0 => Some("feature"),
54 1 => Some("bug"),
55 2 => Some("task"),
56 3 => Some("task"), _ => None,
58 };
59
60 if let Some(template_name) = template_name {
61 self.create_from_template(template_name)
62 } else {
63 self.create_custom()
64 }
65 }
66
67 fn create_from_template(&self, template_name: &str) -> Result<InteractiveTicketData> {
69 let template = self.template_manager.get(template_name).ok_or_else(|| {
70 crate::error::VibeTicketError::TemplateNotFound(template_name.to_string())
71 })?;
72
73 println!("\nš Using {} template\n", template.name);
74
75 let mut values = HashMap::new();
76
77 for field in &template.fields {
79 let value = match &field.field_type {
80 FieldType::Text => {
81 let mut input =
82 Input::<String>::with_theme(&self.theme).with_prompt(&field.label);
83
84 if let Some(default) = &field.default {
85 input = input.default(default.clone());
86 }
87
88 if let Some(help) = &field.help {
89 println!("ā¹ļø {help}");
90 }
91
92 if field.required {
93 input.interact()?
94 } else {
95 input.allow_empty(true).interact()?
96 }
97 },
98 FieldType::LongText => {
99 println!("š {} (Press Enter twice when done)", field.label);
100 if let Some(help) = &field.help {
101 println!("ā¹ļø {help}");
102 }
103 self.read_multiline()?
104 },
105 FieldType::Select(options) => {
106 let selection = Select::with_theme(&self.theme)
107 .with_prompt(&field.label)
108 .items(options)
109 .default(0)
110 .interact()?;
111 options[selection].clone()
112 },
113 FieldType::MultiSelect(options) => {
114 let selections = MultiSelect::with_theme(&self.theme)
115 .with_prompt(&field.label)
116 .items(options)
117 .interact()?;
118 selections
119 .iter()
120 .map(|&i| options[i].clone())
121 .collect::<Vec<_>>()
122 .join(", ")
123 },
124 FieldType::List => {
125 println!("š {} (Enter items, blank line when done)", field.label);
126 if let Some(help) = &field.help {
127 println!("ā¹ļø {help}");
128 }
129 self.read_list()?
130 },
131 _ => String::new(), };
133
134 if !value.is_empty() || !field.required {
135 values.insert(field.name.clone(), value);
136 }
137 }
138
139 let priority = self.select_priority()?;
141 let tags = self.input_tags()?;
142 let start_now = self.confirm_start()?;
143
144 let ticket_data = self
146 .template_manager
147 .create_from_template(template_name, values)?;
148
149 Ok(InteractiveTicketData {
150 title: ticket_data.title,
151 description: ticket_data.description,
152 priority: ticket_data.priority.unwrap_or(priority),
153 tags: if tags.is_empty() {
154 ticket_data.tags
155 } else {
156 tags
157 },
158 start_now,
159 template_used: Some(template_name.to_string()),
160 })
161 }
162
163 fn create_custom(&self) -> Result<InteractiveTicketData> {
165 println!("\nš Creating custom ticket\n");
166
167 let title = Input::<String>::with_theme(&self.theme)
168 .with_prompt("Title")
169 .interact()?;
170
171 let description = if Confirm::with_theme(&self.theme)
172 .with_prompt("Add a detailed description?")
173 .default(true)
174 .interact()?
175 {
176 println!("š Description (Press Enter twice when done)");
177 Some(self.read_multiline()?)
178 } else {
179 None
180 };
181
182 let priority = self.select_priority()?;
183 let tags = self.input_tags()?;
184 let start_now = self.confirm_start()?;
185
186 Ok(InteractiveTicketData {
187 title,
188 description,
189 priority,
190 tags,
191 start_now,
192 template_used: None,
193 })
194 }
195
196 fn select_priority(&self) -> Result<String> {
198 let priorities = vec!["Low", "Medium", "High", "Critical"];
199 let selection = Select::with_theme(&self.theme)
200 .with_prompt("Priority")
201 .items(&priorities)
202 .default(1) .interact()?;
204
205 Ok(priorities[selection].to_lowercase())
206 }
207
208 fn input_tags(&self) -> Result<Vec<String>> {
210 let tags_input = Input::<String>::with_theme(&self.theme)
211 .with_prompt("Tags (comma-separated, optional)")
212 .allow_empty(true)
213 .interact()?;
214
215 Ok(if tags_input.is_empty() {
216 Vec::new()
217 } else {
218 tags_input
219 .split(',')
220 .map(|s| s.trim().to_string())
221 .filter(|s| !s.is_empty())
222 .collect()
223 })
224 }
225
226 fn confirm_start(&self) -> Result<bool> {
228 Confirm::with_theme(&self.theme)
229 .with_prompt("Start working on this ticket now?")
230 .default(false)
231 .interact()
232 .map_err(Into::into)
233 }
234
235 fn read_multiline(&self) -> Result<String> {
237 let mut lines = Vec::new();
238 let stdin = std::io::stdin();
239 let mut empty_lines = 0;
240
241 loop {
242 let mut line = String::new();
243 stdin.read_line(&mut line)?;
244
245 if line.trim().is_empty() {
246 empty_lines += 1;
247 if empty_lines >= 2 {
248 break;
249 }
250 lines.push(String::new());
251 } else {
252 empty_lines = 0;
253 lines.push(line.trim_end().to_string());
254 }
255 }
256
257 while lines.last().is_some_and(|l| l.is_empty()) {
259 lines.pop();
260 }
261
262 Ok(lines.join("\n"))
263 }
264
265 fn read_list(&self) -> Result<String> {
267 let mut items = Vec::new();
268 let stdin = std::io::stdin();
269 let mut counter = 1;
270
271 loop {
272 print!(" {counter}. ");
273 use std::io::Write;
274 std::io::stdout().flush()?;
275
276 let mut line = String::new();
277 stdin.read_line(&mut line)?;
278
279 let trimmed = line.trim();
280 if trimmed.is_empty() {
281 break;
282 }
283
284 items.push(format!("{counter}. {trimmed}"));
285 counter += 1;
286 }
287
288 Ok(items.join("\n"))
289 }
290
291 pub fn select_workflow(&self) -> Result<String> {
293 let workflows = vec![
294 "work-on - Start working on a ticket",
295 "finish - Complete current ticket",
296 "review - Review tickets",
297 "report - Generate reports",
298 ];
299
300 let selection = Select::with_theme(&self.theme)
301 .with_prompt("Select a workflow")
302 .items(&workflows)
303 .interact()?;
304
305 Ok(match selection {
306 0 => "work-on",
307 1 => "finish",
308 2 => "review",
309 _ => "report",
310 }
311 .to_string())
312 }
313
314 pub fn select_ticket(&self, tickets: Vec<(String, String)>) -> Result<String> {
316 if tickets.is_empty() {
317 return Err(crate::error::VibeTicketError::NoTicketsFound);
318 }
319
320 let items: Vec<String> = tickets
321 .iter()
322 .map(|(id, title)| format!("{id} - {title}"))
323 .collect();
324
325 let selection = Select::with_theme(&self.theme)
326 .with_prompt("Select a ticket")
327 .items(&items)
328 .interact()?;
329
330 Ok(tickets[selection].0.clone())
331 }
332}
333
334#[derive(Debug)]
336pub struct InteractiveTicketData {
337 pub title: String,
338 pub description: Option<String>,
339 pub priority: String,
340 pub tags: Vec<String>,
341 pub start_now: bool,
342 pub template_used: Option<String>,
343}
344
345#[derive(Default)]
347pub struct InteractivePrompt {
348 theme: ColorfulTheme,
349}
350
351impl InteractivePrompt {
352 #[must_use]
353 pub fn new() -> Self {
354 Self::default()
355 }
356
357 pub fn run(&self) -> Result<()> {
359 println!("š« vibe-ticket Interactive Mode");
360 println!("Type 'help' for commands, 'exit' to quit\n");
361
362 loop {
363 let input = Input::<String>::with_theme(&self.theme)
364 .with_prompt("vibe-ticket>")
365 .interact()?;
366
367 let parts: Vec<&str> = input.split_whitespace().collect();
368 let Some(&command) = parts.first() else {
369 continue;
370 };
371
372 match command {
373 "help" => self.show_help(),
374 "create" => {
375 let mode = InteractiveMode::new();
376 match mode.create_ticket() {
377 Ok(data) => {
378 println!("ā
Ticket created: {}", data.title);
379 },
381 Err(e) => println!("ā Error: {e}"),
382 }
383 },
384 "list" => println!("š Listing tickets..."), "work-on" => println!("š§ Starting work..."), "exit" | "quit" => {
387 println!("š Goodbye!");
388 break;
389 },
390 _ => println!("ā Unknown command. Type 'help' for available commands."),
391 }
392 }
393
394 Ok(())
395 }
396
397 fn show_help(&self) {
398 println!("\nš Available Commands:");
399 println!(" create - Create a new ticket interactively");
400 println!(" list - List all tickets");
401 println!(" work-on - Start working on a ticket");
402 println!(" finish - Complete current ticket");
403 println!(" help - Show this help message");
404 println!(" exit - Exit interactive mode\n");
405 }
406}