Skip to content

Commit

Permalink
🚧 test
Browse files Browse the repository at this point in the history
  • Loading branch information
janwojcicki committed Mar 24, 2021
1 parent 5c62b58 commit a1a52a5
Showing 1 changed file with 29 additions and 45 deletions.
74 changes: 29 additions & 45 deletions src/ui/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,70 +58,54 @@ impl Buffer {
}

pub fn draw_changes<W: Write>(&mut self, stdout: &mut W) -> Result<(), crossterm::ErrorKind> {
let mut changes = self.changes.clone();
changes.retain(|k, v| {
let mut last_style = None;
let mut last_coord = None;
let mut text = "".to_string();

for (k, v) in self.changes.iter() {
if let Some(pixel) = self.pixels.get(*k) {
*pixel != *v
if *pixel != *v {
self.pixels[*k] = *v;
} else {
continue;
}
} else {
false
}
});

self.changes = changes;

let changes = self.changes.keys().collect_vec();

let mut last = None;
let mut i = 0;
while i < changes.len() {
if self.pixels.get(*changes[i]).is_none() {
i += 1;
continue;
}

let start_i = i;

// decide whether moving the cursor is needed
if last.is_none() || last.unwrap() + 1 != *changes[i] {
let (x, y) = self.coord_to_xy(*changes[i]);
queue!(stdout, cursor::MoveTo(x, y))?;
}

// take as many changes with the same style as possible
if last_style != Some(v.style) || *k == 0 || last_coord != Some(*k - 1) {
if !text.is_empty() {
let style = match self.styles.get(&last_style.unwrap()) {
Some(s) => *s,
None => ContentStyle::default(),
};

let style = self.changes.get(changes[i]).unwrap().style;
while i + 1 < changes.len() {
if changes[i] + 1 != *changes[i + 1]
|| self.pixels.get(*changes[i + 1]).is_none()
|| self.changes.get(changes[i + 1]).unwrap().style != style
{
break;
queue!(stdout, style::PrintStyledContent(style.apply(text)))?;
}

i += 1;
}
let (x, y) = self.coord_to_xy(*k);
queue!(stdout, cursor::MoveTo(x, y))?;

for k in &changes[start_i..i + 1] {
self.pixels[**k] = *self.changes.get(*k).unwrap();
text = v.text.unwrap_or(' ').to_string();
last_style = Some(v.style);
} else {
text = format!("{}{}", text, v.text.unwrap_or(' '));
}

let range = *changes[start_i]..(*changes[i] + 1);
let text = self.pixels[range]
.iter()
.map(|pixel| pixel.text.unwrap_or(' '))
.collect::<String>();
last_coord = Some(*k);
}

let style = match self.styles.get(&style) {
if !text.is_empty() {
let style = match self.styles.get(&last_style.unwrap()) {
Some(s) => *s,
None => ContentStyle::default(),
};

queue!(stdout, style::PrintStyledContent(style.apply(text)))?;

last = Some(*changes[i]);
i += 1;
}

self.changes.clear();

stdout.flush()?;

Ok(())
Expand Down

0 comments on commit a1a52a5

Please sign in to comment.