MCPcopy Create free account
hub / github.com/csskit/csskit / append

Method append

crates/csskit_highlight/src/ansi_highlight_cursor_stream.rs:28–82  ·  view source on GitHub ↗
(&mut self, c: SourceCursor<'a>)

Source from the content-addressed store, hash-verified

26
27impl<'a, 'h, W: Write, T: AnsiTheme> SourceCursorSink<'a> for AnsiHighlightCursorStream<'h, W, T> {
28 fn append(&mut self, c: SourceCursor<'a>) {
29 if self.err.is_err() {
30 return;
31 }
32 if let Some(last) = self.last_token
33 && last.needs_separator_for(c.token())
34 {
35 self.err = self.writer.write_char(' ');
36 }
37 self.last_token = Some(c.token());
38 if let Some(highlight) = self.highlighter.get(c.to_span()) {
39 if let SemanticDecoration::BackgroundColor(bg) = highlight.decoration() {
40 let fg = if bg.wcag_contrast_ratio(Named::White) > bg.wcag_contrast_ratio(Named::Black) {
41 Named::White
42 } else {
43 Named::Black
44 };
45 #[cfg(feature = "anstyle")]
46 {
47 let style = Style::new().bg_color(Some(bg.into())).fg_color(Some(fg.into()));
48 self.err = write!(&mut self.writer, "{style}{c}{style:#}");
49 }
50 #[cfg(feature = "owo-colors")]
51 #[cfg(not(feature = "anstyle"))]
52 {
53 use chromashift::Srgb;
54 let bg_srgb: Srgb = bg.into();
55 let fg_srgb: Srgb = fg.into();
56 self.err = write!(
57 &mut self.writer,
58 "{}",
59 c.to_string().truecolor(fg_srgb.red, fg_srgb.green, fg_srgb.blue).on_truecolor(
60 bg_srgb.red,
61 bg_srgb.green,
62 bg_srgb.blue
63 )
64 );
65 }
66 } else {
67 #[cfg(feature = "anstyle")]
68 {
69 let style = self.theme.get_anstyle(highlight.kind(), highlight.modifier());
70 self.err = write!(&mut self.writer, "{style}{c}{style:#}");
71 }
72 #[cfg(feature = "owo-colors")]
73 #[cfg(not(feature = "anstyle"))]
74 {
75 let style = self.theme.get_owo_style(highlight.kind(), highlight.modifier());
76 self.err = write!(&mut self.writer, "{}", style.style(c.to_string()));
77 }
78 }
79 } else {
80 self.err = write!(&mut self.writer, "{c}");
81 }
82 }
83}

Callers

nothing calls this directly

Calls 10

needs_separator_forMethod · 0.80
decorationMethod · 0.80
wcag_contrast_ratioMethod · 0.80
get_anstyleMethod · 0.80
modifierMethod · 0.80
get_owo_styleMethod · 0.80
tokenMethod · 0.45
getMethod · 0.45
to_spanMethod · 0.45
kindMethod · 0.45

Tested by

no test coverage detected