Skip to content

Commit

Permalink
fixed progressbar, wasn't reporting correctly, so it looked like the …
Browse files Browse the repository at this point in the history
…program was doing nothing for a few moments
  • Loading branch information
Frederik Delaere committed Oct 16, 2018
1 parent 5ad76b1 commit 9ee94b0
Showing 1 changed file with 33 additions and 15 deletions.
48 changes: 33 additions & 15 deletions src/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
use std::io::{BufRead, BufReader, BufWriter};
use std::io::{BufRead, BufReader, BufWriter, Stdout};
use std::path::{Path, PathBuf};
use std::fs::File;
use std::cmp::Ordering;
Expand Down Expand Up @@ -112,15 +112,27 @@ fn get_default_version(modulepath: &str, modulename: &str) -> bool {
false
}

fn progressbar(num: u64, msg: &str) -> ProgressBar<Stdout> {
let mut pb = ProgressBar::new(num);
pb.show_speed = false;
pb.show_time_left = false;
pb.show_counter = false;
pb.show_message = true;
pb.message(msg);

pb
}

pub fn update(modulepath: &str, shell: &str) -> bool {
// list is: path to file, module name, default
let mut list: Vec<(String, String, bool)> = Vec::new();
let module_path = Path::new(&modulepath);
let mut index_succes: i32 = 0;
let mut index_default: i32 = 0;
let mut counter: u64 = 0;

let file_str = format!("{}/{}", modulepath, MODULESINDEX);
let num_modules = count_modules_in_cache(&PathBuf::from(&file_str)) * 2;
let num_modules = count_modules_in_cache(&PathBuf::from(&file_str));

if shell == "progressbar" {
echo("", shell);
Expand All @@ -130,7 +142,7 @@ pub fn update(modulepath: &str, shell: &str) -> bool {
let mut pb = ProgressBar::new(0);

if num_modules != 0 && shell == "progressbar" {
pb = ProgressBar::new(num_modules as u64);
pb = progressbar(num_modules, " Scanning folders ");
}

for entry in WalkDir::new(module_path).into_iter().filter_map(|e| e.ok()) {
Expand All @@ -143,6 +155,7 @@ pub fn update(modulepath: &str, shell: &str) -> bool {
if modulename != "" {
if shell == "progressbar" {
pb.inc();
counter += 1;
}
let first = modulename.chars().next().unwrap();
let second = if modulename.len() >= 2 { &modulename[1..2] } else { "" };
Expand Down Expand Up @@ -170,6 +183,10 @@ pub fn update(modulepath: &str, shell: &str) -> bool {
let default = get_default_version(modulepath, modulename);
list.push((str_path.to_string(), modulename.to_string(), default));
index_succes += 1;
if shell == "progressbar" {
pb = progressbar(list.len() as u64 + num_modules, " Scanning folders ");
pb.set(counter);
}
}
}
}
Expand All @@ -178,12 +195,16 @@ pub fn update(modulepath: &str, shell: &str) -> bool {

// now we have all the module files in the current folder
// we need to parse them to get their description

// our list of modules that we will save into the .modulesindex

let mut modules: Vec<Module> = vec![];

for (modulepath, modulename, default) in list {
let path: PathBuf = PathBuf::from(&modulepath);
if shell == "progressbar" {
pb.inc();
pb.message(" Parsing files ");
}

let description: Vec<String> = get_module_description(&path, "description");
let description = description.join(" ");
Expand All @@ -196,11 +217,14 @@ pub fn update(modulepath: &str, shell: &str) -> bool {
index_default += 1;
}
add_module(modulename, description, flags, &mut modules);
if shell == "progressbar" {
pb.inc();
}
}

if shell == "progressbar" {
echo("", shell);
}
echo("", shell);
echo(&" Writing cache file.", shell);

let file: File = match File::create(&file_str) {
Ok(file) => file,
Err(_) => {
Expand All @@ -220,12 +244,6 @@ pub fn update(modulepath: &str, shell: &str) -> bool {
}
};

if shell == "progressbar" {
echo("", shell);
}
echo("", shell);
echo(&" Writing cache file.", shell);

let mut writer = BufWriter::new(file);
encode_into(&modules, &mut writer, bincode::SizeLimit::Infinite).unwrap();

Expand Down Expand Up @@ -255,7 +273,7 @@ pub fn update(modulepath: &str, shell: &str) -> bool {
true
}

fn count_modules_in_cache(filename: &PathBuf) -> usize {
fn count_modules_in_cache(filename: &PathBuf) -> u64 {
let file: File = match File::open(filename) {
Ok(file) => file,
Err(_) => {
Expand All @@ -269,7 +287,7 @@ fn count_modules_in_cache(filename: &PathBuf) -> usize {
let mut reader = BufReader::new(file);
let decoded: Vec<Module> = decode_from(&mut reader, bincode::SizeLimit::Infinite).unwrap();

decoded.len()
decoded.len() as u64
}

pub fn parse_modules_cache_file(filename: &PathBuf, modules: &mut Vec<(String, i64)>) {
Expand Down

0 comments on commit 9ee94b0

Please sign in to comment.