diff --git a/src/filetype.rs b/src/filetype.rs index 403b984..056e805 100644 --- a/src/filetype.rs +++ b/src/filetype.rs @@ -5,7 +5,8 @@ pub struct FileType { #[derive(Default, Copy, Clone)] pub struct HighlightingOptions { - numbers: bool + numbers: bool, + strings: bool } impl Default for FileType { @@ -28,7 +29,10 @@ impl FileType { if file_name.ends_with(".rs") { return Self { name: String::from("Rust"), - hl_opts: HighlightingOptions{ numbers: true } + hl_opts: HighlightingOptions{ + numbers: true, + strings: true + } } } Self::default() @@ -39,4 +43,7 @@ impl HighlightingOptions { pub fn numbers(&self) -> bool { self.numbers } + pub fn strings(&self) -> bool { + self.strings + } } diff --git a/src/highlighting.rs b/src/highlighting.rs index f236374..753fd48 100644 --- a/src/highlighting.rs +++ b/src/highlighting.rs @@ -4,7 +4,8 @@ use termion::color; pub enum Type { None, Number, - Match + Match, + String } impl Type { @@ -12,6 +13,7 @@ impl Type { match self { Type::Number => color::Rgb(220, 163, 163), Type::Match => color::Rgb(38, 139, 210), + Type::String => color::Rgb(255, 255, 255), _ => color::Rgb(255, 255, 255) } } diff --git a/src/row.rs b/src/row.rs index da7d731..7038961 100644 --- a/src/row.rs +++ b/src/row.rs @@ -170,6 +170,7 @@ impl Row { } let mut prev_is_separator = true; + let mut in_string = false; let mut index = 0; while let Some(c) = chars.get(index) { if let Some(word) = word { @@ -187,11 +188,37 @@ impl Row { } else { &highlighting::Type::None }; + + if opts.strings() { + if in_string { + highlighting.push(highlighting::Type::String); + + if *c == '\\' && index < self.len().saturating_sub(1) { + highlighting.push(highlighting::Type::String); + index += 2; + continue; + } + if *c == '"' { + in_string = false; + prev_is_separator = true; + } else { + prev_is_separator = false; + } + index += 1; + continue; + } else if prev_is_separator && *c == '"' { + highlighting.push(highlighting::Type::String); + in_string = true; + prev_is_separator = true; + index += 1; + continue; + } + } if opts.numbers() { if (c.is_ascii_digit() - && (prev_is_separator || previous_highlight == &highlighting::Type::Number)) - || (c == &'.' && previous_highlight == &highlighting::Type::Number) + && (prev_is_separator || *previous_highlight == highlighting::Type::Number)) + || (*c == '.' && *previous_highlight == highlighting::Type::Number) { highlighting.push(highlighting::Type::Number); } else {