diff options
author | rabite <rabite@posteo.de> | 2019-04-03 00:31:49 +0200 |
---|---|---|
committer | rabite <rabite@posteo.de> | 2019-04-03 00:31:49 +0200 |
commit | 0519f65392289bfdce6e8d140b31384019bd2748 (patch) | |
tree | 48be3f2d1c7f974c0f4c3efd9affb0be98352429 | |
parent | 4d495750968ca2e0bfd507d574689e1369891113 (diff) |
better boomarks + ranger import
-rw-r--r-- | src/bookmarks.rs | 48 | ||||
-rw-r--r-- | src/fail.rs | 6 | ||||
-rw-r--r-- | src/file_browser.rs | 23 | ||||
-rw-r--r-- | src/paths.rs | 6 | ||||
-rw-r--r-- | src/widget.rs | 1 |
5 files changed, 66 insertions, 18 deletions
diff --git a/src/bookmarks.rs b/src/bookmarks.rs index 379565e..d101792 100644 --- a/src/bookmarks.rs +++ b/src/bookmarks.rs @@ -30,27 +30,41 @@ impl Bookmarks { } pub fn load(&mut self) -> HResult<()> { let bm_file = crate::paths::bookmark_path()?; - let bm_content = std::fs::read_to_string(bm_file)?; - - let keys = bm_content.lines().step_by(2).map(|k| k); - let paths = bm_content.lines().skip(1).step_by(2).map(|p| p); + if !bm_file.exists() { + self.import().log(); + } - let mapping = keys.zip(paths).fold(HashMap::new(), |mut mapping, (key, path)| { - if let Some(key) = key.chars().next() { - let path = path.to_string(); - mapping.insert(key, path); + let bm_content = std::fs::read_to_string(bm_file)?; + let mapping = bm_content.lines() + .fold(HashMap::new(), |mut bm, line| { + let parts = line.splitn(2, ":").collect::<Vec<&str>>(); + if parts.len() == 2 { + if let Some(key) = parts[0].chars().next() { + let path = parts[1].to_string(); + bm.insert(key, path); + } } - mapping + bm }); self.mapping = mapping; Ok(()) } + pub fn import(&self) -> HResult<()> { + let mut ranger_bm_path = crate::paths::ranger_path()?; + ranger_bm_path.push("bookmarks"); + + if ranger_bm_path.exists() { + let bm_file = crate::paths::bookmark_path()?; + std::fs::copy(ranger_bm_path, bm_file)?; + } + Ok(()) + } pub fn save(&self) -> HResult<()> { let bm_file = crate::paths::bookmark_path()?; let bookmarks = self.mapping.iter().map(|(key, path)| { - format!("{}\n{}\n", key, path) + format!("{}:{}\n", key, path) }).collect::<String>(); std::fs::write(bm_file, bookmarks)?; @@ -86,6 +100,7 @@ impl BMPopup { Ok(_) => {}, Err(HError::PopupFinnished) => {}, err @ Err(HError::TerminalResizedError) => err?, + err @ Err(HError::WidgetResizedError) => err?, err @ Err(_) => err?, } self.clear()?; @@ -104,6 +119,10 @@ impl BMPopup { Ok(()) } + fn resize(&mut self) -> HResult<()> { + HError::terminal_resized()? + } + pub fn render_line(&self, n: u16, key: &char, path: &str) -> String { let xsize = term::xsize(); let padding = xsize - 4; @@ -134,13 +153,13 @@ impl Widget for BMPopup { HError::terminal_resized() } - fn set_coordinates(&mut self, coordinates: &Coordinates) -> HResult<()> { - let (xsize, ysize) = coordinates.size_u(); + fn set_coordinates(&mut self, _: &Coordinates) -> HResult<()> { + let (xsize, ysize) = crate::term::size()?; let len = self.bookmarks.mapping.len(); let ysize = ysize.saturating_sub( len + 1 ); self.core.coordinates.set_size_u(xsize.saturating_sub(1), len); - self.core.coordinates.set_position_u(1, ysize+2); + self.core.coordinates.set_position_u(1, ysize); Ok(()) } @@ -176,6 +195,7 @@ impl Widget for BMPopup { let path = self.bookmark_path.take()?; self.bookmarks.add(key, &path)?; self.add_mode = false; + self.bookmarks.save().log(); return HError::popup_finnished(); } if let Ok(path) = self.bookmarks.get(key) { @@ -185,6 +205,8 @@ impl Widget for BMPopup { } Key::Alt(key) => { self.bookmarks.mapping.remove(&key); + self.bookmarks.save().log(); + return HError::widget_resized(); } _ => {} } diff --git a/src/fail.rs b/src/fail.rs index 1d5736d..064f721 100644 --- a/src/fail.rs +++ b/src/fail.rs @@ -81,6 +81,8 @@ pub enum HError { WidgetUndefinedKeyError{key: Key}, #[fail(display = "Terminal has been resized!")] TerminalResizedError, + #[fail(display = "Widget has been resized!")] + WidgetResizedError, #[fail(display = "{}", _0)] Log(String), #[fail(display = "Metadata already processed")] @@ -137,6 +139,10 @@ impl HError { Err(HError::TerminalResizedError) } + pub fn widget_resized<T>() -> HResult<T> { + Err(HError::WidgetResizedError) + } + pub fn stale<T>() -> HResult<T> { Err(HError::StaleError) } diff --git a/src/file_browser.rs b/src/file_browser.rs index 19c230f..592eac4 100644 --- a/src/file_browser.rs +++ b/src/file_browser.rs @@ -517,15 +517,26 @@ impl FileBrowser { None => &self.cwd }.path.to_string_lossy().to_string(); + self.bookmarks.lock()?.set_coordinates(&self.core.coordinates).log(); + loop { let bookmark = self.bookmarks.lock()?.pick(cwd.to_string()); if let Err(HError::TerminalResizedError) = bookmark { - self.core.screen.clear().log(); - self.resize().log(); - self.refresh().log(); - self.draw().log(); - continue; + self.core.screen.clear().log(); + self.resize().log(); + self.refresh().log(); + self.draw().log(); + continue; + } + + if let Err(HError::WidgetResizedError) = bookmark { + let coords = &self.core.coordinates; + self.bookmarks.lock()?.set_coordinates(&coords).log(); + self.core.screen.clear().log(); + self.refresh().log(); + self.draw().log(); + continue; } return bookmark; } @@ -540,6 +551,8 @@ impl FileBrowser { pub fn add_bookmark(&mut self) -> HResult<()> { let cwd = self.cwd.path.to_string_lossy().to_string(); + let coords = &self.core.coordinates; + self.bookmarks.lock()?.set_coordinates(&coords).log(); self.bookmarks.lock()?.add(&cwd)?; Ok(()) } diff --git a/src/paths.rs b/src/paths.rs index 8917756..8774597 100644 --- a/src/paths.rs +++ b/src/paths.rs @@ -9,6 +9,12 @@ pub fn home_path() -> HResult<PathBuf> { Ok(home) } +pub fn ranger_path() -> HResult<PathBuf> { + let mut ranger_path = dirs_2::config_dir()?; + ranger_path.push("ranger/"); + Ok(ranger_path) +} + pub fn hunter_path() -> HResult<PathBuf> { let mut hunter_path = dirs_2::config_dir()?; hunter_path.push("hunter/"); diff --git a/src/widget.rs b/src/widget.rs index 46eb340..e1e6055 100644 --- a/src/widget.rs +++ b/src/widget.rs @@ -256,6 +256,7 @@ pub trait Widget { err @ Err(HError::PopupFinnished) | err @ Err(HError::Quit) | err @ Err(HError::MiniBufferCancelledInput) => err?, + err @ Err(HError::WidgetResizedError) => err?, err @ Err(_) => err.log(), Ok(_) => {} } |