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
//! Rendering & extension protocols.
//!
//! Port of upstream `rich/protocol.py` + `rich/abc.py` + the highlighter
//! interface. **These traits are the sanctioned extension points of the port.**
//! Extensions in `rich-ext` (and, later, third-party plugins) implement them;
//! the faithful core only ever ships upstream's built-in implementations. See
//! docs/PLUGINS.md.
use crate;
use crateMeasurement;
use crateSegment;
use crateText;
/// Anything that can be rendered to a stream of [`Segment`]s within a width.
///
/// The Rust equivalent of upstream's `__rich_console__(console, options)`
/// protocol. Implement it to make a custom type printable by [`Console`]. The
/// `options` carry the available width (and, later, height/justify) the
/// renderable must fit into. Newlines between lines are emitted as ordinary
/// segments containing `\n`.
/// Optional line-streaming extension point for renderables.
///
/// Mirrors the incremental consumption of upstream's rendering generators.
/// Consumers can write each visual line immediately instead of collecting the
/// complete segment stream. Implementations may still retain source data for
/// measurement. This trait keeps streaming hooks out of inherent core APIs.
/// Transfer already-owned table rows without cloning every cell string.
///
/// This extension point changes ownership only. Column definitions, measurement
/// and rendering follow the table's existing rules, including missing/extra cells.
/// Producers that parse into owned strings can release their row collection as
/// they populate a table instead of retaining a second complete copy.
/// A transformer that adds style spans to [`Text`] (e.g. syntax/number/URL
/// highlighting). The Rust equivalent of upstream's `Highlighter` ABC.
///
/// This is the primary *plugin* seam for the first slice: `rich-ext` registers
/// [`Highlighter`]s onto a [`Console`] without the core knowing they exist.
/// One styled byte range of a highlighted line.
/// The spans of one source line, plus the style of the line break after it.
/// What a [`CodeHighlighter`] returns: one [`HighlightedLine`] per element of
/// `code.split('\n')`, so a trailing newline yields a final empty line.
/// Why a [`CodeHighlighter`] could not highlight. An unknown *language* is not an
/// error: highlighters fall back to plain text.
/// A syntax-highlighting engine behind [`Syntax`](crate::syntax::Syntax) and
/// Markdown code blocks.
///
/// Upstream highlights with Pygments; the port's default is
/// [`SyntectHighlighter`](crate::syntax::SyntectHighlighter), and anything that
/// implements this trait can replace it (see [`Syntax::highlighter`](crate::syntax::Syntax::highlighter)).
///
/// # Contract
///
/// - `lines` has one entry per element of `code.split('\n')`.
/// - Spans are sorted, do not overlap, stay inside their line and start and end
/// on UTF-8 character boundaries.
/// - An unknown language highlights as plain text rather than failing.
///
/// Core validates what an implementation returns: out-of-range, overlapping or
/// misaligned spans are dropped, missing lines render unstyled, and span styles
/// lose any hyperlink. The rendered characters always come from the source, so
/// a highlighter cannot add text or terminal control sequences.
///
/// ```
/// use rich::protocol::{CodeHighlighter, HighlightError, HighlightSpan, HighlightedCode, HighlightedLine};
/// use rich::{Console, Style, Syntax};
/// use std::sync::Arc;
///
/// /// Makes every line bold.
/// struct Bold;
///
/// impl CodeHighlighter for Bold {
/// fn highlight(&self, code: &str, _language: Option<&str>, _theme: &str)
/// -> Result<HighlightedCode, HighlightError>
/// {
/// let bold = Style::parse("bold").unwrap();
/// let lines = code
/// .split('\n')
/// .map(|line| HighlightedLine {
/// spans: vec![HighlightSpan { range: 0..line.len(), style: bold.clone() }]
/// .into_iter()
/// .filter(|span| !span.range.is_empty())
/// .collect(),
/// newline_style: None,
/// })
/// .collect();
/// Ok(HighlightedCode { lines, ..Default::default() })
/// }
/// fn default_theme(&self) -> &str { "bold" }
/// fn themes(&self) -> Vec<String> { vec!["bold".into()] }
/// fn languages(&self) -> Vec<String> { Vec::new() }
/// }
///
/// let console = Console::builder().width(20).force_terminal(true).build();
/// let out = console.render_to_string(&Syntax::new("x = 1", "python").highlighter(Arc::new(Bold)));
/// assert!(out.contains("\x1b[1mx = 1"));
/// ```
/// Renders fenced Markdown code blocks of particular languages (for example
/// ```` ```mermaid ````) in place of the usual highlighted code.
///
/// Upstream always renders a fence through `Syntax`, and so does
/// [`Markdown`](crate::markdown::Markdown) unless a renderer is added with
/// [`Markdown::fence_renderer`](crate::markdown::Markdown::fence_renderer).
/// Markdown asks each renderer in turn; the first to return `Some` wins, and if
/// none does the block is highlighted as code as before.
///
/// The fence body comes from the document, so treat it as untrusted: whatever
/// text of it an implementation echoes must not carry terminal control
/// sequences.
/// A console-wide default [`CodeHighlighter`] and the theme to use with it.
/// See [`ConsoleCodeHighlighting`].
/// Attach/query a console's default code highlighter.
///
/// A [`Syntax`](crate::syntax::Syntax) without a highlighter of its own, and
/// so Markdown code blocks, highlights with the console's when it renders;
/// the console's theme applies when the `Syntax` names none. Without one,
/// the default [`SyntectHighlighter`](crate::syntax::SyntectHighlighter) is
/// used, as before: upstream has no such setting.
/// Evidence for an optional output protocol; inference is not confirmation.
/// Immutable destination capabilities supplied by an extension. No detection or I/O.
/// Optional context shared by nested renderables without changing their protocol.
/// Attach/query a per-console immutable extension environment.