|
14 | 14 | //! }
|
15 | 15 | //! }
|
16 | 16 | //! ```
|
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}; |
19 | 18 | use terminal_size::{terminal_size, Height, Width};
|
20 | 19 |
|
21 | 20 | pub struct Prgrs<T: Iterator> {
|
22 | 21 | iter: T,
|
23 | 22 | size: usize,
|
24 | 23 | curr: usize,
|
25 | 24 | len: Length,
|
26 |
| - term: Terminal<Stdout>, |
27 | 25 | }
|
28 | 26 |
|
29 | 27 | /// Use this struct to set the length of the progress debug_assert!
|
@@ -61,7 +59,6 @@ impl<T: Iterator> Prgrs<T> {
|
61 | 59 | size,
|
62 | 60 | curr: 0,
|
63 | 61 | len: Length::Proportional(0.33),
|
64 |
| - term: terminal::stdout(), |
65 | 62 | }
|
66 | 63 | }
|
67 | 64 |
|
@@ -100,7 +97,7 @@ impl<T: Iterator> Prgrs<T> {
|
100 | 97 | match self.len {
|
101 | 98 | Length::Absolute(l) => l,
|
102 | 99 | 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() { |
104 | 101 | if p > 1. {
|
105 | 102 | p = 1.;
|
106 | 103 | }
|
@@ -135,36 +132,19 @@ impl<T: Iterator> Prgrs<T> {
|
135 | 132 | buf.push_str("]");
|
136 | 133 | buf
|
137 | 134 | }
|
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 |
| - } |
153 | 135 | }
|
154 | 136 |
|
155 | 137 | impl<T: Iterator> Iterator for Prgrs<T> {
|
156 | 138 | type Item = T::Item;
|
157 | 139 |
|
158 | 140 | fn next(&mut self) -> std::option::Option<Self::Item> {
|
159 | 141 | 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.; |
167 | 145 | }
|
| 146 | + print!("{} ({:3.0}%)\r", self.create_bar(), percentage); |
| 147 | + io::stdout().flush().ok(); |
168 | 148 |
|
169 | 149 | if let None = next {
|
170 | 150 | println!("");
|
|
0 commit comments