Skip to content
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

Add configuration for Travis CI #86

Merged
merged 1 commit into from
Sep 14, 2019
Merged
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
17 changes: 17 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
language: rust

sudo: required

rust:
- stable
- beta
- nightly

matrix:
allow_failures:
- rust: nightly

script:
- cargo test --all --verbose
- rustup component add rustfmt-preview
- cargo fmt -- --check
5 changes: 2 additions & 3 deletions examples/multi.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
extern crate rand;
extern crate pbr;
use rand::prelude::*;
extern crate rand;
use pbr::MultiBar;
use rand::prelude::*;
use std::thread;
use std::time::Duration;

Expand Down Expand Up @@ -38,7 +38,6 @@ fn main() {
});
}


mb.println("");
mb.println("Text lines separate between two sections: ");
mb.println("");
Expand Down
4 changes: 2 additions & 2 deletions examples/simple.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
extern crate rand;
extern crate pbr;
use rand::prelude::*;
extern crate rand;
use pbr::ProgressBar;
use rand::prelude::*;
use std::thread;
use std::time::Duration;

Expand Down
26 changes: 15 additions & 11 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,32 +113,35 @@ macro_rules! printfl {

#[macro_use]
extern crate time;
mod tty;
mod pb;
mod multi;
pub use pb::{ProgressBar, Units};
mod pb;
mod tty;
pub use multi::{MultiBar, Pipe};
use std::io::{Write, Stdout, stdout};
pub use pb::{ProgressBar, Units};
use std::io::{stdout, Stdout, Write};

pub struct PbIter<T, I>
where I: Iterator,
T: Write
where
I: Iterator,
T: Write,
{
iter: I,
progress_bar: ProgressBar<T>,
}

impl<I> PbIter<Stdout, I>
where I: Iterator
where
I: Iterator,
{
pub fn new(iter: I) -> Self {
Self::on(stdout(), iter)
}
}

impl<T, I> PbIter<T, I>
where I: Iterator,
T: Write
where
I: Iterator,
T: Write,
{
pub fn on(handle: T, iter: I) -> Self {
let size = iter.size_hint().0;
Expand All @@ -150,8 +153,9 @@ impl<T, I> PbIter<T, I>
}

impl<T, I> Iterator for PbIter<T, I>
where I: Iterator,
T: Write
where
I: Iterator,
T: Write,
{
type Item = I::Item;

Expand Down
20 changes: 10 additions & 10 deletions src/multi.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use pb::ProgressBar;
use std::io::{Result, Stdout, Write};
use std::str::from_utf8;
use tty::move_cursor_up;
use std::io::{Stdout, Result, Write};
use std::sync::mpsc;
use std::sync::mpsc::{Sender, Receiver};
use std::sync::mpsc::{Receiver, Sender};
use tty::move_cursor_up;

pub struct MultiBar<T: Write> {
nlines: usize,
Expand Down Expand Up @@ -153,17 +153,18 @@ impl<T: Write> MultiBar<T> {
pub fn create_bar(&mut self, total: u64) -> ProgressBar<Pipe> {
self.println("");
self.nbars += 1;
let mut p = ProgressBar::on(Pipe {
level: self.nlines - 1,
chan: self.chan.0.clone(),
},
total);
let mut p = ProgressBar::on(
Pipe {
level: self.nlines - 1,
chan: self.chan.0.clone(),
},
total,
);
p.is_multibar = true;
p.add(0);
p
}


/// listen start listen to all bars changes.
///
/// `ProgressBar` that finish its work, must call `finish()` (or `finish_print`)
Expand Down Expand Up @@ -196,7 +197,6 @@ impl<T: Write> MultiBar<T> {
let mut first = true;
let mut nbars = self.nbars;
while nbars > 0 {

// receive message
let msg = self.chan.1.recv().unwrap();
if msg.done {
Expand Down
61 changes: 35 additions & 26 deletions src/pb.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use std::io::Stdout;
use std::io::{self, Write};
use std::iter::repeat;
use std::time::Duration;
use time::{self, SteadyTime};
use std::io::Stdout;
use tty::{Width, terminal_size};
use tty::{terminal_size, Width};

macro_rules! kb_fmt {
($n: ident) => {{
Expand All @@ -13,15 +13,15 @@ macro_rules! kb_fmt {
$n if $n >= kb.powf(3_f64) => format!("{:.*} GB", 2, $n / kb.powf(3_f64)),
$n if $n >= kb.powf(2_f64) => format!("{:.*} MB", 2, $n / kb.powf(2_f64)),
$n if $n >= kb => format!("{:.*} KB", 2, $n / kb),
_ => format!("{:.*} B", 0, $n)
_ => format!("{:.*} B", 0, $n),
}
}}
}};
}

macro_rules! repeat {
($s: expr, $n: expr) => {{
&repeat($s).take($n).collect::<String>()
}}
}};
}

const FORMAT: &'static str = "[=>-]";
Expand Down Expand Up @@ -214,7 +214,11 @@ impl<T: Write> ProgressBar<T> {
if tick_fmt != TICK_FORMAT {
self.show_tick = true;
}
self.tick = tick_fmt.split("").map(|x| x.to_owned()).filter(|x| x != "").collect();
self.tick = tick_fmt
.split("")
.map(|x| x.to_owned())
.filter(|x| x != "")
.collect();
}

/// Set width, or `None` for default.
Expand Down Expand Up @@ -287,11 +291,11 @@ impl<T: Write> ProgressBar<T> {
}

/// Manually set the current value of the bar
///
///
/// # Examples
/// ```no_run
/// use pbr::ProgressBar;
///
///
/// let mut pb = ProgressBar::new(10);
/// pb.set(8);
/// pb.finish();
Expand Down Expand Up @@ -326,8 +330,8 @@ impl<T: Write> ProgressBar<T> {
// precent box
if self.show_percent {
let percent = self.current as f64 / (self.total as f64 / 100f64);
suffix = suffix +
&format!(" {:.*} % ", 2, if percent.is_nan() { 0.0 } else { percent });
suffix =
suffix + &format!(" {:.*} % ", 2, if percent.is_nan() { 0.0 } else { percent });
}
// speed box
if self.show_speed {
Expand All @@ -354,11 +358,11 @@ impl<T: Write> ProgressBar<T> {
// counter box
if self.show_counter {
let (c, t) = (self.current as f64, self.total as f64);
prefix = prefix +
&match self.units {
Units::Default => format!("{} / {} ", c, t),
Units::Bytes => format!("{} / {} ", kb_fmt!(c), kb_fmt!(t)),
};
prefix = prefix
+ &match self.units {
Units::Default => format!("{} / {} ", c, t),
Units::Bytes => format!("{} / {} ", kb_fmt!(c), kb_fmt!(t)),
};
}
// tick box
if self.show_tick {
Expand All @@ -369,14 +373,15 @@ impl<T: Write> ProgressBar<T> {
let p = prefix.len() + suffix.len() + 3;
if p < width {
let size = width - p;
let curr_count = ((self.current as f64 / self.total as f64) * size as f64)
.ceil() as usize;
let curr_count =
((self.current as f64 / self.total as f64) * size as f64).ceil() as usize;
if size >= curr_count {
let rema_count = size - curr_count;
base = self.bar_start.clone();
if rema_count > 0 && curr_count > 0 {
base = base + repeat!(self.bar_current.to_string(), curr_count - 1) +
&self.bar_current_n;
base = base
+ repeat!(self.bar_current.to_string(), curr_count - 1)
+ &self.bar_current_n;
} else {
base = base + repeat!(self.bar_current.to_string(), curr_count);
}
Expand Down Expand Up @@ -426,7 +431,6 @@ impl<T: Write> ProgressBar<T> {
printfl!(self.handle, "");
}


/// Call finish and write string `s` that will replace the progress bar.
pub fn finish_print(&mut self, s: &str) {
self.finish_draw();
Expand All @@ -439,7 +443,6 @@ impl<T: Write> ProgressBar<T> {
self.finish();
}


/// Call finish and write string `s` below the progress bar.
///
/// If the ProgressBar is part of MultiBar instance, you should use
Expand Down Expand Up @@ -481,7 +484,9 @@ impl<T: Write> Write for ProgressBar<T> {
fn time_to_std(d: time::Duration) -> Duration {
if d > time::Duration::zero() {
let secs = d.num_seconds();
let nsecs = (d - time::Duration::seconds(secs)).num_nanoseconds().unwrap();
let nsecs = (d - time::Duration::seconds(secs))
.num_nanoseconds()
.unwrap();
Duration::new(secs as u64, nsecs as u32)
} else {
Duration::new(0, 1)
Expand All @@ -501,8 +506,10 @@ mod test {
let mut pb = ProgressBar::new(10);
pb.add(2);
assert!(pb.current == 2, "should add the given `n` to current");
assert!(pb.add(2) == pb.current,
"add should return the current value");
assert!(
pb.add(2) == pb.current,
"add should return the current value"
);
}

#[test]
Expand All @@ -517,8 +524,10 @@ mod test {
let fmt = "[~> ]";
let mut pb = ProgressBar::new(1);
pb.format(fmt);
assert!(pb.bar_start + &pb.bar_current + &pb.bar_current_n + &pb.bar_remain +
&pb.bar_end == fmt);
assert!(
pb.bar_start + &pb.bar_current + &pb.bar_current_n + &pb.bar_remain + &pb.bar_end
== fmt
);
}

#[test]
Expand Down
4 changes: 2 additions & 2 deletions src/tty/redox.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
extern crate termion;
use super::{Width, Height};
use super::{Height, Width};

pub fn terminal_size() -> Option<(Width, Height)> {
match termion::terminal_size() {
Ok((cols, rows)) => Some((Width(cols), Height(rows))),
Err(..) => None
Err(..) => None,
}
}

Expand Down
12 changes: 8 additions & 4 deletions src/tty/unix.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
extern crate libc;
use super::{Width, Height};
use super::{Height, Width};

// We need to convert from c_int to c_ulong at least on DragonFly and FreeBSD.
#[cfg(any(target_os = "dragonfly", target_os = "freebsd"))]
fn ioctl_conv<T: Into<libc::c_ulong>>(v: T) -> libc::c_ulong { v.into() }
fn ioctl_conv<T: Into<libc::c_ulong>>(v: T) -> libc::c_ulong {
v.into()
}

// No-op on any other operating system.
#[cfg(not(any(target_os = "dragonfly", target_os = "freebsd")))]
fn ioctl_conv<T: Copy>(v: T) -> T { v }
fn ioctl_conv<T: Copy>(v: T) -> T {
v
}

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

if !is_tty {
Expand Down
23 changes: 15 additions & 8 deletions src/tty/windows.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
extern crate winapi;

use super::{Width, Height};
use super::{Height, Width};

/// Returns the size of the terminal, if available.
///
Expand All @@ -22,21 +22,28 @@ pub fn move_cursor_up(n: usize) -> String {
use self::winapi::um::wincon::{SetConsoleCursorPosition, COORD};
if let Some((hand, csbi)) = get_csbi() {
unsafe {
SetConsoleCursorPosition(hand,
COORD {
X: 0,
Y: csbi.dwCursorPosition.Y - n as i16,
});
SetConsoleCursorPosition(
hand,
COORD {
X: 0,
Y: csbi.dwCursorPosition.Y - n as i16,
},
);
}
}
"".to_string()
}

fn get_csbi() -> Option<(self::winapi::shared::ntdef::HANDLE, self::winapi::um::wincon::CONSOLE_SCREEN_BUFFER_INFO)> {
fn get_csbi() -> Option<(
self::winapi::shared::ntdef::HANDLE,
self::winapi::um::wincon::CONSOLE_SCREEN_BUFFER_INFO,
)> {
use self::winapi::shared::ntdef::HANDLE;
use self::winapi::um::processenv::GetStdHandle;
use self::winapi::um::winbase::STD_OUTPUT_HANDLE;
use self::winapi::um::wincon::{GetConsoleScreenBufferInfo, CONSOLE_SCREEN_BUFFER_INFO, COORD, SMALL_RECT};
use self::winapi::um::wincon::{
GetConsoleScreenBufferInfo, CONSOLE_SCREEN_BUFFER_INFO, COORD, SMALL_RECT,
};

let hand: HANDLE = unsafe { GetStdHandle(STD_OUTPUT_HANDLE) };

Expand Down
Loading