Skip to content

Commit bb17b47

Browse files
author
phil
committed
Removed terminal dependency altogether. Now printing in the same line is
done with \r again. Should fix Issue #1.
1 parent c31765e commit bb17b47

File tree

3 files changed

+7
-32
lines changed

3 files changed

+7
-32
lines changed

Cargo.toml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,3 @@ repository = "https://github.com/phil0x2e/prgrs/"
1313

1414
[dependencies]
1515
terminal_size = "0.1.10"
16-
17-
[dependencies.terminal]
18-
version = "0.2"
19-
features = ["crossterm-backend"]

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
prgrs is a progress bar for rust, that aims to work like the python package [tqdm](https://github.com/tqdm/tqdm).
33

44

5-
65
prgrs should work for almost any linux terminal emulator. Windows could work too, because terminal supports windows but I haven't tested yet, so please let me know if you have.
76

87
Have a look at the [Documentation](https://docs.rs/prgrs)

src/lib.rs

Lines changed: 7 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,14 @@
1414
//! }
1515
//! }
1616
//! ```
17-
use std::io::{self, Error, ErrorKind, Stdout, Write};
18-
use terminal::{error, Action, Clear, Retrieved, Terminal, Value};
17+
use std::io::{self, Error, ErrorKind, Write};
1918
use terminal_size::{terminal_size, Height, Width};
2019

2120
pub struct Prgrs<T: Iterator> {
2221
iter: T,
2322
size: usize,
2423
curr: usize,
2524
len: Length,
26-
term: Terminal<Stdout>,
2725
}
2826

2927
/// Use this struct to set the length of the progress debug_assert!
@@ -61,7 +59,6 @@ impl<T: Iterator> Prgrs<T> {
6159
size,
6260
curr: 0,
6361
len: Length::Proportional(0.33),
64-
term: terminal::stdout(),
6562
}
6663
}
6764

@@ -100,7 +97,7 @@ impl<T: Iterator> Prgrs<T> {
10097
match self.len {
10198
Length::Absolute(l) => l,
10299
Length::Proportional(mut p) => {
103-
if let Ok(Retrieved::TerminalSize(x, _y)) = self.term.get(Value::TerminalSize) {
100+
if let Some((Width(x), Height(_y))) = terminal_size() {
104101
if p > 1. {
105102
p = 1.;
106103
}
@@ -135,36 +132,19 @@ impl<T: Iterator> Prgrs<T> {
135132
buf.push_str("]");
136133
buf
137134
}
138-
139-
fn print_bar(&mut self) -> error::Result<()> {
140-
if let Retrieved::CursorPosition(_x, y) = self.term.get(Value::CursorPosition)? {
141-
self.term.batch(Action::MoveCursorTo(0, y))?;
142-
self.term.act(Action::ClearTerminal(Clear::CurrentLine))?;
143-
let mut percentage = (self.curr as f32 / self.size as f32) * 100.;
144-
if percentage > 100. {
145-
percentage = 100.;
146-
}
147-
self.term
148-
.write(format!("{} ({:3.0}%)", self.create_bar(), percentage).as_bytes())?;
149-
self.term.flush_batch()?;
150-
}
151-
Ok(())
152-
}
153135
}
154136

155137
impl<T: Iterator> Iterator for Prgrs<T> {
156138
type Item = T::Item;
157139

158140
fn next(&mut self) -> std::option::Option<Self::Item> {
159141
let next = self.iter.next();
160-
if let Err(_e) = self.print_bar() {
161-
let mut percentage = (self.curr as f32 / self.size as f32) * 100.;
162-
if percentage > 100. {
163-
percentage = 100.;
164-
}
165-
print!("{} ({:3.0}%)\r", self.create_bar(), percentage);
166-
io::stdout().flush().ok();
142+
let mut percentage = (self.curr as f32 / self.size as f32) * 100.;
143+
if percentage > 100. {
144+
percentage = 100.;
167145
}
146+
print!("{} ({:3.0}%)\r", self.create_bar(), percentage);
147+
io::stdout().flush().ok();
168148

169149
if let None = next {
170150
println!("");

0 commit comments

Comments
 (0)