1
Fork 0
This commit is contained in:
klensy 2021-04-18 02:15:15 +03:00
parent cb2d52282f
commit 8ebd811b32

View file

@ -7,21 +7,17 @@ pub struct StyledBuffer {
lines: Vec<Vec<StyledChar>>, lines: Vec<Vec<StyledChar>>,
} }
#[derive(Debug)] #[derive(Debug, Clone)]
struct StyledChar { struct StyledChar {
chr: char, chr: char,
style: Style, style: Style,
} }
impl StyledChar { impl StyledChar {
fn new(chr: char, style: Style) -> Self { const SPACE: Self = StyledChar::new(' ', Style::NoStyle);
StyledChar { chr, style }
}
}
impl Default for StyledChar { const fn new(chr: char, style: Style) -> Self {
fn default() -> Self { StyledChar { chr, style }
StyledChar::new(' ', Style::NoStyle)
} }
} }
@ -66,31 +62,25 @@ impl StyledBuffer {
} }
fn ensure_lines(&mut self, line: usize) { fn ensure_lines(&mut self, line: usize) {
while line >= self.lines.len() { if line >= self.lines.len() {
self.lines.push(vec![]); self.lines.resize(line + 1, Vec::new());
} }
} }
/// Sets `chr` with `style` for given `line`, `col`. /// Sets `chr` with `style` for given `line`, `col`.
/// If line not exist in `StyledBuffer`, adds lines up to given /// If `line` does not exist in our buffer, adds empty lines up to the given
/// and fills last line with spaces and `Style::NoStyle` style /// and fills the last line with unstyled whitespace.
pub fn putc(&mut self, line: usize, col: usize, chr: char, style: Style) { pub fn putc(&mut self, line: usize, col: usize, chr: char, style: Style) {
self.ensure_lines(line); self.ensure_lines(line);
if col < self.lines[line].len() { if col >= self.lines[line].len() {
self.lines[line][col] = StyledChar::new(chr, style); self.lines[line].resize(col + 1, StyledChar::SPACE);
} else {
let mut i = self.lines[line].len();
while i < col {
self.lines[line].push(StyledChar::default());
i += 1;
}
self.lines[line].push(StyledChar::new(chr, style));
} }
self.lines[line][col] = StyledChar::new(chr, style);
} }
/// Sets `string` with `style` for given `line`, starting from `col`. /// Sets `string` with `style` for given `line`, starting from `col`.
/// If line not exist in `StyledBuffer`, adds lines up to given /// If `line` does not exist in our buffer, adds empty lines up to the given
/// and fills last line with spaces and `Style::NoStyle` style /// and fills the last line with unstyled whitespace.
pub fn puts(&mut self, line: usize, col: usize, string: &str, style: Style) { pub fn puts(&mut self, line: usize, col: usize, string: &str, style: Style) {
let mut n = col; let mut n = col;
for c in string.chars() { for c in string.chars() {
@ -108,7 +98,7 @@ impl StyledBuffer {
if !self.lines[line].is_empty() { if !self.lines[line].is_empty() {
// Push the old content over to make room for new content // Push the old content over to make room for new content
for _ in 0..string_len { for _ in 0..string_len {
self.lines[line].insert(0, StyledChar::default()); self.lines[line].insert(0, StyledChar::SPACE);
} }
} }