show tab direcories

This commit is contained in:
rabite 2019-02-26 00:32:24 +01:00
parent 184f4916d3
commit a1230fed0d
2 changed files with 41 additions and 42 deletions

View File

@ -11,7 +11,7 @@ use crate::miller_columns::MillerColumns;
use crate::widget::Widget;
use crate::tabview::{TabView, Tabbable};
use crate::preview::WillBeWidget;
use crate::fail::{HError, HResult};
use crate::fail::HResult;
#[derive(PartialEq)]
pub struct FileBrowser {
@ -34,6 +34,15 @@ impl Tabbable for TabView<FileBrowser> {
self.next_tab_();
}
fn get_tab_names(&self) -> Vec<Option<String>> {
self.widgets.iter().map(|filebrowser| {
let path = filebrowser.cwd.path();
let last_dir = path.components().last().unwrap();
let dir_name = last_dir.as_os_str().to_string_lossy().to_string();
Some(dir_name)
}).collect()
}
fn active_tab(& self) -> & dyn Widget {
self.active_tab_()
}
@ -59,7 +68,7 @@ impl FileBrowser {
miller.set_coordinates(&coords);
let (left_coords, main_coords, _) = miller.calculate_coordinates();
let (_, main_coords, _) = miller.calculate_coordinates();
let main_path: std::path::PathBuf = cwd.ancestors().take(1).map(|path| std::path::PathBuf::from(path)).collect();
let main_widget = WillBeWidget::new(Box::new(move |_| {
@ -69,28 +78,13 @@ impl FileBrowser {
Ok(list)
}));
let left_path: std::path::PathBuf = cwd.ancestors().skip(1).take(1).map(|path| std::path::PathBuf::from(path)).collect();
let left_widget = WillBeWidget::new(Box::new(move |_| {
let mut list = ListView::new(Files::new_from_path(&left_path).unwrap());
list.set_coordinates(&left_coords);
list.animate_slide_up();
Ok(list)
}));
miller.push_widget(left_widget);
miller.push_widget(main_widget);
let cwd = File::new_from_path(&cwd).unwrap();
let mut file_browser = FileBrowser { columns: miller,
cwd: cwd };
Ok(file_browser)
Ok(FileBrowser { columns: miller,
cwd: cwd })
}
pub fn enter_dir(&mut self) -> HResult<()> {
@ -100,6 +94,7 @@ impl FileBrowser {
match file.read_dir() {
Ok(files) => {
std::env::set_current_dir(&file.path).unwrap();
self.cwd = file.clone();
let view = WillBeWidget::new(Box::new(move |_| {
let files = files.clone();
let mut list = ListView::new(files);
@ -129,14 +124,9 @@ impl FileBrowser {
}
pub fn go_back(&mut self) -> HResult<()> {
let path = self.selected_file()?.grand_parent()?;
std::env::set_current_dir(path)?;
self.columns.pop_widget();
// Make sure there's a directory on the left unless it's /
self.fix_left()?;
self.columns.refresh();
self.refresh();
Ok(())
}
@ -158,7 +148,7 @@ impl FileBrowser {
let file = self.selected_file()?.clone();
if let Some(grand_parent) = file.grand_parent() {
let (coords, _, _) = self.columns.calculate_coordinates();
let mut left_view = WillBeWidget::new(Box::new(move |_| {
let left_view = WillBeWidget::new(Box::new(move |_| {
let mut view
= ListView::new(Files::new_from_path(&grand_parent)?);
view.set_coordinates(&coords);
@ -176,6 +166,13 @@ impl FileBrowser {
Ok(cwd)
}
pub fn set_cwd(&mut self) -> HResult<()> {
let cwd = self.cwd()?;
std::env::set_current_dir(&cwd.path)?;
self.cwd = cwd;
Ok(())
}
pub fn selected_file(&self) -> HResult<File> {
let widget = self.main_widget()?;
let file = widget.lock()?.as_ref()?.selected_file().clone();
@ -210,11 +207,6 @@ impl FileBrowser {
Ok(())
}
pub fn animate_columns(&mut self) {
self.columns.get_left_widget_mut().map(|w| w.animate_slide_up());
self.columns.get_main_widget_mut().unwrap().animate_slide_up();
}
pub fn turbo_cd(&mut self) -> HResult<()> {
let dir = self.minibuffer("cd: ");
@ -299,8 +291,10 @@ impl Widget for FileBrowser {
crate::term::goto_xy(count_xpos, count_ypos), file_count)
}
fn refresh(&mut self) {
self.update_preview();
self.fix_selection();
self.update_preview().ok();
self.fix_left().ok();
self.fix_selection().ok();
self.set_cwd().ok();
self.columns.refresh();
}
@ -314,12 +308,12 @@ impl Widget for FileBrowser {
fn on_key(&mut self, key: Key) {
match key {
Key::Char('/') => { self.turbo_cd(); },
Key::Char('Q') => { self.quit_with_dir(); },
Key::Right | Key::Char('f') => { self.enter_dir(); },
Key::Left | Key::Char('b') => { self.go_back(); },
Key::Char('/') => { self.turbo_cd().ok(); },
Key::Char('Q') => { self.quit_with_dir().ok(); },
Key::Right | Key::Char('f') => { self.enter_dir().ok(); },
Key::Left | Key::Char('b') => { self.go_back().ok(); },
_ => self.columns.get_main_widget_mut().unwrap().on_key(key),
}
self.update_preview();
self.update_preview().ok();
}
}

View File

@ -8,6 +8,7 @@ pub trait Tabbable {
fn close_tab(&mut self);
fn next_tab(&mut self);
fn on_next_tab(&mut self);
fn get_tab_names(&self) -> Vec<Option<String>>;
fn active_tab(&self) -> &dyn Widget;
fn active_tab_mut(&mut self) -> &mut dyn Widget;
fn on_key(&mut self, key: Key) {
@ -23,7 +24,7 @@ pub trait Tabbable {
#[derive(PartialEq)]
pub struct TabView<T> where T: Widget, TabView<T>: Tabbable {
widgets: Vec<T>,
pub widgets: Vec<T>,
pub active: usize,
coordinates: Coordinates
}
@ -77,17 +78,21 @@ impl<T> Widget for TabView<T> where T: Widget, TabView<T>: Tabbable {
fn render_header(&self) -> String {
let xsize = self.get_coordinates().xsize();
let header = self.active_tab_().render_header();
let mut tab_names = self.get_tab_names();
let mut nums_length = 0;
let tabnums = (0..self.widgets.len()).map(|num| {
nums_length += format!("{} ", num).len();
nums_length += format!("{}:{} ",
num,
tab_names[num].as_ref().unwrap()).len();
if num == self.active {
format!(" {}{}{}{}",
format!(" {}{}:{}{}{}",
crate::term::invert(),
num,
tab_names[num].as_ref().unwrap(),
crate::term::reset(),
crate::term::header_color())
} else {
format!(" {}", num)
format!(" {}:{}", num, tab_names[num].as_ref().unwrap())
}
}).collect::<String>();