aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2018-05-14 19:19:36 -0400
committerJesse Luehrs <doy@tozt.net>2018-05-14 19:19:36 -0400
commitf22fd3a3fc606d94f68386526f6e4fd56ab7a3b2 (patch)
treec4be0e8f6b8266f8921d9f584a238802b55f82dd
parente28033a794f70f0051c5d47651da90830dd6d1ad (diff)
downloadfancy-prompt-f22fd3a3fc606d94f68386526f6e4fd56ab7a3b2.tar.gz
fancy-prompt-f22fd3a3fc606d94f68386526f6e4fd56ab7a3b2.zip
suppress output when testing rendering
-rw-r--r--src/colors.rs68
-rw-r--r--src/main.rs6
-rw-r--r--src/prompt.rs137
3 files changed, 138 insertions, 73 deletions
diff --git a/src/colors.rs b/src/colors.rs
index ab5e179..848507a 100644
--- a/src/colors.rs
+++ b/src/colors.rs
@@ -1,8 +1,6 @@
use std;
use term;
-use std::io::Write;
-
#[derive(Debug, Clone)]
pub enum ShellType {
Unknown,
@@ -97,51 +95,73 @@ impl Colors {
}
}
- pub fn print(&self, color: &str, text: &str) {
+ pub fn print<W: std::io::Write>(
+ &self,
+ t: &mut term::Terminal<Output=W>,
+ color: &str,
+ text: &str,
+ ) {
let color = self.color_map.get(color);
- self.print_with_color(color, text);
+ self.print_with_color(t, color, text);
}
- pub fn pad(&self, len: usize) {
- print!("{}", " ".repeat(len));
+ pub fn pad<W: std::io::Write>(
+ &self,
+ t: &mut term::Terminal<Output=W>,
+ len: usize,
+ ) {
+ write!(t, "{}", " ".repeat(len)).unwrap();
}
- pub fn newline(&self) {
- self.print_wrapped(|| {
- print!("{}", "\n");
+ pub fn newline<W: std::io::Write>(
+ &self,
+ t: &mut term::Terminal<Output=W>,
+ ) {
+ self.print_wrapped(t, |t| {
+ write!(t, "{}", "\n").unwrap();
});
}
- pub fn print_host(&self, host: Option<&str>, text: &str) {
+ pub fn print_host<W: std::io::Write>(
+ &self,
+ t: &mut term::Terminal<Output=W>,
+ host: Option<&str>,
+ text: &str,
+ ) {
let color = host.and_then(|hostname| {
self.color_map.get(&format!("host_{}", hostname))
});
- self.print_with_color(color, text);
+ self.print_with_color(t, color, text);
}
- pub fn print_user(&self, user: Option<&str>, text: &str) {
+ pub fn print_user<W: std::io::Write>(
+ &self,
+ t: &mut term::Terminal<Output=W>,
+ user: Option<&str>,
+ text: &str,
+ ) {
let color = user.and_then(|username| {
self.color_map.get(&format!("user_{}", username))
});
- self.print_with_color(color, text);
+ self.print_with_color(t, color, text);
}
- fn print_with_color(
+ fn print_with_color<W: std::io::Write>(
&self,
+ t: &mut term::Terminal<Output=W>,
color: Option<&term::color::Color>,
text: &str,
) {
- let mut t = term::TerminfoTerminal::new(std::io::stdout()).unwrap();
- self.print_color(&mut t, color);
+ self.print_color(t, color);
write!(t, "{}", text).unwrap();
- self.print_reset(&mut t);
+ self.print_reset(t);
}
fn print_reset<W: std::io::Write>(
&self,
t: &mut term::Terminal<Output=W>,
) {
- self.print_wrapped(|| {
+ self.print_wrapped(t, |t| {
t.reset().unwrap();
})
}
@@ -151,7 +171,7 @@ impl Colors {
t: &mut term::Terminal<Output=W>,
color: Option<&term::color::Color>,
) {
- self.print_wrapped(|| {
+ self.print_wrapped(t, |t| {
let real_color = *color.unwrap_or(&self.unknown_color);
t.fg(real_color).unwrap();
match real_color {
@@ -170,9 +190,13 @@ impl Colors {
})
}
- fn print_wrapped<T>(&self, printer: T)
+ fn print_wrapped<W: std::io::Write, T>(
+ &self,
+ t: &mut term::Terminal<Output=W>,
+ printer: T,
+ )
where
- T: FnOnce(),
+ T: FnOnce(&mut term::Terminal<Output=W>),
{
match self.shell_type {
ShellType::Bash => {
@@ -184,7 +208,7 @@ impl Colors {
_ => {}
}
- printer();
+ printer(t);
match self.shell_type {
ShellType::Bash => {
diff --git a/src/main.rs b/src/main.rs
index 70c6adf..b32c2b7 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -26,7 +26,8 @@ fn main() {
talk_about_time!("parsing args");
let data = data::collect(opts);
talk_about_time!("collecting data");
- prompt::Prompt::new(data).display();
+ let w = std::io::stdout();
+ prompt::Prompt::new(data).display(w);
talk_about_time!("displaying data");
stop_talking_about_time!();
}
@@ -51,6 +52,7 @@ mod tests {
power_info: power::PowerInfo::new(),
vcs_info: None,
};
- prompt::Prompt::new(data).display();
+ let w = vec![];
+ prompt::Prompt::new(data).display(w);
}
}
diff --git a/src/prompt.rs b/src/prompt.rs
index 620bea5..4b207db 100644
--- a/src/prompt.rs
+++ b/src/prompt.rs
@@ -1,5 +1,6 @@
use regex;
use std;
+use term;
use std::fmt::Write;
@@ -22,7 +23,9 @@ impl Prompt {
}
}
- pub fn display(&self) {
+ pub fn display<W: std::io::Write>(&self, w: W) {
+ let mut t = term::TerminfoTerminal::new(w).unwrap();
+
let user =
self.data.user
.as_ref()
@@ -70,8 +73,9 @@ impl Prompt {
let path =
compress_path(&self.data.pwd, &self.data.home, max_path_len);
- self.colors.pad(1);
+ self.colors.pad(&mut t, 1);
self.display_path(
+ &mut t,
&path,
&path_color(
self.data.pwd.as_ref().map(std::path::PathBuf::as_ref),
@@ -80,116 +84,143 @@ impl Prompt {
&self.vcs_color(),
);
- self.colors.pad(1);
- self.display_border(max_path_len - path.len() + 1);
- self.colors.pad(1);
+ self.colors.pad(&mut t, 1);
+ self.display_border(&mut t, max_path_len - path.len() + 1);
+ self.colors.pad(&mut t, 1);
if self.data.power_info.has_batteries() {
- self.display_battery(battery_len);
- self.colors.pad(1);
+ self.display_battery(&mut t, battery_len);
+ self.colors.pad(&mut t, 1);
}
- self.display_identity(user, host);
- self.colors.pad(1);
+ self.display_identity(&mut t, user, host);
+ self.colors.pad(&mut t, 1);
- self.display_time();
- self.colors.pad(1);
+ self.display_time(&mut t);
+ self.colors.pad(&mut t, 1);
- self.colors.newline();
+ self.colors.newline(&mut t);
- self.display_error_code();
- self.colors.pad(1);
+ self.display_error_code(&mut t);
+ self.colors.pad(&mut t, 1);
- self.display_prompt();
- self.colors.pad(1);
+ self.display_prompt(&mut t);
+ self.colors.pad(&mut t, 1);
#[cfg(feature = "verbose")]
- self.colors.newline();
+ self.colors.newline(&mut t);
}
- fn display_path(
+ fn display_path<W: std::io::Write>(
&self,
+ t: &mut term::Terminal<Output=W>,
path: &str,
path_color: &str,
vcs: Option<&str>,
vcs_color: &str,
) {
- self.print_host("(");
- self.colors.print(path_color, path);
+ self.print_host(t, "(");
+ self.colors.print(t, path_color, path);
if let Some(vcs) = vcs {
- self.print_host("|");
- self.colors.print(vcs_color, vcs);
+ self.print_host(t, "|");
+ self.colors.print(t, vcs_color, vcs);
}
- self.print_host(")");
+ self.print_host(t, ")");
}
- fn display_border(&self, len: usize) {
- self.colors.print("default", &"-".repeat(len));
+ fn display_border<W: std::io::Write>(
+ &self,
+ t: &mut term::Terminal<Output=W>,
+ len: usize
+ ) {
+ self.colors.print(t, "default", &"-".repeat(len));
}
- fn display_battery(&self, len: usize) {
- self.print_host("{");
+ fn display_battery<W: std::io::Write>(
+ &self,
+ t: &mut term::Terminal<Output=W>,
+ len: usize
+ ) {
+ self.print_host(t, "{");
if let Some(battery_usage) = self.data.power_info.battery_usage() {
let charging = self.data.power_info.charging();
let color = battery_discharge_color(battery_usage, charging);
let filled = (battery_usage * (len as f64)).ceil() as usize;
if len > filled {
let unfilled = len - filled;
- self.colors.print(color, &"-".repeat(unfilled));
+ self.colors.print(t, color, &"-".repeat(unfilled));
}
if len >= filled {
if charging {
- self.colors.print("battery_charging", "<");
+ self.colors.print(t, "battery_charging", "<");
}
else {
- self.colors.print(color, ">");
+ self.colors.print(t, color, ">");
}
}
if filled > 1 {
self.colors
- .print("battery_charging", &"=".repeat(filled - 1));
+ .print(t, "battery_charging", &"=".repeat(filled - 1));
}
}
else {
- self.colors.print("error", &"?".repeat(len));
+ self.colors.print(t, "error", &"?".repeat(len));
}
- self.print_host("}");
+ self.print_host(t, "}");
}
- fn display_identity(&self, user: &str, host: &str) {
- self.print_user(user);
- self.colors.print("default", "@");
- self.print_host(host);
+ fn display_identity<W: std::io::Write>(
+ &self,
+ t: &mut term::Terminal<Output=W>,
+ user: &str,
+ host: &str,
+ ) {
+ self.print_user(t, user);
+ self.colors.print(t, "default", "@");
+ self.print_host(t, host);
}
- fn display_time(&self) {
- self.print_host("[");
+ fn display_time<W: std::io::Write>(
+ &self,
+ t: &mut term::Terminal<Output=W>,
+ ) {
+ self.print_host(t, "[");
self.colors.print(
+ t,
"default",
&format!("{}", self.data.time.format("%H:%M:%S")),
);
- self.print_host("]");
+ self.print_host(t, "]");
}
- fn display_error_code(&self) {
+ fn display_error_code<W: std::io::Write>(
+ &self,
+ t: &mut term::Terminal<Output=W>,
+ ) {
let error_code_color = if self.data.error_code == 0 {
"default"
}
else {
"error"
};
- self.colors
- .print(error_code_color, &format!("{:03}", self.data.error_code));
+ self.colors.print(
+ t,
+ error_code_color,
+ &format!("{:03}", self.data.error_code)
+ );
}
- fn display_prompt(&self) {
+ fn display_prompt<W: std::io::Write>(
+ &self,
+ t: &mut term::Terminal<Output=W>,
+ ) {
let prompt = if self.data.is_root {
"#"
}
else {
"$"
};
- self.print_user(prompt);
+ self.print_user(t, prompt);
}
fn format_vcs(&self) -> Option<String> {
@@ -200,12 +231,20 @@ impl Prompt {
vcs_color(self.data.vcs_info.as_ref().map(|v| &**v))
}
- fn print_host(&self, text: &str) {
- self.colors.print_host(self.hostname(), text);
+ fn print_host<W: std::io::Write>(
+ &self,
+ t: &mut term::Terminal<Output=W>,
+ text: &str
+ ) {
+ self.colors.print_host(t, self.hostname(), text);
}
- fn print_user(&self, text: &str) {
- self.colors.print_user(self.user(), text);
+ fn print_user<W: std::io::Write>(
+ &self,
+ t: &mut term::Terminal<Output=W>,
+ text: &str
+ ) {
+ self.colors.print_user(t, self.user(), text);
}
fn hostname(&self) -> Option<&str> {