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
|
use super::{
is_too_wide, parse_disabled, parse_indices, parse_opaque, parse_selected, Coordinates,
};
use crate::panes::terminal_character::RESET_STYLES;
use crate::panes::{terminal_character::CharacterStyles, AnsiCode};
use zellij_utils::{
data::{PaletteColor, Style, StyleDeclaration},
shared::ansi_len,
};
use unicode_width::UnicodeWidthChar;
use zellij_utils::errors::prelude::*;
pub fn text(content: Text, style: &Style, component_coordinates: Option<Coordinates>) -> Vec<u8> {
if content.disabled {
let declaration = style.colors.text_unselected;
let mut base_text_style = RESET_STYLES
.foreground(Some(declaration.base.into()))
.italic(Some(AnsiCode::On));
if content.opaque || content.selected {
base_text_style = base_text_style.background(Some(declaration.background.into()));
}
let disabled_content = content.into_disabled();
let (text, _text_width) = stringify_text(
&disabled_content,
None,
&component_coordinates,
&declaration,
&style.colors,
base_text_style,
);
return match component_coordinates {
Some(component_coordinates) => {
format!("{}{}{}", component_coordinates, base_text_style, text)
.as_bytes()
.to_vec()
},
None => format!("{}{}", base_text_style, text).as_bytes().to_vec(),
};
}
let declaration = if content.selected {
style.colors.text_selected
} else {
style.colors.text_unselected
};
// Start with base style from declaration
let base_text_style = CharacterStyles::from(declaration).bold(Some(AnsiCode::On));
let (text, _text_width) = stringify_text(
&content,
None,
&component_coordinates,
&declaration,
&style.colors,
base_text_style,
);
match component_coordinates {
Some(component_coordinates) => {
format!("{}{}{}", component_coordinates, base_text_style, text)
.as_bytes()
.to_vec()
},
None => format!("{}{}", base_text_style, text).as_bytes().to_vec(),
}
}
pub fn stringify_text(
text: &Text,
left_padding: Option<usize>,
coordinates: &Option<Coordinates>,
style: &StyleDeclaration,
styling: &zellij_utils::data::Styling,
component_text_style: CharacterStyles,
) -> (String, usize) {
let mut text_width = 0;
let mut stringified = String::new();
let base_text_style = if text.opaque || text.selected {
component_text_style.background(Some(style.background.into()))
} else {
component_text_style
};
stringified.push_str(&format!("{}", base_text_style));
for (i, character) in text.text.chars().enumerate() {
let character_width = character.width().unwrap_or(0);
if is_too_wide(
character_width,
left_padding.unwrap_or(0) + text_width,
&coordinates,
) {
break;
}
text_width += character_width;
if text.selected || text.opaque {
// we do this so that selected text will appear selected
// even if it does not have color indices
stringified.push_str(&format!("{}", base_text_style));
}
if !text.indices.is_empty() || text.selected || text.opaque {
let character_with_styling =
color_index_character(character, i, &text, style, styling, base_text_style);
stringified.push_str(&character_with_styling);
} else {
stringified.push(character)
}
}
let coordinates_width = coordinates.as_ref().and_then(|c| c.width);
match (coordinates_width, base_text_style.background) {
(Some(coordinates_width), Some(_background_style)) => {
let text_width_with_left_padding = text_width + left_padding.unwrap_or(0);
let background_padding_length =
coordinates_width.saturating_sub(text_width_with_left_padding);
if text_width_with_left_padding < coordinates_width {
// here we pad the string with whitespace until the end so that the background
// style will apply the whole length of the coordinates
stringified.push_str(&format!(
"{:width$}",
" ",
width = background_padding_length
));
}
text_width += background_padding_length;
},
_ => {},
}
(stringified, text_width)
}
pub fn color_index_character(
character: char,
index: usize,
text: &Text,
declaration: &StyleDeclaration,
styling: &zellij_utils::data::Styling,
base_text_style: CharacterStyles,
) -> String {
let mut character_style = text
.style_of_index(index, declaration, styling)
.map(|foreground_style| base_text_style.foreground(Some(foreground_style.into())))
.unwrap_or(base_text_style);
// Apply dim and unbold per-character based on index levels 4 and 5
if text.is_unbold_at(index) {
// Remove bold for this character
character_style = character_style.bold(Some(AnsiCode::Reset));
} else if text.is_dimmed_at(index) {
// Apply dim for this character
character_style = character_style
.foreground(Some(AnsiCode::Reset)) // some terminals (eg. alacritty) do not support dimming non 16
// colors, so we have to defer to the terminal's default here
.dim(Some(AnsiCode::On));
} else {
character_style = character_style
.bold(Some(AnsiCode::On))
.dim(Some(AnsiCode::Reset)); // default, to reset any
// possible dim/bold values
// from previous indices
}
format!("{}{}{}", character_style, character, base_text_style)
}
pub fn parse_text_params<'a>(params_iter: impl Iterator<Item = &'a mut String>) -> Vec<Text> {
params_iter
.flat_map(|mut stringified| {
let selected = parse_selected(&mut stringified);
let opaque = parse_opaque(&mut stringified);
let disabled = parse_disabled(&mut stringified);
let indices = parse_indices(&mut stringified);
let text = parse_text(&mut stringified).map_err(|e| e.to_string())?;
Ok::<Text, String>(Text {
text,
opaque,
selected,
disabled,
indices,
})
})
.collect::<Vec<Text>>()
}
#[derive(Debug, Clone)]
pub struct Text {
pub text: String,
pub selected: bool,
pub opaque: bool,
pub disabled: bool,
pub indices: Vec<Vec<usize>>,
}
impl Text {
pub fn into_disabled(mut self) -> Self {
self.selected = false;
self.opaque = false;
self.disabled = true;
self.indices = vec![];
self
}
pub fn pad_text(&mut self, max_column_width: usize) {
for _ in ansi_len(&self.text)..max_column_width {
self.text.push(' ');
}
}
pub fn is_dimmed_at(&self, index: usize) -> bool {
const DIM_LEVEL: usize = 4;
self.indices
.get(DIM_LEVEL)
.map(|indices| indices.contains(&index))
.unwrap_or(false)
}
pub fn is_unbold_at(&self, index: usize) -> bool {
const UNBOLD_LEVEL: usize = 5;
self.indices
.get(UNBOLD_LEVEL)
.map(|indices| indices.contains(&index))
.unwrap_or(false)
}
pub fn style_of_index(
&self,
index: usize,
style: &StyleDeclaration,
styling: &zellij_utils::data::Styling,
) -> Option<PaletteColor> {
const ERROR_COLOR_LEVEL: usize = 6;
const SUCCESS_COLOR_LEVEL: usize = 7;
// Check error color first (highest precedence)
if let Some(indices) = self.indices.get(ERROR_COLOR_LEVEL) {
if indices.contains(&index) {
return Some(styling.exit_code_error.base);
}
}
// Check success color (second highest precedence)
if let Some(indices) = self.indices.get(SUCCESS_COLOR_LEVEL) {
if indices.contains(&index) {
return Some(styling.exit_code_success.base);
}
}
// Check regular emphasis levels (existing code)
let index_variant_styles = [
style.emphasis_0,
style.emphasis_1,
style.emphasis_2,
style.emphasis_3,
];
for i in (0..=3).rev() {
// we do this in reverse to give precedence to the last applied
// style
if let Some(indices) = self.indices.get(i) {
if indices.contains(&index) {
return Some(index_variant_styles[i]);
}
}
}
Some(style.base)
}
}
pub fn parse_text(stringified: &mut String) -> Result<String> {
let mut utf8 = vec![];
for stringified_character in stringified.split(',') {
utf8.push(
stringified_character
.to_string()
.parse::<u8>()
.with_context(|| format!("Failed to parse utf8"))?,
);
}
Ok(String::from_utf8_lossy(&utf8).to_string())
}
|