Skip to main content

render_grid

Function render_grid 

Source
pub fn render_grid(files: &[RenderedFile], columns: usize)
Expand description

Renders files in responsive grid layout.

Arranges files horizontally in columns, printing them row by row. Each file occupies one column, with rows printed until all files are displayed. Handles variable line counts by padding short files with empty lines.

§Algorithm

  1. If single column: print each file vertically
  2. Calculate column width (max file width)
  3. Process files in chunks of columns size
  4. For each chunk:
    • Find max line count
    • Print each row across all columns
    • Pad columns to align properly

§Arguments

  • files - Slice of rendered files to display
  • columns - Number of columns to use (from calculate_columns)

§Performance

  • Single allocation per output line
  • Pre-calculates maximum line count per chunk
  • Uses padding with pre-calculated widths

§Examples

use cargo_quality::differ::display::{grid::render_grid, types::RenderedFile};

let file1 = RenderedFile {
    lines: vec!["Line 1".to_string(), "Line 2".to_string()],
    width: 40
};

let file2 = RenderedFile {
    lines: vec!["Other 1".to_string(), "Other 2".to_string()],
    width: 40
};

render_grid(&[file1, file2], 2);