Skip to main content

wright/terminal_interface/
mod.rs

1//! Some utility functions/modules shared by various binaries for things like checking unicode support.
2
3#[cfg(feature = "lexer")]
4use crate::lexer::token::Token;
5#[cfg(feature = "lexer")]
6use std::fmt::{self, Display};
7
8#[cfg(feature = "reporting")]
9pub mod reporting;
10pub mod supports_unicode;
11
12#[cfg(feature = "lexer")]
13impl Display for Token {
14    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
15        // If the host terminal supports unicode, replace the newline & carriage return characters with pictures,
16        // otherwise use ascii.
17        let replacements = match supports_unicode::supports_unicode() {
18            true => &[("\n", "\u{240A}"), ("\r", "\u{240D}")],
19            false => &[("\n", "[nl]"), ("\r", "[cr]")],
20        };
21
22        let mut with_replacements = self.fragment.as_str().to_owned();
23
24        for (replace, replace_with) in replacements {
25            with_replacements = with_replacements.replace(replace, replace_with);
26        }
27
28        write!(f, "\"{with_replacements}\" ({:?})", self.variant)
29    }
30}