some small clean-ups

This commit is contained in:
rabite 2019-06-16 12:43:08 +02:00
parent 8f5ad6b3c9
commit 48cbbf3b88
4 changed files with 11 additions and 124 deletions

View File

@ -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!("{}{} / {}",

View File

@ -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));
}
}
}

View File

@ -58,19 +58,21 @@ impl FsStat {
}
pub trait FsExt {
fn get_dev(&self) -> String;
fn get_dev(&self) -> Option<String>;
fn get_total(&self) -> String;
fn get_free(&self) -> String;
}
impl FsExt for Filesystem {
fn get_dev(&self) -> String {
fn get_dev(&self) -> Option<String> {
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 {

View File

@ -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<TextView> {
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|