prompthive 0.2.8

Open source prompt manager for developers. Terminal-native, sub-15ms operations, works with any AI tool.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
use anyhow::Result;
use crossterm::{
    event::{self, DisableMouseCapture, EnableMouseCapture, Event, KeyCode},
    execute,
    terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
};
use fuzzy_matcher::FuzzyMatcher;
use fuzzy_matcher::skim::SkimMatcherV2;
use ratatui::{
    backend::{Backend, CrosstermBackend},
    layout::{Alignment, Constraint, Direction, Layout, Rect},
    style::{Color, Modifier, Style},
    text::{Line, Span},
    widgets::{Block, Borders, List, ListItem, ListState, Paragraph, Wrap},
    Frame, Terminal,
};
use std::{
    io,
    time::{Duration, Instant},
};

use crate::{Clipboard, Storage};

#[derive(Debug, Clone)]
pub struct Prompt {
    pub name: String,
    pub description: String,
    pub content: String,
    pub created_at: Option<String>,
    pub updated_at: Option<String>,
    pub tags: Vec<String>,
}

pub struct PromptTui {
    prompts: Vec<Prompt>,
    filtered: Vec<usize>,
    list_state: ListState,
    search_query: String,
    mode: Mode,
    preview_scroll: u16,
    status_message: Option<(String, Instant)>,
    fuzzy_matcher: SkimMatcherV2,
    pending_delete: Option<String>,
}

#[derive(Debug, Clone, Copy, PartialEq)]
enum Mode {
    Normal,
    Search,
}

impl PromptTui {
    pub fn new(storage: &Storage) -> Result<Self> {
        let prompts = load_prompts(storage)?;
        let filtered: Vec<usize> = (0..prompts.len()).collect();
        
        let mut list_state = ListState::default();
        if !prompts.is_empty() {
            list_state.select(Some(0));
        }

        Ok(Self {
            prompts,
            filtered,
            list_state,
            search_query: String::new(),
            mode: Mode::Normal,
            preview_scroll: 0,
            status_message: None,
            fuzzy_matcher: SkimMatcherV2::default(),
            pending_delete: None,
        })
    }

    pub fn set_initial_search(&mut self, query: &str) {
        self.search_query = query.to_string();
        self.mode = Mode::Search;
        self.update_filter();
        
        // Select first match if any
        if !self.filtered.is_empty() {
            self.list_state.select(Some(0));
        }
    }

    pub fn run(mut self, storage: &Storage) -> Result<()> {
        // Setup terminal
        enable_raw_mode()?;
        let mut stdout = io::stdout();
        execute!(stdout, EnterAlternateScreen, EnableMouseCapture)?;
        let backend = CrosstermBackend::new(stdout);
        let mut terminal = Terminal::new(backend)?;

        // Run app
        let res = self.run_app(&mut terminal, storage);

        // Restore terminal
        disable_raw_mode()?;
        execute!(
            terminal.backend_mut(),
            LeaveAlternateScreen,
            DisableMouseCapture
        )?;
        terminal.show_cursor()?;

        if let Err(err) = res {
            eprintln!("Error: {}", err);
        }

        Ok(())
    }

    fn run_app<B: Backend>(&mut self, terminal: &mut Terminal<B>, storage: &Storage) -> Result<()> {
        loop {
            terminal.draw(|f| self.ui(f))?;

            if let Event::Key(key) = event::read()? {
                match self.mode {
                    Mode::Normal => match key.code {
                        KeyCode::Char('q') => return Ok(()),
                        KeyCode::Char('/') => {
                            self.mode = Mode::Search;
                            self.search_query.clear();
                        }
                        KeyCode::Char('j') | KeyCode::Down => self.next(),
                        KeyCode::Char('k') | KeyCode::Up => self.previous(),
                        KeyCode::Char('g') => self.first(),
                        KeyCode::Char('G') => self.last(),
                        KeyCode::Enter | KeyCode::Char('u') | KeyCode::Char('l') => self.use_prompt()?,
                        KeyCode::Char('s') => self.show_prompt()?,
                        KeyCode::Char('e') => self.edit_prompt(storage)?,
                        KeyCode::Char('d') => {
                            if self.pending_delete.is_some() {
                                self.confirm_delete(storage)?
                            } else {
                                self.delete_prompt(storage)?
                            }
                        }
                        KeyCode::Char('n') => self.new_prompt(storage)?,
                        KeyCode::Char('h') => {
                            // For now, h just shows a message. Later it can navigate back from detail view
                            self.status_message = Some((
                                "Press 'q' to quit, '/' to search".to_string(),
                                Instant::now(),
                            ));
                        }
                        KeyCode::Char('?') => self.show_help(),
                        KeyCode::PageDown => self.scroll_preview_down(),
                        KeyCode::PageUp => self.scroll_preview_up(),
                        _ => {
                            // Cancel delete if any other key pressed
                            if self.pending_delete.is_some() {
                                self.pending_delete = None;
                                self.status_message = Some((
                                    "Delete cancelled".to_string(),
                                    Instant::now(),
                                ));
                            }
                        }
                    },
                    Mode::Search => match key.code {
                        KeyCode::Esc => {
                            self.mode = Mode::Normal;
                            self.search_query.clear();
                            self.update_filter();
                        }
                        KeyCode::Enter => {
                            self.mode = Mode::Normal;
                            self.update_filter();
                        }
                        KeyCode::Char(c) => {
                            self.search_query.push(c);
                            self.update_filter();
                        }
                        KeyCode::Backspace => {
                            self.search_query.pop();
                            self.update_filter();
                        }
                        _ => {}
                    },
                }
            }

            // Clear status message after 3 seconds
            if let Some((_, time)) = &self.status_message {
                if time.elapsed() > Duration::from_secs(3) {
                    self.status_message = None;
                }
            }
        }
    }

    fn ui(&mut self, f: &mut Frame) {
        let chunks = Layout::default()
            .direction(Direction::Horizontal)
            .constraints([Constraint::Percentage(40), Constraint::Percentage(60)].as_ref())
            .split(f.area());

        self.draw_prompt_list(f, chunks[0]);
        self.draw_preview(f, chunks[1]);
        self.draw_status_bar(f);
    }

    fn draw_prompt_list(&mut self, f: &mut Frame, area: Rect) {
        let search_indicator = if self.mode == Mode::Search {
            format!(" [/{}]", self.search_query)
        } else if !self.search_query.is_empty() {
            format!(" [/{}]", self.search_query)
        } else {
            String::new()
        };

        let title = format!(" 📋 Prompts ({}){} ", self.filtered.len(), search_indicator);
        
        let items: Vec<ListItem> = self
            .filtered
            .iter()
            .map(|&i| {
                let prompt = &self.prompts[i];
                let content = format!("{:<20} {}", 
                    if prompt.name.len() > 20 {
                        format!("{}...", &prompt.name[..17])
                    } else {
                        prompt.name.clone()
                    },
                    prompt.description
                );
                ListItem::new(content)
            })
            .collect();

        let highlight_style = Style::default()
            .bg(Color::DarkGray)
            .add_modifier(Modifier::BOLD);

        let list = List::new(items)
            .block(Block::default().borders(Borders::ALL).title(title))
            .highlight_style(highlight_style)
            .highlight_symbol("> ");

        f.render_stateful_widget(list, area, &mut self.list_state);
    }

    fn draw_preview(&mut self, f: &mut Frame, area: Rect) {
        if let Some(selected) = self.list_state.selected() {
            if selected < self.filtered.len() {
                let prompt = &self.prompts[self.filtered[selected]];
                
                let mut content = vec![
                    Line::from(vec![Span::styled(
                        format!("# {}", prompt.name),
                        Style::default().add_modifier(Modifier::BOLD),
                    )]),
                    Line::from(""),
                ];

                // Add content lines
                for line in prompt.content.lines() {
                    content.push(Line::from(line.to_string()));
                }

                // Add metadata
                content.push(Line::from(""));
                if let Some(created) = &prompt.created_at {
                    content.push(Line::from(vec![
                        Span::raw("Created: "),
                        Span::styled(created, Style::default().fg(Color::DarkGray)),
                    ]));
                }
                if let Some(updated) = &prompt.updated_at {
                    content.push(Line::from(vec![
                        Span::raw("Updated: "),
                        Span::styled(updated, Style::default().fg(Color::DarkGray)),
                    ]));
                }
                if !prompt.tags.is_empty() {
                    content.push(Line::from(vec![
                        Span::raw("Tags: "),
                        Span::styled(
                            prompt.tags.join(", "),
                            Style::default().fg(Color::DarkGray),
                        ),
                    ]));
                }

                let paragraph = Paragraph::new(content)
                    .block(Block::default().borders(Borders::ALL).title(" Preview "))
                    .wrap(Wrap { trim: true })
                    .scroll((self.preview_scroll, 0));

                f.render_widget(paragraph, area);
            }
        } else {
            let paragraph = Paragraph::new("No prompt selected")
                .block(Block::default().borders(Borders::ALL).title(" Preview "))
                .alignment(Alignment::Center);
            f.render_widget(paragraph, area);
        }
    }

    fn draw_status_bar(&mut self, f: &mut Frame) {
        let area = Rect {
            x: 0,
            y: f.area().height - 1,
            width: f.area().width,
            height: 1,
        };

        let help_text = if let Some((msg, _)) = &self.status_message {
            msg.clone()
        } else {
            match self.mode {
                Mode::Normal => {
                    " [↑↓/jk/hl] Navigate  [/] Search  [Enter/u/l] Use  [e] Edit  [n] New  [q] Quit".to_string()
                }
                Mode::Search => {
                    " [Esc] Cancel  [Enter] Confirm  Type to search...".to_string()
                }
            }
        };

        let help = Paragraph::new(help_text)
            .style(Style::default().bg(Color::DarkGray).fg(Color::White));
        
        f.render_widget(help, area);
    }

    fn next(&mut self) {
        if self.filtered.is_empty() {
            return;
        }
        
        let i = match self.list_state.selected() {
            Some(i) => {
                if i >= self.filtered.len() - 1 {
                    0
                } else {
                    i + 1
                }
            }
            None => 0,
        };
        self.list_state.select(Some(i));
        self.preview_scroll = 0;
    }

    fn previous(&mut self) {
        if self.filtered.is_empty() {
            return;
        }
        
        let i = match self.list_state.selected() {
            Some(i) => {
                if i == 0 {
                    self.filtered.len() - 1
                } else {
                    i - 1
                }
            }
            None => 0,
        };
        self.list_state.select(Some(i));
        self.preview_scroll = 0;
    }

    fn first(&mut self) {
        if !self.filtered.is_empty() {
            self.list_state.select(Some(0));
            self.preview_scroll = 0;
        }
    }

    fn last(&mut self) {
        if !self.filtered.is_empty() {
            self.list_state.select(Some(self.filtered.len() - 1));
            self.preview_scroll = 0;
        }
    }

    fn scroll_preview_down(&mut self) {
        self.preview_scroll = self.preview_scroll.saturating_add(5);
    }

    fn scroll_preview_up(&mut self) {
        self.preview_scroll = self.preview_scroll.saturating_sub(5);
    }

    fn update_filter(&mut self) {
        if self.search_query.is_empty() {
            self.filtered = (0..self.prompts.len()).collect();
        } else {
            self.filtered = self
                .prompts
                .iter()
                .enumerate()
                .filter_map(|(i, prompt)| {
                    let search_text = format!("{} {} {}", 
                        prompt.name, 
                        prompt.description, 
                        prompt.content
                    );
                    
                    if self.fuzzy_matcher.fuzzy_match(&search_text, &self.search_query).is_some() {
                        Some(i)
                    } else {
                        None
                    }
                })
                .collect();
        }

        // Reset selection if needed
        if self.filtered.is_empty() {
            self.list_state.select(None);
        } else if self.list_state.selected().map_or(true, |s| s >= self.filtered.len()) {
            self.list_state.select(Some(0));
        }
    }

    fn use_prompt(&mut self) -> Result<()> {
        if let Some(selected) = self.list_state.selected() {
            if selected < self.filtered.len() {
                let prompt = &self.prompts[self.filtered[selected]];
                let mut clipboard = Clipboard::new();
                clipboard.copy_to_clipboard(&prompt.content)?;
                
                self.status_message = Some((
                    format!("✓ Copied '{}' to clipboard", prompt.name),
                    Instant::now(),
                ));
            }
        }
        Ok(())
    }

    fn show_prompt(&mut self) -> Result<()> {
        if let Some(selected) = self.list_state.selected() {
            if selected < self.filtered.len() {
                let prompt = &self.prompts[self.filtered[selected]];
                self.status_message = Some((
                    format!("Showing '{}' (press any key to continue)", prompt.name),
                    Instant::now(),
                ));
            }
        }
        Ok(())
    }

    fn edit_prompt(&mut self, storage: &Storage) -> Result<()> {
        if let Some(selected) = self.list_state.selected() {
            if selected < self.filtered.len() {
                let prompt_name = self.prompts[self.filtered[selected]].name.clone();
                
                // Launch editor
                let editor = std::env::var("EDITOR").unwrap_or_else(|_| "vim".to_string());
                let prompt_path = storage.prompt_path(&prompt_name);
                
                // Temporarily leave TUI mode
                disable_raw_mode()?;
                execute!(
                    io::stdout(),
                    LeaveAlternateScreen,
                    DisableMouseCapture
                )?;
                
                // Run editor
                std::process::Command::new(&editor)
                    .arg(&prompt_path)
                    .status()?;
                
                // Restore TUI mode
                enable_raw_mode()?;
                execute!(
                    io::stdout(),
                    EnterAlternateScreen,
                    EnableMouseCapture
                )?;
                
                // Reload prompts
                self.prompts = load_prompts(storage)?;
                self.update_filter();
                
                self.status_message = Some((
                    format!("✓ Edited '{}'", prompt_name),
                    Instant::now(),
                ));
            }
        }
        Ok(())
    }

    fn delete_prompt(&mut self, _storage: &Storage) -> Result<()> {
        if let Some(selected) = self.list_state.selected() {
            if selected < self.filtered.len() {
                let prompt_name = self.prompts[self.filtered[selected]].name.clone();
                
                // Simple confirmation in status
                self.status_message = Some((
                    format!("Press 'd' again to delete '{}' or any other key to cancel", prompt_name),
                    Instant::now(),
                ));
                
                // Set delete pending state
                self.pending_delete = Some(prompt_name);
            }
        }
        Ok(())
    }
    
    fn confirm_delete(&mut self, storage: &Storage) -> Result<()> {
        if let Some(prompt_name) = self.pending_delete.take() {
            // Delete the prompt
            storage.delete_prompt(&prompt_name)?;
            
            // Reload prompts
            self.prompts = load_prompts(storage)?;
            self.update_filter();
            
            self.status_message = Some((
                format!("✓ Deleted '{}'", prompt_name),
                Instant::now(),
            ));
        }
        Ok(())
    }

    fn new_prompt(&mut self, _storage: &Storage) -> Result<()> {
        self.status_message = Some((
            "New prompt creation not yet implemented in TUI".to_string(),
            Instant::now(),
        ));
        Ok(())
    }

    fn show_help(&mut self) {
        self.status_message = Some((
            "PromptHive TUI v1.0 - The fastest way to manage prompts".to_string(),
            Instant::now(),
        ));
    }
}

fn load_prompts(storage: &Storage) -> Result<Vec<Prompt>> {
    let prompt_names = storage.list_prompts()?;
    let mut prompts = Vec::new();

    for name in prompt_names {
        if let Ok((metadata, content)) = storage.read_prompt(&name) {
            prompts.push(Prompt {
                name: name.clone(),
                description: metadata.description,
                content,
                created_at: metadata.created_at,
                updated_at: metadata.updated_at,
                tags: metadata.tags.unwrap_or_default(),
            });
        }
    }

    Ok(prompts)
}