From 18b0d42fa6490fb0cd4b0bb2cc17858b848f48ed Mon Sep 17 00:00:00 2001 From: trivernis Date: Wed, 5 Oct 2022 11:00:26 +0200 Subject: [PATCH] Change tree viewer to not allow one to change dirs --- helix-term/src/ui/explore.rs | 78 +++++------------------------------- helix-term/src/ui/tree.rs | 8 ++-- 2 files changed, 13 insertions(+), 73 deletions(-) diff --git a/helix-term/src/ui/explore.rs b/helix-term/src/ui/explore.rs index 929cccf4..143ae1c7 100644 --- a/helix-term/src/ui/explore.rs +++ b/helix-term/src/ui/explore.rs @@ -94,7 +94,13 @@ impl FileInfo { match self.file_type { FileType::Parent => "..".into(), FileType::Placeholder => "---".into(), - FileType::Root => return format!("{}", self.path.display()).into(), + FileType::Root => { + if let Some(path) = self.path.iter().last() { + format!("- {} -", path.to_string_lossy()).into() + } else { + Cow::from("/") + } + } FileType::File | FileType::Exe | FileType::Dir => self .path .file_name() @@ -238,39 +244,6 @@ impl TreeItem for FileInfo { } } -// #[derive(Default, Debug, Clone)] -// struct PathState { -// root: PathBuf, -// sub_items: Vec, -// selected: usize, -// save_view: (usize, usize), // (selected, row) -// row: usize, -// col: usize, -// max_len: usize, -// } - -// impl PathState { - -// fn mkdir(&mut self, dir: &str) -> Result<()> { -// self.new_path(dir, FileType::Dir) -// } - -// fn create_file(&mut self, f: &str) -> Result<()> { -// self.new_path(f, FileType::File) -// } - -// fn remove_current_file(&mut self) -> Result<()> { -// let item = &self.sub_items[self.selected]; -// std::fs::remove_file(item.path_with_root(&self.root))?; -// self.sub_items.remove(self.selected); -// if self.selected >= self.sub_items.len() { -// self.selected = self.sub_items.len() - 1; -// } -// Ok(()) -// } - -// } - #[derive(Clone, Copy, Debug)] enum PromptAction { Search(bool), // search next/search pre @@ -346,18 +319,8 @@ impl Explorer { prompt: None, on_next_key: None, }) - // let mut root = vec![, FileInfo::root(p)]; } - // pub fn new_with_uri(uri: String) -> Result { - // // support remote file? - - // let p = Path::new(&uri); - // ensure!(p.exists(), "path: {uri} is not exist"); - // ensure!(p.is_dir(), "path: {uri} is not dir"); - // Ok(Self::default().with_list(get_sub(p, None)?)) - // } - pub fn focus(&mut self) { self.state.focus = true } @@ -371,7 +334,7 @@ impl Explorer { } fn get_items(p: PathBuf, cx: &mut Context) -> Result> { - let mut items = vec![FileInfo::parent(p.as_path())]; + let mut items = Vec::new(); let root = FileInfo::root(p); let childs = root.get_childs()?; if cx.editor.config().explorer.is_tree() { @@ -510,7 +473,7 @@ impl Explorer { } fn fold_current(item: &mut FileInfo, _cx: &mut Context, _state: &mut State) { - if item.path.is_dir() { + if item.path.is_dir() && item.file_type != FileType::Root { item.expanded = false; } } @@ -618,15 +581,6 @@ impl Explorer { }; let area = side_area.clip_top(list_area.height).clip_right(1); surface.clear_with(area, statusline); - // surface.set_string_truncated( - // area.x, - // area.y, - // &self.path_state.root.to_string_lossy(), - // area.width as usize, - // |_| statusline, - // true, - // true, - // ); } if self.is_focus() { @@ -850,19 +804,6 @@ impl Component for Explorer { self.repeat_motion = Some(repeat_motion); } } - key!('b') => { - if let Some(p) = self.state.current_root.parent() { - match Self::get_items(p.to_path_buf(), cx) { - Ok(items) => { - self.state.current_root = p.to_path_buf(); - self.tree = Tree::build_tree(items) - .with_enter_fn(Self::toggle_current) - .with_folded_fn(Self::fold_current); - } - Err(e) => cx.editor.set_error(format!("{e}")), - } - } - } key!('f') => self.new_filter_prompt(), key!('/') => self.new_search_prompt(true), key!('?') => self.new_search_prompt(false), @@ -969,7 +910,6 @@ fn render_block( if let Some(style) = border_style { block = block.border_style(style); } - //let block = Block::default(); let inner = block.inner(area); block.render(area, surface); inner diff --git a/helix-term/src/ui/tree.rs b/helix-term/src/ui/tree.rs index 6ad7c3b1..6c643b8f 100644 --- a/helix-term/src/ui/tree.rs +++ b/helix-term/src/ui/tree.rs @@ -585,16 +585,16 @@ impl Tree { let count = std::mem::replace(&mut self.count, 0); match key_event.into() { key!(i @ '0'..='9') => self.count = i.to_digit(10).unwrap() as usize + count * 10, - key!('k') | shift!(Tab) | key!(Up) | ctrl!('k') => self.move_up(1.max(count)), - key!('j') | key!(Tab) | key!(Down) | ctrl!('j') => self.move_down(1.max(count)), + key!('k') | shift!(Tab) | key!(Up) => self.move_up(1.max(count)), + key!('j') | key!(Tab) | key!(Down) => self.move_down(1.max(count)), key!('z') => self.fold_current_level(), key!('h') => self.move_left(1.max(count)), key!('l') => self.move_right(1.max(count)), shift!('G') => self.move_down(usize::MAX / 2), key!(Enter) => self.on_enter(cx, params), key!(' ') => self.on_enter(cx, params), - ctrl!('d') => self.move_down_half_page(), - ctrl!('u') => self.move_up_half_page(), + ctrl!('d') | ctrl!('j') => self.move_down_half_page(), + ctrl!('u') | ctrl!('k') => self.move_up_half_page(), shift!('D') => self.move_down_page(), shift!('U') => self.move_up_page(), key!('g') => {