1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
|
//! fonctions printing a tree or a path
use {
crate::{
app::{AppContext, AppStateCmdResult},
display::{DisplayableTree, Screen},
errors::ProgramError,
launchable::Launchable,
skin::{ExtColorMap, PanelSkin, StyleMap},
tree::Tree,
},
pathdiff,
std::{
fs::OpenOptions,
io::{self, Write},
path::Path,
},
};
pub fn print_path(path: &Path, con: &AppContext) -> io::Result<AppStateCmdResult> {
let path = path.to_string_lossy().to_string();
Ok(
if let Some(ref output_path) = con.launch_args.file_export_path {
// an output path was provided, we write to it
let f = OpenOptions::new()
.create(true)
.append(true)
.open(output_path)?;
writeln!(&f, "{}", path)?;
AppStateCmdResult::Quit
} else {
// no output path provided. We write on stdout, but we must
// do it after app closing to have the normal terminal
AppStateCmdResult::from(Launchable::printer(path))
},
)
}
pub fn print_relative_path(path: &Path, con: &AppContext) -> io::Result<AppStateCmdResult> {
let relative_path = match pathdiff::diff_paths(path, &con.launch_args.root) {
None => {
return Ok(AppStateCmdResult::DisplayError(
format!("Cannot relativize {:?}", path), // does this happen ? how ?
));
}
Some(p) => p,
};
if relative_path.components().next().is_some() {
print_path(&relative_path, con)
} else {
print_path(Path::new("."), con)
}
}
fn print_tree_to_file(
tree: &Tree,
screen: Screen,
file_path: &str,
ext_colors: &ExtColorMap,
) -> Result<AppStateCmdResult, ProgramError> {
let no_style_skin = StyleMap::no_term();
let dp = DisplayableTree::out_of_app(tree, &no_style_skin, ext_colors, screen.width);
let mut f = OpenOptions::new()
.create(true)
.append(true)
.open(file_path)?;
dp.write_on(&mut f)?;
Ok(AppStateCmdResult::Quit)
}
pub fn print_tree(
tree: &Tree,
screen: Screen,
panel_skin: &PanelSkin,
con: &AppContext,
) -> Result<AppStateCmdResult, ProgramError> {
if let Some(ref output_path) = con.launch_args.file_export_path {
// an output path was provided, we write to it
print_tree_to_file(tree, screen, output_path, &con.ext_colors)
} else {
// no output path provided. We write on stdout, but we must
// do it after app closing to have the normal terminal
let styles = if con.launch_args.no_style {
StyleMap::no_term()
} else {
panel_skin.styles.clone()
};
Ok(AppStateCmdResult::from(Launchable::tree_printer(
tree,
screen,
styles,
con.ext_colors.clone(),
)))
}
}
|