Skip to content

Commit 2016a61

Browse files
authored
Merge pull request #32 from solidiquis/lossy-conversion-file-name
lossy unicode conversion of file name
2 parents 6f130f3 + 8a55ed4 commit 2016a61

File tree

1 file changed

+24
-16
lines changed

1 file changed

+24
-16
lines changed

src/fs/erdtree/node.rs

+24-16
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use ansi_term::Style;
88
use ignore::DirEntry;
99
use lscolors::Style as LS_Style;
1010
use std::{
11+
borrow::Cow,
1112
convert::From,
1213
fmt::{self, Display, Formatter},
1314
ffi::{OsStr, OsString},
@@ -77,6 +78,15 @@ impl Node {
7778
&self.file_name
7879
}
7980

81+
/// Converts `OsStr` to `String`; if fails does a lossy conversion replacing non-Unicode
82+
/// sequences with Unicode replacement scalar value.
83+
pub fn file_name_lossy(&self) -> Cow<'_, str> {
84+
self.file_name().to_str().map_or_else(
85+
|| self.file_name().to_string_lossy(),
86+
|s| Cow::from(s)
87+
)
88+
}
89+
8090
/// Returns `true` if node is a directory.
8191
pub fn is_dir(&self) -> bool {
8292
self.file_type()
@@ -146,17 +156,17 @@ impl Node {
146156
let path = self.symlink_target_path().unwrap_or_else(|| self.path());
147157

148158
if let Some(icon) = path.extension().map(icon_from_ext).flatten() {
149-
return self.stylize(icon)
159+
return Some(self.stylize(icon));
150160
}
151161

152162
if let Some(icon) = self.file_type().map(icon_from_file_type).flatten() {
153-
return self.stylize(icon);
163+
return Some(self.stylize(icon));
154164
}
155165

156166
let file_name = self.symlink_target_file_name().unwrap_or_else(|| self.file_name());
157167

158168
if let Some(icon) = icon_from_file_name(file_name) {
159-
return self.stylize(icon);
169+
return Some(self.stylize(icon));
160170
}
161171

162172
Some(icons::get_default_icon().to_owned())
@@ -165,20 +175,21 @@ impl Node {
165175
/// Stylizes input, `entity` based on [`LS_COLORS`]
166176
///
167177
/// [`LS_COLORS`]: super::tree::ui::LS_COLORS
168-
fn stylize(&self, entity: &str) -> Option<String> {
169-
self.style()
170-
.foreground
171-
.map(|fg| fg.bold().paint(entity).to_string())
172-
.or_else(|| Some(entity.to_string()))
178+
fn stylize(&self, entity: &str) -> String {
179+
self.style().foreground.map_or_else(
180+
|| entity.to_string(),
181+
|fg| fg.bold().paint(entity).to_string()
182+
)
173183
}
174184

175185
/// Stylizes symlink name for display.
176186
fn stylize_link_name(&self) -> Option<String> {
177187
self.symlink_target_file_name()
178188
.map(|name| {
179-
let file_name = self.file_name().to_str().map(|s| self.stylize(s)).flatten().unwrap();
180-
let target_name = Color::Red.paint(format!("\u{2192} {}", name.to_str().unwrap()));
181-
format!("{} {}", file_name, target_name)
189+
let file_name = self.file_name_lossy();
190+
let styled_name = self.stylize(&file_name);
191+
let target_name = Color::Red.paint(format!("\u{2192} {}", name.to_string_lossy()));
192+
format!("{} {}", styled_name, target_name)
182193
})
183194
}
184195
}
@@ -268,11 +279,8 @@ impl Display for Node {
268279
(name, padding)
269280
})
270281
.or_else(|| {
271-
let name = self.file_name()
272-
.to_str()
273-
.map(|name| self.stylize(name))
274-
.flatten()
275-
.unwrap();
282+
let file_name = self.file_name_lossy();
283+
let name = self.stylize(&file_name);
276284
let padding = name.len() + 1;
277285

278286
Some((name, padding))

0 commit comments

Comments
 (0)