From bea8eb55cb26db5dc50958665668759716ff6830 Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Tue, 27 Feb 2018 02:24:07 -0500 Subject: clean up the verbose output process --- src/main.rs | 3 +++ src/prompt.rs | 3 +++ src/vcs/git.rs | 47 ++++++++++-------------------------- src/verbose.rs | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 95 insertions(+), 34 deletions(-) create mode 100644 src/verbose.rs diff --git a/src/main.rs b/src/main.rs index 85b4627..2a748cf 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,6 +8,9 @@ extern crate term_size; extern crate users; extern crate walkdir; +#[macro_use] +mod verbose; + mod colors; mod power; mod prompt; diff --git a/src/prompt.rs b/src/prompt.rs index 709c518..7914b39 100644 --- a/src/prompt.rs +++ b/src/prompt.rs @@ -109,6 +109,9 @@ impl Prompt { self.display_prompt(); self.colors.pad(1); + + #[cfg(feature = "verbose")] + self.colors.newline(); } fn display_path( diff --git a/src/vcs/git.rs b/src/vcs/git.rs index 29d50da..32369a3 100644 --- a/src/vcs/git.rs +++ b/src/vcs/git.rs @@ -16,8 +16,7 @@ pub struct GitInfo { impl GitInfo { pub fn new(git: &git2::Repository) -> GitInfo { - #[cfg(feature = "verbose")] - let now = std::time::Instant::now(); + start_talking_about_time!("git"); let mut modified_statuses = git2::Status::empty(); modified_statuses.insert(git2::STATUS_WT_DELETED); @@ -33,9 +32,7 @@ impl GitInfo { staged_statuses.insert(git2::STATUS_INDEX_TYPECHANGE); let mut new_statuses = git2::Status::empty(); new_statuses.insert(git2::STATUS_WT_NEW); - - #[cfg(feature = "verbose")] - let now = talk_about_time(now, "status bitsets"); + talk_about_time!("status bitsets"); let mut status_options = git2::StatusOptions::new(); status_options.include_untracked(true); @@ -47,11 +44,11 @@ impl GitInfo { status_options.update_index(false); status_options.no_refresh(true); } - #[cfg(feature = "verbose")] - let now = talk_about_time(now, "status options"); + talk_about_time!("status options"); + let status = git.statuses(Some(&mut status_options)); - #[cfg(feature = "verbose")] - let now = talk_about_time(now, "statuses"); + talk_about_time!("statuses"); + let mut modified_files = false; let mut staged_files = false; let mut new_files = false; @@ -68,12 +65,11 @@ impl GitInfo { } } } - #[cfg(feature = "verbose")] - let now = talk_about_time(now, "status iteration"); + talk_about_time!("status iteration"); let head = git.head(); - #[cfg(feature = "verbose")] - let now = talk_about_time(now, "head"); + talk_about_time!("head"); + let commits = head.is_ok(); let branch = head.ok().and_then(|head| { if head.is_branch() { @@ -92,8 +88,7 @@ impl GitInfo { ) } }); - #[cfg(feature = "verbose")] - let now = talk_about_time(now, "branch"); + talk_about_time!("branch"); let active_operation = match git.state() { git2::RepositoryState::Merge => super::ActiveOperation::Merge, @@ -113,8 +108,7 @@ impl GitInfo { } _ => super::ActiveOperation::None, }; - #[cfg(feature = "verbose")] - let now = talk_about_time(now, "active operation"); + talk_about_time!("active operation"); let remote_branch_diff = git.head() .ok() @@ -143,8 +137,8 @@ impl GitInfo { }) }) }); - #[cfg(feature = "verbose")] - let _ = talk_about_time(now, "remote branch diff"); + talk_about_time!("remote branch diff"); + stop_talking_about_time!(); GitInfo { modified_files, @@ -158,21 +152,6 @@ impl GitInfo { } } -#[cfg(feature = "verbose")] -fn talk_about_time( - now: std::time::Instant, - what: &str, -) -> std::time::Instant { - let elapsed = now.elapsed(); - println!( - "calculating {} took {}.{:09}s", - what, - elapsed.as_secs(), - elapsed.subsec_nanos() - ); - std::time::Instant::now() -} - impl super::VcsInfo for GitInfo { fn vcs(&self) -> super::VcsType { super::VcsType::Git diff --git a/src/verbose.rs b/src/verbose.rs new file mode 100644 index 0000000..8f23c03 --- /dev/null +++ b/src/verbose.rs @@ -0,0 +1,76 @@ +#[cfg(feature = "verbose")] +use std; + +#[cfg(feature = "verbose")] +pub static mut STACK: Option> = None; + +#[cfg(feature = "verbose")] +macro_rules! start_talking_about_time { + ($category:expr) => ( + use std; + use verbose; + unsafe { + let stack = verbose::STACK.get_or_insert_with(|| Vec::new()); + let len = stack.len(); + let now = std::time::Instant::now(); + let category = $category; + stack.push((String::from(category), now.clone(), now.clone())); + eprintln!("{}starting {}", " ".repeat(len), category); + } + ) +} + +#[cfg(feature = "verbose")] +macro_rules! talk_about_time { + ($what:expr) => ( + unsafe { + let stack = verbose::STACK.get_or_insert_with(|| Vec::new()); + let len = stack.len(); + let last = stack.last_mut().unwrap(); + let elapsed = last.1.elapsed(); + eprintln!( + "{}{}: {} took {}.{:09}s", + " ".repeat(len), + last.0, + $what, + elapsed.as_secs(), + elapsed.subsec_nanos() + ); + last.1 = std::time::Instant::now(); + } + ) +} + +#[cfg(feature = "verbose")] +macro_rules! stop_talking_about_time { + () => ( + unsafe { + let stack = verbose::STACK.get_or_insert_with(|| Vec::new()); + let last = stack.pop().unwrap(); + let elapsed = last.2.elapsed(); + let len = stack.len(); + eprintln!( + "{}ending {} (total {}.{:09}s)", + " ".repeat(len), + last.0, + elapsed.as_secs(), + elapsed.subsec_nanos() + ); + } + ) +} + +#[cfg(not(feature = "verbose"))] +macro_rules! start_talking_about_time { + ($e:expr) => () +} + +#[cfg(not(feature = "verbose"))] +macro_rules! talk_about_time { + ($e:expr) => () +} + +#[cfg(not(feature = "verbose"))] +macro_rules! stop_talking_about_time { + () => () +} -- cgit v1.2.3