|
Added
Link Here
|
| 1 |
--- src/main.rs.orig 2021-03-09 04:30:36 UTC |
| 2 |
+++ src/main.rs |
| 3 |
@@ -4,7 +4,7 @@ |
| 4 |
|
| 5 |
use std::collections::BTreeMap; |
| 6 |
use std::fs::File; |
| 7 |
-use std::io::{self, BufRead, BufReader}; |
| 8 |
+use std::io::{self, BufRead, BufReader, Write}; |
| 9 |
use std::path::{Path, PathBuf}; |
| 10 |
|
| 11 |
use lscolors::{LsColors, Style}; |
| 12 |
@@ -41,13 +41,14 @@ |
| 13 |
|
| 14 |
fn _print( |
| 15 |
&self, |
| 16 |
+ stdout: &mut io::StdoutLock, |
| 17 |
top: bool, |
| 18 |
prefix: &str, |
| 19 |
join_with_parent: bool, |
| 20 |
lscolors: &LsColors, |
| 21 |
parent_path: PathBuf, |
| 22 |
full_path: bool, |
| 23 |
- ) { |
| 24 |
+ ) -> io::Result<()> { |
| 25 |
let normal_prefix = format!("{}│ ", prefix); |
| 26 |
let last_prefix = format!("{} ", prefix); |
| 27 |
|
| 28 |
@@ -84,48 +85,54 @@ |
| 29 |
"/" |
| 30 |
}; |
| 31 |
if should_print { |
| 32 |
- print!("{}{}{}", style.paint(joiner), painted, newline); |
| 33 |
+ write!(stdout, "{}{}{}", style.paint(joiner), painted, newline)?; |
| 34 |
} |
| 35 |
prefix |
| 36 |
} else if !is_last { |
| 37 |
if should_print { |
| 38 |
- print!("{}├── {}{}", prefix, painted, newline); |
| 39 |
+ write!(stdout, "{}├── {}{}", prefix, painted, newline)?; |
| 40 |
} |
| 41 |
&normal_prefix |
| 42 |
} else { |
| 43 |
if should_print { |
| 44 |
- print!("{}└── {}{}", prefix, painted, newline); |
| 45 |
+ write!(stdout, "{}└── {}{}", prefix, painted, newline)?; |
| 46 |
} |
| 47 |
&last_prefix |
| 48 |
}; |
| 49 |
|
| 50 |
it._print( |
| 51 |
+ stdout, |
| 52 |
false, |
| 53 |
next_prefix, |
| 54 |
contains_singleton_dir, |
| 55 |
lscolors, |
| 56 |
current_path, |
| 57 |
full_path, |
| 58 |
- ) |
| 59 |
+ )?; |
| 60 |
} |
| 61 |
+ |
| 62 |
+ Ok(()) |
| 63 |
} |
| 64 |
|
| 65 |
- fn print(&self, lscolors: &LsColors, full_path: bool) { |
| 66 |
+ fn print(&self, lscolors: &LsColors, full_path: bool) -> io::Result<()> { |
| 67 |
if self.trie.is_empty() { |
| 68 |
- println!(); |
| 69 |
- return; |
| 70 |
+ return Ok(()); |
| 71 |
} |
| 72 |
|
| 73 |
+ let stdout = io::stdout(); |
| 74 |
+ let handle = &mut stdout.lock(); |
| 75 |
+ |
| 76 |
// This works because PathBuf::from(".").join(PathBuf::from("/")) == PathBuf::from("/") |
| 77 |
let current_path = PathBuf::from("."); |
| 78 |
let contains_singleton_dir = self.contains_singleton_dir(); |
| 79 |
|
| 80 |
if !contains_singleton_dir { |
| 81 |
let style = ansi_style_for_path(&lscolors, ¤t_path); |
| 82 |
- println!("{}", style.paint(current_path.to_string_lossy())); |
| 83 |
+ writeln!(handle, "{}", style.paint(current_path.to_string_lossy()))?; |
| 84 |
} |
| 85 |
|
| 86 |
self._print( |
| 87 |
+ handle, |
| 88 |
true, |
| 89 |
"", |
| 90 |
contains_singleton_dir, |
| 91 |
@@ -175,7 +182,14 @@ |
| 92 |
options::Colorize::Never => LsColors::empty(), |
| 93 |
}; |
| 94 |
|
| 95 |
- trie.print(&lscolors, options.full_path); |
| 96 |
+ let result = trie.print(&lscolors, options.full_path); |
| 97 |
|
| 98 |
- io::Result::Ok(()) |
| 99 |
+ match result { |
| 100 |
+ Err(e) if e.kind() == io::ErrorKind::BrokenPipe => { |
| 101 |
+ // ignore broken pipe errors |
| 102 |
+ io::Result::Ok(()) |
| 103 |
+ }, |
| 104 |
+ e@Err(_) => e, |
| 105 |
+ _ => io::Result::Ok(()) |
| 106 |
+ } |
| 107 |
} |