mirror of
https://github.com/bobwen-dev/hunter
synced 2025-04-12 00:55:41 +02:00
colored file names
This commit is contained in:
parent
a1900941ce
commit
f3f783a3a0
@ -10,3 +10,4 @@ unicode-width = "0.1.5"
|
|||||||
lazy_static = "*"
|
lazy_static = "*"
|
||||||
x11-clipboard = "*"
|
x11-clipboard = "*"
|
||||||
alphanumeric-sort = "1.0.6"
|
alphanumeric-sort = "1.0.6"
|
||||||
|
lscolors = { version = "0.5.0", features = [ "ansi_term" ] }
|
23
src/files.rs
23
src/files.rs
@ -5,12 +5,15 @@ use std::ffi::OsStr;
|
|||||||
use std::cmp::{Ord, Ordering};
|
use std::cmp::{Ord, Ordering};
|
||||||
|
|
||||||
pub struct Files(Vec<File>);
|
pub struct Files(Vec<File>);
|
||||||
|
use lscolors::{LsColors, Style};
|
||||||
|
|
||||||
impl Index<usize> for Files {
|
impl Index<usize> for Files {
|
||||||
type Output = File;
|
type Output = File;
|
||||||
fn index(&self, pos: usize) -> &Self::Output {
|
fn index(&self, pos: usize) -> &Self::Output {
|
||||||
&self.0[pos]
|
&self.0[pos]
|
||||||
}
|
}
|
||||||
|
lazy_static! {
|
||||||
|
static ref COLORS: LsColors = LsColors::from_env().unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
impl PartialOrd for File {
|
impl PartialOrd for File {
|
||||||
@ -45,8 +48,14 @@ impl Files {
|
|||||||
let name = name.to_string_lossy();
|
let name = name.to_string_lossy();
|
||||||
let kind = get_kind(&file);
|
let kind = get_kind(&file);
|
||||||
let path = file.path();
|
let path = file.path();
|
||||||
let size = file.metadata()?.len() / 1024;
|
let meta = file.metadata()?;
|
||||||
let file = File::new(&name, path, kind, size as usize);
|
let size = meta.len() / 1024;
|
||||||
|
let style
|
||||||
|
= match COLORS.style_for_path_with_metadata(file.path(), Some(&meta)) {
|
||||||
|
Some(style) => Some(style.clone()),
|
||||||
|
None => None
|
||||||
|
};
|
||||||
|
let file = File::new(&name, path, kind, size as usize, style);
|
||||||
match kind {
|
match kind {
|
||||||
Kind::Directory => dirs.push(file),
|
Kind::Directory => dirs.push(file),
|
||||||
_ => files.push(file),
|
_ => files.push(file),
|
||||||
@ -84,6 +93,7 @@ pub struct File {
|
|||||||
pub path: PathBuf,
|
pub path: PathBuf,
|
||||||
pub size: Option<usize>,
|
pub size: Option<usize>,
|
||||||
pub kind: Kind,
|
pub kind: Kind,
|
||||||
|
pub style: Option<Style>
|
||||||
// owner: Option<String>,
|
// owner: Option<String>,
|
||||||
// group: Option<String>,
|
// group: Option<String>,
|
||||||
// flags: Option<String>,
|
// flags: Option<String>,
|
||||||
@ -93,12 +103,17 @@ pub struct File {
|
|||||||
|
|
||||||
|
|
||||||
impl File {
|
impl File {
|
||||||
pub fn new(name: &str, path: PathBuf, kind: Kind, size: usize) -> File {
|
pub fn new(name: &str,
|
||||||
|
path: PathBuf,
|
||||||
|
kind: Kind,
|
||||||
|
size: usize,
|
||||||
|
style: Option<Style>) -> File {
|
||||||
File {
|
File {
|
||||||
name: name.to_string(),
|
name: name.to_string(),
|
||||||
path: path,
|
path: path,
|
||||||
size: Some(size),
|
size: Some(size),
|
||||||
kind: kind
|
kind: kind,
|
||||||
|
style: style
|
||||||
// owner: None,
|
// owner: None,
|
||||||
// group: None,
|
// group: None,
|
||||||
// flags: None,
|
// flags: None,
|
||||||
|
@ -61,17 +61,22 @@ impl<T: 'static> ListView<T> where ListView<T>: Widget {
|
|||||||
self.selection += 1;
|
self.selection += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn render_line(&self, name: &str, size: usize, unit: &str) -> String {
|
fn render_line(&self, file: &File) -> String {
|
||||||
let (xsize, _) = self.get_dimensions();
|
let name = &file.name;
|
||||||
let sized_string = term::sized_string(name, xsize);
|
let (size, unit) = file.calculate_size();
|
||||||
let padding = xsize - sized_string.width() as u16;
|
|
||||||
|
|
||||||
|
let (xsize, _) = self.get_dimensions();
|
||||||
|
let sized_string = term::sized_string(&name, xsize);
|
||||||
|
let padding = xsize - sized_string.width() as u16;
|
||||||
|
let styled_string = match &file.style {
|
||||||
|
Some(style) => style.to_ansi_term_style().paint(sized_string).to_string(),
|
||||||
|
_ => format!("{}{}", term::normal_color(), sized_string),
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
format!(
|
format!(
|
||||||
"{}{}{:padding$}{}{}{}{}",
|
"{}{:padding$}{}{}{}{}",
|
||||||
term::normal_color(),
|
styled_string,
|
||||||
sized_string,
|
|
||||||
" ",
|
" ",
|
||||||
term::highlight_color(),
|
term::highlight_color(),
|
||||||
term::cursor_left(size.to_string().width() + unit.width()),
|
term::cursor_left(size.to_string().width() + unit.width()),
|
||||||
@ -150,8 +155,7 @@ impl Widget for ListView<Files> {
|
|||||||
|
|
||||||
fn render(&self) -> Vec<String> {
|
fn render(&self) -> Vec<String> {
|
||||||
self.content.iter().map(|file| {
|
self.content.iter().map(|file| {
|
||||||
let (size, unit) = file.calculate_size();
|
self.render_line(&file)
|
||||||
self.render_line(&file.name, size, &unit)
|
|
||||||
}).collect()
|
}).collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -3,6 +3,7 @@ extern crate unicode_width;
|
|||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate lazy_static;
|
extern crate lazy_static;
|
||||||
extern crate alphanumeric_sort;
|
extern crate alphanumeric_sort;
|
||||||
|
extern crate lscolors;
|
||||||
|
|
||||||
use std::io::{stdout, Write};
|
use std::io::{stdout, Write};
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user