Improve ls indentation with term_grid

This commit is contained in:
Peltoche
2018-11-25 15:56:39 +01:00
parent b130533e73
commit de3a3afeb7
4 changed files with 38 additions and 39 deletions

10
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)",
"term_grid 0.1.7 (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)",
@@ -183,6 +184,14 @@ dependencies = [
"unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "term_grid"
version = "0.1.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"unicode-width 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "terminal_size"
version = "0.1.8"
@@ -285,6 +294,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 term_grid 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)" = "230d3e804faaed5a39b08319efb797783df2fd9671b39b7596490cb486d702cf"
"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"

View File

@@ -5,29 +5,26 @@ description = "A ls command with a lot of pretty colors."
homepage = "https://github.com/Peltoche/lsd"
keywords = ["ls"]
license-file = "./LICENSE"
license = "Apache-2.0"
name = "lsd"
readme = "./README.md"
repository = "https://github.com/Peltoche/lsd"
version = "0.1.0"
[metadata.deb]
assets = [["target/release/lsd", "usr/bin/", "755"], ["README.md", "usr/share/doc/cargo-deb/README", "644"]]
depends = "$auto"
license-file = ["LICENSE", "2"]
maintainer = "Peltoche <dev@halium.fr>"
priority = "optional"
section = "utility"
[dependencies]
ansi_term = "0.11.0"
clap = "2.32.0"
failure = "0.1.3"
lazy_static = "1.2.0"
size = "0.1.1"
term_grid = "0.1.7"
terminal_size = "0.1.8"
time = "0.1.40"
users = "0.8.0"
[package.metadata.deb]
maintainer = "Peltoche <dev@halium.fr>"
license-file = ["LICENSE", "2"]
depends = "$auto"
section = "utility"
priority = "optional"
assets = [
["target/release/lsd", "usr/bin/", "755"],
["README.md", "usr/share/doc/cargo-deb/README", "644"],
]

View File

@@ -2,6 +2,7 @@ use formatter::*;
use meta::Meta;
use std::cmp::Ordering;
use std::path::Path;
use term_grid::{Cell, Direction, Filling, Grid, GridOptions};
use terminal_size::terminal_size;
use Options;
@@ -71,40 +72,30 @@ impl<'a> Core<'a> {
}
fn print_short(&self, metas: &[Meta]) {
let width = match terminal_size() {
let term_width = match terminal_size() {
Some((w, _)) => w.0 as usize,
None => panic!("failed to retrieve terminal size"),
};
let mut max_name_width = 0;
let mut grid = Grid::new(GridOptions {
filling: Filling::Spaces(1),
direction: Direction::LeftToRight,
});
for meta in metas {
if meta.name.len() > max_name_width {
max_name_width = meta.name.len();
}
let mut content = String::from(" ");
content = content + &self.formatter.format_name(&meta);
grid.add(Cell {
width: content.len(),
contents: content,
});
}
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);
}
println!(
"{}",
grid.fit_into_width(term_width * 2)
.expect("failed to print the grid")
);
}
fn print_long(&self, metas: &[Meta]) {

View File

@@ -4,6 +4,7 @@ extern crate lazy_static;
extern crate ansi_term;
extern crate failure;
extern crate size;
extern crate term_grid;
extern crate terminal_size;
extern crate time;
extern crate users;