-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Alexander Weber
committed
Jul 18, 2023
1 parent
797aa0d
commit e129699
Showing
2 changed files
with
9 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,30 @@ | ||
use std::{ | ||
error::Error, | ||
io::Write, | ||
result::Result, | ||
}; | ||
use std::{error::Error, io::Write, result::Result}; | ||
|
||
use crossterm::{ | ||
style::Print, | ||
terminal, | ||
QueueableCommand, | ||
}; | ||
use crossterm::{style::Print, QueueableCommand}; | ||
|
||
pub(crate) struct Controller { | ||
pending_lines: Vec<String>, | ||
prefix: String, | ||
max_lines: usize, | ||
lines: Vec<String>, | ||
drawn_lines: usize, | ||
} | ||
|
||
impl Controller { | ||
pub fn new(prefix: String, max: usize) -> Self { | ||
pub fn new(prefix: String) -> Self { | ||
Self { | ||
prefix, | ||
max_lines: max, | ||
drawn_lines: 0, | ||
lines: Vec::<String>::with_capacity(max + 1), | ||
pending_lines: vec![], | ||
} | ||
} | ||
|
||
pub fn append(&mut self, s: String) { | ||
self.lines.push(s); | ||
if self.lines.len() > self.max_lines { | ||
self.lines.remove(0); | ||
} | ||
self.pending_lines.push(s); | ||
} | ||
|
||
pub fn draw(&mut self) -> Result<(), Box<dyn Error>> { | ||
let mut stdout = std::io::stdout(); | ||
if self.drawn_lines > 0 { | ||
stdout | ||
.queue(crossterm::cursor::MoveToColumn(0u16)) | ||
.unwrap() | ||
.queue(crossterm::cursor::MoveUp(self.drawn_lines as u16)) | ||
.unwrap(); | ||
} | ||
stdout.queue(terminal::Clear(terminal::ClearType::FromCursorDown))?; | ||
|
||
for l in &self.lines { | ||
for l in &self.pending_lines { | ||
stdout.queue(Print(format!("{}{}\n", &self.prefix, l))).unwrap(); | ||
} | ||
stdout.flush()?; | ||
self.drawn_lines = self.lines.len(); | ||
Ok(()) | ||
} | ||
} |