1 use std::{borrow::Cow, cmp, fmt::Display, path::Path};
3 use crate::CURRENT_DIRECTORY;
5 /// Converts invalid UTF-8 bytes to the Unicode replacement codepoint (�) in its Display implementation.
6 pub struct EscapedPathDisplay<'a> {
10 impl<'a> EscapedPathDisplay<'a> {
11 pub fn new(path: &'a Path) -> Self {
17 impl Display for EscapedPathDisplay<'_> {
18 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
19 use std::os::unix::prelude::OsStrExt;
21 let bstr = bstr::BStr::new(self.path.as_os_str().as_bytes());
28 impl Display for EscapedPathDisplay<'_> {
29 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
30 use std::{char, fmt::Write, os::windows::prelude::OsStrExt};
32 let utf16 = self.path.as_os_str().encode_wide();
33 let chars = char::decode_utf16(utf16).map(|decoded| decoded.unwrap_or(char::REPLACEMENT_CHARACTER));
43 /// Converts an OsStr to utf8 with custom formatting.
45 /// This is different from [`Path::display`].
47 /// See <https://gist.github.com/marcospb19/ebce5572be26397cf08bbd0fd3b65ac1> for a comparison.
48 pub fn to_utf(os_str: &Path) -> Cow<str> {
50 let text = format!("{os_str:?}");
51 Cow::Owned(text.trim_matches('"').to_string())
54 os_str.to_str().map_or_else(format, Cow::Borrowed)
57 /// Removes the current dir from the beginning of a path as it's redundant information,
58 /// useful for presentation sake.
59 pub fn strip_cur_dir(source_path: &Path) -> &Path {
60 let current_dir = &*CURRENT_DIRECTORY;
62 source_path.strip_prefix(current_dir).unwrap_or(source_path)
65 /// Converts a slice of `AsRef<OsStr>` to comma separated String
67 /// Panics if the slice is empty.
68 pub fn pretty_format_list_of_paths(os_strs: &[impl AsRef<Path>]) -> String {
69 let mut iter = os_strs.iter().map(AsRef::as_ref);
71 let first_element = iter.next().unwrap();
72 let mut string = to_utf(first_element).into_owned();
76 string += &to_utf(os_str);
81 /// Display the directory name, but use "current directory" when necessary.
82 pub fn nice_directory_display(path: &Path) -> Cow<str> {
83 if path == Path::new(".") {
84 Cow::Borrowed("current directory")
90 /// Struct useful to printing bytes as kB, MB, GB, etc.
91 pub struct Bytes(f64);
94 const UNIT_PREFIXES: [&'static str; 6] = ["", "ki", "Mi", "Gi", "Ti", "Pi"];
96 /// Create a new Bytes.
97 pub fn new(bytes: u64) -> Self {
102 impl std::fmt::Display for Bytes {
103 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
104 let &Self(num) = self;
106 debug_assert!(num >= 0.0);
108 return write!(f, "{} B", num);
111 let delimiter = 1000_f64;
112 let exponent = cmp::min((num.ln() / 6.90775).floor() as i32, 4);
117 num / delimiter.powi(exponent),
118 Bytes::UNIT_PREFIXES[exponent as usize]
128 fn test_pretty_bytes_formatting() {
129 fn format_bytes(bytes: u64) -> String {
130 format!("{}", Bytes::new(bytes))
137 assert_eq!("0 B", format_bytes(0)); // This is weird
138 assert_eq!("1.00 B", format_bytes(b));
139 assert_eq!("999.00 B", format_bytes(b * 999));
140 assert_eq!("12.00 MiB", format_bytes(mb * 12));
141 assert_eq!("123.00 MiB", format_bytes(mb * 123));
142 assert_eq!("5.50 MiB", format_bytes(mb * 5 + kb * 500));
143 assert_eq!("7.54 GiB", format_bytes(gb * 7 + 540 * mb));
144 assert_eq!("1.20 TiB", format_bytes(gb * 1200));
147 assert_eq!("234.00 B", format_bytes(234));
148 assert_eq!("999.00 B", format_bytes(999));
150 assert_eq!("2.23 kiB", format_bytes(2234));
151 assert_eq!("62.50 kiB", format_bytes(62500));
152 assert_eq!("329.99 kiB", format_bytes(329990));
154 assert_eq!("2.75 MiB", format_bytes(2750000));
155 assert_eq!("55.00 MiB", format_bytes(55000000));
156 assert_eq!("987.65 MiB", format_bytes(987654321));
158 assert_eq!("5.28 GiB", format_bytes(5280000000));
159 assert_eq!("95.20 GiB", format_bytes(95200000000));
160 assert_eq!("302.00 GiB", format_bytes(302000000000));
161 assert_eq!("302.99 GiB", format_bytes(302990000000));
162 // Weird aproximation cases:
163 assert_eq!("999.90 GiB", format_bytes(999900000000));
164 assert_eq!("1.00 TiB", format_bytes(999990000000));