flop_cli/
processor.rs

1use anyhow::Result;
2use std::io::{self, Write};
3use std::path::Path;
4
5use crate::editor::{apply_changes, delete_changes};
6use crate::finder::find_debug_printfs;
7use crate::ui::{display_matches, select_statements_interactive};
8
9pub fn process_path(
10    path: &Path,
11    uncomment: bool,
12    skip_confirm: bool,
13    detect_all: bool,
14    interactive: bool,
15    dry_run: bool,
16) -> Result<()> {
17    let matches = find_debug_printfs(path, uncomment, detect_all)?;
18
19    if matches.is_empty() {
20        println!("No matching debug statements found.");
21        return Ok(());
22    }
23
24    // Interactive mode: let user select specific statements
25    let selected_matches = if interactive {
26        select_statements_interactive(&matches)?
27    } else {
28        // Non-interactive: display and confirm
29        display_matches(&matches);
30
31        // Ask for confirmation unless --yes or --dry-run flag is set
32        if !skip_confirm && !dry_run {
33            print!(
34                "Do you want to {} these statements? (y/n): ",
35                if uncomment {
36                    "enable (uncomment)"
37                } else {
38                    "disable (comment out)"
39                }
40            );
41            io::stdout().flush()?;
42
43            let mut input = String::new();
44            io::stdin().read_line(&mut input)?;
45
46            if input.trim().to_lowercase() != "y" {
47                println!("\nOperation cancelled.");
48                return Ok(());
49            }
50        }
51
52        matches
53    };
54
55    if selected_matches.is_empty() {
56        println!("\nNo statements selected.");
57        return Ok(());
58    }
59
60    if dry_run {
61        println!(
62            "\n[DRY RUN] Would {} {} statement(s).",
63            if uncomment {
64                "enable (uncomment)"
65            } else {
66                "disable (comment out)"
67            },
68            selected_matches.len()
69        );
70    } else {
71        apply_changes(&selected_matches, uncomment)?;
72        println!(
73            "\nSuccessfully processed {} statement(s).",
74            selected_matches.len()
75        );
76    }
77
78    Ok(())
79}
80
81pub fn process_path_delete(
82    path: &Path,
83    skip_confirm: bool,
84    detect_all: bool,
85    interactive: bool,
86    dry_run: bool,
87) -> Result<()> {
88    // Find both commented and uncommented debug statements
89    let uncommented_matches = find_debug_printfs(path, false, detect_all)?;
90    let commented_matches = find_debug_printfs(path, true, detect_all)?;
91
92    // Combine both lists
93    let mut all_matches = uncommented_matches;
94    all_matches.extend(commented_matches);
95
96    if all_matches.is_empty() {
97        println!("No matching debug statements found.");
98        return Ok(());
99    }
100
101    // Interactive mode: let user select specific statements
102    let selected_matches = if interactive {
103        println!("Select statements to DELETE:");
104        select_statements_interactive(&all_matches)?
105    } else {
106        // Non-interactive: display and confirm
107        println!(
108            "\nFound {} debug statement(s) to delete:\n",
109            all_matches.len()
110        );
111        display_matches(&all_matches);
112
113        // Ask for confirmation unless --yes or --dry-run flag is set
114        if !skip_confirm && !dry_run {
115            print!("Do you want to delete these statements? (y/n): ");
116            io::stdout().flush()?;
117
118            let mut input = String::new();
119            io::stdin().read_line(&mut input)?;
120
121            if input.trim().to_lowercase() != "y" {
122                println!("\nOperation cancelled.");
123                return Ok(());
124            }
125        }
126
127        all_matches
128    };
129
130    if selected_matches.is_empty() {
131        println!("\nNo statements selected.");
132        return Ok(());
133    }
134
135    if dry_run {
136        println!(
137            "\n[DRY RUN] Would delete {} statement(s).",
138            selected_matches.len()
139        );
140    } else {
141        delete_changes(&selected_matches)?;
142        println!(
143            "\nSuccessfully deleted {} statement(s).",
144            selected_matches.len()
145        );
146    }
147
148    Ok(())
149}