• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

rust-lang / annotate-snippets-rs / 12696048737

09 Jan 2025 06:15PM UTC coverage: 81.977% (-3.5%) from 85.466%
12696048737

Pull #172

github

web-flow
Merge 179098fee into 7132bf31c
Pull Request #172: feat: Add `Renderer::cut_indicator`

14 of 23 new or added lines in 2 files covered. (60.87%)

35 existing lines in 1 file now uncovered.

796 of 971 relevant lines covered (81.98%)

5.77 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

100.0
/src/renderer/styled_buffer.rs
1
//! Adapted from [styled_buffer]
2
//!
3
//! [styled_buffer]: https://github.com/rust-lang/rust/blob/894f7a4ba6554d3797404bbf550d9919df060b97/compiler/rustc_errors/src/styled_buffer.rs
4

5
use crate::renderer::stylesheet::Stylesheet;
6
use anstyle::Style;
7
use std::fmt;
8
use std::fmt::Write;
9

10
#[derive(Debug)]
11
pub(crate) struct StyledBuffer {
12
    lines: Vec<Vec<StyledChar>>,
13
}
14

15
#[derive(Clone, Copy, Debug, PartialEq)]
16
pub(crate) struct StyledChar {
17
    ch: char,
18
    style: Style,
19
}
20

21
impl StyledChar {
22
    pub(crate) const SPACE: Self = StyledChar::new(' ', Style::new());
23

24
    pub(crate) const fn new(ch: char, style: Style) -> StyledChar {
7✔
25
        StyledChar { ch, style }
26
    }
27
}
28

29
impl StyledBuffer {
30
    pub(crate) fn new() -> StyledBuffer {
8✔
31
        StyledBuffer { lines: vec![] }
9✔
32
    }
33

34
    fn ensure_lines(&mut self, line: usize) {
8✔
35
        if line >= self.lines.len() {
4✔
36
            self.lines.resize(line + 1, Vec::new());
8✔
37
        }
38
    }
39

40
    pub(crate) fn render(&self, stylesheet: &Stylesheet) -> Result<String, fmt::Error> {
4✔
41
        let mut str = String::new();
7✔
42
        for (i, line) in self.lines.iter().enumerate() {
19✔
43
            let mut current_style = stylesheet.none;
6✔
44
            for ch in line {
16✔
45
                if ch.style != current_style {
11✔
46
                    if !line.is_empty() {
1✔
47
                        write!(str, "{}", current_style.render_reset())?;
5✔
48
                    }
49
                    current_style = ch.style;
1✔
50
                    write!(str, "{}", current_style.render())?;
3✔
51
                }
52
                write!(str, "{}", ch.ch)?;
7✔
53
            }
54
            write!(str, "{}", current_style.render_reset())?;
15✔
55
            if i != self.lines.len() - 1 {
11✔
56
                writeln!(str)?;
6✔
57
            }
58
        }
59
        Ok(str)
5✔
60
    }
61

62
    /// Sets `chr` with `style` for given `line`, `col`.
63
    /// If `line` does not exist in our buffer, adds empty lines up to the given
64
    /// and fills the last line with unstyled whitespace.
65
    pub(crate) fn putc(&mut self, line: usize, col: usize, chr: char, style: Style) {
7✔
66
        self.ensure_lines(line);
4✔
67
        if col >= self.lines[line].len() {
4✔
68
            self.lines[line].resize(col + 1, StyledChar::SPACE);
7✔
69
        }
70
        self.lines[line][col] = StyledChar::new(chr, style);
5✔
71
    }
72

73
    /// Sets `string` with `style` for given `line`, starting from `col`.
74
    /// If `line` does not exist in our buffer, adds empty lines up to the given
75
    /// and fills the last line with unstyled whitespace.
76
    pub(crate) fn puts(&mut self, line: usize, col: usize, string: &str, style: Style) {
8✔
77
        let mut n = col;
7✔
78
        for c in string.chars() {
21✔
79
            self.putc(line, n, c, style);
5✔
80
            n += 1;
5✔
81
        }
82
    }
83
    /// For given `line` inserts `string` with `style` after old content of that line,
84
    /// adding lines if needed
85
    pub(crate) fn append(&mut self, line: usize, string: &str, style: Style) {
7✔
86
        if line >= self.lines.len() {
8✔
87
            self.puts(line, 0, string, style);
8✔
88
        } else {
89
            let col = self.lines[line].len();
6✔
90
            self.puts(line, col, string, style);
6✔
91
        }
92
    }
93

94
    pub(crate) fn num_lines(&self) -> usize {
7✔
95
        self.lines.len()
6✔
96
    }
97
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc