1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
use std::collections::HashMap; use std::rc::Rc; use fnv::FnvHashMap; use crate::color::*; use crate::ui_model::Cell; use neovim_lib::Value; pub struct HighlightMap { highlights: FnvHashMap<u64, Rc<Highlight>>, default_hl: Rc<Highlight>, bg_color: Color, fg_color: Color, sp_color: Color, cterm_bg_color: Color, cterm_fg_color: Color, cterm_color: bool, pmenu: Rc<Highlight>, pmenu_sel: Rc<Highlight>, cursor: Rc<Highlight>, } impl HighlightMap { pub fn new() -> Self { let default_hl = Rc::new(Highlight::new()); HighlightMap { highlights: FnvHashMap::default(), bg_color: COLOR_BLACK, fg_color: COLOR_WHITE, sp_color: COLOR_RED, cterm_bg_color: COLOR_BLACK, cterm_fg_color: COLOR_WHITE, cterm_color: false, pmenu: default_hl.clone(), pmenu_sel: default_hl.clone(), cursor: default_hl.clone(), default_hl, } } pub fn default_hl(&self) -> Rc<Highlight> { self.default_hl.clone() } pub fn set_defaults( &mut self, fg: Color, bg: Color, sp: Color, cterm_fg: Color, cterm_bg: Color, ) { self.fg_color = fg; self.bg_color = bg; self.sp_color = sp; self.cterm_fg_color = cterm_fg; self.cterm_bg_color = cterm_bg; } pub fn set_use_cterm(&mut self, cterm_color: bool) { self.cterm_color = cterm_color; } pub fn bg(&self) -> &Color { if self.cterm_color { &self.cterm_bg_color } else { &self.bg_color } } pub fn fg(&self) -> &Color { if self.cterm_color { &self.cterm_fg_color } else { &self.fg_color } } pub fn get(&self, idx: Option<u64>) -> Rc<Highlight> { idx.and_then(|idx| self.highlights.get(&idx)) .map(Rc::clone) .unwrap_or_else(|| { self.highlights .get(&0) .map(Rc::clone) .unwrap_or_else(|| self.default_hl.clone()) }) } pub fn set( &mut self, idx: u64, hl: &HashMap<String, Value>, info: &Vec<HashMap<String, Value>>, ) { let hl = Rc::new(Highlight::from_value_map(&hl)); for item in info { match item.get("hi_name").and_then(Value::as_str) { Some("Pmenu") => self.pmenu = hl.clone(), Some("PmenuSel") => self.pmenu_sel = hl.clone(), Some("Cursor") => self.cursor = hl.clone(), _ => (), } } self.highlights.insert(idx, hl); } pub fn cell_fg<'a>(&'a self, cell: &'a Cell) -> Option<&'a Color> { if !cell.hl.reverse { cell.hl.foreground.as_ref() } else { cell.hl.background.as_ref().or(Some(self.bg())) } } pub fn actual_cell_fg<'a>(&'a self, cell: &'a Cell) -> &'a Color { if !cell.hl.reverse { cell.hl.foreground.as_ref().unwrap_or(self.fg()) } else { cell.hl.background.as_ref().unwrap_or(self.bg()) } } pub fn cell_bg<'a>(&'a self, cell: &'a Cell) -> Option<&'a Color> { if !cell.hl.reverse { cell.hl.background.as_ref() } else { cell.hl.foreground.as_ref().or(Some(self.fg())) } } #[inline] pub fn actual_cell_sp<'a>(&'a self, cell: &'a Cell) -> &'a Color { cell.hl.special.as_ref().unwrap_or(&self.sp_color) } pub fn pmenu_bg(&self) -> &Color { if !self.pmenu.reverse { self.pmenu.background.as_ref().unwrap_or(self.bg()) } else { self.pmenu.foreground.as_ref().unwrap_or(self.fg()) } } pub fn pmenu_fg(&self) -> &Color { if !self.pmenu.reverse { self.pmenu.foreground.as_ref().unwrap_or(self.fg()) } else { self.pmenu.background.as_ref().unwrap_or(self.bg()) } } pub fn pmenu_bg_sel(&self) -> &Color { if !self.pmenu_sel.reverse { self.pmenu_sel.background.as_ref().unwrap_or(self.bg()) } else { self.pmenu_sel.foreground.as_ref().unwrap_or(self.fg()) } } pub fn pmenu_fg_sel(&self) -> &Color { if !self.pmenu_sel.reverse { self.pmenu_sel.foreground.as_ref().unwrap_or(self.fg()) } else { self.pmenu_sel.background.as_ref().unwrap_or(self.bg()) } } pub fn cursor_bg(&self) -> &Color { if !self.cursor.reverse { self.cursor.background.as_ref().unwrap_or(self.bg()) } else { self.cursor.foreground.as_ref().unwrap_or(self.fg()) } } } #[derive(Clone)] pub struct Highlight { pub italic: bool, pub bold: bool, pub underline: bool, pub undercurl: bool, pub foreground: Option<Color>, pub background: Option<Color>, pub special: Option<Color>, pub reverse: bool, } impl Highlight { pub fn new() -> Self { Highlight { foreground: None, background: None, special: None, italic: false, bold: false, underline: false, undercurl: false, reverse: false, } } pub fn from_value_map(attrs: &HashMap<String, Value>) -> Self { let mut model_attrs = Highlight::new(); for (ref key, ref val) in attrs { match key.as_ref() { "foreground" => { if let Some(fg) = val.as_u64() { model_attrs.foreground = Some(Color::from_indexed_color(fg)); } } "background" => { if let Some(bg) = val.as_u64() { model_attrs.background = Some(Color::from_indexed_color(bg)); } } "special" => { if let Some(bg) = val.as_u64() { model_attrs.special = Some(Color::from_indexed_color(bg)); } } "reverse" => model_attrs.reverse = true, "bold" => model_attrs.bold = true, "italic" => model_attrs.italic = true, "underline" => model_attrs.underline = true, "undercurl" => model_attrs.undercurl = true, attr_key => error!("unknown attribute {}", attr_key), }; } model_attrs } }