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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
|
use std::cmp::max;
use std::iter;
use std::iter::Peekable;
use std::mem;
use std::ops::RangeInclusive;
use crate::ansi::{Color, CursorShape, NamedColor};
use crate::config::Config;
use crate::grid::{Dimensions, DisplayIter, Indexed};
use crate::index::{Column, Direction, Line, Point};
use crate::selection::SelectionRange;
use crate::term::cell::{Cell, Flags};
use crate::term::color::{self, CellRgb, Rgb, DIM_FACTOR};
use crate::term::search::RegexIter;
use crate::term::{Term, TermMode};
/// Minimum contrast between a fixed cursor color and the cell's background.
pub const MIN_CURSOR_CONTRAST: f64 = 1.5;
/// Maximum number of linewraps followed outside of the viewport during search highlighting.
const MAX_SEARCH_LINES: usize = 100;
/// Renderable terminal content.
///
/// This provides the terminal cursor and an iterator over all non-empty cells.
pub struct RenderableContent<'a, T, C> {
term: &'a Term<T>,
config: &'a Config<C>,
display_iter: DisplayIter<'a, Cell>,
selection: Option<SelectionRange<Line>>,
search: RenderableSearch<'a>,
cursor: Option<RenderableCursor>,
cursor_shape: CursorShape,
cursor_point: Point,
}
impl<'a, T, C> RenderableContent<'a, T, C> {
pub fn new(term: &'a Term<T>, config: &'a Config<C>, show_cursor: bool) -> Self {
// Cursor position.
let vi_mode = term.mode.contains(TermMode::VI);
let mut cursor_point = if vi_mode {
term.vi_mode_cursor.point
} else {
let mut point = term.grid.cursor.point;
point.line += term.grid.display_offset();
point
};
// Cursor shape.
let cursor_shape = if !show_cursor
|| (!term.mode.contains(TermMode::SHOW_CURSOR) && !vi_mode)
|| cursor_point.line >= term.screen_lines()
{
cursor_point.line = Line(0);
CursorShape::Hidden
} else if !term.is_focused && config.cursor.unfocused_hollow {
CursorShape::HollowBlock
} else {
let cursor_style = term.cursor_style.unwrap_or(term.default_cursor_style);
if vi_mode {
term.vi_mode_cursor_style.unwrap_or(cursor_style).shape
} else {
cursor_style.shape
}
};
Self {
display_iter: term.grid.display_iter(),
selection: term.visible_selection(),
search: RenderableSearch::new(term),
cursor: None,
cursor_shape,
cursor_point,
config,
term,
}
}
/// Get the terminal cursor.
pub fn cursor(mut self) -> Option<RenderableCursor> {
// Drain the iterator to make sure the cursor is created.
while self.next().is_some() && self.cursor.is_none() {}
self.cursor
}
/// Assemble the information required to render the terminal cursor.
///
/// This will return `None` when there is no cursor visible.
fn renderable_cursor(&mut self, cell: &RenderableCell) -> Option<RenderableCursor> {
if self.cursor_shape == CursorShape::Hidden {
return None;
}
// Expand across wide cell when inside wide char or spacer.
let is_wide = if cell.flags.contains(Flags::WIDE_CHAR_SPACER) {
self.cursor_point.col -= 1;
true
} else {
cell.flags.contains(Flags::WIDE_CHAR)
};
// Cursor colors.
let color = if self.term.mode.contains(TermMode::VI) {
self.config.colors.vi_mode_cursor
} else {
self.config.colors.cursor
};
let mut cursor_color = if self.term.color_modified[NamedColor::Cursor as usize] {
CellRgb::Rgb(self.term.colors[NamedColor::Cursor])
} else {
color.background
};
let mut text_color = color.foreground;
// Invert the cursor if it has a fixed background close to the cell's background.
if matches!(
cursor_color,
CellRgb::Rgb(color) if color.contrast(cell.bg) < MIN_CURSOR_CONTRAST
) {
cursor_color = CellRgb::CellForeground;
text_color = CellRgb::CellBackground;
}
// Convert from cell colors to RGB.
let text_color = text_color.color(cell.fg, cell.bg);
let cursor_color = cursor_color.color(cell.fg, cell.bg);
Some(RenderableCursor {
point: self.cursor_point,
shape: self.cursor_shape,
cursor_color,
text_color,
is_wide,
})
}
}
impl<'a, T, C> Iterator for RenderableContent<'a, T, C> {
type Item = RenderableCell;
/// Gets the next renderable cell.
///
/// Skips empty (background) cells and applies any flags to the cell state
/// (eg. invert fg and bg colors).
#[inline]
fn next(&mut self) -> Option<Self::Item> {
loop {
if self.cursor_point == self.display_iter.point() {
// Handle cell at cursor position.
let cell = self.display_iter.next()?;
let mut cell = RenderableCell::new(self, cell);
// Store the cursor which should be rendered.
self.cursor = self.renderable_cursor(&cell).map(|cursor| {
if cursor.shape == CursorShape::Block {
cell.fg = cursor.text_color;
cell.bg = cursor.cursor_color;
// Since we draw Block cursor by drawing cell below it with a proper color,
// we must adjust alpha to make it visible.
cell.bg_alpha = 1.;
}
cursor
});
return Some(cell);
} else {
// Handle non-cursor cells.
let cell = self.display_iter.next()?;
let cell = RenderableCell::new(self, cell);
// Skip empty cells and wide char spacers.
if !cell.is_empty() && !cell.flags.contains(Flags::WIDE_CHAR_SPACER) {
return Some(cell);
}
}
}
}
}
/// Cell ready for rendering.
#[derive(Clone, Debug)]
pub struct RenderableCell {
pub character: char,
pub zerowidth: Option<Vec<char>>,
pub line: Line,
pub column: Column,
pub fg: Rgb,
pub bg: Rgb,
pub bg_alpha: f32,
pub flags: Flags,
pub is_match: bool,
}
impl RenderableCell {
fn new<'a, T, C>(content: &mut RenderableContent<'a, T, C>, cell: Indexed<&Cell>) -> Self {
let point = Point::new(cell.line, cell.column);
// Lookup RGB values.
let mut fg_rgb =
Self::compute_fg_rgb(content.config, &content.term.colors, cell.fg, cell.flags);
let mut bg_rgb = Self::compute_bg_rgb(&content.term.colors, cell.bg);
let mut bg_alpha = if cell.flags.contains(Flags::INVERSE) {
mem::swap(&mut fg_rgb, &mut bg_rgb);
1.0
} else {
Self::compute_bg_alpha(cell.bg)
};
let grid = content.term.grid();
let is_selected = content.selection.map_or(false, |selection| {
selection.contains_cell(grid, point, content.cursor_point, content.cursor_shape)
});
let mut is_match = false;
if is_selected {
let config_bg = content.config.colors.selection.background;
let selected_fg = content.config.colors.selection.foreground.color(fg_rgb, bg_rgb);
bg_rgb = config_bg.color(fg_rgb, bg_rgb);
fg_rgb = selected_fg;
if fg_rgb == bg_rgb && !cell.flags.contains(Flags::HIDDEN) {
// Reveal inversed text when fg/bg is the same.
fg_rgb = content.term.colors[NamedColor::Background];
bg_rgb = content.term.colors[NamedColor::Foreground];
bg_alpha = 1.0;
} else if config_bg != CellRgb::CellBackground {
bg_alpha = 1.0;
}
} else if content.search.advance(grid.visible_to_buffer(point)) {
// Highlight the cell if it is part of a search match.
let config_bg = content.config.colors.search.matches.background;
let matched_fg = content.config.colors.search.matches.foreground.color(fg_rgb, bg_rgb);
bg_rgb = config_bg.color(fg_rgb, bg_rgb);
fg_rgb = matched_fg;
if config_bg != CellRgb::CellBackground {
bg_alpha = 1.0;
}
is_match = true;
}
RenderableCell {
character: cell.c,
zerowidth: cell.zerowidth().map(|zerowidth| zerowidth.to_vec()),
line: cell.line,
column: cell.column,
fg: fg_rgb,
bg: bg_rgb,
bg_alpha,
flags: cell.flags,
is_match,
}
}
/// Position of the cell.
pub fn point(&self) -> Point {
Point::new(self.line, self.column)
}
/// Check if cell contains any renderable content.
fn is_empty(&self) -> bool {
self.bg_alpha == 0.
&& !self.flags.intersects(Flags::UNDERLINE | Flags::STRIKEOUT | Flags::DOUBLE_UNDERLINE)
&& self.character == ' '
&& self.zerowidth.is_none()
}
/// Get the RGB color from a cell's foreground color.
fn compute_fg_rgb<C>(config: &Config<C>, colors: &color::List, fg: Color, flags: Flags) -> Rgb {
match fg {
Color::Spec(rgb) => match flags & Flags::DIM {
Flags::DIM => rgb * DIM_FACTOR,
_ => rgb,
},
Color::Named(ansi) => {
match (config.draw_bold_text_with_bright_colors, flags & Flags::DIM_BOLD) {
// If no bright foreground is set, treat it like the BOLD flag doesn't exist.
(_, Flags::DIM_BOLD)
if ansi == NamedColor::Foreground
&& config.colors.primary.bright_foreground.is_none() =>
{
colors[NamedColor::DimForeground]
},
// Draw bold text in bright colors *and* contains bold flag.
(true, Flags::BOLD) => colors[ansi.to_bright()],
// Cell is marked as dim and not bold.
(_, Flags::DIM) | (false, Flags::DIM_BOLD) => colors[ansi.to_dim()],
// None of the above, keep original color..
_ => colors[ansi],
}
},
Color::Indexed(idx) => {
let idx = match (
config.draw_bold_text_with_bright_colors,
flags & Flags::DIM_BOLD,
idx,
) {
(true, Flags::BOLD, 0..=7) => idx as usize + 8,
(false, Flags::DIM, 8..=15) => idx as usize - 8,
(false, Flags::DIM, 0..=7) => NamedColor::DimBlack as usize + idx as usize,
_ => idx as usize,
};
colors[idx]
},
}
}
/// Get the RGB color from a cell's background color.
#[inline]
fn compute_bg_rgb(colors: &color::List, bg: Color) -> Rgb {
match bg {
Color::Spec(rgb) => rgb,
Color::Named(ansi) => colors[ansi],
Color::Indexed(idx) => colors[idx],
}
}
/// Compute background alpha based on cell's original color.
///
/// Since an RGB color matching the background should not be transparent, this is computed
/// using the named input color, rather than checking the RGB of the background after its color
/// is computed.
#[inline]
fn compute_bg_alpha(bg: Color) -> f32 {
if bg == Color::Named(NamedColor::Background) {
0.
} else {
1.
}
}
}
/// Cursor storing all information relevant for rendering.
#[derive(Debug, Eq, PartialEq, Copy, Clone)]
pub struct RenderableCursor {
shape: CursorShape,
cursor_color: Rgb,
text_color: Rgb,
is_wide: bool,
point: Point,
}
impl RenderableCursor {
pub fn color(&self) -> Rgb {
self.cursor_color
}
pub fn shape(&self) -> CursorShape {
self.shape
}
pub fn is_wide(&self) -> bool {
self.is_wide
}
pub fn point(&self) -> Point {
self.point
}
}
type MatchIter<'a> = Box<dyn Iterator<Item = RangeInclusive<Point<usize>>> + 'a>;
/// Regex search highlight tracking.
struct RenderableSearch<'a> {
iter: Peekable<MatchIter<'a>>,
}
impl<'a> RenderableSearch<'a> {
/// Create a new renderable search iterator.
fn new<T>(term: &'a Term<T>) -> Self {
// Avoid constructing search if there is none.
if term.regex_search.is_none() {
let iter: MatchIter<'a> = Box::new(iter::empty());
return Self { iter: iter.peekable() };
}
let viewport_end = term.grid().display_offset();
let viewport_start = viewport_end + term.screen_lines().0 - 1;
// Compute start of the first and end of the last line.
let start_point = Point::new(viewport_start, Column(0));
let mut start = term.line_search_left(start_point);
let end_point = Point::new(viewport_end, term.cols() - 1);
let mut end = term.line_search_right(end_point);
// Set upper bound on search before/after the viewport to prevent excessive blocking.
if start.line > viewport_start + MAX_SEARCH_LINES {
if start.line == 0 {
// Do not highlight anything if this line is the last.
let iter: MatchIter<'a> = Box::new(iter::empty());
return Self { iter: iter.peekable() };
} else {
// Start at next line if this one is too long.
start.line -= 1;
}
}
end.line = max(end.line, viewport_end.saturating_sub(MAX_SEARCH_LINES));
// Create an iterater for the current regex search for all visible matches.
let iter: MatchIter<'a> = Box::new(
RegexIter::new(start, end, Direction::Right, &term)
.skip_while(move |rm| rm.end().line > viewport_start)
.take_while(move |rm| rm.start().line >= viewport_end),
);
Self { iter: iter.peekable() }
}
/// Advance the search tracker to the next point.
///
/// This will return `true` if the point passed is part of a search match.
fn advance(&mut self, point: Point<usize>) -> bool {
while let Some(regex_match) = &self.iter.peek() {
if regex_match.start() > &point {
break;
} else if regex_match.end() < &point {
let _ = self.iter.next();
} else {
return true;
}
}
false
}
}
|