Skip to content

various fixes #57

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ license = "MIT"
[dependencies]
libc = "0.2.9"
time = "0.1.35"

[target.'cfg(target_os = "windows")'.dependencies]
winapi = "0.2"
kernel32-sys = "0.2"

[target.'cfg(target_os = "redox")'.dependencies]
termion = "1.4"
[target.'cfg(not(target_os = "windows"))'.dependencies]
termion = "1.5"

[dev-dependencies]
rand = "0.3.14"
1 change: 0 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@ macro_rules! printfl {
}}
}

#[macro_use]
extern crate time;
mod tty;
mod pb;
Expand Down
22 changes: 14 additions & 8 deletions src/multi.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use pb::ProgressBar;
use std::str::from_utf8;
use tty::move_cursor_up;
use tty;
use std::io::{Stdout, Result, Write};
use std::sync::mpsc;
use std::sync::mpsc::{Sender, Receiver};
Expand All @@ -22,7 +22,7 @@ impl MultiBar<Stdout> {
///
/// # Examples
///
/// ```no_run
/// ```ignore
/// use std::thread;
/// use pbr::MultiBar;
///
Expand Down Expand Up @@ -91,7 +91,7 @@ impl<T: Write> MultiBar<T> {
///
/// # Examples
///
/// ```no_run
/// ```ignore
/// use pbr::MultiBar;
///
/// let mut mb = MultiBar::new();
Expand Down Expand Up @@ -127,7 +127,7 @@ impl<T: Write> MultiBar<T> {
///
/// # Examples
///
/// ```no_run
/// ```ignore
/// use pbr::MultiBar;
///
/// let mut mb = MultiBar::new();
Expand Down Expand Up @@ -171,8 +171,11 @@ impl<T: Write> MultiBar<T> {
///
/// # Examples
///
/// ```no_run
/// use pbr::MultiBar;
/// ```
/// # extern crate pbr;
/// # use std::thread;
/// # fn main() {
/// use ::pbr::MultiBar;
///
/// let mut mb = MultiBar::new();
///
Expand All @@ -186,6 +189,7 @@ impl<T: Write> MultiBar<T> {
/// });
///
/// // ...
/// # }
/// ```
pub fn listen(&mut self) {
let mut first = true;
Expand All @@ -203,12 +207,14 @@ impl<T: Write> MultiBar<T> {
// and draw
let mut out = String::new();
if !first {
out += &move_cursor_up(self.nlines);
out += &tty::move_cursor_up(self.nlines);
} else {
first = false;
}
for l in self.lines.iter() {
out.push_str(&format!("\r{}\n", l));
out += "\r";
out += &l;
out += "\n";
}
printfl!(self.handle, "{}", out);
}
Expand Down
20 changes: 11 additions & 9 deletions src/pb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ pub struct ProgressBar<T: Write> {
pub show_time_left: bool,
pub show_tick: bool,
pub show_message: bool,
handle: T,
handle: Option<T>,
}

impl ProgressBar<Stdout> {
Expand Down Expand Up @@ -134,7 +134,7 @@ impl<T: Write> ProgressBar<T> {
message: String::new(),
last_refresh_time: SteadyTime::now(),
max_refresh_rate: None,
handle: handle,
handle: Some(handle),
};
pb.format(FORMAT);
pb.tick_format(TICK_FORMAT);
Expand Down Expand Up @@ -264,7 +264,7 @@ impl<T: Write> ProgressBar<T> {
/// ```
pub fn tick(&mut self) {
self.tick_state = (self.tick_state + 1) % self.tick.len();
if self.current <= self.total {
if self.handle.is_some() && self.current <= self.total {
self.draw()
}
}
Expand Down Expand Up @@ -391,7 +391,7 @@ impl<T: Write> ProgressBar<T> {
out = out + repeat!(" ", gap);
}
// print
printfl!(self.handle, "\r{}", out);
self.handle.as_mut().map(|h| printfl!(h, "\r{}", out));

self.last_refresh_time = SteadyTime::now();
}
Expand All @@ -413,7 +413,7 @@ impl<T: Write> ProgressBar<T> {
redraw = true;
}

if redraw {
if self.handle.is_some() && redraw {
self.draw();
}
self.is_finish = true;
Expand All @@ -423,7 +423,7 @@ impl<T: Write> ProgressBar<T> {
/// the last time
pub fn finish(&mut self) {
self.finish_draw();
printfl!(self.handle, "");
self.handle.take().map(|mut h| printfl!(h, ""));
}


Expand All @@ -435,8 +435,10 @@ impl<T: Write> ProgressBar<T> {
if s.len() < width {
out += repeat!(" ", width - s.len());
};
printfl!(self.handle, "\r{}", out);
self.finish();
self.handle.take().map(|mut h| {
printfl!(h, "\r{}", out);
printfl!(h, "");
});
}


Expand All @@ -451,7 +453,7 @@ impl<T: Write> ProgressBar<T> {
return self.finish_print(s);
}
self.finish_draw();
printfl!(self.handle, "\n{}", s);
self.handle.take().map(|mut h| printfl!(h, "\n{}", s));
}

/// Get terminal width, from configuration, terminal size, or default(80)
Expand Down
9 changes: 2 additions & 7 deletions src/tty/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,12 @@ pub struct Width(pub u16);
#[derive(Debug)]
pub struct Height(pub u16);

#[cfg(unix)]
#[cfg(not(windows))]
mod unix;
#[cfg(unix)]
#[cfg(not(windows))]
pub use self::unix::*;

#[cfg(windows)]
mod windows;
#[cfg(windows)]
pub use self::windows::*;

#[cfg(target_os = "redox")]
mod redox;
#[cfg(target_os = "redox")]
pub use self::redox::*;
13 changes: 0 additions & 13 deletions src/tty/redox.rs

This file was deleted.

57 changes: 30 additions & 27 deletions src/tty/unix.rs
Original file line number Diff line number Diff line change
@@ -1,50 +1,53 @@
extern crate termion;
extern crate libc;
use super::{Width, Height};

/// Returns the size of the terminal, if available.
///
/// If STDOUT is not a tty, returns `None`
#[cfg(target_os = "redox")]
pub fn terminal_size() -> Option<(Width, Height)> {
use self::libc::{ioctl, isatty, STDOUT_FILENO, TIOCGWINSZ, winsize};
let is_tty: bool = unsafe { isatty(STDOUT_FILENO) == 1 };

if !is_tty {
return None;
match termion::terminal_size() {
Ok((cols, rows)) => Some((Width(cols), Height(rows))),
Err(..) => None
}
}

let (rows, cols) = unsafe {
let mut winsize = winsize {
ws_row: 0,
ws_col: 0,
ws_xpixel: 0,
ws_ypixel: 0,
};
ioctl(STDOUT_FILENO, TIOCGWINSZ, &mut winsize);
let rows = if winsize.ws_row > 0 {
winsize.ws_row
} else {
0
};
let cols = if winsize.ws_col > 0 {
winsize.ws_col
#[cfg(not(target_os = "redox"))]
fn terminal_size_fd(fd: libc::c_int) -> Option<(Width, Height)> {
use std::mem;

unsafe {
let mut size: libc::winsize = mem::zeroed();
if libc::ioctl(fd, libc::TIOCGWINSZ, &mut size as *mut _) == 0 {
Some((Width(size.ws_col), Height(size.ws_row)))
} else {
0
};
(rows as u16, cols as u16)
};
None
}
}
}

if rows > 0 && cols > 0 {
Some((Width(cols), Height(rows)))
/// Returns the size of the terminal, if available.
///
/// If neither STDOUT nor STDERR is a tty, returns `None`
#[cfg(not(target_os = "redox"))]
pub fn terminal_size() -> Option<(Width, Height)> {
if unsafe { libc::isatty(libc::STDOUT_FILENO) == 1 } {
terminal_size_fd(libc::STDOUT_FILENO)
} else if unsafe { libc::isatty(libc::STDERR_FILENO) == 1 }{
terminal_size_fd(libc::STDERR_FILENO)
} else {
None
}
}

/// Return string that move the cursor `n` lines up.
pub fn move_cursor_up(n: usize) -> String {
format!("\x1B[{}A", n)
assert!(n < 0x10000);
format!("{}", termion::cursor::Up(n as u16))
}

#[cfg(not(target_os = "redox"))]
#[test]
/// Compare with the output of `stty size`
fn compare_with_stty() {
Expand Down
19 changes: 18 additions & 1 deletion tests/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
extern crate pbr;

use pbr::{ProgressBar, PbIter};
use pbr::{ProgressBar, PbIter, MultiBar};
use std::time::Duration;
use std::thread;

Expand Down Expand Up @@ -107,3 +107,20 @@ fn npm_bar() {
}
pb.finish_println("done!");
}

#[test]
fn multi_finish_print() {
let count = 10;
let mut mb = MultiBar::new();
let mut pb = mb.create_bar(10);
pb.tick();
let t = thread::spawn(move || {
mb.listen();
});
for _ in 0..count {
thread::sleep(Duration::from_millis(30));
pb.tick();
}
pb.finish_print("done");
t.join().unwrap();
}