From ff3175125e1c8570ba735a2b4e31b043fb180524 Mon Sep 17 00:00:00 2001 From: Winston Ewert Date: Sat, 14 Sep 2019 12:39:45 -0700 Subject: [PATCH] Add .travis.yml Also run Cargo.fmt (.travis.yml enforces correct formatting) --- .travis.yml | 17 +++++++++++++ examples/multi.rs | 5 ++-- examples/simple.rs | 4 +-- src/lib.rs | 26 +++++++++++--------- src/multi.rs | 20 +++++++-------- src/pb.rs | 61 ++++++++++++++++++++++++++-------------------- src/tty/redox.rs | 4 +-- src/tty/unix.rs | 12 ++++++--- src/tty/windows.rs | 23 +++++++++++------ tests/lib.rs | 5 ++-- 10 files changed, 108 insertions(+), 69 deletions(-) create mode 100644 .travis.yml diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 00000000..1453b208 --- /dev/null +++ b/.travis.yml @@ -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 diff --git a/examples/multi.rs b/examples/multi.rs index 98607280..3416045b 100644 --- a/examples/multi.rs +++ b/examples/multi.rs @@ -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; @@ -38,7 +38,6 @@ fn main() { }); } - mb.println(""); mb.println("Text lines separate between two sections: "); mb.println(""); diff --git a/examples/simple.rs b/examples/simple.rs index bb3d34b5..65711449 100644 --- a/examples/simple.rs +++ b/examples/simple.rs @@ -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; diff --git a/src/lib.rs b/src/lib.rs index 236f7035..6837aa0f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -113,23 +113,25 @@ 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 - where I: Iterator, - T: Write +where + I: Iterator, + T: Write, { iter: I, progress_bar: ProgressBar, } impl PbIter - where I: Iterator +where + I: Iterator, { pub fn new(iter: I) -> Self { Self::on(stdout(), iter) @@ -137,8 +139,9 @@ impl PbIter } impl PbIter - where I: Iterator, - T: Write +where + I: Iterator, + T: Write, { pub fn on(handle: T, iter: I) -> Self { let size = iter.size_hint().0; @@ -150,8 +153,9 @@ impl PbIter } impl Iterator for PbIter - where I: Iterator, - T: Write +where + I: Iterator, + T: Write, { type Item = I::Item; diff --git a/src/multi.rs b/src/multi.rs index 8dc33e19..a2afa50c 100644 --- a/src/multi.rs +++ b/src/multi.rs @@ -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 { nlines: usize, @@ -153,17 +153,18 @@ impl MultiBar { pub fn create_bar(&mut self, total: u64) -> ProgressBar { 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`) @@ -196,7 +197,6 @@ impl MultiBar { let mut first = true; let mut nbars = self.nbars; while nbars > 0 { - // receive message let msg = self.chan.1.recv().unwrap(); if msg.done { diff --git a/src/pb.rs b/src/pb.rs index 004ec6e9..5dd2c97d 100644 --- a/src/pb.rs +++ b/src/pb.rs @@ -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) => {{ @@ -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::() - }} + }}; } const FORMAT: &'static str = "[=>-]"; @@ -214,7 +214,11 @@ impl ProgressBar { 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. @@ -287,11 +291,11 @@ impl ProgressBar { } /// 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(); @@ -326,8 +330,8 @@ impl ProgressBar { // 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 { @@ -354,11 +358,11 @@ impl ProgressBar { // 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 { @@ -369,14 +373,15 @@ impl ProgressBar { 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); } @@ -426,7 +431,6 @@ impl ProgressBar { 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(); @@ -439,7 +443,6 @@ impl ProgressBar { self.finish(); } - /// Call finish and write string `s` below the progress bar. /// /// If the ProgressBar is part of MultiBar instance, you should use @@ -481,7 +484,9 @@ impl Write for ProgressBar { 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) @@ -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] @@ -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] diff --git a/src/tty/redox.rs b/src/tty/redox.rs index 2f9bd8c2..dcf96c21 100644 --- a/src/tty/redox.rs +++ b/src/tty/redox.rs @@ -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, } } diff --git a/src/tty/unix.rs b/src/tty/unix.rs index e4e8fb0f..038b40c3 100644 --- a/src/tty/unix.rs +++ b/src/tty/unix.rs @@ -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>(v: T) -> libc::c_ulong { v.into() } +fn ioctl_conv>(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(v: T) -> T { v } +fn ioctl_conv(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 { diff --git a/src/tty/windows.rs b/src/tty/windows.rs index 27647ae3..7c9be88d 100644 --- a/src/tty/windows.rs +++ b/src/tty/windows.rs @@ -1,6 +1,6 @@ extern crate winapi; -use super::{Width, Height}; +use super::{Height, Width}; /// Returns the size of the terminal, if available. /// @@ -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) }; diff --git a/tests/lib.rs b/tests/lib.rs index 4c291343..fd06ee63 100644 --- a/tests/lib.rs +++ b/tests/lib.rs @@ -1,8 +1,8 @@ extern crate pbr; -use pbr::{ProgressBar, PbIter}; -use std::time::Duration; +use pbr::{PbIter, ProgressBar}; use std::thread; +use std::time::Duration; #[test] fn simple_example() { @@ -63,7 +63,6 @@ fn timeout_example() { pb.finish_println("done!"); } - #[test] // see: issue #11 fn tick_before_start() {