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
|
use std::sync::atomic::{AtomicU32, Ordering};
use std::sync::Arc;
use bitflags::bitflags;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
use crate::grid::{self, GridCell};
use crate::index::Column;
use crate::vte::ansi::{Color, Hyperlink as VteHyperlink, NamedColor};
bitflags! {
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Flags: u16 {
const INVERSE = 0b0000_0000_0000_0001;
const BOLD = 0b0000_0000_0000_0010;
const ITALIC = 0b0000_0000_0000_0100;
const BOLD_ITALIC = 0b0000_0000_0000_0110;
const UNDERLINE = 0b0000_0000_0000_1000;
const WRAPLINE = 0b0000_0000_0001_0000;
const WIDE_CHAR = 0b0000_0000_0010_0000;
const WIDE_CHAR_SPACER = 0b0000_0000_0100_0000;
const DIM = 0b0000_0000_1000_0000;
const DIM_BOLD = 0b0000_0000_1000_0010;
const HIDDEN = 0b0000_0001_0000_0000;
const STRIKEOUT = 0b0000_0010_0000_0000;
const LEADING_WIDE_CHAR_SPACER = 0b0000_0100_0000_0000;
const DOUBLE_UNDERLINE = 0b0000_1000_0000_0000;
const UNDERCURL = 0b0001_0000_0000_0000;
const DOTTED_UNDERLINE = 0b0010_0000_0000_0000;
const DASHED_UNDERLINE = 0b0100_0000_0000_0000;
const ALL_UNDERLINES = Self::UNDERLINE.bits() | Self::DOUBLE_UNDERLINE.bits()
| Self::UNDERCURL.bits() | Self::DOTTED_UNDERLINE.bits()
| Self::DASHED_UNDERLINE.bits();
}
}
/// Counter for hyperlinks without explicit ID.
static HYPERLINK_ID_SUFFIX: AtomicU32 = AtomicU32::new(0);
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Hyperlink {
inner: Arc<HyperlinkInner>,
}
impl Hyperlink {
pub fn new<T: ToString>(id: Option<T>, uri: String) -> Self {
let inner = Arc::new(HyperlinkInner::new(id, uri));
Self { inner }
}
pub fn id(&self) -> &str {
&self.inner.id
}
pub fn uri(&self) -> &str {
&self.inner.uri
}
}
impl From<VteHyperlink> for Hyperlink {
fn from(value: VteHyperlink) -> Self {
Self::new(value.id, value.uri)
}
}
impl From<Hyperlink> for VteHyperlink {
fn from(val: Hyperlink) -> Self {
VteHyperlink { id: Some(val.id().to_owned()), uri: val.uri().to_owned() }
}
}
#[derive(Debug, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
struct HyperlinkInner {
/// Identifier for the given hyperlink.
id: String,
/// Resource identifier of the hyperlink.
uri: String,
}
impl HyperlinkInner {
pub fn new<T: ToString>(id: Option<T>, uri: String) -> Self {
let id = match id {
Some(id) => id.to_string(),
None => {
let mut id = HYPERLINK_ID_SUFFIX.fetch_add(1, Ordering::Relaxed).to_string();
id.push_str("_alacritty");
id
},
};
Self { id, uri }
}
}
/// Trait for determining if a reset should be performed.
pub trait ResetDiscriminant<T> {
/// Value based on which equality for the reset will be determined.
fn discriminant(&self) -> T;
}
impl<T: Copy> ResetDiscriminant<T> for T {
fn discriminant(&self) -> T {
*self
}
}
impl ResetDiscriminant<Color> for Cell {
fn discriminant(&self) -> Color {
self.bg
}
}
/// Dynamically allocated cell content.
///
/// This storage is reserved for cell attributes which are rarely set. This allows reducing the
/// allocation required ahead of time for every cell, with some additional overhead when the extra
/// storage is actually required.
#[derive(Default, Debug, Clone, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct CellExtra {
zerowidth: Vec<char>,
underline_color: Option<Color>,
hyperlink: Option<Hyperlink>,
}
/// Content and attributes of a single cell in the terminal grid.
#[derive(Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Cell {
pub c: char,
pub fg: Color,
pub bg: Color,
pub flags: Flags,
pub extra: Option<Arc<CellExtra>>,
}
impl Default for Cell {
#[inline]
fn default() -> Cell {
Cell {
c: ' ',
bg: Color::Named(NamedColor::Background),
fg: Color::Named(NamedColor::Foreground),
flags: Flags::empty(),
extra: None,
}
}
}
impl Cell {
/// Zerowidth characters stored in this cell.
#[inline]
pub fn zerowidth(&self) -> Option<&[char]> {
self.extra.as_ref().map(|extra| extra.zerowidth.as_slice())
}
/// Write a new zerowidth character to this cell.
#[inline]
pub fn push_zerowidth(&mut self, character: char) {
let extra = self.extra.get_or_insert(Default::default());
Arc::make_mut(extra).zerowidth.push(character);
}
/// Remove all wide char data from a cell.
#[inline(never)]
pub fn clear_wide(&mut self) {
self.flags.remove(Flags::WIDE_CHAR);
if let Some(extra) = self.extra.as_mut() {
Arc::make_mut(extra).zerowidth = Vec::new();
}
self.c = ' ';
}
/// Set underline color on the cell.
pub fn set_underline_color(&mut self, color: Option<Color>) {
// If we reset color and we don't have zerowidth we should drop extra storage.
if color.is_none()
&& self
.extra
.as_ref()
.map_or(true, |extra| extra.zerowidth.is_empty() && extra.hyperlink.is_none())
{
self.extra = None;
} else {
let extra = self.extra.get_or_insert(Default::default());
Arc::make_mut(extra).underline_color = color;
}
}
/// Underline color stored in this cell.
#[inline]
pub fn underline_color(&self) -> Option<Color> {
self.extra.as_ref()?.underline_color
}
/// Set hyperlink.
pub fn set_hyperlink(&mut self, hyperlink: Option<Hyperlink>) {
let should_drop = hyperlink.is_none()
&& self.extra.as_ref().map_or(true, |extra| {
extra.zerowidth.is_empty() && extra.underline_color.is_none()
});
if should_drop {
self.extra = None;
} else {
let extra = self.extra.get_or_insert(Default::default());
Arc::make_mut(extra).hyperlink = hyperlink;
}
}
/// Hyperlink stored in this cell.
#[inline]
pub fn hyperlink(&self) -> Option<Hyperlink> {
self.extra.as_ref()?.hyperlink.clone()
}
}
impl GridCell for Cell {
#[inline]
fn is_empty(&self) -> bool {
(self.c == ' ' || self.c == '\t')
&& self.bg == Color::Named(NamedColor::Background)
&& self.fg == Color::Named(NamedColor::Foreground)
&& !self.flags.intersects(
Flags::INVERSE
| Flags::ALL_UNDERLINES
| Flags::STRIKEOUT
| Flags::WRAPLINE
| Flags::WIDE_CHAR_SPACER
| Flags::LEADING_WIDE_CHAR_SPACER,
)
&& self.extra.as_ref().map(|extra| extra.zerowidth.is_empty()) != Some(false)
}
#[inline]
fn flags(&self) -> &Flags {
&self.flags
}
#[inline]
fn flags_mut(&mut self) -> &mut Flags {
&mut self.flags
}
#[inline]
fn reset(&mut self, template: &Self) {
*self = Cell { bg: template.bg, ..Cell::default() };
}
}
impl From<Color> for Cell {
#[inline]
fn from(color: Color) -> Self {
Self { bg: color, ..Cell::default() }
}
}
/// Get the length of occupied cells in a line.
pub trait LineLength {
/// Calculate the occupied line length.
fn line_length(&self) -> Column;
}
impl LineLength for grid::Row<Cell> {
fn line_length(&self) -> Column {
let mut length = Column(0);
if self[Column(self.len() - 1)].flags.contains(Flags::WRAPLINE) {
return Column(self.len());
}
for (index, cell) in self[..].iter().rev().enumerate() {
if cell.c != ' '
|| cell.extra.as_ref().map(|extra| extra.zerowidth.is_empty()) == Some(false)
{
length = Column(self.len() - index);
break;
}
}
length
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::mem;
use crate::grid::Row;
use crate::index::Column;
#[test]
fn cell_size_is_below_cap() {
// Expected cell size on 64-bit architectures.
const EXPECTED_CELL_SIZE: usize = 24;
// Ensure that cell size isn't growing by accident.
assert!(mem::size_of::<Cell>() <= EXPECTED_CELL_SIZE);
}
#[test]
fn line_length_works() {
let mut row = Row::<Cell>::new(10);
row[Column(5)].c = 'a';
assert_eq!(row.line_length(), Column(6));
}
#[test]
fn line_length_works_with_wrapline() {
let mut row = Row::<Cell>::new(10);
row[Column(9)].flags.insert(super::Flags::WRAPLINE);
assert_eq!(row.line_length(), Column(10));
}
}
|