annotate-snippets-rs icon indicating copy to clipboard operation
annotate-snippets-rs copied to clipboard

docs: Fix and add docs for `Level`

Open icecream17 opened this issue 1 year ago • 3 comments

Have the documentation for Level reflect actual appearance.


The output from this file:

(43 lines)
//! Provides a user-friendly error if the given file has the string `tab`
//!
//! A silly utility to test annotate_snippets (and anstream)

use annotate_snippets::{Level, Renderer, Snippet};
use clap::Parser;

#[derive(Parser)]
struct Cli {
    file_path: std::path::PathBuf,
}

fn main() {
    let Cli { file_path } = Cli::parse();
    let path_string = file_path.to_string_lossy().to_string();

    // Bad error message; don't do this!
    let contents =
        std::fs::read_to_string(file_path).expect("Should have been able to read the file");

    let renderer = Renderer::styled();
    let message = if let Some(index) = contents.find("tab") {
        Level::Error.title("no tabs allowed!").snippet(
            Snippet::source(&contents)
                .line_start(1)
                .origin(&path_string)
                .fold(true)
                .annotation(
                    Level::Error
                        .span(index..(index + 3))
                        .label("Found `tab` here"),
                )
                .annotation(Level::Warning.span(index..(index + 3)).label("Warning"))
                .annotation(Level::Info.span(index..(index + 3)).label("Info"))
                .annotation(Level::Note.span(index..(index + 3)).label("Note"))
                .annotation(Level::Help.span(index..(index + 3)).label("remove `tab`")),
        )
    } else {
        Level::Info.title("No \"tabs\" found, congrats!")
    };
    anstream::println!("{}", renderer.render(message));
}

Results in this output: image

icecream17 avatar Aug 19 '24 14:08 icecream17