syncable-cli 0.37.1

A Rust-based CLI that analyzes code repositories and generates Infrastructure as Code configurations
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
//! Diff rendering for file change confirmation
//!
//! Provides visual diff display for file modifications, showing
//! additions in green and deletions in red with line numbers.
//!
//! When an IDE companion extension is connected, diffs can be shown
//! in the IDE's native diff viewer for a better experience.

use colored::Colorize;
use crossterm::{cursor, execute, terminal};
use inquire::ui::{Color, IndexPrefix, RenderConfig, StyleSheet, Styled};
use similar::{ChangeTag, TextDiff};
use std::io::{self, Write};

use crate::agent::ide::{DiffResult, IdeClient};

/// Safely truncate a string to a maximum number of characters (not bytes)
/// This avoids panics when slicing UTF-8 strings with multi-byte characters
fn truncate_str(s: &str, max_chars: usize) -> String {
    if s.chars().count() <= max_chars {
        s.to_string()
    } else {
        let truncated: String = s.chars().take(max_chars.saturating_sub(3)).collect();
        format!("{}...", truncated)
    }
}

/// Get custom render config for file confirmation prompts
fn get_file_confirmation_render_config() -> RenderConfig<'static> {
    RenderConfig::default()
        .with_highlighted_option_prefix(Styled::new("> ").with_fg(Color::LightCyan))
        .with_option_index_prefix(IndexPrefix::Simple)
        .with_selected_option(Some(StyleSheet::new().with_fg(Color::LightCyan)))
}

/// Render a diff between old and new content
pub fn render_diff(old_content: &str, new_content: &str, filename: &str) {
    let term_width = term_size::dimensions().map(|(w, _)| w).unwrap_or(80);
    let box_width = term_width.min(80);
    let inner_width = box_width - 4;

    // Header
    let header = format!(" {} ", filename);
    let header_len = header.len();
    let left_dashes = (inner_width.saturating_sub(header_len)) / 2;
    let right_dashes = inner_width
        .saturating_sub(header_len)
        .saturating_sub(left_dashes);

    println!(
        "{}{}{}{}{}",
        "".dimmed(),
        "".repeat(left_dashes).dimmed(),
        header.white().bold(),
        "".repeat(right_dashes).dimmed(),
        "".dimmed()
    );

    let diff = TextDiff::from_lines(old_content, new_content);
    let mut old_line = 1usize;
    let mut new_line = 1usize;

    for change in diff.iter_all_changes() {
        let (line_num_display, prefix, content, style) = match change.tag() {
            ChangeTag::Delete => {
                let ln = format!("{:>4}", old_line);
                old_line += 1;
                (ln, "-", change.value().trim_end(), "red")
            }
            ChangeTag::Insert => {
                let ln = format!("{:>4}", new_line);
                new_line += 1;
                (ln, "+", change.value().trim_end(), "green")
            }
            ChangeTag::Equal => {
                let ln = format!("{:>4}", new_line);
                old_line += 1;
                new_line += 1;
                (ln, " ", change.value().trim_end(), "normal")
            }
        };

        // Truncate content if needed (using character count, not bytes)
        let max_content_len = inner_width.saturating_sub(8); // line num + prefix + spaces
        let truncated = truncate_str(content, max_content_len);

        match style {
            "red" => println!(
                "{} {} {} {}",
                "".dimmed(),
                line_num_display.dimmed(),
                prefix.red().bold(),
                truncated.red()
            ),
            "green" => println!(
                "{} {} {} {}",
                "".dimmed(),
                line_num_display.dimmed(),
                prefix.green().bold(),
                truncated.green()
            ),
            _ => println!(
                "{} {} {} {}",
                "".dimmed(),
                line_num_display.dimmed(),
                prefix,
                truncated
            ),
        }
    }

    // Footer
    println!(
        "{}{}{}",
        "".dimmed(),
        "".repeat(box_width - 2).dimmed(),
        "".dimmed()
    );
    println!();

    let _ = io::stdout().flush();
}

/// Render a new file (all additions)
pub fn render_new_file(content: &str, filename: &str) {
    let term_width = term_size::dimensions().map(|(w, _)| w).unwrap_or(80);
    let box_width = term_width.min(80);
    let inner_width = box_width - 4;

    // Header with "new file" indicator
    let header = format!(" {} (new file) ", filename);
    let header_len = header.len();
    let left_dashes = (inner_width.saturating_sub(header_len)) / 2;
    let right_dashes = inner_width
        .saturating_sub(header_len)
        .saturating_sub(left_dashes);

    println!(
        "{}{}{}{}{}",
        "".dimmed(),
        "".repeat(left_dashes).dimmed(),
        header.green().bold(),
        "".repeat(right_dashes).dimmed(),
        "".dimmed()
    );

    // Show first N lines as preview
    const MAX_PREVIEW_LINES: usize = 20;
    let lines: Vec<&str> = content.lines().collect();
    let show_truncation = lines.len() > MAX_PREVIEW_LINES;

    for (i, line) in lines.iter().take(MAX_PREVIEW_LINES).enumerate() {
        let line_num = format!("{:>4}", i + 1);
        let max_content_len = inner_width.saturating_sub(8);
        let truncated = truncate_str(line, max_content_len);

        println!(
            "{} {} {} {}",
            "".dimmed(),
            line_num.dimmed(),
            "+".green().bold(),
            truncated.green()
        );
    }

    if show_truncation {
        let remaining = lines.len() - MAX_PREVIEW_LINES;
        println!(
            "{} {} {} {}",
            "".dimmed(),
            "    ".dimmed(),
            "...".dimmed(),
            format!("({} more lines)", remaining).dimmed()
        );
    }

    // Footer
    println!(
        "{}{}{}",
        "".dimmed(),
        "".repeat(box_width - 2).dimmed(),
        "".dimmed()
    );
    println!();

    let _ = io::stdout().flush();
}

/// Confirm file write with diff display and optional IDE integration
pub fn confirm_file_write(
    path: &str,
    old_content: Option<&str>,
    new_content: &str,
) -> crate::agent::ui::confirmation::ConfirmationResult {
    use crate::agent::ui::confirmation::ConfirmationResult;
    use inquire::{InquireError, Select, Text};

    // Show terminal diff
    match old_content {
        Some(old) => render_diff(old, new_content, path),
        None => render_new_file(new_content, path),
    };

    let options = vec![
        "Yes, allow once".to_string(),
        "Yes, allow always".to_string(),
        "Type here to suggest changes".to_string(),
    ];

    println!("{}", "Apply this change?".white());

    let selection = Select::new("", options.clone())
        .with_render_config(get_file_confirmation_render_config())
        .with_page_size(3)  // Show all 3 options
        .with_help_message("↑↓ to move, Enter to select, Esc to cancel")
        .prompt();

    match selection {
        Ok(answer) => {
            if answer == options[0] {
                ConfirmationResult::Proceed
            } else if answer == options[1] {
                // Allow always for this file pattern
                let filename = std::path::Path::new(path)
                    .file_name()
                    .map(|n| n.to_string_lossy().to_string())
                    .unwrap_or_else(|| path.to_string());
                ConfirmationResult::ProceedAlways(filename)
            } else {
                // User wants to type feedback
                println!();
                match Text::new("What changes would you like?")
                    .with_help_message("Press Enter to submit, Esc to cancel")
                    .prompt()
                {
                    Ok(feedback) if !feedback.trim().is_empty() => {
                        ConfirmationResult::Modify(feedback)
                    }
                    _ => ConfirmationResult::Cancel,
                }
            }
        }
        Err(InquireError::OperationCanceled) | Err(InquireError::OperationInterrupted) => {
            ConfirmationResult::Cancel
        }
        Err(_) => ConfirmationResult::Cancel,
    }
}

/// Confirm file write with IDE integration
///
/// When an IDE client is connected, this shows BOTH:
/// 1. The diff in the IDE's native diff viewer
/// 2. A terminal menu for confirmation
///
/// The user can respond from either place - first response wins.
///
/// # Arguments
/// * `path` - Path to the file being modified
/// * `old_content` - Current file content (None for new files)
/// * `new_content` - Proposed new content
/// * `ide_client` - Optional IDE client for native diff viewing
///
/// # Returns
/// A `ConfirmationResult` indicating the user's decision
pub async fn confirm_file_write_with_ide(
    path: &str,
    old_content: Option<&str>,
    new_content: &str,
    ide_client: Option<&IdeClient>,
) -> crate::agent::ui::confirmation::ConfirmationResult {
    use crate::agent::ui::confirmation::ConfirmationResult;
    use inquire::{InquireError, Select, Text};
    use tokio::sync::oneshot;

    // Show terminal diff first
    match old_content {
        Some(old) => render_diff(old, new_content, path),
        None => render_new_file(new_content, path),
    };

    // Try to open IDE diff if connected (non-blocking)
    let ide_connected = ide_client.map(|c| c.is_connected()).unwrap_or(false);

    if ide_connected {
        let client = ide_client.unwrap();

        // Convert to absolute path for IDE
        let abs_path = std::path::Path::new(path)
            .canonicalize()
            .map(|p| p.to_string_lossy().to_string())
            .unwrap_or_else(|_| path.to_string());

        // Create channels for communication
        let (terminal_tx, terminal_rx) = oneshot::channel::<ConfirmationResult>();
        let (cancel_tx, _cancel_rx) = oneshot::channel::<()>();
        let (menu_ready_tx, menu_ready_rx) = oneshot::channel::<()>();

        // Spawn terminal input on blocking thread
        let path_owned = path.to_string();
        let _ide_name = client.ide_name().unwrap_or("IDE").to_string();
        let terminal_handle = tokio::task::spawn_blocking(move || {
            let options = vec![
                "Yes, allow once".to_string(),
                "Yes, allow always".to_string(),
                "Type here to suggest changes".to_string(),
            ];

            println!("{}", "Apply this change?".white());

            // Signal that the menu is about to be displayed
            let _ = menu_ready_tx.send(());

            let selection = Select::new("", options.clone())
                .with_render_config(get_file_confirmation_render_config())
                .with_page_size(3)
                .with_help_message("↑↓ to move, Enter to select, Esc to cancel (or use IDE)")
                .prompt();

            let result = match selection {
                Ok(answer) => {
                    if answer == options[0] {
                        ConfirmationResult::Proceed
                    } else if answer == options[1] {
                        let filename = std::path::Path::new(&path_owned)
                            .file_name()
                            .map(|n| n.to_string_lossy().to_string())
                            .unwrap_or_else(|| path_owned.clone());
                        ConfirmationResult::ProceedAlways(filename)
                    } else {
                        // User wants to type feedback
                        println!();
                        match Text::new("What changes would you like?")
                            .with_help_message("Press Enter to submit, Esc to cancel")
                            .prompt()
                        {
                            Ok(feedback) if !feedback.trim().is_empty() => {
                                ConfirmationResult::Modify(feedback)
                            }
                            _ => ConfirmationResult::Cancel,
                        }
                    }
                }
                Err(InquireError::OperationCanceled) | Err(InquireError::OperationInterrupted) => {
                    ConfirmationResult::Cancel
                }
                Err(_) => ConfirmationResult::Cancel,
            };

            let _ = terminal_tx.send(result);
        });

        // Wait for terminal menu to be ready before opening IDE diff
        let _ = menu_ready_rx.await;

        // Small delay to ensure terminal has rendered the Select prompt
        tokio::time::sleep(std::time::Duration::from_millis(50)).await;

        // Now open IDE diff
        let ide_future = client.open_diff(&abs_path, new_content);

        // Race: first response wins
        tokio::select! {
            // IDE responded
            ide_result = ide_future => {
                // Cancel terminal input (it's blocking so we can't really cancel it,
                // but we'll ignore its result)
                let _ = cancel_tx.send(());
                terminal_handle.abort();

                // CRITICAL: Reset terminal state since abort() doesn't let inquire cleanup
                // This prevents terminal corruption when rendering subsequent diffs
                let _ = terminal::disable_raw_mode();
                let _ = execute!(
                    std::io::stdout(),
                    cursor::Show,
                    terminal::Clear(terminal::ClearType::FromCursorDown)
                );
                print!("\r");
                let _ = std::io::stdout().flush();

                match ide_result {
                    Ok(DiffResult::Accepted { content: _ }) => {
                        println!("\n{} Changes accepted in IDE", "".green());
                        return ConfirmationResult::Proceed;
                    }
                    Ok(DiffResult::Rejected) => {
                        println!("\n{} Changes rejected in IDE", "".red());
                        return ConfirmationResult::Cancel;
                    }
                    Err(e) => {
                        println!("\n{} IDE error: {}", "!".yellow(), e);
                        // IDE failed but terminal thread is already running
                        // Just return Cancel - user can retry
                        return ConfirmationResult::Cancel;
                    }
                }
            }
            // Terminal responded
            terminal_result = terminal_rx => {
                // Close IDE diff
                let _ = client.close_diff(&abs_path).await;

                match terminal_result {
                    Ok(result) => {
                        match &result {
                            ConfirmationResult::Proceed => {
                                println!("{} Changes accepted", "".green());
                            }
                            ConfirmationResult::ProceedAlways(_) => {
                                println!("{} Changes accepted (always for this file type)", "".green());
                            }
                            ConfirmationResult::Cancel => {
                                println!("{} Changes cancelled", "".red());
                            }
                            ConfirmationResult::Modify(_) => {
                                println!("{} Feedback provided", "".cyan());
                            }
                        }
                        return result;
                    }
                    Err(_) => {
                        return ConfirmationResult::Cancel;
                    }
                }
            }
        }
    }

    // No IDE connection - just use terminal
    confirm_file_write(path, old_content, new_content)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_diff_render_doesnt_panic() {
        let old = "line 1\nline 2\nline 3";
        let new = "line 1\nmodified line 2\nline 3\nline 4";
        // Just verify it doesn't panic
        render_diff(old, new, "test.txt");
    }

    #[test]
    fn test_new_file_render_doesnt_panic() {
        let content = "new content\nline 2";
        render_new_file(content, "new_file.txt");
    }
}