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
use AnsiNode;
use ;
use StyledStringDisplay;
use crate::;
use fmt;
/// A [`String`] containing [`Ansi`] styles, which can be overridden.
///
/// Instances of `StyledString`:
///
/// - are created using the [`styled_format!`](crate::styled_format!) macro,
/// or by calling [`Styled::to_styled_string`].
/// - can be converted to a plain [`String`] with [`to_string`](std::string::ToString::to_string),
/// or by using the [`format!`] macro.
///
/// *Note: this library provides a similarly-named [`Styled<T>`], which offers the same
/// functionality as `StyledString`, but stores an unformatted [`std::fmt::Display`] rather
/// than a formatted [`String`]. This may be less convenient for certain use cases,
/// but is more efficient since it avoids the runtime overhead of `StyledString`.*
///
/// ## Discussion
///
/// When [`Ansi`] styles are formatted into codes insides a [`String`], the references to
/// those styles are lost and they are "baked-into" the [`String`]'s contents.
/// This means one loses the ability to subsequently override those styles by nesting
/// that [`String`] inside a parent [`Styled<T>`]:
///
/// ### Example
/// ```
/// use ansiconst::styled_format_args;
///
/// let red: String = styled_format_args!(Red, "Red").to_string();
///
/// assert_eq!("\x1B[31mRed\x1B[39m", red);
///
/// let sadly_still_red = styled_format_args!(Ansi::no_ansi(), "{}", red).to_string();
///
/// assert_eq!("\x1B[31mRed\x1B[39m", sadly_still_red);
/// ```
///
/// `StyledString` solves this problem by keeping track of the contained [`Ansi`] styles
/// inside a formatted [`String`], such that those styles can subsequently be overridden:
///
/// ### Example
/// ```
/// use ansiconst::{styled_format, styled_format_args, StyledString};
///
/// let red: StyledString = styled_format!(Red, "Red");
///
/// assert_eq!("\x1B[31mRed\x1B[39m", red.to_string());
///
/// let no_longer_red = styled_format_args!(Ansi::no_ansi(), "{}", red).to_string();
///
/// assert_eq!("Red", no_longer_red);
/// ```
///
/// The flexibility afforded by `StyledString` comes at the expense of additional runtime overhead.
/// Therefore in performance critical situations, it is better not to create `StyledString`s,
/// but instead to use [`styled_format_args!`](crate::styled_format_args) to style output
/// at the moment when it is displayed.
///
/// Like [`Styled<T>`], `StyledString` uses [`thread_local!`] to pass style information between
/// nested styles and outer styles during formatting.
pub