cargo-quality 0.2.0

Professional Rust code quality analysis tool with hardcoded standards
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
// SPDX-FileCopyrightText: 2025 RAprogramm <andrey.rozanov.vl@gmail.com>
// SPDX-License-Identifier: MIT

//! Professional responsive diff display with grid layout.
//!
//! This module provides a sophisticated diff visualization system that adapts
//! to terminal width, offering newspaper-style column layouts for optimal
//! screen space utilization. Features include intelligent import grouping,
//! ANSI-aware text measurement, and zero-allocation rendering paths.
//!
//! # Architecture
//!
//! The display system is organized into specialized modules:
//!
//! - `types` - Core data structures for rendered output
//! - `formatting` - Text padding and width calculation
//! - `grouping` - Import deduplication and intelligent grouping
//! - `grid` - Responsive column layout calculations
//! - `render` - File diff block rendering
//!
//! # Performance
//!
//! - Pre-allocated vectors with estimated capacities
//! - Single-pass width calculations
//! - ANSI-aware measurements using `console` crate
//! - Minimal string allocations
//! - Zero-cost abstractions for layout logic
//!
//! # Examples
//!
//! ```no_run
//! use cargo_quality::differ::{DiffResult, display::show_full};
//!
//! let result = DiffResult::new();
//! show_full(&result, false);
//! ```

pub mod formatting;
pub mod grid;
pub mod grouping;
pub mod render;
pub mod types;

// Re-export key types and functions for public API
use std::{
    collections::HashMap,
    io::{self, Write}
};

use masterror::AppResult;
use owo_colors::OwoColorize;
use terminal_size::{Width, terminal_size};

pub use self::{
    grid::{calculate_columns, render_grid},
    render::render_file_block
};
use super::types::{DiffEntry, DiffResult};
use crate::error::IoError;

/// Displays diff in summary mode with brief statistics.
///
/// Shows a compact overview of changes grouped by file and analyzer,
/// providing quick insight into the scope of modifications without
/// showing detailed line-by-line changes.
///
/// # Output Format
///
/// ```text
/// DIFF SUMMARY
///
/// file1.rs:
///   analyzer1: 3 issues
///   analyzer2: 1 issue
///
/// file2.rs:
///   analyzer1: 2 issues
///
/// Total: 6 changes in 2 files
/// ```
///
/// # Arguments
///
/// * `result` - Diff results to display
///
/// # Examples
///
/// ```no_run
/// use cargo_quality::differ::{DiffResult, display::show_summary};
///
/// let result = DiffResult::new();
/// show_summary(&result, false);
/// ```
pub fn show_summary(result: &DiffResult, color: bool) {
    if color {
        println!("\n{}\n", "DIFF SUMMARY".bold());
    } else {
        println!("\nDIFF SUMMARY\n");
    }

    for file in &result.files {
        if color {
            println!("{}:", file.path.cyan().bold());
        } else {
            println!("{}:", file.path);
        }

        let mut analyzer_counts = HashMap::new();
        for entry in &file.entries {
            *analyzer_counts.entry(&entry.analyzer).or_insert(0) += 1;
        }

        for (analyzer, count) in analyzer_counts {
            if color {
                println!(
                    "  {}: {} {}",
                    analyzer.green(),
                    count,
                    if count == 1 { "issue" } else { "issues" }
                );
            } else {
                println!(
                    "  {}: {} {}",
                    analyzer,
                    count,
                    if count == 1 { "issue" } else { "issues" }
                );
            }
        }
        println!();
    }

    let summary = format!(
        "Total: {} changes in {} files",
        result.total_changes(),
        result.total_files()
    );

    if color {
        println!("{}", summary.yellow().bold());
    } else {
        println!("{}", summary);
    }
}

/// Displays full responsive diff output with adaptive grid layout.
///
/// Automatically arranges file diffs in newspaper-style columns based on
/// terminal width. On narrow terminals, displays one file per row. On wider
/// terminals, arranges multiple files side-by-side for efficient space usage.
///
/// # Layout Modes
///
/// - **Narrow** (< 100 chars): Single column, vertical stacking
/// - **Medium** (100-200 chars): 2 columns side-by-side
/// - **Wide** (> 200 chars): 3+ columns based on content width
///
/// # Arguments
///
/// * `result` - Diff results to display
///
/// # Performance
///
/// - Pre-renders all files once
/// - Calculates optimal column count based on terminal width
/// - Uses ANSI-aware padding for perfect alignment
/// - Minimal allocations during grid rendering
///
/// # Examples
///
/// ```no_run
/// use cargo_quality::differ::{DiffResult, display::show_full};
///
/// let result = DiffResult::new();
/// show_full(&result, false);
/// ```
pub fn show_full(result: &DiffResult, color: bool) {
    if color {
        println!("\n{}\n", "DIFF OUTPUT".bold());
    } else {
        println!("\nDIFF OUTPUT\n");
    }

    let term_width = terminal_size()
        .map(|(Width(w), _)| w as usize)
        .unwrap_or(80);

    let rendered: Vec<_> = result
        .files
        .iter()
        .map(|f| render_file_block(f, color))
        .collect();

    let columns = calculate_columns(&rendered, term_width);

    if columns > 1 {
        let layout_info = format!(
            "Layout: {} columns (terminal width: {})",
            columns, term_width
        );

        if color {
            println!("{}\n", layout_info.dimmed());
        } else {
            println!("{}\n", layout_info);
        }
    }

    render_grid(&rendered, columns);

    let summary = format!(
        "Total: {} changes in {} files",
        result.total_changes(),
        result.total_files()
    );

    if color {
        println!("{}", summary.yellow().bold());
    } else {
        println!("{}", summary);
    }
}

/// Displays interactive diff with user prompts for selective application.
///
/// Presents each change individually and asks for user confirmation before
/// applying. Supports batch operations (apply all, quit) for efficiency.
///
/// # Commands
///
/// - `y` / `yes` - Apply this change
/// - `n` / `no` - Skip this change
/// - `a` / `all` - Apply all remaining changes
/// - `q` / `quit` - Exit without processing remaining changes
///
/// # Arguments
///
/// * `result` - Diff results to display
///
/// # Returns
///
/// `AppResult<Vec<DiffEntry>>` - Selected entries for application, or error
///
/// # Errors
///
/// Returns error if I/O operations fail during user input reading.
///
/// # Examples
///
/// ```no_run
/// use cargo_quality::differ::{DiffResult, display::show_interactive};
///
/// let result = DiffResult::new();
/// let selected = show_interactive(&result, false).unwrap();
/// println!("Selected {} changes", selected.len());
/// ```
pub fn show_interactive(result: &DiffResult, color: bool) -> AppResult<Vec<DiffEntry>> {
    let mut selected = Vec::with_capacity(result.total_changes());
    let mut apply_all = false;

    if color {
        println!("\n{}\n", "INTERACTIVE DIFF".bold());
        println!("{}", "Commands: y=yes, n=no, a=all, q=quit\n".dimmed());
    } else {
        println!("\nINTERACTIVE DIFF\n");
        println!("Commands: y=yes, n=no, a=all, q=quit\n");
    }

    for file in &result.files {
        if color {
            println!("{}", format!("File: {}", file.path).cyan().bold());
        } else {
            println!("File: {}", file.path);
        }
        println!();

        for (idx, entry) in file.entries.iter().enumerate() {
            if color {
                println!(
                    "{} {}",
                    format!("[{}/{}]", idx + 1, file.entries.len()).yellow(),
                    entry.analyzer.green()
                );
                println!("{}", format!("Line {}:", entry.line).dimmed());
                println!("{}", format!("- {}", entry.original).red());

                if let Some(import) = &entry.import {
                    println!("{}", format!("+ {}", import).green());
                }

                println!("{}", format!("+ {}", entry.modified).green());
            } else {
                println!("[{}/{}] {}", idx + 1, file.entries.len(), entry.analyzer);
                println!("Line {}:", entry.line);
                println!("- {}", entry.original);

                if let Some(import) = &entry.import {
                    println!("+ {}", import);
                }

                println!("+ {}", entry.modified);
            }
            println!();

            if apply_all {
                selected.push(entry.clone());
                continue;
            }

            print!("{}", "Apply this fix? [y/n/a/q]: ".bold());
            io::stdout().flush().map_err(IoError::from)?;

            let mut input = String::new();
            io::stdin().read_line(&mut input).map_err(IoError::from)?;

            match input.trim().to_lowercase().as_str() {
                "y" | "yes" => {
                    selected.push(entry.clone());
                    println!("{}", "Applied".green());
                }
                "n" | "no" => {
                    println!("{}", "Skipped".yellow());
                }
                "a" | "all" => {
                    apply_all = true;
                    selected.push(entry.clone());
                    println!("{}", "Applying all remaining changes".green().bold());
                }
                "q" | "quit" => {
                    println!("{}", "Quit".red());
                    break;
                }
                _ => {
                    println!("{}", "Invalid input, skipping".red());
                }
            }
            println!();
        }
    }

    println!(
        "\n{}",
        format!("Selected {} changes for application", selected.len())
            .yellow()
            .bold()
    );

    Ok(selected)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::differ::types::FileDiff;

    #[test]
    fn test_show_summary_empty() {
        let result = DiffResult::new();
        show_summary(&result, false);
    }

    #[test]
    fn test_show_full_empty() {
        let result = DiffResult::new();
        show_full(&result, false);
    }

    #[test]
    fn test_show_summary_with_data() {
        let mut result = DiffResult::new();
        let mut file = FileDiff::new("test.rs".to_string());

        file.add_entry(DiffEntry {
            line:        1,
            analyzer:    "test".to_string(),
            original:    "old".to_string(),
            modified:    "new".to_string(),
            description: "desc".to_string(),
            import:      None
        });

        result.add_file(file);
        show_summary(&result, false);
    }

    #[test]
    fn test_show_full_with_data() {
        let mut result = DiffResult::new();
        let mut file = FileDiff::new("test.rs".to_string());

        file.add_entry(DiffEntry {
            line:        10,
            analyzer:    "test".to_string(),
            original:    "old".to_string(),
            modified:    "new".to_string(),
            description: "desc".to_string(),
            import:      None
        });

        result.add_file(file);
        show_full(&result, false);
    }
}