From b7c861de2d1db977976dd740b21e43be0befac8d Mon Sep 17 00:00:00 2001 From: Sean Hellum Date: Wed, 13 May 2020 19:41:20 -0500 Subject: [PATCH] Cross platform as you type syntax highlighting (#12) --- Cargo.toml | 1 + src/lib.rs | 25 +++++++++++++------------ 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index b9a96d2..9da276e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,3 +11,4 @@ readme = "README.md" [dependencies] crossterm = "0.17.3" logos = "0.11" +colored = "1.9.3" diff --git a/src/lib.rs b/src/lib.rs index 3919d77..5b4734f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,8 +3,8 @@ use crossterm::{ terminal::{disable_raw_mode, enable_raw_mode}, tty::IsTty, }; +use colored::*; use std::{ - fmt, fs::{File, OpenOptions}, io::{stdin, stdout, BufRead, BufReader, Write}, path::Path, @@ -22,16 +22,16 @@ pub enum Color { Cyan, } -impl fmt::Display for Color { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { +impl Color { + pub fn write(&self, text: &str) { match *self { - Self::White => write!(f, "26"), - Self::Red => write!(f, "31"), - Self::Green => write!(f, "32"), - Self::Yellow => write!(f, "33"), - Self::Blue => write!(f, "34"), - Self::Magenta => write!(f, "35"), - Self::Cyan => write!(f, "36"), + Self::White => print!("{}", text.white()), + Self::Red => print!("{}", text.red()), + Self::Green => print!("{}", text.green()), + Self::Yellow => print!("{}", text.yellow()), + Self::Blue => print!("{}", text.blue()), + Self::Magenta => print!("{}", text.magenta()), + Self::Cyan => print!("{}", text.cyan()), } } } @@ -104,13 +104,14 @@ macro_rules! gen_lexer { macro_rules! gen_parse { ($enumName:ident, $funcName:ident, $(($token:ident, $ansi:expr)), *) => { use logos::{Logos, Lexer}; + use std::io::{Write, stdout}; fn $funcName(mut tokens: Lexer<$enumName>) { while let Some(token) = tokens.next() { match token { $( - $enumName::$token => print!("\x1b[{}m{}\x1b[m", $ansi, tokens.slice()), + $enumName::$token => $ansi.write(tokens.slice()), )* - _ => print!("{}", tokens.slice()) + _ => print!("{}", tokens.slice()), } } }