From 48cbbf3b8850992d23bf822be46144fbf0e443b5 Mon Sep 17 00:00:00 2001 From: rabite Date: Sun, 16 Jun 2019 12:43:08 +0200 Subject: [PATCH] some small clean-ups --- src/file_browser.rs | 2 +- src/icon.rs | 115 -------------------------------------------- src/stats.rs | 12 +++-- src/textview.rs | 6 +-- 4 files changed, 11 insertions(+), 124 deletions(-) diff --git a/src/file_browser.rs b/src/file_browser.rs index 578a0bb..8361580 100644 --- a/src/file_browser.rs +++ b/src/file_browser.rs @@ -1181,7 +1181,7 @@ impl FileBrowser { let fs = self.fs_stat.read()?.find_fs(&file.path)?.clone(); - let dev = fs.get_dev(); + let dev = fs.get_dev().unwrap_or(String::from("")); let free_space = fs.get_free(); let total_space = fs.get_total(); let space = format!("{}{} / {}", diff --git a/src/icon.rs b/src/icon.rs index f261d1c..9916804 100644 --- a/src/icon.rs +++ b/src/icon.rs @@ -278,118 +278,3 @@ impl Icons { m } } - -#[cfg(test)] -mod test { - use super::{Icons, Theme, ICON_SPACE}; - use crate::meta::Meta; - use std::fs::File; - use tempdir::TempDir; - - #[test] - fn get_no_icon() { - let tmp_dir = TempDir::new("test_file_type").expect("failed to create temp dir"); - let file_path = tmp_dir.path().join("file.txt"); - File::create(&file_path).expect("failed to create file"); - let meta = Meta::from_path(&file_path).unwrap(); - - let icon = Icons::new(Theme::NoIcon); - let icon = icon.get(&meta.name); - - assert_eq!(icon, ""); - } - - #[test] - fn get_default_file_icon() { - let tmp_dir = TempDir::new("test_file_type").expect("failed to create temp dir"); - let file_path = tmp_dir.path().join("file"); - File::create(&file_path).expect("failed to create file"); - let meta = Meta::from_path(&file_path).unwrap(); - - let icon = Icons::new(Theme::Fancy); - let icon = icon.get(&meta.name); - - assert_eq!(icon, format!("{}{}", "\u{f016}", ICON_SPACE)); //  - } - - #[test] - fn get_default_file_icon_unicode() { - let tmp_dir = TempDir::new("test_file_type").expect("failed to create temp dir"); - let file_path = tmp_dir.path().join("file"); - File::create(&file_path).expect("failed to create file"); - let meta = Meta::from_path(&file_path).unwrap(); - - let icon = Icons::new(Theme::Unicode); - let icon = icon.get(&meta.name); - - assert_eq!(icon, format!("{}{}", "\u{1f5cb}", ICON_SPACE)); - } - - #[test] - fn get_directory_icon() { - let tmp_dir = TempDir::new("test_file_type").expect("failed to create temp dir"); - let file_path = tmp_dir.path(); - let meta = Meta::from_path(&file_path.to_path_buf()).unwrap(); - - let icon = Icons::new(Theme::Fancy); - let icon = icon.get(&meta.name); - - assert_eq!(icon, format!("{}{}", "\u{f115}", ICON_SPACE)); //  - } - - #[test] - fn get_directory_icon_unicode() { - let tmp_dir = TempDir::new("test_file_type").expect("failed to create temp dir"); - let file_path = tmp_dir.path(); - let meta = Meta::from_path(&file_path.to_path_buf()).unwrap(); - - let icon = Icons::new(Theme::Unicode); - let icon = icon.get(&meta.name); - - assert_eq!(icon, format!("{}{}", "\u{1f5c1}", ICON_SPACE)); - } - - #[test] - fn get_directory_icon_with_ext() { - let tmp_dir = TempDir::new("test_file_type.rs").expect("failed to create temp dir"); - let file_path = tmp_dir.path(); - let meta = Meta::from_path(&file_path.to_path_buf()).unwrap(); - - let icon = Icons::new(Theme::Fancy); - let icon = icon.get(&meta.name); - - assert_eq!(icon, format!("{}{}", "\u{f115}", ICON_SPACE)); //  - } - - #[test] - fn get_icon_by_name() { - let tmp_dir = TempDir::new("test_file_type").expect("failed to create temp dir"); - - for (file_name, file_icon) in &Icons::get_default_icons_by_name() { - let file_path = tmp_dir.path().join(file_name); - File::create(&file_path).expect("failed to create file"); - let meta = Meta::from_path(&file_path).unwrap(); - - let icon = Icons::new(Theme::Fancy); - let icon = icon.get(&meta.name); - - assert_eq!(icon, format!("{}{}", file_icon, ICON_SPACE)); - } - } - - #[test] - fn get_icon_by_extension() { - let tmp_dir = TempDir::new("test_file_type").expect("failed to create temp dir"); - - for (ext, file_icon) in &Icons::get_default_icons_by_extension() { - let file_path = tmp_dir.path().join(format!("file.{}", ext)); - File::create(&file_path).expect("failed to create file"); - let meta = Meta::from_path(&file_path).unwrap(); - - let icon = Icons::new(Theme::Fancy); - let icon = icon.get(&meta.name); - - assert_eq!(icon, format!("{}{}", file_icon, ICON_SPACE)); - } - } -} diff --git a/src/stats.rs b/src/stats.rs index 1d93e1b..b996772 100644 --- a/src/stats.rs +++ b/src/stats.rs @@ -58,19 +58,21 @@ impl FsStat { } pub trait FsExt { - fn get_dev(&self) -> String; + fn get_dev(&self) -> Option; fn get_total(&self) -> String; fn get_free(&self) -> String; } impl FsExt for Filesystem { - fn get_dev(&self) -> String { + fn get_dev(&self) -> Option { let path = PathBuf::from(&self.fs_mounted_from); - let dev = match path.components().last() { - Some(Component::Normal(dev)) => dev.to_string_lossy().to_string() + ": ", + let dev = path.components().last()?; + let dev = match dev { + Component::Normal(dev) => dev.to_string_lossy().to_string() + ": ", + // zfs on FBSD doesn't return a device path _ => "".to_string() }; - dev + Some(dev) } fn get_total(&self) -> String { diff --git a/src/textview.rs b/src/textview.rs index 7dbf35f..159ac5f 100644 --- a/src/textview.rs +++ b/src/textview.rs @@ -1,4 +1,4 @@ -use std::io::BufRead; +use std::io::{BufRead, BufReader}; use crate::files::File; use crate::term::sized_string; @@ -42,8 +42,8 @@ impl TextView { pub fn new_from_file_limit_lines(core: &WidgetCore, file: &File, num: usize) -> HResult { - let file = std::fs::File::open(&file.path).unwrap(); - let file = std::io::BufReader::new(file); + let file = std::fs::File::open(&file.path)?; + let file = BufReader::new(file); let lines = file.lines() .take(num) .map(|line|