Skip to content

Commit

Permalink
removed output mode
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander Weber committed Jul 18, 2023
1 parent 797aa0d commit e129699
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 33 deletions.
2 changes: 1 addition & 1 deletion src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ impl Config {

let cfg: crate::config::Config = serde_yaml::from_str(&data)?;
Ok(Self {
output: Arc::new(Mutex::new(output::Controller::new("==> ".to_owned(), 10))),
output: Arc::new(Mutex::new(output::Controller::new("==> ".to_owned()))),
chains: cfg.chains,
env: if let Some(e) = cfg.env {
e
Expand Down
40 changes: 8 additions & 32 deletions src/output.rs
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(())
}
}

0 comments on commit e129699

Please sign in to comment.