erebus 0.1.1

A CLI message generation library
Documentation
use unicode_segmentation::UnicodeSegmentation;
use yansi::Paint;

use crate::{files::FileId, style::Style, Files, Message, Render};

use super::{break_line, identifier::identifier};

impl<Id: FileId> Message<Id> {
    /// Render the message to a string.
    pub fn render(&self, files: &impl Files<FileId = Id>, style: &Style) -> String {
        let mut result = String::new();

        if let Some(severity) = &self.severity {
            let sev = severity.as_str();
            result.push_str(&severity.colorize(sev));
        }

        if let Some(id) = &self.identifier {
            result.push_str(&identifier(id, self.severity.as_ref(), style));
        }

        if self.severity.is_some() || self.identifier.is_some() {
            result.push_str(": ");
        }

        let ln_width = self
            .labels
            .iter()
            .map(|label| {
                let start = files
                    .line_from_char(&label.file, label.span.start)
                    .unwrap_or(0);
                let end = files
                    .line_from_char(&label.file, label.span.end)
                    .unwrap_or(0);
                start.max(end)
            })
            .max()
            .map(|ln| ln.to_string().len());

        result.push_str(&break_line(
            &self.text,
            result.graphemes(true).count(),
            ln_width,
            style,
        ));

        let labels = self.labels.render(ln_width, files, style);
        if !labels.is_empty() {
            result.push('\n');
            result.push_str(&labels);
        }

        for note in &self.notes {
            result.push('\n');
            result.push_str(&note.render(ln_width, files, style));
        }

        if let Some(ln_width) = ln_width {
            result.push(' ');
            result.push_str(&" ".repeat(ln_width));
            result.push(' ');
            result.push_str(style.characters.rbot.to_string().dim().to_string().as_str());
        }

        result
    }
}