cirious_codex_term 0.2.1

Next-generation native ANSI terminal control and formatting.
Documentation
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
use std::fmt;

use super::{Color, Style};
use crate::prompt::are_colors_enabled;

/// Bit flags for text styles.
const STYLE_BOLD: u8 = 1 << 0;
const STYLE_DIM: u8 = 1 << 1;
const STYLE_ITALIC: u8 = 1 << 2;
const STYLE_UNDERLINE: u8 = 1 << 3;
const STYLE_BLINK: u8 = 1 << 4;
const STYLE_REVERSE: u8 = 1 << 5;
const STYLE_HIDDEN: u8 = 1 << 6;
const STYLE_STRIKETHROUGH: u8 = 1 << 7;

/// A deferred evaluation builder for ANSI terminal styles.
///
/// Instead of allocating strings immediately, `StyledText` stores the
/// requested styles and injects the ANSI escape codes directly into the
/// buffer when `std::fmt::Display` is invoked. This ensures maximum
/// performance and prevents duplicate reset codes.
#[derive(Debug)]
pub struct StyledText<T> {
  /// The underlying text or data to be styled.
  pub text: T,

  /// The foreground color to be applied, if any.
  pub fg: Option<Color>,

  /// The background color to be applied, if any.
  pub bg: Option<Color>,

  /// A list of text formatting styles to be applied.
  pub style_mask: u8,
}

impl<T> StyledText<T> {
  /// Creates a new `StyledText` instance wrapping the provided text.
  #[must_use]
  pub const fn new(text: T) -> Self {
    Self {
      text,
      fg: None,
      bg: None,
      style_mask: 0,
    }
  }

  /// Sets the foreground color to the specified `Color`.
  #[must_use]
  pub const fn color(mut self, color: Color) -> Self {
    self.fg = Some(color);
    self
  }

  /// Sets the background color to the specified `Color`.
  #[must_use]
  pub const fn bg(mut self, color: Color) -> Self {
    self.bg = Some(color);
    self
  }

  /// Sets the foreground color using an 8-bit (256-color) palette index.
  #[must_use]
  pub const fn fixed(mut self, n: u8) -> Self {
    self.fg = Some(Color::Fixed(n));
    self
  }

  /// Sets the foreground color using an RGB `TrueColor` value.
  #[must_use]
  pub const fn rgb(mut self, r: u8, g: u8, b: u8) -> Self {
    self.fg = Some(Color::Rgb(r, g, b));
    self
  }

  /// Sets the background color using an 8-bit (256-color) palette index.
  #[must_use]
  pub const fn bg_fixed(mut self, n: u8) -> Self {
    self.bg = Some(Color::Fixed(n));
    self
  }

  /// Sets the background color using an RGB `TrueColor` value.
  #[must_use]
  pub const fn bg_rgb(mut self, r: u8, g: u8, b: u8) -> Self {
    self.bg = Some(Color::Rgb(r, g, b));
    self
  }

  /// Clears all applied styles, foreground, and background colors.
  #[must_use]
  pub const fn reset(mut self) -> Self {
    self.fg = Some(Color::Reset);
    self.bg = Some(Color::Reset);
    self.style_mask = 0;
    self
  }

  /// Sets the foreground color to black.
  #[must_use]
  pub const fn black(mut self) -> Self {
    self.fg = Some(Color::Black);
    self
  }

  /// Sets the foreground color to bright black (gray).
  #[must_use]
  pub const fn bright_black(mut self) -> Self {
    self.fg = Some(Color::BrightBlack);
    self
  }

  /// Sets the foreground color to bright red.
  #[must_use]
  pub const fn bright_red(mut self) -> Self {
    self.fg = Some(Color::BrightRed);
    self
  }

  /// Sets the foreground color to bright green.
  #[must_use]
  pub const fn bright_green(mut self) -> Self {
    self.fg = Some(Color::BrightGreen);
    self
  }

  /// Sets the foreground color to bright yellow.
  #[must_use]
  pub const fn bright_yellow(mut self) -> Self {
    self.fg = Some(Color::BrightYellow);
    self
  }

  /// Sets the foreground color to bright blue.
  #[must_use]
  pub const fn bright_blue(mut self) -> Self {
    self.fg = Some(Color::BrightBlue);
    self
  }

  /// Sets the foreground color to bright magenta.
  #[must_use]
  pub const fn bright_magenta(mut self) -> Self {
    self.fg = Some(Color::BrightMagenta);
    self
  }

  /// Sets the foreground color to bright cyan.
  #[must_use]
  pub const fn bright_cyan(mut self) -> Self {
    self.fg = Some(Color::BrightCyan);
    self
  }

  /// Sets the foreground color to bright white.
  #[must_use]
  pub const fn bright_white(mut self) -> Self {
    self.fg = Some(Color::BrightWhite);
    self
  }

  /// Sets the foreground color to red.
  #[must_use]
  pub const fn red(mut self) -> Self {
    self.fg = Some(Color::Red);
    self
  }

  /// Sets the foreground color to green.
  #[must_use]
  pub const fn green(mut self) -> Self {
    self.fg = Some(Color::Green);
    self
  }

  /// Sets the foreground color to yellow.
  #[must_use]
  pub const fn yellow(mut self) -> Self {
    self.fg = Some(Color::Yellow);
    self
  }

  /// Sets the foreground color to blue.
  #[must_use]
  pub const fn blue(mut self) -> Self {
    self.fg = Some(Color::Blue);
    self
  }

  /// Sets the foreground color to magenta.
  #[must_use]
  pub const fn magenta(mut self) -> Self {
    self.fg = Some(Color::Magenta);
    self
  }

  /// Sets the foreground color to cyan.
  #[must_use]
  pub const fn cyan(mut self) -> Self {
    self.fg = Some(Color::Cyan);
    self
  }

  /// Sets the foreground color to white.
  #[must_use]
  pub const fn white(mut self) -> Self {
    self.fg = Some(Color::White);
    self
  }

  /// Sets the background color to black.
  #[must_use]
  pub const fn bg_black(mut self) -> Self {
    self.bg = Some(Color::Black);
    self
  }

  /// Sets the background color to bright black (gray).
  #[must_use]
  pub const fn bg_bright_black(mut self) -> Self {
    self.bg = Some(Color::BrightBlack);
    self
  }

  /// Sets the background color to bright red.
  #[must_use]
  pub const fn bg_bright_red(mut self) -> Self {
    self.bg = Some(Color::BrightRed);
    self
  }

  /// Sets the background color to bright green.
  #[must_use]
  pub const fn bg_bright_green(mut self) -> Self {
    self.bg = Some(Color::BrightGreen);
    self
  }

  /// Sets the background color to bright yellow.
  #[must_use]
  pub const fn bg_bright_yellow(mut self) -> Self {
    self.bg = Some(Color::BrightYellow);
    self
  }

  /// Sets the background color to bright blue.
  #[must_use]
  pub const fn bg_bright_blue(mut self) -> Self {
    self.bg = Some(Color::BrightBlue);
    self
  }

  /// Sets the background color to bright magenta.
  #[must_use]
  pub const fn bg_bright_magenta(mut self) -> Self {
    self.bg = Some(Color::BrightMagenta);
    self
  }

  /// Sets the background color to bright cyan.
  #[must_use]
  pub const fn bg_bright_cyan(mut self) -> Self {
    self.bg = Some(Color::BrightCyan);
    self
  }

  /// Sets the background color to bright white.
  #[must_use]
  pub const fn bg_bright_white(mut self) -> Self {
    self.bg = Some(Color::BrightWhite);
    self
  }

  /// Sets the background color to red.
  #[must_use]
  pub const fn bg_red(mut self) -> Self {
    self.bg = Some(Color::Red);
    self
  }

  /// Sets the background color to green.
  #[must_use]
  pub const fn bg_green(mut self) -> Self {
    self.bg = Some(Color::Green);
    self
  }

  /// Sets the background color to yellow.
  #[must_use]
  pub const fn bg_yellow(mut self) -> Self {
    self.bg = Some(Color::Yellow);
    self
  }

  /// Sets the background color to blue.
  #[must_use]
  pub const fn bg_blue(mut self) -> Self {
    self.bg = Some(Color::Blue);
    self
  }

  /// Sets the background color to magenta.
  #[must_use]
  pub const fn bg_magenta(mut self) -> Self {
    self.bg = Some(Color::Magenta);
    self
  }

  /// Sets the background color to cyan.
  #[must_use]
  pub const fn bg_cyan(mut self) -> Self {
    self.bg = Some(Color::Cyan);
    self
  }

  /// Sets the background color to white.
  #[must_use]
  pub const fn bg_white(mut self) -> Self {
    self.bg = Some(Color::White);
    self
  }

  /// Adds the underline style to the text.
  #[must_use]
  pub const fn underline(mut self) -> Self {
    self.style_mask |= STYLE_UNDERLINE;
    self
  }

  /// Decreases the intensity of the text color.
  #[must_use]
  pub const fn dim(mut self) -> Self {
    self.style_mask |= STYLE_DIM;
    self
  }

  /// Adds the italic style to the text.
  #[must_use]
  pub const fn italic(mut self) -> Self {
    self.style_mask |= STYLE_ITALIC;
    self
  }

  /// Adds the blink style to the text.
  #[must_use]
  pub const fn blink(mut self) -> Self {
    self.style_mask |= STYLE_BLINK;
    self
  }

  /// Reverses the foreground and background colors.
  #[must_use]
  pub const fn reverse(mut self) -> Self {
    self.style_mask |= STYLE_REVERSE;
    self
  }

  /// Hides the text completely.
  #[must_use]
  pub const fn hidden(mut self) -> Self {
    self.style_mask |= STYLE_HIDDEN;
    self
  }

  /// Adds a strikethrough line to the text.
  #[must_use]
  pub const fn strikethrough(mut self) -> Self {
    self.style_mask |= STYLE_STRIKETHROUGH;
    self
  }

  /// Adds the bold style to the text.
  #[must_use]
  pub const fn bold(mut self) -> Self {
    self.style_mask |= STYLE_BOLD;
    self
  }
}

// Display implementation ensuring a single Reset at the end
impl<T: fmt::Display> fmt::Display for StyledText<T> {
  fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
    // THE MAGIC: If NO_COLOR is set, bypass all styling logic!
    if !are_colors_enabled() {
      return write!(f, "{}", self.text);
    }

    if self.style_mask & STYLE_BOLD != 0 {
      write!(f, "{}", Style::Bold.to_str())?;
    }
    if self.style_mask & STYLE_DIM != 0 {
      write!(f, "{}", Style::Dim.to_str())?;
    }
    if self.style_mask & STYLE_ITALIC != 0 {
      write!(f, "{}", Style::Italic.to_str())?;
    }
    if self.style_mask & STYLE_UNDERLINE != 0 {
      write!(f, "{}", Style::Underline.to_str())?;
    }
    if self.style_mask & STYLE_BLINK != 0 {
      write!(f, "{}", Style::Blink.to_str())?;
    }
    if self.style_mask & STYLE_REVERSE != 0 {
      write!(f, "{}", Style::Reverse.to_str())?;
    }
    if self.style_mask & STYLE_HIDDEN != 0 {
      write!(f, "{}", Style::Hidden.to_str())?;
    }
    if self.style_mask & STYLE_STRIKETHROUGH != 0 {
      write!(f, "{}", Style::Strikethrough.to_str())?;
    }

    if let Some(fg) = &self.fg {
      fg.write_fg(f)?;
    }
    if let Some(bg) = &self.bg {
      bg.write_bg(f)?;
    }

    write!(f, "{}", self.text)?;
    write!(f, "{}", Style::Reset.to_str())
  }
}