| 1 | //! Pure path and size display shared by host and portable commands. |
| 2 | use std::path::Path; |
| 3 | |
| 4 | /// Quote an OS path for terminals, logs, JSON display fields, and errors. |
| 5 | /// |
| 6 | /// The result is always one line. Terminal controls, line separators, bidi |
| 7 | /// formatting controls, quotes, and backslashes are escaped. Unix paths keep |
| 8 | /// non-UTF-8 bytes exact as `\xNN`; Windows preserves unpaired UTF-16 units as |
| 9 | /// `\u{NNNN}`. |
| 10 | #[must_use] |
| 11 | pub fn quote_os_path(path: &Path) -> String { |
| 12 | quote_os_path_inner(path) |
| 13 | } |
| 14 | |
| 15 | #[cfg(unix)] |
| 16 | fn quote_os_path_inner(path: &Path) -> String { |
| 17 | use std::os::unix::ffi::OsStrExt as _; |
| 18 | let bytes = path.as_os_str().as_bytes(); |
| 19 | if let Ok(text) = std::str::from_utf8(bytes) { |
| 20 | return quote_path_text(text); |
| 21 | } |
| 22 | let mut out = String::from("\""); |
| 23 | for byte in bytes { |
| 24 | match byte { |
| 25 | b'"' => out.push_str("\\\""), |
| 26 | b'\\' => out.push_str("\\\\"), |
| 27 | 0x20..=0x7e => out.push(char::from(*byte)), |
| 28 | _ => out.push_str(&format!("\\x{byte:02x}")), |
| 29 | } |
| 30 | } |
| 31 | out.push('"'); |
| 32 | out |
| 33 | } |
| 34 | |
| 35 | #[cfg(windows)] |
| 36 | fn quote_os_path_inner(path: &Path) -> String { |
| 37 | use std::os::windows::ffi::OsStrExt as _; |
| 38 | let mut out = String::from("\""); |
| 39 | for decoded in char::decode_utf16(path.as_os_str().encode_wide()) { |
| 40 | match decoded { |
| 41 | Ok(character) => push_escaped_path_character(&mut out, character), |
| 42 | Err(error) => out.push_str(&format!("\\u{{{:04x}}}", error.unpaired_surrogate())), |
| 43 | } |
| 44 | } |
| 45 | out.push('"'); |
| 46 | out |
| 47 | } |
| 48 | |
| 49 | #[cfg(not(any(unix, windows)))] |
| 50 | fn quote_os_path_inner(path: &Path) -> String { |
| 51 | quote_path_text(&path.to_string_lossy()) |
| 52 | } |
| 53 | |
| 54 | #[cfg(not(windows))] |
| 55 | fn quote_path_text(text: &str) -> String { |
| 56 | let mut out = String::with_capacity(text.len() + 2); |
| 57 | out.push('"'); |
| 58 | for character in text.chars() { |
| 59 | push_escaped_path_character(&mut out, character); |
| 60 | } |
| 61 | out.push('"'); |
| 62 | out |
| 63 | } |
| 64 | |
| 65 | fn push_escaped_path_character(out: &mut String, character: char) { |
| 66 | match character { |
| 67 | '"' => out.push_str("\\\""), |
| 68 | '\\' => out.push_str("\\\\"), |
| 69 | '\n' => out.push_str("\\n"), |
| 70 | '\r' => out.push_str("\\r"), |
| 71 | '\t' => out.push_str("\\t"), |
| 72 | '\u{1b}' => out.push_str("\\x1b"), |
| 73 | character if character.is_control() || is_bidi_format_control(character) => { |
| 74 | out.extend(character.escape_unicode()); |
| 75 | } |
| 76 | character => out.push(character), |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | fn is_bidi_format_control(character: char) -> bool { |
| 81 | matches!( |
| 82 | character, |
| 83 | '\u{061c}' |
| 84 | | '\u{200e}' |
| 85 | | '\u{200f}' |
| 86 | | '\u{2028}' |
| 87 | | '\u{2029}' |
| 88 | | '\u{202a}'..='\u{202e}' |
| 89 | | '\u{2066}'..='\u{2069}' |
| 90 | ) |
| 91 | } |
| 92 | |
| 93 | pub fn display_path_with_home(path: &Path, home: Option<&Path>) -> String { |
| 94 | let Some(home) = home else { |
| 95 | return path.display().to_string(); |
| 96 | }; |
| 97 | if let Ok(rest) = path.strip_prefix(home) { |
| 98 | if rest.as_os_str().is_empty() { |
| 99 | return "~".to_string(); |
| 100 | } |
| 101 | let sep = std::path::MAIN_SEPARATOR_STR; |
| 102 | let mut out = String::from("~"); |
| 103 | for component in rest.components() { |
| 104 | out.push_str(sep); |
| 105 | out.push_str(&component.as_os_str().to_string_lossy()); |
| 106 | } |
| 107 | return out; |
| 108 | } |
| 109 | path.display().to_string() |
| 110 | } |
| 111 | |
| 112 | pub fn format_byte_size(bytes: u64) -> String { |
| 113 | const KIB: u64 = 1024; |
| 114 | const MIB: u64 = KIB * 1024; |
| 115 | if bytes >= MIB { |
| 116 | format!("{} MB", bytes.div_ceil(MIB)) |
| 117 | } else if bytes >= KIB { |
| 118 | format!("{} KB", bytes.div_ceil(KIB)) |
| 119 | } else { |
| 120 | format!("{bytes} B") |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | /// Substitute placeholders in the template once; runtime values are opaque. |
| 125 | pub fn interpolate(template: &str, replacements: &[(&str, &str)]) -> String { |
| 126 | let mut message = String::with_capacity(template.len()); |
| 127 | let mut cursor = 0; |
| 128 | while let Some(relative_start) = template[cursor..].find('{') { |
| 129 | let start = cursor + relative_start; |
| 130 | message.push_str(&template[cursor..start]); |
| 131 | let Some(relative_end) = template[start..].find('}') else { |
| 132 | message.push_str(&template[start..]); |
| 133 | return message; |
| 134 | }; |
| 135 | let end = start + relative_end + 1; |
| 136 | let placeholder = &template[start..end]; |
| 137 | if let Some(value) = replacements |
| 138 | .iter() |
| 139 | .find_map(|(candidate, value)| (*candidate == placeholder).then_some(*value)) |
| 140 | { |
| 141 | message.push_str(value); |
| 142 | } else { |
| 143 | message.push_str(placeholder); |
| 144 | } |
| 145 | cursor = end; |
| 146 | } |
| 147 | message.push_str(&template[cursor..]); |
| 148 | message |
| 149 | } |
| 150 |