#line-ending #string #indentation #multi-line #preserving #indent #auto

string-auto-indent

Normalizes multi-line string indentation while preserving platform-specific line endings

4 releases

new 0.1.1 Feb 20, 2025
0.1.0 Feb 19, 2025
0.1.0-alpha1 Feb 18, 2025
0.1.0-alpha Feb 17, 2025

#305 in Text processing

Download history 68/week @ 2025-02-12

68 downloads per month
Used in cargo-pkg-info-struct-bui…

MIT license

14KB
174 lines

Multi-line String Auto-Indent

made-with-rust crates.io Documentation MIT licensed

OS Status
Ubuntu-latest Ubuntu Tests
macOS-latest macOS Tests
Windows-latest Windows Tests

A Rust utility for automatically normalizing multi-line string indentation while preserving platform-specific line endings.

Overview

When working with multi-line strings inside indented code blocks, unwanted leading spaces may be introduced. This can affect readability, logging output, and formatted text generation.

string-auto-indent provides an automated way to normalize multi-line strings without modifying the first line's indentation.

Install

cargo add string-auto-indent

Usage

use string_auto_indent::{auto_indent, LineEnding};

let excessively_indented_text = r#"
                    Best Practices for Text Indentation
                    -----------------------------------

                        1. Importance of Proper Indentation
                            a. Enhances readability by clearly defining structure.
                            b. Prevents misinterpretation of hierarchical content.
                            c. Improves maintainability in collaborative environments.

                        2. Common Indentation Guidelines
                            a. Use consistent spacing (e.g., 2 or 4 spaces per level).
                            b. Avoid mixing spaces and tabs to ensure uniform formatting.
                            c. Align nested elements to maintain structural clarity.
                                1b. Maintain relative indentation depth across all nested elements.
                                2b. Ensure indentation reflects logical hierarchy.
            "#;


// Expected output after applying `auto_indent`
let normalized_indentation = r#"
Best Practices for Text Indentation
-----------------------------------

    1. Importance of Proper Indentation
        a. Enhances readability by clearly defining structure.
        b. Prevents misinterpretation of hierarchical content.
        c. Improves maintainability in collaborative environments.

    2. Common Indentation Guidelines
        a. Use consistent spacing (e.g., 2 or 4 spaces per level).
        b. Avoid mixing spaces and tabs to ensure uniform formatting.
        c. Align nested elements to maintain structural clarity.
            1b. Maintain relative indentation depth across all nested elements.
            2b. Ensure indentation reflects logical hierarchy.
"#;

// Verify that `auto_indent` correctly normalizes indentation
assert_eq!(
    auto_indent(excessively_indented_text),
    normalized_indentation,
    "The auto_indent function should normalize leading whitespace."
);

How It Works

  1. Detects the platform’s line endings (\n, \r\n, \r) and normalizes input for processing.
  2. Preserves the first line exactly as written.
  3. Finds the least-indented non-empty line (excluding the first) and adjusts all others accordingly.
  4. Ensures blank lines remain but contain no extra spaces.
  5. Restores platform-specific line endings when outputting the result.

When to Use

  • Formatting log messages or CLI output while ensuring alignment.
  • Cleaning up documentation strings or multi-line literals in indented Rust code.
  • Processing structured text while ensuring consistent indentation.
  • Declaring multi-line variables in code where the indentation should match the codebase for readability, but the actual string content should not retain unnecessary leading spaces.
  • Ensuring consistent formatting in generated strings for use in templates, serialization, or output rendering.

License

Licensed under MIT. See LICENSE for details.

Dependencies

~37KB