Skip to content

Commit acecaf4

Browse files
committed
Print the root path relative to the initial root
1 parent 51c71d6 commit acecaf4

File tree

16 files changed

+104
-35
lines changed

16 files changed

+104
-35
lines changed

man/page

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ Show folders and files alike
3737
\fB\-\-show\-root\-fs\fR
3838
Show filesystem info on top
3939
.TP
40+
\fB\-\-root\-relative\-path\fR
41+
Show the root path relative to the launch directory
42+
.TP
4043
\fB\-g\fR, \fB\-\-show\-git\-info\fR
4144
Show git statuses on files and stats on repo
4245
.TP

src/app/app_context.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ pub struct AppContext {
3434
/// Initial tree options
3535
pub initial_tree_options: TreeOptions,
3636

37+
/// Initial working directory. This is where `broot` is launched, not the root path!
38+
pub initial_working_dir: PathBuf,
39+
3740
/// where's the config file we're using
3841
/// This vec can't be empty
3942
pub config_paths: Vec<PathBuf>,
@@ -159,6 +162,7 @@ impl AppContext {
159162
initial_root,
160163
initial_file,
161164
initial_tree_options,
165+
initial_working_dir: std::env::current_dir()?,
162166
config_paths,
163167
launch_args,
164168
verb_store,

src/app/panel_state.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,21 @@ pub trait PanelState {
390390
con,
391391
)
392392
}
393+
Internal::toggle_root_relative => {
394+
self.with_new_options(
395+
screen,
396+
&|o| {
397+
o.relative_root ^= true;
398+
if o.relative_root {
399+
"*displaying root relative to launch dir*"
400+
} else {
401+
"*displaying root as absolute path*"
402+
}
403+
},
404+
bang,
405+
con,
406+
)
407+
}
393408
Internal::toggle_git_ignore => {
394409
self.with_new_options(
395410
screen,

src/browser/browser_state.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -708,7 +708,7 @@ impl PanelState for BrowserState {
708708
disc: &DisplayContext,
709709
) -> Result<(), ProgramError> {
710710
let dp = DisplayableTree {
711-
app_state: Some(disc.app_state),
711+
display_context: Some(disc),
712712
tree: self.displayed_tree(),
713713
skin: &disc.panel_skin.styles,
714714
ext_colors: &disc.con.ext_colors,

src/cli/args.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ pub struct Args {
4444
#[arg(long)]
4545
pub show_root_fs: bool,
4646

47+
/// Show the root path relative to the launch directory.
48+
#[arg(long)]
49+
pub root_relative_path: bool,
50+
4751
/// Show git statuses on files and stats on repo
4852
#[arg(short='g', long)]
4953
pub show_git_info: bool,

src/conf/conf.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,9 @@ pub struct Conf {
107107

108108
#[serde(alias="content-search-max-file-size", deserialize_with="file_size::deserialize", default)]
109109
pub content_search_max_file_size: Option<u64>,
110+
111+
#[serde(alias="root-relative-path")]
112+
pub root_relative_path: Option<bool>,
110113
}
111114

112115
impl Conf {
@@ -187,6 +190,7 @@ impl Conf {
187190
overwrite!(self, max_staged_count, conf);
188191
overwrite!(self, show_matching_characters_on_path_searches, conf);
189192
overwrite!(self, content_search_max_file_size, conf);
193+
overwrite!(self, root_relative_path, conf);
190194
self.verbs.append(&mut conf.verbs);
191195
// the following maps are "additive": we can add entries from several
192196
// config files and they still make sense

src/display/displayable_tree.rs

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
use crate::app::DisplayContext;
2+
use crate::path::relativize_path;
3+
14
use {
25
super::{
36
cond_bg,
@@ -9,7 +12,6 @@ use {
912
SPACE_FILLING, BRANCH_FILLING,
1013
},
1114
crate::{
12-
app::AppState,
1315
content_search::ContentMatch,
1416
errors::ProgramError,
1517
file_sum::FileSum,
@@ -37,7 +39,7 @@ use {
3739
/// - a scrollbar may be drawn
3840
/// - the empty lines will be erased
3941
pub struct DisplayableTree<'a, 's, 't> {
40-
pub app_state: Option<&'a AppState>,
42+
pub display_context: Option<&'a DisplayContext<'a>>,
4143
pub tree: &'t Tree,
4244
pub skin: &'s StyleMap,
4345
pub area: termimad::Area,
@@ -55,7 +57,7 @@ impl<'a, 's, 't> DisplayableTree<'a, 's, 't> {
5557
height: u16,
5658
) -> DisplayableTree<'a, 's, 't> {
5759
DisplayableTree {
58-
app_state: None,
60+
display_context: None,
5961
tree,
6062
skin,
6163
ext_colors,
@@ -413,8 +415,15 @@ impl<'a, 's, 't> DisplayableTree<'a, 's, 't> {
413415
)?;
414416
}
415417
}
416-
let title = line.path.to_string_lossy();
417-
cw.queue_str(style, &title)?;
418+
match self.display_context {
419+
Some(context) if self.tree.options.relative_root => {
420+
cw.queue_str(style, &relativize_path(&line.path, context.con)?)?;
421+
},
422+
_ => {
423+
cw.queue_str(style, &line.path.to_string_lossy())?;
424+
},
425+
}
426+
418427
if self.in_app && !cw.is_full() {
419428
if let ComputationResult::Done(git_status) = &self.tree.git_status {
420429
let git_status_display = GitStatusDisplay::from(
@@ -486,7 +495,7 @@ impl<'a, 's, 't> DisplayableTree<'a, 's, 't> {
486495
.options
487496
.cols_order
488497
.iter()
489-
.filter(|col| col.is_visible(tree, self.app_state))
498+
.filter(|col| col.is_visible(tree, self.display_context.map(|con| con.app_state)))
490499
.cloned()
491500
.collect();
492501

@@ -536,8 +545,8 @@ impl<'a, 's, 't> DisplayableTree<'a, 's, 't> {
536545
if visible_cols[0].needs_left_margin() {
537546
cw.queue_char(space_style, ' ')?;
538547
}
539-
let staged = self.app_state
540-
.map_or(false, |a| a.stage.contains(&line.path));
548+
let staged = self.display_context
549+
.map_or(false, |a| a.app_state.stage.contains(&line.path));
541550
for col in &visible_cols {
542551
let void_len = match col {
543552

src/path/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ mod closest;
44
mod from;
55
mod normalize;
66
mod special_path;
7+
mod relative;
78

89
pub use {
910
anchor::*,
@@ -12,4 +13,5 @@ pub use {
1213
from::*,
1314
normalize::*,
1415
special_path::*,
16+
relative::*,
1517
};

src/path/relative.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
use std::io;
2+
use std::path::Path;
3+
4+
use crate::app::AppContext;
5+
6+
pub fn relativize_path(path: &Path, con: &AppContext) -> io::Result<String> {
7+
let relative_path = match pathdiff::diff_paths(path, &con.initial_working_dir) {
8+
None => {
9+
return Err(io::Error::new(
10+
io::ErrorKind::Other,
11+
format!("Cannot relativize {path:?}"), // does this happen ? how ?
12+
));
13+
}
14+
Some(p) => p,
15+
};
16+
Ok(
17+
if relative_path.components().next().is_some() {
18+
relative_path.to_string_lossy().to_string()
19+
} else {
20+
".".to_string()
21+
}
22+
)
23+
}

src/preview/dir_view.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ impl DirView {
6767
self.page_height = Some(page_height);
6868
}
6969
let dp = DisplayableTree {
70-
app_state: None,
70+
display_context: None,
7171
tree: &self.tree,
7272
skin: &disc.panel_skin.styles,
7373
ext_colors: &disc.con.ext_colors,

0 commit comments

Comments
 (0)