Skip to content

Commit

Permalink
Cross platform as you type syntax highlighting (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
Sean Hellum authored May 14, 2020
1 parent 236415d commit b7c861d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ readme = "README.md"
[dependencies]
crossterm = "0.17.3"
logos = "0.11"
colored = "1.9.3"
25 changes: 13 additions & 12 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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()),
}
}
}
Expand Down Expand Up @@ -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()),
}
}
}
Expand Down

0 comments on commit b7c861d

Please sign in to comment.