Add ls without the -l flag

This commit is contained in:
Peltoche 2018-11-24 17:42:39 +01:00
parent cd3dc39f03
commit 125cfbaa19
No known key found for this signature in database
GPG key ID: CED68D0487156952
5 changed files with 94 additions and 26 deletions

11
Cargo.lock generated
View file

@ -105,6 +105,7 @@ dependencies = [
"failure 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)",
"lazy_static 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
"size 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
"terminal_size 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)",
"time 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)",
"users 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
@ -182,6 +183,15 @@ dependencies = [
"unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "terminal_size"
version = "0.1.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"libc 0.2.43 (registry+https://github.com/rust-lang/crates.io-index)",
"winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "termion"
version = "1.5.1"
@ -275,6 +285,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
"checksum strsim 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "bb4f380125926a99e52bc279241539c018323fab05ad6368b56f93d9369ff550"
"checksum syn 0.15.21 (registry+https://github.com/rust-lang/crates.io-index)" = "816b7af21405b011a23554ea2dc3f6576dc86ca557047c34098c1d741f10f823"
"checksum synstructure 0.10.1 (registry+https://github.com/rust-lang/crates.io-index)" = "73687139bf99285483c96ac0add482c3776528beac1d97d444f6e91f203a2015"
"checksum terminal_size 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)" = "023345d35850b69849741bd9a5432aa35290e3d8eb76af8717026f270d1cf133"
"checksum termion 1.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "689a3bdfaab439fd92bc87df5c4c78417d3cbe537487274e9b0b2dce76e92096"
"checksum textwrap 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)" = "307686869c93e71f94da64286f9a9524c0f308a9e1c87a583de8e9c9039ad3f6"
"checksum time 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)" = "d825be0eb33fda1a7e68012d51e9c7f451dc1a69391e7fdc197060bb8c56667b"

View file

@ -9,5 +9,6 @@ clap = "2.32.0"
failure = "0.1.3"
lazy_static = "1.2.0"
size = "0.1.1"
terminal_size = "0.1.8"
time = "0.1.40"
users = "0.8.0"

View file

@ -2,6 +2,7 @@ use formatter::*;
use meta::Meta;
use std::cmp::Ordering;
use std::path::Path;
use terminal_size::terminal_size;
use Options;
pub struct Core<'a> {
@ -17,7 +18,7 @@ impl<'a> Core<'a> {
}
}
pub fn print(&self, inputs: Vec<&str>) {
pub fn run(&self, inputs: Vec<&str>) {
let print_folder_name: bool = inputs.len() > 1;
let mut dirs = Vec::new();
@ -45,7 +46,7 @@ impl<'a> Core<'a> {
dirs.sort_unstable();
if !files.is_empty() {
self.print_long(&files);
self.print(&files);
}
for dir in dirs {
@ -57,7 +58,72 @@ impl<'a> Core<'a> {
if print_folder_name {
println!("\n{}:", dir.display())
}
self.print_long(&folder_metas);
self.print(&folder_metas);
}
}
fn print(&self, metas: &[Meta]) {
if self.options.display_long {
self.print_long(metas)
} else {
self.print_short(metas)
}
}
fn print_short(&self, metas: &[Meta]) {
let width = match terminal_size() {
Some((w, _)) => w.0 as usize,
None => panic!("failed to retrieve terminal size"),
};
let mut max_name_width = 0;
for meta in metas {
if meta.name.len() > max_name_width {
max_name_width = meta.name.len();
}
}
let nb_entry_per_row = width / (max_name_width + 4 + 3);
let mut x = 1;
let mut line = String::new();
for meta in metas {
line += " ";
line += &self.formatter.format_name(&meta);
for _ in 0..(max_name_width - meta.name.len()) {
line.push(' ');
}
x += 1;
if x >= nb_entry_per_row {
x = 1;
println!("{}", line);
line = String::new();
}
}
if !line.is_empty() {
println!("{}", line);
}
}
fn print_long(&self, metas: &[Meta]) {
let max_user_length = self.detect_user_lenght(&metas);
let max_group_length = self.detect_group_lenght(&metas);
let (max_size_value_length, max_size_unit_length) = self.detect_size_lenghts(&metas);
for meta in metas {
println!(
" {} {} {} {} {} {}{}",
self.formatter.format_permissions(&meta),
self.formatter.format_user(&meta.user, max_user_length),
self.formatter.format_group(&meta.group, max_group_length),
self.formatter
.format_size(&meta, max_size_value_length, max_size_unit_length),
self.formatter.format_date(&meta),
self.formatter.format_name(&meta),
self.formatter.format_symlink(&meta),
);
}
}
@ -90,25 +156,6 @@ impl<'a> Core<'a> {
content
}
fn print_long(&self, metas: &[Meta]) {
let max_user_length = self.detect_user_lenght(&metas);
let max_group_length = self.detect_group_lenght(&metas);
let (max_size_value_length, max_size_unit_length) = self.detect_size_lenghts(&metas);
for meta in metas {
println!(
" {} {} {} {} {} {}",
self.formatter.format_permissions(&meta),
self.formatter.format_user(&meta.user, max_user_length),
self.formatter.format_group(&meta.group, max_group_length),
self.formatter
.format_size(&meta, max_size_value_length, max_size_unit_length),
self.formatter.format_date(&meta),
self.formatter.format_name(&meta),
);
}
}
fn detect_user_lenght(&self, paths: &[Meta]) -> usize {
let mut max: usize = 0;

View file

@ -19,7 +19,7 @@ impl Formatter {
let mut content = String::new();
let color = if meta.metadata.is_dir() {
content = content + Logo::folder().as_str() + " " + &meta.name;
content = content + Logo::folder().as_str() + " " + &meta.name;
Colors[&Elem::Dir]
} else {
content = content + Logo::from_pathbuf(&meta.path).as_str() + " " + &meta.name;
@ -28,10 +28,15 @@ impl Formatter {
content = color.paint(content).to_string();
content
}
pub fn format_symlink(&self, meta: &Meta) -> String {
let mut content = String::new();
let color = Colors[&Elem::Link];
if let Some(ref link) = meta.symlink_target {
content =
content + &color.paint(String::from("") + &color.paint(link).to_string());
content += &color.paint(String::from("") + &color.paint(link).to_string());
}
content

View file

@ -4,6 +4,7 @@ extern crate lazy_static;
extern crate ansi_term;
extern crate failure;
extern crate size;
extern crate terminal_size;
extern crate time;
extern crate users;
@ -18,6 +19,7 @@ use core::Core;
pub struct Options {
pub display_all: bool,
pub display_long: bool,
}
fn main() {
@ -25,10 +27,12 @@ fn main() {
.about("A ls comment with a lot of pretty colors and some other stuff.")
.arg(Arg::with_name("FILE").multiple(true).default_value("."))
.arg(Arg::with_name("all").short("a"))
.arg(Arg::with_name("long").short("l"))
.get_matches();
let options = Options {
display_all: matches.is_present("all"),
display_long: matches.is_present("long"),
};
let inputs: Vec<&str> = matches
@ -38,5 +42,5 @@ fn main() {
let core = Core::new(&options);
core.print(inputs);
core.run(inputs);
}