command_vault/cli/
commands.rs

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
use anyhow::{Result, anyhow};
use chrono::{Local, Utc};
use std::io::{self, Stdout};
use crossterm::{
    execute,
    terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
};
use ratatui::{
    backend::CrosstermBackend,
    layout::{Constraint, Direction, Layout},
    style::{Color, Style},
    text::{Line, Span},
    widgets::{Block, Borders, Paragraph},
    Terminal,
};
use colored::*;

use crate::db::{Command, Database};
use crate::ui::App;
use crate::utils::params::parse_parameters;
use crate::utils::params::substitute_parameters;
use crate::exec::{ExecutionContext, execute_shell_command};

use super::args::{Commands, TagCommands};

fn print_commands(commands: &[Command]) -> Result<()> {
    let terminal_result = setup_terminal();
    
    match terminal_result {
        Ok(mut terminal) => {
            let res = print_commands_ui(&mut terminal, commands);
            restore_terminal(&mut terminal)?;
            res
        }
        Err(_) => {
            // Fallback to simple text output
            println!("Command History:");
            println!("─────────────────────────────────────────────");
            for cmd in commands {
                let local_time = cmd.timestamp.with_timezone(&Local);
                println!("{} │ {}", local_time.format("%Y-%m-%d %H:%M:%S"), cmd.command);
                if !cmd.tags.is_empty() {
                    println!("    Tags: {}", cmd.tags.join(", "));
                }
                if !cmd.parameters.is_empty() {
                    println!("    Parameters:");
                    for param in &cmd.parameters {
                        let desc = param.description.as_deref().unwrap_or("None");
                        println!("      - {}: {} (default: {})", param.name, desc, "None");
                    }
                }
                println!("    Directory: {}", cmd.directory);
                println!();
            }
            Ok(())
        }
    }
}

fn print_commands_ui(terminal: &mut Terminal<CrosstermBackend<Stdout>>, commands: &[Command]) -> Result<()> {
    terminal.draw(|f| {
        let chunks = Layout::default()
            .direction(Direction::Vertical)
            .margin(1)
            .constraints([Constraint::Min(0)])
            .split(f.size());

        let mut lines = vec![];
        lines.push(Line::from(Span::styled(
            "Command History:",
            Style::default().fg(Color::Cyan),
        )));
        lines.push(Line::from(Span::raw("─────────────────────────────────────────────")));

        for cmd in commands {
            let local_time = cmd.timestamp.with_timezone(&Local);
            lines.push(Line::from(vec![
                Span::styled(local_time.format("%Y-%m-%d %H:%M:%S").to_string(), Style::default().fg(Color::Yellow)),
                Span::raw(" │ "),
                Span::raw(&cmd.command),
            ]));
            lines.push(Line::from(vec![
                Span::raw("    Directory: "),
                Span::raw(&cmd.directory),
            ]));
            if !cmd.tags.is_empty() {
                lines.push(Line::from(vec![
                    Span::raw("    Tags: "),
                    Span::raw(cmd.tags.join(", ")),
                ]));
            }
            lines.push(Line::from(Span::raw("─────────────────────────────────────────────")));
        }

        let paragraph = Paragraph::new(lines).block(Block::default().borders(Borders::ALL));
        f.render_widget(paragraph, chunks[0]);
    })?;
    Ok(())
}

fn setup_terminal() -> Result<Terminal<CrosstermBackend<Stdout>>> {
    enable_raw_mode()?;
    let mut stdout = io::stdout();
    execute!(stdout, EnterAlternateScreen)?;
    let backend = CrosstermBackend::new(stdout);
    Terminal::new(backend).map_err(|e| e.into())
}

fn restore_terminal(terminal: &mut Terminal<CrosstermBackend<Stdout>>) -> Result<()> {
    disable_raw_mode()?;
    execute!(terminal.backend_mut(), LeaveAlternateScreen)?;
    terminal.show_cursor()?;
    Ok(())
}

pub fn handle_command(command: Commands, db: &mut Database, debug: bool) -> Result<()> {
    match command {
        Commands::Add { command, tags } => {
            // Process command parts with special handling for git format strings
            let command_str = command.iter().enumerate().fold(String::new(), |mut acc, (i, arg)| {
                if i > 0 {
                    acc.push(' ');
                }
                // Special case for git format strings
                if arg.starts_with("--pretty=format:") {
                    acc.push_str(&format!("\"{}\"", arg));
                } else {
                    acc.push_str(arg);
                }
                acc
            });
            
            // Don't allow empty commands
            if command_str.trim().is_empty() {
                return Err(anyhow!("Cannot add empty command"));
            }
            
            // Get the current directory
            let directory = std::env::current_dir()?
                .to_string_lossy()
                .to_string();
            
            let timestamp = Local::now().with_timezone(&Utc);
            
            // Parse parameters from command string
            let parameters = parse_parameters(&command_str);
            
            let cmd = Command {
                id: None,
                command: command_str.clone(),
                timestamp,
                directory,
                tags,
                parameters,
            };
            let id = db.add_command(&cmd)?;
            println!("Command added to history with ID: {}", id);
            
            // If command has parameters, show them
            if !cmd.parameters.is_empty() {
                println!("\nDetected parameters:");
                for param in &cmd.parameters {
                    let desc = param.description.as_deref().unwrap_or("None");
                    println!("  {} - Description: {}", param.name.yellow(), desc);
                }
            }
        }
        Commands::Search { query, limit } => {
            let commands = db.search_commands(&query, limit)?;
            let mut app = App::new(commands.clone(), db, debug);
            match app.run() {
                Ok(_) => (),
                Err(e) => {
                    if e.to_string() == "Operation cancelled by user" {
                        print!("\n{}", "Operation cancelled.".yellow());
                        return Ok(());
                    }
                    eprintln!("Failed to start TUI mode: {}", e);
                    print_commands(&commands)?;
                }
            }
        }
        Commands::Ls { limit, asc } => {
            let commands = db.list_commands(limit, asc)?;
            if commands.is_empty() {
                print!("No commands found.");
                return Ok(());
            }

            // Check if TUI should be disabled (useful for testing or non-interactive environments)
            if std::env::var("COMMAND_VAULT_NO_TUI").is_ok() {
                for cmd in commands {
                    print!("{}: {} ({})", cmd.id.unwrap_or(0), cmd.command, cmd.directory);
                }
                return Ok(());
            }

            let mut app = App::new(commands.clone(), db, debug);
            match app.run() {
                Ok(_) => (),
                Err(e) => {
                    if e.to_string() == "Operation cancelled by user" {
                        print!("\n{}", "Operation cancelled.".yellow());
                        return Ok(());
                    }
                    eprintln!("Failed to start TUI mode: {}", e);
                    print_commands(&commands)?;
                }
            }
        }
        Commands::Tag { action } => match action {
            TagCommands::Add { command_id, tags } => {
                match db.add_tags_to_command(command_id, &tags) {
                    Ok(_) => print!("Tags added successfully"),
                    Err(e) => eprintln!("Failed to add tags: {}", e),
                }
            }
            TagCommands::Remove { command_id, tag } => {
                match db.remove_tag_from_command(command_id, &tag) {
                    Ok(_) => print!("Tag removed successfully"),
                    Err(e) => eprintln!("Failed to remove tag: {}", e),
                }
            }
            TagCommands::List => {
                match db.list_tags() {
                    Ok(tags) => {
                        if tags.is_empty() {
                            print!("No tags found");
                            return Ok(());
                        }
                        
                        print!("\nTags and their usage:");
                        print!("─────────────────────────────────────────────");
                        for (tag, count) in tags {
                            print!("{}: {} command{}", tag, count, if count == 1 { "" } else { "s" });
                        }
                    }
                    Err(e) => eprintln!("Failed to list tags: {}", e),
                }
            }
            TagCommands::Search { tag, limit } => {
                match db.search_by_tag(&tag, limit) {
                    Ok(commands) => print_commands(&commands)?,
                    Err(e) => eprintln!("Failed to search by tag: {}", e),
                }
            }
        },
        Commands::Exec { command_id, debug } => {
            let command = db.get_command(command_id)?
                .ok_or_else(|| anyhow!("Command not found with ID: {}", command_id))?;
            
            // Create the directory if it doesn't exist
            if !std::path::Path::new(&command.directory).exists() {
                std::fs::create_dir_all(&command.directory)?;
            }
            
            let current_params = parse_parameters(&command.command);
            let final_command = substitute_parameters(&command.command, &current_params, None)?;

            let ctx = ExecutionContext {
                command: final_command.clone(),
                directory: command.directory.clone(),
                test_mode: std::env::var("COMMAND_VAULT_TEST").is_ok(),
                debug_mode: debug,
            };

            println!("\n─────────────────────────────────────────────");
            println!("Command to execute: {}", final_command);
            println!("Working directory: {}", command.directory);
            println!();  // Add extra newline before command output

            execute_shell_command(&ctx)?;
        }
        Commands::ShellInit { shell } => {
            let script_path = crate::shell::hooks::init_shell(shell)?;
            if !script_path.exists() {
                return Err(anyhow!("Shell integration script not found at: {}", script_path.display()));
            }
            print!("{}", script_path.display());
            return Ok(());
        },
        Commands::Delete { command_id } => {
            // First check if the command exists
            if let Some(command) = db.get_command(command_id)? {
                // Show the command that will be deleted
                println!("Deleting command:");
                print_commands(&[command])?;
                
                // Delete the command
                db.delete_command(command_id)?;
                println!("Command deleted successfully");
            } else {
                return Err(anyhow!("Command with ID {} not found", command_id));
            }
        }
    }
    Ok(())
}