Batch source formatting

I read through every file and applied a couple of rustfmt suggestions. The brace placement and alignment of items on similar lines has been made consistent, even if neither are rustfmt's default style (a file has been put in place to enforce this). Other changes are:

• Alphabetical imports and modules
• Comma placement at the end of match blocks
• Use newlines and indentation judiciously
• Spaces around associated types
• Spaces after negations (it makes it more clear imho)
• Comment formatting
• Use early-returns and Optional `?` where appropriate
This commit is contained in:
Benjamin Sago 2020-10-10 19:49:46 +01:00
parent c3c39fee0a
commit f8df02dae7
49 changed files with 861 additions and 607 deletions

1
.rustfmt.toml Normal file
View File

@ -0,0 +1 @@
disable_all_formatting = true

View File

@ -11,8 +11,9 @@
/// - https://crates.io/crates/vergen
extern crate datetime;
use std::io::Result as IOResult;
use std::env;
use std::io::Result as IOResult;
fn git_hash() -> String {
use std::process::Command;
@ -52,7 +53,8 @@ fn write_statics() -> IOResult<()> {
let ver = if is_development_version() {
format!("exa v{} ({} built on {})", cargo_version(), git_hash(), build_date())
} else {
}
else {
format!("exa v{}", cargo_version())
};

View File

@ -10,7 +10,7 @@ use log::*;
use crate::fs::File;
/// A **Dir** provides a cached list of the file paths in a directory that's
/// A **Dir** provides a cached list of the file paths in a directory thats
/// being listed.
///
/// This object gets passed to the Files themselves, in order for them to
@ -53,7 +53,8 @@ impl Dir {
dir: self,
dotfiles: dots.shows_dotfiles(),
dots: dots.dots(),
git, git_ignoring,
git,
git_ignoring,
}
}
@ -106,7 +107,9 @@ impl<'dir, 'ig> Files<'dir, 'ig> {
loop {
if let Some(path) = self.inner.next() {
let filename = File::filename(path);
if !self.dotfiles && filename.starts_with('.') { continue }
if ! self.dotfiles && filename.starts_with('.') {
continue;
}
if self.git_ignoring {
let git_status = self.git.map(|g| g.get(path, false)).unwrap_or_default();
@ -139,7 +142,6 @@ enum DotsNext {
Files,
}
impl<'dir, 'ig> Iterator for Files<'dir, 'ig> {
type Item = Result<File<'dir>, (PathBuf, io::Error)>;
@ -149,22 +151,24 @@ impl<'dir, 'ig> Iterator for Files<'dir, 'ig> {
self.dots = DotsNext::DotDot;
Some(File::new_aa_current(self.dir)
.map_err(|e| (Path::new(".").to_path_buf(), e)))
},
}
DotsNext::DotDot => {
self.dots = DotsNext::Files;
Some(File::new_aa_parent(self.parent(), self.dir)
.map_err(|e| (self.parent(), e)))
},
}
DotsNext::Files => {
self.next_visible_file()
},
}
}
}
}
/// Usually files in Unix use a leading dot to be hidden or visible, but two
/// entries in particular are "extra-hidden": `.` and `..`, which only become
/// entries in particular are “extra-hidden”: `.` and `..`, which only become
/// visible after an extra `-a` option.
#[derive(PartialEq, Debug, Copy, Clone)]
pub enum DotFilter {

View File

@ -36,7 +36,9 @@ impl GitCache {
use std::iter::FromIterator;
impl FromIterator<PathBuf> for GitCache {
fn from_iter<I: IntoIterator<Item=PathBuf>>(iter: I) -> Self {
fn from_iter<I>(iter: I) -> Self
where I: IntoIterator<Item=PathBuf>
{
let iter = iter.into_iter();
let mut git = Self {
repos: Vec::with_capacity(iter.size_hint().0),
@ -61,8 +63,10 @@ impl FromIterator<PathBuf> for GitCache {
debug!("Discovered new Git repo");
git.repos.push(r);
},
Err(miss) => git.misses.push(miss),
}
Err(miss) => {
git.misses.push(miss)
}
}
}
}
@ -72,8 +76,6 @@ impl FromIterator<PathBuf> for GitCache {
}
/// A **Git repository** is one weve discovered somewhere on the filesystem.
pub struct GitRepo {
@ -99,7 +101,9 @@ pub struct GitRepo {
enum GitContents {
/// All the interesting Git stuff goes through this.
Before { repo: git2::Repository },
Before {
repo: git2::Repository,
},
/// Temporary value used in `repo_to_statuses` so we can move the
/// repository out of the `Before` variant.
@ -107,7 +111,9 @@ enum GitContents {
/// The data weve extracted from the repository, but only after weve
/// actually done so.
After { statuses: Git }
After {
statuses: Git,
},
}
impl GitRepo {
@ -116,7 +122,7 @@ impl GitRepo {
/// depending on the prefix-lookup flag) and returns its Git status.
///
/// Actually querying the `git2` repository for the mapping of paths to
/// Git statuses is only done once, and gets cached so we don't need to
/// Git statuses is only done once, and gets cached so we dont need to
/// re-query the entire repository the times after that.
///
/// The temporary `Processing` enum variant is used after the `git2`
@ -166,7 +172,7 @@ impl GitRepo {
let workdir = workdir.to_path_buf();
let contents = Mutex::new(GitContents::Before { repo });
Ok(Self { contents, workdir, original_path: path, extra_paths: Vec::new() })
},
}
None => {
warn!("Repository has no workdir?");
Err(path)
@ -205,8 +211,10 @@ fn repo_to_statuses(repo: &git2::Repository, workdir: &Path) -> Git {
let elem = (path, e.status());
statuses.push(elem);
}
},
Err(e) => error!("Error looking up Git statuses: {:?}", e),
}
Err(e) => {
error!("Error looking up Git statuses: {:?}", e)
}
}
Git { statuses }
@ -217,7 +225,7 @@ fn repo_to_statuses(repo: &git2::Repository, workdir: &Path) -> Git {
// 20.311276 INFO:exa::fs::feature::git: Getting Git statuses for repo with workdir "/vagrant/"
// 20.799610 DEBUG:exa::output::table: Getting Git status for file "./Cargo.toml"
//
// Even inserting another logging line immediately afterwards doesn't make it
// Even inserting another logging line immediately afterwards doesnt make it
// look any faster.
@ -239,6 +247,7 @@ impl Git {
/// Get the status for the file at the given path.
fn file_status(&self, file: &Path) -> f::Git {
let path = reorient(file);
self.statuses.iter()
.find(|p| p.0.as_path() == path)
.map(|&(_, s)| f::Git { staged: index_status(s), unstaged: working_tree_status(s) })
@ -250,25 +259,31 @@ impl Git {
/// directories, which dont really have an official status.
fn dir_status(&self, dir: &Path) -> f::Git {
let path = reorient(dir);
let s = self.statuses.iter()
.filter(|p| p.0.starts_with(&path))
.fold(git2::Status::empty(), |a, b| a | b.1);
f::Git { staged: index_status(s), unstaged: working_tree_status(s) }
let staged = index_status(s);
let unstaged = working_tree_status(s);
f::Git { staged, unstaged }
}
}
/// Converts a path to an absolute path based on the current directory.
/// Paths need to be absolute for them to be compared properly, otherwise
/// youd ask a repo about “./README.md” but it only knows about
/// “/vagrant/README.md”, prefixed by the workdir.
fn reorient(path: &Path) -> PathBuf {
use std::env::current_dir;
// Im not 100% on this func tbh
// TODO: Im not 100% on this func tbh
let path = match current_dir() {
Err(_) => Path::new(".").join(&path),
Ok(dir) => dir.join(&path),
};
path.canonicalize().unwrap_or(path)
}

View File

@ -1,6 +1,7 @@
pub mod xattr;
#[cfg(feature="git")] pub mod git;
#[cfg(feature = "git")]
pub mod git;
#[cfg(not(feature = "git"))]
pub mod git {
@ -13,8 +14,10 @@ pub mod git {
pub struct GitCache;
impl FromIterator<PathBuf> for GitCache {
fn from_iter<I: IntoIterator<Item=PathBuf>>(_iter: I) -> Self {
GitCache
fn from_iter<I>(_iter: I) -> Self
where I: IntoIterator<Item=PathBuf>
{
Self
}
}

View File

@ -1,4 +1,5 @@
//! Extended attribute support for Darwin and Linux systems.
#![allow(trivial_casts)] // for ARM
extern crate libc;
@ -6,7 +7,11 @@ use std::cmp::Ordering;
use std::io;
use std::path::Path;
pub const ENABLED: bool = cfg!(feature="git") && cfg!(any(target_os="macos", target_os="linux"));
pub const ENABLED: bool =
cfg!(feature="git") &&
cfg!(any(target_os = "macos", target_os = "linux"));
pub trait FileAttributes {
fn attributes(&self) -> io::Result<Vec<Attribute>>;
@ -27,20 +32,21 @@ impl FileAttributes for Path {
#[cfg(not(any(target_os = "macos", target_os = "linux")))]
impl FileAttributes for Path {
fn attributes(&self) -> io::Result<Vec<Attribute>> {
Ok(vec![])
Ok(Vec::new())
}
fn symlink_attributes(&self) -> io::Result<Vec<Attribute>> {
Ok(vec![])
Ok(Vec::new())
}
}
/// Attributes which can be passed to `Attribute::list_with_flags`
#[cfg(any(target_os = "macos", target_os = "linux"))]
#[derive(Copy, Clone)]
pub enum FollowSymlinks {
Yes,
No
No,
}
/// Extended attribute
@ -50,13 +56,16 @@ pub struct Attribute {
pub size: usize,
}
#[cfg(any(target_os = "macos", target_os = "linux"))]
pub fn list_attrs(lister: &lister::Lister, path: &Path) -> io::Result<Vec<Attribute>> {
use std::ffi::CString;
let c_path = match path.to_str().and_then(|s| { CString::new(s).ok() }) {
let c_path = match path.to_str().and_then(|s| CString::new(s).ok()) {
Some(cstring) => cstring,
None => return Err(io::Error::new(io::ErrorKind::Other, "Error: path somehow contained a NUL?")),
None => {
return Err(io::Error::new(io::ErrorKind::Other, "Error: path somehow contained a NUL?"));
}
};
let bufsize = lister.listxattr_first(&c_path);
@ -91,33 +100,40 @@ pub fn list_attrs(lister: &lister::Lister, path: &Path) -> io::Result<Vec<Attrib
if size > 0 {
names.push(Attribute {
name: lister.translate_attribute_name(&buf[start..end]),
size: size as usize
size: size as usize,
});
}
start = c_end;
}
}
Ok(names)
}
#[cfg(target_os = "macos")]
mod lister {
use std::ffi::CString;
use libc::{c_int, size_t, ssize_t, c_char, c_void};
use super::FollowSymlinks;
use libc::{c_int, size_t, ssize_t, c_char, c_void};
use std::ffi::CString;
use std::ptr;
extern "C" {
fn listxattr(
path: *const c_char, namebuf: *mut c_char,
size: size_t, options: c_int
path: *const c_char,
namebuf: *mut c_char,
size: size_t,
options: c_int,
) -> ssize_t;
fn getxattr(
path: *const c_char, name: *const c_char,
value: *mut c_void, size: size_t, position: u32,
options: c_int
path: *const c_char,
name: *const c_char,
value: *mut c_void,
size: size_t,
position: u32,
options: c_int,
) -> ssize_t;
}
@ -136,16 +152,17 @@ mod lister {
}
pub fn translate_attribute_name(&self, input: &[u8]) -> String {
use std::str::from_utf8_unchecked;
unsafe {
from_utf8_unchecked(input).into()
}
unsafe { std::str::from_utf8_unchecked(input).into() }
}
pub fn listxattr_first(&self, c_path: &CString) -> ssize_t {
unsafe {
listxattr(c_path.as_ptr(), ptr::null_mut(), 0, self.c_flags)
listxattr(
c_path.as_ptr(),
ptr::null_mut(),
0,
self.c_flags,
)
}
}
@ -154,7 +171,8 @@ mod lister {
listxattr(
c_path.as_ptr(),
buf.as_mut_ptr() as *mut c_char,
bufsize as size_t, self.c_flags
bufsize as size_t,
self.c_flags,
)
}
}
@ -164,13 +182,17 @@ mod lister {
getxattr(
c_path.as_ptr(),
buf.as_ptr() as *const c_char,
ptr::null_mut(), 0, 0, self.c_flags
ptr::null_mut(),
0,
0,
self.c_flags,
)
}
}
}
}
#[cfg(target_os = "linux")]
mod lister {
use std::ffi::CString;
@ -180,21 +202,29 @@ mod lister {
extern "C" {
fn listxattr(
path: *const c_char, list: *mut c_char, size: size_t
path: *const c_char,
list: *mut c_char,
size: size_t,
) -> ssize_t;
fn llistxattr(
path: *const c_char, list: *mut c_char, size: size_t
path: *const c_char,
list: *mut c_char,
size: size_t,
) -> ssize_t;
fn getxattr(
path: *const c_char, name: *const c_char,
value: *mut c_void, size: size_t
path: *const c_char,
name: *const c_char,
value: *mut c_void,
size: size_t,
) -> ssize_t;
fn lgetxattr(
path: *const c_char, name: *const c_char,
value: *mut c_void, size: size_t
path: *const c_char,
name: *const c_char,
value: *mut c_void,
size: size_t,
) -> ssize_t;
}
@ -218,7 +248,11 @@ mod lister {
};
unsafe {
listxattr(c_path.as_ptr() as *const _, ptr::null_mut(), 0)
listxattr(
c_path.as_ptr() as *const _,
ptr::null_mut(),
0,
)
}
}
@ -232,7 +266,7 @@ mod lister {
listxattr(
c_path.as_ptr() as *const _,
buf.as_mut_ptr() as *mut c_char,
bufsize as size_t
bufsize as size_t,
)
}
}
@ -247,7 +281,8 @@ mod lister {
getxattr(
c_path.as_ptr() as *const _,
buf.as_ptr() as *const c_char,
ptr::null_mut(), 0
ptr::null_mut(),
0,
)
}
}

View File

@ -1,4 +1,3 @@
//! Wrapper types for the values returned from `File`s.
//!
//! The methods of `File` that return information about the entry on the
@ -45,7 +44,14 @@ pub type uid_t = u32;
/// Its ordering is used when sorting by type.
#[derive(PartialEq, Eq, PartialOrd, Ord, Copy, Clone)]
pub enum Type {
Directory, File, Link, Pipe, Socket, CharDevice, BlockDevice, Special,
Directory,
File,
Link,
Pipe,
Socket,
CharDevice,
BlockDevice,
Special,
}
impl Type {
@ -152,7 +158,7 @@ pub enum Size {
/// have a file size. For example, a directory will just contain a list of
/// its files as its “contents” and will be specially flagged as being a
/// directory, rather than a file. However, seeing the “file size” of this
/// data is rarely useful -- I cant think of a time when Ive seen it and
/// data is rarely useful I cant think of a time when Ive seen it and
/// learnt something. So we discard it and just output “-” instead.
///
/// See this answer for more: http://unix.stackexchange.com/a/68266
@ -214,10 +220,11 @@ pub enum GitStatus {
/// A file thats ignored (that matches a line in .gitignore)
Ignored,
/// A file that's updated but unmerged.
/// A file thats updated but unmerged.
Conflicted,
}
/// A files complete Git status. Its possible to make changes to a file, add
/// it to the staging area, then make *more* changes, so we need to list each
/// files status for both of these.
@ -227,11 +234,13 @@ pub struct Git {
pub unstaged: GitStatus,
}
use std::default::Default;
impl Default for Git {
/// Create a Git status for a file with nothing done to it.
fn default() -> Self {
Self { staged: GitStatus::NotModified, unstaged: GitStatus::NotModified }
Self {
staged: GitStatus::NotModified,
unstaged: GitStatus::NotModified,
}
}
}

View File

@ -2,7 +2,7 @@
use std::io::Error as IOError;
use std::io::Result as IOResult;
use std::os::unix::fs::{MetadataExt, PermissionsExt, FileTypeExt};
use std::os::unix::fs::{FileTypeExt, MetadataExt, PermissionsExt};
use std::path::{Path, PathBuf};
use std::time::{Duration, SystemTime, UNIX_EPOCH};
@ -11,7 +11,8 @@ use log::*;
use crate::fs::dir::Dir;
use crate::fs::fields as f;
/// A **File** is a wrapper around one of Rust's Path objects, along with
/// A **File** is a wrapper around one of Rusts `PathBuf` values, along with
/// associated data about the file.
///
/// Each file is definitely going to have its filename displayed at least
@ -44,7 +45,7 @@ pub struct File<'dir> {
///
/// This too is queried multiple times, and is *not* cached by the OS, as
/// it could easily change between invocations — but exa is so short-lived
/// it's better to just cache it.
/// its better to just cache it.
pub metadata: std::fs::Metadata,
/// A reference to the directory that contains this file, if any.
@ -60,7 +61,7 @@ pub struct File<'dir> {
/// Whether this is one of the two `--all all` directories, `.` and `..`.
///
/// Unlike all other entries, these are not returned as part of the
/// directory's children, and are in fact added specifically by exa; this
/// directorys children, and are in fact added specifically by exa; this
/// means that they should be skipped when recursing.
pub is_all_all: bool,
}
@ -88,8 +89,9 @@ impl<'dir> File<'dir> {
debug!("Statting file {:?}", &path);
let metadata = std::fs::symlink_metadata(&path)?;
let is_all_all = true;
let parent_dir = Some(parent_dir);
Ok(File { path, parent_dir: Some(parent_dir), metadata, ext, name: ".".to_string(), is_all_all })
Ok(File { path, parent_dir, metadata, ext, name: ".".into(), is_all_all })
}
pub fn new_aa_parent(path: PathBuf, parent_dir: &'dir Dir) -> IOResult<File<'dir>> {
@ -98,8 +100,9 @@ impl<'dir> File<'dir> {
debug!("Statting file {:?}", &path);
let metadata = std::fs::symlink_metadata(&path)?;
let is_all_all = true;
let parent_dir = Some(parent_dir);
Ok(File { path, parent_dir: Some(parent_dir), metadata, ext, name: "..".to_string(), is_all_all })
Ok(File { path, parent_dir, metadata, ext, name: "..".into(), is_all_all })
}
/// A files name is derived from its string. This needs to handle directories
@ -127,7 +130,9 @@ impl<'dir> File<'dir> {
fn ext(path: &Path) -> Option<String> {
let name = path.file_name().map(|f| f.to_string_lossy().to_string())?;
name.rfind('.').map(|p| name[p+1..].to_ascii_lowercase())
name.rfind('.')
.map(|p| name[p + 1 ..]
.to_ascii_lowercase())
}
/// Whether this file is a directory on the filesystem.
@ -249,7 +254,8 @@ impl<'dir> File<'dir> {
Ok(metadata) => {
let ext = File::ext(&path);
let name = File::filename(&path);
FileTarget::Ok(Box::new(File { parent_dir: None, path, ext, metadata, name, is_all_all: false }))
let file = File { parent_dir: None, path, ext, metadata, name, is_all_all: false };
FileTarget::Ok(Box::new(file))
}
Err(e) => {
error!("Error following link {:?}: {:#?}", &path, e);
@ -274,14 +280,14 @@ impl<'dir> File<'dir> {
}
}
/// This file's inode.
/// This files inode.
pub fn inode(&self) -> f::Inode {
f::Inode(self.metadata.ino())
}
/// This file's number of filesystem blocks.
/// This files number of filesystem blocks.
///
/// (Not the size of each block, which we don't actually report on)
/// (Not the size of each block, which we dont actually report on)
pub fn blocks(&self) -> f::Blocks {
if self.is_file() || self.is_link() {
f::Blocks::Some(self.metadata.blocks())
@ -334,17 +340,19 @@ impl<'dir> File<'dir> {
pub fn changed_time(&self) -> Option<SystemTime> {
let (mut sec, mut nsec) = (self.metadata.ctime(), self.metadata.ctime_nsec());
Some(
if sec < 0 {
if nsec > 0 {
sec += 1;
nsec -= 1_000_000_000;
}
UNIX_EPOCH - Duration::new(sec.abs() as u64, nsec.abs() as u32)
} else {
UNIX_EPOCH + Duration::new(sec as u64, nsec as u32)
let duration = Duration::new(sec.abs() as u64, nsec.abs() as u32);
Some(UNIX_EPOCH - duration)
}
else {
let duration = Duration::new(sec as u64, nsec as u32);
Some(UNIX_EPOCH + duration)
}
)
}
/// This files last accessed timestamp, if available on this platform.
@ -392,7 +400,7 @@ impl<'dir> File<'dir> {
/// This files permissions, with flags for each bit.
pub fn permissions(&self) -> f::Permissions {
let bits = self.metadata.mode();
let has_bit = |bit| { bits & bit == bit };
let has_bit = |bit| bits & bit == bit;
f::Permissions {
user_read: has_bit(modes::USER_READ),
@ -423,7 +431,7 @@ impl<'dir> File<'dir> {
}
}
/// Whether this file's name, including extension, is any of the strings
/// Whether this files name, including extension, is any of the strings
/// that get passed in.
pub fn name_is_one_of(&self, choices: &[&str]) -> bool {
choices.contains(&&self.name[..])
@ -455,7 +463,7 @@ pub enum FileTarget<'dir> {
// Err is its own variant, instead of having the whole thing be inside an
// `IOResult`, because being unable to follow a symlink is not a serious
// error -- we just display the error message and move on.
// error we just display the error message and move on.
}
impl<'dir> FileTarget<'dir> {
@ -471,9 +479,10 @@ impl<'dir> FileTarget<'dir> {
/// More readable aliases for the permission bits exposed by libc.
#[allow(trivial_numeric_casts)]
mod modes {
pub type Mode = u32;
// The `libc::mode_t` types actual type varies, but the value returned
// from `metadata.permissions().mode()` is always `u32`.
pub type Mode = u32;
pub const USER_READ: Mode = libc::S_IRUSR as Mode;
pub const USER_WRITE: Mode = libc::S_IWUSR as Mode;

View File

@ -5,8 +5,8 @@ use std::iter::FromIterator;
use std::os::unix::fs::MetadataExt;
use std::path::Path;
use crate::fs::File;
use crate::fs::DotFilter;
use crate::fs::File;
/// The **file filter** processes a list of files before displaying them to
@ -88,7 +88,6 @@ pub struct FileFilter {
pub git_ignore: GitIgnore,
}
impl FileFilter {
/// Remove every file in the given vector that does *not* pass the
/// filter predicate for files found inside a directory.
@ -110,14 +109,18 @@ impl FileFilter {
/// `exa -I='*.ogg' music/*` should filter out the ogg files obtained
/// from the glob, even though the globbing is done by the shell!
pub fn filter_argument_files(&self, files: &mut Vec<File>) {
files.retain(|f| !self.ignore_patterns.is_ignored(&f.name));
files.retain(|f| {
! self.ignore_patterns.is_ignored(&f.name)
});
}
/// Sort the files in the given vector based on the sort field option.
pub fn sort_files<'a, F>(&self, files: &mut Vec<F>)
where F: AsRef<File<'a>> {
files.sort_by(|a, b| self.sort_field.compare_files(a.as_ref(), b.as_ref()));
where F: AsRef<File<'a>>
{
files.sort_by(|a, b| {
self.sort_field.compare_files(a.as_ref(), b.as_ref())
});
if self.reverse {
files.reverse();
@ -126,7 +129,8 @@ impl FileFilter {
if self.list_dirs_first {
// This relies on the fact that `sort_by` is *stable*: it will keep
// adjacent elements next to each other.
files.sort_by(|a, b| {b.as_ref().points_to_directory()
files.sort_by(|a, b| {
b.as_ref().points_to_directory()
.cmp(&a.as_ref().points_to_directory())
});
}
@ -175,13 +179,13 @@ pub enum SortField {
/// The time the file was changed (the “ctime”).
///
/// This field is used to mark the time when a files metadata
/// changed -- its permissions, owners, or link count.
/// changed its permissions, owners, or link count.
///
/// In original Unix, this was, however, meant as creation time.
/// https://www.bell-labs.com/usr/dmr/www/cacm.html
ChangedDate,
/// The time the file was created (the "btime" or "birthtime").
/// The time the file was created (the “btime” or “birthtime”).
CreatedDate,
/// The type of the file: directories, links, pipes, regular, files, etc.
@ -280,11 +284,8 @@ impl SortField {
}
fn strip_dot(n: &str) -> &str {
if n.starts_with('.') {
&n[1..]
} else {
n
}
if n.starts_with('.') { &n[1..] }
else { n }
}
}
@ -298,8 +299,12 @@ pub struct IgnorePatterns {
}
impl FromIterator<glob::Pattern> for IgnorePatterns {
fn from_iter<I: IntoIterator<Item = glob::Pattern>>(iter: I) -> Self {
Self { patterns: iter.into_iter().collect() }
fn from_iter<I>(iter: I) -> Self
where I: IntoIterator<Item = glob::Pattern>
{
let patterns = iter.into_iter().collect();
Self { patterns }
}
}
@ -371,7 +376,6 @@ pub enum GitIgnore {
// > usually found in $XDG_CONFIG_HOME/git/ignore.
#[cfg(test)]
mod test_ignores {
use super::*;

View File

@ -4,7 +4,7 @@ pub use self::dir::{Dir, DotFilter};
mod file;
pub use self::file::{File, FileTarget};
pub mod dir_action;
pub mod feature;
pub mod fields;
pub mod filter;
pub mod dir_action;

View File

@ -124,11 +124,17 @@ impl FileIcon for FileExtensions {
fn icon_file(&self, file: &File) -> Option<char> {
use crate::output::icons::Icons;
Some(match file {
f if self.is_music(f) || self.is_lossless(f) => Icons::Audio.value(),
f if self.is_image(f) => Icons::Image.value(),
f if self.is_video(f) => Icons::Video.value(),
_ => return None,
})
if self.is_music(file) || self.is_lossless(file) {
Some(Icons::Audio.value())
}
else if self.is_image(file) {
Some(Icons::Image.value())
}
else if self.is_video(file) {
Some(Icons::Video.value())
}
else {
None
}
}
}

View File

@ -11,7 +11,7 @@ impl<'a> File<'a> {
/// The point of this is to highlight compiled files such as `foo.js` when
/// their source file `foo.coffee` exists in the same directory.
/// For example, `foo.js` is perfectly valid without `foo.coffee`, so we
/// don't want to always blindly highlight `*.js` as compiled.
/// dont want to always blindly highlight `*.js` as compiled.
/// (See also `FileExtensions#is_compiled`)
pub fn get_source_files(&self) -> Vec<PathBuf> {
if let Some(ext) = &self.ext {
@ -34,7 +34,7 @@ impl<'a> File<'a> {
}
}
else {
vec![] // No source files if there's no extension, either!
vec![] // No source files if theres no extension, either!
}
}
}

View File

@ -11,11 +11,10 @@ use ansi_term::{ANSIStrings, Style};
use log::*;
use crate::fs::{Dir, File};
use crate::fs::filter::GitIgnore;
use crate::fs::feature::git::GitCache;
use crate::fs::filter::GitIgnore;
use crate::options::{Options, Vars};
pub use crate::options::vars;
pub use crate::options::Misfire;
pub use crate::options::{Misfire, vars};
use crate::output::{escape, lines, grid, grid_details, details, View, Mode};
mod fs;
@ -35,18 +34,24 @@ fn main() {
match Exa::from_args(args.iter(), stdout()) {
Ok(mut exa) => {
match exa.run() {
Ok(exit_status) => exit(exit_status),
Ok(exit_status) => {
exit(exit_status)
}
Err(e) => {
match e.kind() {
ErrorKind::BrokenPipe => exit(exits::SUCCESS),
ErrorKind::BrokenPipe => {
exit(exits::SUCCESS);
}
_ => {
eprintln!("{}", e);
exit(exits::RUNTIME_ERROR);
},
}
};
}
};
},
}
Err(ref e) if e.is_error() => {
let mut stderr = stderr();
@ -57,13 +62,13 @@ fn main() {
}
exit(exits::OPTIONS_ERROR);
},
}
Err(ref e) => {
println!("{}", e);
exit(exits::SUCCESS);
},
};
}
}
}
@ -136,7 +141,8 @@ impl<'args> Exa<'args> {
Err(e) => {
exit_status = 2;
writeln!(stderr(), "{:?}: {}", file_path, e)?;
},
}
Ok(f) => {
if f.points_to_directory() && ! self.options.dir_action.treat_dirs_as_files() {
match f.to_dir() {
@ -147,7 +153,7 @@ impl<'args> Exa<'args> {
else {
files.push(f);
}
},
}
}
}
@ -225,7 +231,10 @@ impl<'args> Exa<'args> {
/// For various annoying logistical reasons, each one handles
/// printing differently...
fn print_files(&mut self, dir: Option<&Dir>, files: Vec<File>) -> IOResult<()> {
if !files.is_empty() {
if files.is_empty() {
return Ok(());
}
let View { ref mode, ref colours, ref style } = self.options.view;
match mode {
@ -260,10 +269,6 @@ impl<'args> Exa<'args> {
}
}
}
else {
Ok(())
}
}
}

View File

@ -42,29 +42,64 @@ impl SortField {
};
let field = match word {
"name" | "filename" => Self::Name(SortCase::AaBbCc),
"Name" | "Filename" => Self::Name(SortCase::ABCabc),
".name" | ".filename" => Self::NameMixHidden(SortCase::AaBbCc),
".Name" | ".Filename" => Self::NameMixHidden(SortCase::ABCabc),
"size" | "filesize" => Self::Size,
"ext" | "extension" => Self::Extension(SortCase::AaBbCc),
"Ext" | "Extension" => Self::Extension(SortCase::ABCabc),
"name" | "filename" => {
Self::Name(SortCase::AaBbCc)
}
"Name" | "Filename" => {
Self::Name(SortCase::ABCabc)
}
".name" | ".filename" => {
Self::NameMixHidden(SortCase::AaBbCc)
}
".Name" | ".Filename" => {
Self::NameMixHidden(SortCase::ABCabc)
}
"size" | "filesize" => {
Self::Size
}
"ext" | "extension" => {
Self::Extension(SortCase::AaBbCc)
}
"Ext" | "Extension" => {
Self::Extension(SortCase::ABCabc)
}
// “new” sorts oldest at the top and newest at the bottom; “old”
// sorts newest at the top and oldest at the bottom. I think this
// is the right way round to do this: “size” puts the smallest at
// the top and the largest at the bottom, doesnt it?
"date" | "time" | "mod" | "modified" | "new" | "newest" => Self::ModifiedDate,
"date" | "time" | "mod" | "modified" | "new" | "newest" => {
Self::ModifiedDate
}
// Similarly, “age” means that files with the least age (the
// newest files) get sorted at the top, and files with the most
// age (the oldest) at the bottom.
"age" | "old" | "oldest" => Self::ModifiedAge,
"ch" | "changed" => Self::ChangedDate,
"acc" | "accessed" => Self::AccessedDate,
"cr" | "created" => Self::CreatedDate,
"inode" => Self::FileInode,
"type" => Self::FileType,
"none" => Self::Unsorted,
_ => return Err(Misfire::BadArgument(&flags::SORT, word.into()))
"age" | "old" | "oldest" => {
Self::ModifiedAge
}
"ch" | "changed" => {
Self::ChangedDate
}
"acc" | "accessed" => {
Self::AccessedDate
}
"cr" | "created" => {
Self::CreatedDate
}
"inode" => {
Self::FileInode
}
"type" => {
Self::FileType
}
"none" => {
Self::Unsorted
}
_ => {
return Err(Misfire::BadArgument(&flags::SORT, word.into()));
}
};
Ok(field)
@ -103,7 +138,6 @@ impl SortField {
// “apps” first, then “Documents”.
//
// You can get the old behaviour back by sorting with `--sort=Name`.
impl Default for SortField {
fn default() -> Self {
Self::Name(SortCase::AaBbCc)
@ -151,8 +185,8 @@ impl IgnorePatterns {
// If there are no inputs, we return a set of patterns that doesnt
// match anything, rather than, say, `None`.
let inputs = match matches.get(&flags::IGNORE_GLOB)? {
None => return Ok(Self::empty()),
Some(is) => is,
None => return Ok(Self::empty()),
};
// Awkwardly, though, a glob pattern can be invalid, and we need to
@ -171,11 +205,14 @@ impl IgnorePatterns {
impl GitIgnore {
pub fn deduce(matches: &MatchedFlags) -> Result<Self, Misfire> {
Ok(if matches.has(&flags::GIT_IGNORE)? { Self::CheckAndIgnore }
else { Self::Off })
if matches.has(&flags::GIT_IGNORE)? {
Ok(Self::CheckAndIgnore)
}
else {
Ok(Self::Off)
}
}
}
#[cfg(test)]

View File

@ -1,4 +1,4 @@
use crate::options::parser::{Arg, Args, Values, TakesValue};
use crate::options::parser::{Arg, Args, TakesValue, Values};
// exa options

View File

@ -1,8 +1,8 @@
use std::fmt;
use crate::fs::feature::xattr;
use crate::options::flags;
use crate::options::parser::MatchedFlags;
use crate::fs::feature::xattr;
static OPTIONS: &str = r##"
@ -127,7 +127,6 @@ impl fmt::Display for HelpString {
}
#[cfg(test)]
mod test {
use crate::options::Options;

View File

@ -6,7 +6,7 @@ use crate::options::{flags, HelpString, VersionString};
use crate::options::parser::{Arg, Flag, ParseError};
/// A **misfire** is a thing that can happen instead of listing files -- a
/// A **misfire** is a thing that can happen instead of listing files a
/// catch-all for anything outside the programs normal execution.
#[derive(PartialEq, Debug)]
pub enum Misfire {
@ -34,7 +34,7 @@ pub enum Misfire {
Conflict(&'static Arg, &'static Arg),
/// An option was given that does nothing when another one either is or
/// isn't present.
/// isnt present.
Useless(&'static Arg, bool, &'static Arg),
/// An option was given that does nothing when either of two other options

View File

@ -60,7 +60,7 @@
//!
//! `--sort=size` should override `--sort=Name` because its closer to the end
//! of the arguments array. In fact, because theres no way to tell where the
//! arguments came from -- its just a heuristic -- this will still work even
//! arguments came from — its just a heuristic — this will still work even
//! if no aliases are being used!
//!
//! Finally, this isnt just useful when options could override each other.
@ -75,9 +75,10 @@ use crate::fs::dir_action::DirAction;
use crate::fs::filter::{FileFilter, GitIgnore};
use crate::output::{View, Mode, details, grid_details};
mod style;
mod dir_action;
mod filter;
mod flags;
mod style;
mod view;
mod help;
@ -93,7 +94,6 @@ pub mod vars;
pub use self::vars::Vars;
mod parser;
mod flags;
use self::parser::MatchedFlags;
@ -121,7 +121,8 @@ impl Options {
#[allow(unused_results)]
pub fn parse<'args, I, V>(args: I, vars: &V) -> Result<(Self, Vec<&'args OsStr>), Misfire>
where I: IntoIterator<Item = &'args OsString>,
V: Vars {
V: Vars
{
use crate::options::parser::{Matches, Strictness};
let strictness = match vars.get(vars::EXA_STRICT) {
@ -169,7 +170,6 @@ impl Options {
}
#[cfg(test)]
pub mod test {
use super::{Options, Misfire, flags};

View File

@ -144,7 +144,8 @@ impl Args {
/// Iterates over the given list of command-line arguments and parses
/// them into a list of matched flags and free strings.
pub fn parse<'args, I>(&self, inputs: I, strictness: Strictness) -> Result<Matches<'args>, ParseError>
where I: IntoIterator<Item=&'args OsString> {
where I: IntoIterator<Item = &'args OsString>
{
use std::os::unix::ffi::OsStrExt;
let mut parsing = true;
@ -348,7 +349,7 @@ pub struct Matches<'args> {
pub flags: MatchedFlags<'args>,
/// All the strings that werent matched as arguments, as well as anything
/// after the special "--" string.
/// after the special “--” string.
pub frees: Vec<&'args OsStr>,
}
@ -373,7 +374,8 @@ impl<'a> MatchedFlags<'a> {
/// Returns `true` if it was, `false` if it wasnt, and an error in
/// strict mode if it was specified more than once.
pub fn has(&self, arg: &'static Arg) -> Result<bool, Misfire> {
self.has_where(|flag| flag.matches(arg)).map(|flag| flag.is_some())
self.has_where(|flag| flag.matches(arg))
.map(|flag| flag.is_some())
}
/// Returns the first found argument that satisfies the predicate, or
@ -569,7 +571,9 @@ mod parse_test {
let strictness = Strictness::UseLastArguments; // this isnt even used
let got = Args(TEST_ARGS).parse(inputs.iter(), strictness);
let expected = Ok(Matches { frees, flags: MatchedFlags { flags, strictness } });
let flags = MatchedFlags { flags, strictness };
let expected = Ok(Matches { frees, flags });
assert_eq!(got, expected);
}
};
@ -580,9 +584,11 @@ mod parse_test {
use self::ParseError::*;
let strictness = Strictness::UseLastArguments; // this isnt even used
let bits = $inputs.as_ref().into_iter().map(|&o| os(o)).collect::<Vec<OsString>>();
let got = Args(TEST_ARGS).parse(bits.iter(), strictness);
let bits = $inputs.as_ref().into_iter()
.map(|&o| os(o))
.collect::<Vec<_>>();
let got = Args(TEST_ARGS).parse(bits.iter(), strictness);
assert_eq!(got, Err($error));
}
};
@ -719,6 +725,6 @@ mod matches_test {
fn no_count() {
let flags = MatchedFlags { flags: Vec::new(), strictness: Strictness::UseLastArguments };
assert!(!flags.has(&COUNT).unwrap());
assert_eq!(flags.has(&COUNT).unwrap(), false);
}
}

View File

@ -3,7 +3,7 @@ use ansi_term::Style;
use crate::fs::File;
use crate::options::{flags, Vars, Misfire};
use crate::options::parser::MatchedFlags;
use crate::output::file_name::{FileStyle, Classify};
use crate::output::file_name::{Classify, FileStyle};
use crate::style::Colours;
@ -38,7 +38,6 @@ impl TerminalColours {
/// Determine which terminal colour conditions to use.
fn deduce(matches: &MatchedFlags) -> Result<Self, Misfire> {
let word = match matches.get_where(|f| f.matches(&flags::COLOR) || f.matches(&flags::COLOUR))? {
Some(w) => w,
None => return Ok(Self::default()),
@ -77,7 +76,7 @@ pub struct Styles {
impl Styles {
#[allow(trivial_casts)] // the "as Box<_>" stuff below warns about this for some reason
#[allow(trivial_casts)] // the `as Box<_>` stuff below warns about this for some reason
pub fn deduce<V, TW>(matches: &MatchedFlags, vars: &V, widther: TW) -> Result<Self, Misfire>
where TW: Fn() -> Option<usize>, V: Vars {
use crate::info::filetype::FileExtensions;
@ -89,12 +88,12 @@ impl Styles {
// custom colours at all
let tc = TerminalColours::deduce(matches)?;
if tc == TerminalColours::Never
|| (tc == TerminalColours::Automatic && widther().is_none())
{
if tc == TerminalColours::Never || (tc == TerminalColours::Automatic && widther().is_none()) {
let exts = Box::new(NoFileColours);
return Ok(Self {
colours: Colours::plain(),
style: FileStyle { classify, exts: Box::new(NoFileColours) },
style: FileStyle { classify, exts },
});
}
@ -133,11 +132,16 @@ fn parse_color_vars<V: Vars>(vars: &V, colours: &mut Colours) -> (ExtensionMappi
if let Some(lsc) = vars.get(vars::LS_COLORS) {
let lsc = lsc.to_string_lossy();
LSColors(lsc.as_ref()).each_pair(|pair| {
if ! colours.set_ls(&pair) {
match glob::Pattern::new(pair.key) {
Ok(pat) => exts.add(pat, pair.to_style()),
Err(e) => warn!("Couldn't parse glob pattern {:?}: {}", pair.key, e),
Ok(pat) => {
exts.add(pat, pair.to_style());
}
Err(e) => {
warn!("Couldn't parse glob pattern {:?}: {}", pair.key, e);
}
}
}
});
@ -156,8 +160,12 @@ fn parse_color_vars<V: Vars>(vars: &V, colours: &mut Colours) -> (ExtensionMappi
LSColors(exa.as_ref()).each_pair(|pair| {
if ! colours.set_ls(&pair) && ! colours.set_exa(&pair) {
match glob::Pattern::new(pair.key) {
Ok(pat) => exts.add(pat, pair.to_style()),
Err(e) => warn!("Couldn't parse glob pattern {:?}: {}", pair.key, e),
Ok(pat) => {
exts.add(pat, pair.to_style());
}
Err(e) => {
warn!("Couldn't parse glob pattern {:?}: {}", pair.key, e);
}
}
};
});
@ -169,7 +177,7 @@ fn parse_color_vars<V: Vars>(vars: &V, colours: &mut Colours) -> (ExtensionMappi
#[derive(PartialEq, Debug, Default)]
struct ExtensionMappings {
mappings: Vec<(glob::Pattern, Style)>
mappings: Vec<(glob::Pattern, Style)>,
}
// Loop through backwards so that colours specified later in the list override
@ -178,9 +186,7 @@ struct ExtensionMappings {
use crate::output::file_name::FileColours;
impl FileColours for ExtensionMappings {
fn colour_file(&self, file: &File) -> Option<Style> {
self.mappings
.iter()
.rev()
self.mappings.iter().rev()
.find(|t| t.0.matches(&file.name))
.map (|t| t.1)
}
@ -197,18 +203,16 @@ impl ExtensionMappings {
}
impl Classify {
fn deduce(matches: &MatchedFlags) -> Result<Self, Misfire> {
let flagged = matches.has(&flags::CLASSIFY)?;
Ok(if flagged { Self::AddFileIndicators }
else { Self::JustFilenames })
if flagged { Ok(Self::AddFileIndicators) }
else { Ok(Self::JustFilenames) }
}
}
#[cfg(test)]
mod terminal_test {
use super::*;
@ -338,7 +342,6 @@ mod colour_test {
}
#[cfg(test)]
mod customs_test {
use std::ffi::OsString;
@ -411,10 +414,10 @@ mod customs_test {
use crate::options::vars;
if name == vars::LS_COLORS && ! self.ls.is_empty() {
OsString::from(self.ls.clone()).into()
Some(OsString::from(self.ls.clone()))
}
else if name == vars::EXA_COLORS && ! self.exa.is_empty() {
OsString::from(self.exa.clone()).into()
Some(OsString::from(self.exa.clone()))
}
else {
None

View File

@ -15,6 +15,7 @@ pub static COLUMNS: &str = "COLUMNS";
/// Environment variable used to datetime format.
pub static TIME_STYLE: &str = "TIME_STYLE";
// exa-specific variables
/// Environment variable used to colour exas interface when colours are
@ -39,7 +40,6 @@ pub static EXA_DEBUG: &str = "EXA_DEBUG";
pub static EXA_GRID_ROWS: &str = "EXA_GRID_ROWS";
/// Mockable wrapper for `std::env::var_os`.
pub trait Vars {
fn get(&self, name: &'static str) -> Option<OsString>;

View File

@ -1,5 +1,6 @@
use lazy_static::lazy_static;
use crate::fs::feature::xattr;
use crate::options::{flags, Misfire, Vars};
use crate::options::parser::MatchedFlags;
use crate::output::{View, Mode, grid, details, lines};
@ -7,8 +8,6 @@ use crate::output::grid_details::{self, RowThreshold};
use crate::output::table::{TimeTypes, Environment, SizeFormat, Columns, Options as TableOptions};
use crate::output::time::TimeFormat;
use crate::fs::feature::xattr;
impl View {
@ -104,7 +103,8 @@ impl Mode {
let other_options_mode = other_options_scan()?;
if let Self::Grid(grid) = other_options_mode {
let row_threshold = RowThreshold::deduce(vars)?;
return Ok(Self::GridDetails(grid_details::Options { grid, details, row_threshold }));
let opts = grid_details::Options { grid, details, row_threshold };
return Ok(Self::GridDetails(opts));
}
else {
return Ok(other_options_mode);
@ -129,7 +129,7 @@ impl Mode {
return Err(Misfire::Useless(&flags::GIT, false, &flags::LONG));
}
else if matches.has(&flags::LEVEL)? && ! matches.has(&flags::RECURSE)? && ! matches.has(&flags::TREE)? {
// TODO: I'm not sure if the code even gets this far.
// TODO: Im not sure if the code even gets this far.
// There is an identical check in dir_action
return Err(Misfire::Useless2(&flags::LEVEL, &flags::RECURSE, &flags::TREE));
}
@ -266,7 +266,9 @@ impl TimeFormat {
pub use crate::output::time::{DefaultFormat, ISOFormat};
let word = match matches.get(&flags::TIME_STYLE)? {
Some(w) => w.to_os_string(),
Some(w) => {
w.to_os_string()
}
None => {
use crate::options::vars;
match vars.get(vars::TIME_STYLE) {
@ -373,7 +375,6 @@ lazy_static! {
}
#[cfg(test)]
mod test {
use super::*;

View File

@ -149,7 +149,7 @@ impl Deref for TextCellContents {
}
}
// No DerefMut implementation here -- it would be publicly accessible, and as
// No DerefMut implementation here it would be publicly accessible, and as
// the contents only get changed in this module, the mutators in the struct
// above can just access the value directly.
@ -188,7 +188,7 @@ impl TextCellContents {
/// when calculating widths for displaying tables in a terminal.
///
/// This type is used to ensure that the width, rather than the length, is
/// used when constructing a `TextCell` -- it's too easy to write something
/// used when constructing a `TextCell` — its too easy to write something
/// like `file_name.len()` and assume it will work!
///
/// It has `From` impls that convert an input string or fixed with to values
@ -239,7 +239,9 @@ impl Add<usize> for DisplayWidth {
}
impl Sum for DisplayWidth {
fn sum<I>(iter: I) -> Self where I: Iterator<Item=Self> {
fn sum<I>(iter: I) -> Self
where I: Iterator<Item = Self>
{
iter.fold(Self(0), Add::add)
}
}

View File

@ -1,6 +1,6 @@
//! The **Details** output view displays each file as a row in a table.
//!
//! It's used in the following situations:
//! Its used in the following situations:
//!
//! - Most commonly, when using the `--long` command-line argument to display the
//! details of each file, which requires using a table view to hold all the data;
@ -66,20 +66,19 @@ use std::path::PathBuf;
use std::vec::IntoIter as VecIntoIter;
use ansi_term::{ANSIGenericString, Style};
use scoped_threadpool::Pool;
use crate::fs::{Dir, File};
use crate::fs::dir_action::RecurseOptions;
use crate::fs::filter::FileFilter;
use crate::fs::feature::git::GitCache;
use crate::fs::feature::xattr::{Attribute, FileAttributes};
use crate::fs::filter::FileFilter;
use crate::style::Colours;
use crate::output::cell::TextCell;
use crate::output::tree::{TreeTrunk, TreeParams, TreeDepth};
use crate::output::icons::painted_icon;
use crate::output::file_name::FileStyle;
use crate::output::table::{Table, Options as TableOptions, Row as TableRow};
use crate::output::icons::painted_icon;
use scoped_threadpool::Pool;
use crate::output::tree::{TreeTrunk, TreeParams, TreeDepth};
/// With the **Details** view, the output gets formatted into columns, with
@ -105,15 +104,14 @@ pub struct Options {
/// Whether to show a header line or not.
pub header: bool,
/// Whether to show each file's extended attributes.
/// Whether to show each files extended attributes.
pub xattr: bool,
/// Enables --icons mode
/// Whether icons mode is enabled.
pub icons: bool,
}
pub struct Render<'a> {
pub dir: Option<&'a Dir>,
pub files: Vec<File<'a>>,
@ -248,7 +246,8 @@ impl<'a> Render<'a> {
}
}
let table_row = table.as_ref().map(|t| t.row_for_file(file, !xattrs.is_empty()));
let table_row = table.as_ref()
.map(|t| t.row_for_file(file, ! xattrs.is_empty()));
if ! self.opts.xattr {
xattrs.clear();
@ -265,9 +264,8 @@ impl<'a> Render<'a> {
}
};
let icon = if self.opts.icons {
Some(painted_icon(file, self.style))
} else { None };
let icon = if self.opts.icons { Some(painted_icon(file, self.style)) }
else { None };
let egg = Egg { table_row, xattrs, errors, dir, file, icon };
unsafe { std::ptr::write(file_eggs.lock().unwrap()[idx].as_mut_ptr(), egg) }
@ -291,10 +289,12 @@ impl<'a> Render<'a> {
if let Some(icon) = egg.icon {
name_cell.push(ANSIGenericString::from(icon), 2)
}
name_cell.append(self.style.for_file(egg.file, self.colours)
let style = self.style.for_file(egg.file, self.colours)
.with_link_paths()
.paint()
.promote());
.promote();
name_cell.append(style);
let row = Row {
@ -308,8 +308,12 @@ impl<'a> Render<'a> {
if let Some(ref dir) = egg.dir {
for file_to_add in dir.files(self.filter.dot_filter, git, self.git_ignoring) {
match file_to_add {
Ok(f) => files.push(f),
Err((path, e)) => errors.push((e, Some(path)))
Ok(f) => {
files.push(f);
}
Err((path, e)) => {
errors.push((e, Some(path)));
}
}
}
@ -331,12 +335,16 @@ impl<'a> Render<'a> {
let count = egg.xattrs.len();
for (index, xattr) in egg.xattrs.into_iter().enumerate() {
rows.push(self.render_xattr(&xattr, TreeParams::new(depth.deeper(), errors.is_empty() && index == count - 1)));
let params = TreeParams::new(depth.deeper(), errors.is_empty() && index == count - 1);
let r = self.render_xattr(&xattr, params);
rows.push(r);
}
let count = errors.len();
for (index, (error, path)) in errors.into_iter().enumerate() {
rows.push(self.render_error(&error, TreeParams::new(depth.deeper(), index == count - 1), path));
let params = TreeParams::new(depth.deeper(), index == count - 1);
let r = self.render_error(&error, params, path);
rows.push(r);
}
}
}
@ -396,13 +404,13 @@ pub struct Row {
/// Vector of cells to display.
///
/// Most of the rows will be used to display files' metadata, so this will
/// Most of the rows will be used to display files metadata, so this will
/// almost always be `Some`, containing a vector of cells. It will only be
/// `None` for a row displaying an attribute or error, neither of which
/// have cells.
pub cells: Option<TableRow>,
/// This file's name, in coloured output. The name is treated separately
/// This files name, in coloured output. The name is treated separately
/// from the other cells, as it never requires padding.
pub name: TextCell,

View File

@ -4,8 +4,9 @@ use ansi_term::{ANSIString, Style};
pub fn escape<'a>(string: String, bits: &mut Vec<ANSIString<'a>>, good: Style, bad: Style) {
if string.chars().all(|c| c >= 0x20 as char && c != 0x7f as char) {
bits.push(good.paint(string));
return;
}
else {
for c in string.chars() {
// The `escape_default` method on `char` is *almost* what we want here, but
// it still escapes non-ASCII UTF-8 characters, which are still printable.
@ -16,10 +17,10 @@ pub fn escape<'a>(string: String, bits: &mut Vec<ANSIString<'a>>, good: Style, b
let mut s = String::new();
s.push(c);
bits.push(good.paint(s));
} else {
}
else {
let s = c.escape_default().collect::<String>();
bits.push(bad.paint(s));
}
}
}
}

View File

@ -1,10 +1,12 @@
use std::fmt::Debug;
use std::marker::Sync;
use std::path::Path;
use ansi_term::{ANSIString, Style};
use crate::fs::{File, FileTarget};
use crate::output::escape;
use crate::output::cell::TextCellContents;
use crate::output::escape;
use crate::output::render::FiletypeColours;
@ -25,7 +27,8 @@ impl FileStyle {
/// with the remaining arguments.
pub fn for_file<'a, 'dir, C: Colours>(&'a self, file: &'a File<'dir>, colours: &'a C) -> FileName<'a, 'dir, C> {
FileName {
file, colours,
file,
colours,
link_style: LinkStyle::JustFilenames,
classify: self.classify,
exts: &*self.exts,
@ -71,7 +74,6 @@ impl Default for Classify {
}
/// A **file name** holds all the information necessary to display the name
/// of the given file. This is used in all of the views.
pub struct FileName<'a, 'dir: 'a, C: Colours+'a> {
@ -122,7 +124,7 @@ impl<'a, 'dir, C: Colours> FileName<'a, 'dir, C> {
if ! self.file.name.is_empty() {
// The “missing file” colour seems like it should be used here,
// but its not! In a grid view, where there's no space to display
// but its not! In a grid view, where theres no space to display
// link targets, the filename has to have a different style to
// indicate this fact. But when showing targets, we can just
// colour the path instead (see below), and leave the broken
@ -157,18 +159,24 @@ impl<'a, 'dir, C: Colours> FileName<'a, 'dir, C> {
bits.push(bit);
}
}
},
}
FileTarget::Broken(broken_path) => {
bits.push(Style::default().paint(" "));
bits.push(self.colours.broken_symlink().paint("->"));
bits.push(Style::default().paint(" "));
escape(broken_path.display().to_string(), &mut bits, self.colours.broken_filename(), self.colours.broken_control_char());
},
escape(
broken_path.display().to_string(),
&mut bits,
self.colours.broken_filename(),
self.colours.broken_control_char(),
);
}
FileTarget::Err(_) => {
// Do nothing -- the error gets displayed on the next line
},
// Do nothing the error gets displayed on the next line
}
}
}
else if let Classify::AddFileIndicators = self.classify {
@ -190,7 +198,12 @@ impl<'a, 'dir, C: Colours> FileName<'a, 'dir, C> {
bits.push(self.colours.symlink_path().paint("/"));
}
else if coconut >= 1 {
escape(parent.to_string_lossy().to_string(), bits, self.colours.symlink_path(), self.colours.control_char());
escape(
parent.to_string_lossy().to_string(),
bits,
self.colours.symlink_path(),
self.colours.control_char(),
);
bits.push(self.colours.symlink_path().paint("/"));
}
}
@ -201,15 +214,20 @@ impl<'a, 'dir, C: Colours> FileName<'a, 'dir, C> {
fn classify_char(&self) -> Option<&'static str> {
if self.file.is_executable_file() {
Some("*")
} else if self.file.is_directory() {
}
else if self.file.is_directory() {
Some("/")
} else if self.file.is_pipe() {
}
else if self.file.is_pipe() {
Some("|")
} else if self.file.is_link() {
}
else if self.file.is_link() {
Some("@")
} else if self.file.is_socket() {
}
else if self.file.is_socket() {
Some("=")
} else {
}
else {
None
}
}
@ -228,13 +246,20 @@ impl<'a, 'dir, C: Colours> FileName<'a, 'dir, C> {
fn coloured_file_name<'unused>(&self) -> Vec<ANSIString<'unused>> {
let file_style = self.style();
let mut bits = Vec::new();
escape(self.file.name.clone(), &mut bits, file_style, self.colours.control_char());
escape(
self.file.name.clone(),
&mut bits,
file_style,
self.colours.control_char(),
);
bits
}
/// Figures out which colour to paint the filename part of the output,
/// depending on which “type” of file it appears to be -- either from the
/// depending on which “type” of file it appears to be either from the
/// class on the filesystem or from its name. (Or the broken link colour,
/// if theres nowhere else for that fact to be shown.)
pub fn style(&self) -> Style {
@ -298,8 +323,6 @@ pub trait Colours: FiletypeColours {
// needs Debug because FileStyle derives it
use std::fmt::Debug;
use std::marker::Sync;
pub trait FileColours: Debug + Sync {
fn colour_file(&self, file: &File) -> Option<Style>;
}
@ -308,7 +331,9 @@ pub trait FileColours: Debug+Sync {
#[derive(PartialEq, Debug)]
pub struct NoFileColours;
impl FileColours for NoFileColours {
fn colour_file(&self, _file: &File) -> Option<Style> { None }
fn colour_file(&self, _file: &File) -> Option<Style> {
None
}
}
// When getting the colour of a file from a *pair* of colourisers, try the
@ -316,8 +341,11 @@ impl FileColours for NoFileColours {
// file type associations, while falling back to the default set if not set
// explicitly.
impl<A, B> FileColours for (A, B)
where A: FileColours, B: FileColours {
where A: FileColours,
B: FileColours,
{
fn colour_file(&self, file: &File) -> Option<Style> {
self.0.colour_file(file).or_else(|| self.1.colour_file(file))
self.0.colour_file(file)
.or_else(|| self.1.colour_file(file))
}
}

View File

@ -3,10 +3,10 @@ use std::io::{Write, Result as IOResult};
use term_grid as tg;
use crate::fs::File;
use crate::style::Colours;
use crate::output::cell::DisplayWidth;
use crate::output::file_name::FileStyle;
use crate::output::icons::painted_icon;
use crate::output::cell::DisplayWidth;
use crate::style::Colours;
#[derive(PartialEq, Debug, Copy, Clone)]
@ -41,16 +41,16 @@ impl<'a> Render<'a> {
grid.reserve(self.files.len());
for file in &self.files {
let icon = if self.opts.icons { Some(painted_icon(file, self.style)) } else { None };
let icon = if self.opts.icons { Some(painted_icon(file, self.style)) }
else { None };
let filename = self.style.for_file(file, self.colours).paint();
let width = if self.opts.icons {
DisplayWidth::from(2) + filename.width()
} else {
filename.width()
};
let width = if self.opts.icons { DisplayWidth::from(2) + filename.width() }
else { filename.width() };
grid.add(tg::Cell {
contents: format!("{icon}{filename}", icon=&icon.unwrap_or_default(), filename=filename.strings().to_string()),
contents: format!("{}{}", &icon.unwrap_or_default(), filename.strings()),
width: *width,
});
}
@ -66,9 +66,11 @@ impl<'a> Render<'a> {
if self.opts.icons {
write!(w, "{}", painted_icon(file, self.style))?;
}
let name_cell = self.style.for_file(file, self.colours).paint();
writeln!(w, "{}", name_cell.strings())?;
}
Ok(())
}
}

View File

@ -9,15 +9,14 @@ use crate::fs::{Dir, File};
use crate::fs::feature::git::GitCache;
use crate::fs::feature::xattr::FileAttributes;
use crate::fs::filter::FileFilter;
use crate::style::Colours;
use crate::output::cell::TextCell;
use crate::output::details::{Options as DetailsOptions, Row as DetailsRow, Render as DetailsRender};
use crate::output::grid::Options as GridOptions;
use crate::output::file_name::FileStyle;
use crate::output::grid::Options as GridOptions;
use crate::output::icons::painted_icon;
use crate::output::table::{Table, Row as TableRow, Options as TableOptions};
use crate::output::tree::{TreeParams, TreeDepth};
use crate::output::icons::painted_icon;
use crate::style::Colours;
#[derive(Debug)]
@ -84,7 +83,7 @@ pub struct Render<'a> {
impl<'a> Render<'a> {
/// Create a temporary Details render that gets used for the columns of
/// the grid-details render that's being generated.
/// the grid-details render thats being generated.
///
/// This includes an empty files vector because the files get added to
/// the table in *this* file, not in details: we only want to insert every
@ -138,7 +137,7 @@ impl<'a> Render<'a> {
let rows = self.files.iter()
.map(|file| first_table.row_for_file(file, file_has_xattrs(file)))
.collect::<Vec<TableRow>>();
.collect::<Vec<_>>();
let file_names = self.files.iter()
.map(|file| {
@ -148,11 +147,12 @@ impl<'a> Render<'a> {
let file_cell = self.style.for_file(file, self.colours).paint().promote();
icon_cell.append(file_cell);
icon_cell
} else {
}
else {
self.style.for_file(file, self.colours).paint().promote()
}
})
.collect::<Vec<TextCell>>();
.collect::<Vec<_>>();
let mut last_working_table = self.make_grid(1, options, git, &file_names, rows.clone(), &drender);
@ -206,7 +206,6 @@ impl<'a> Render<'a> {
}
fn make_grid(&'a self, column_count: usize, options: &'a TableOptions, git: Option<&GitCache>, file_names: &[TextCell], rows: Vec<TableRow>, drender: &DetailsRender) -> grid::Grid {
let mut tables = Vec::new();
for _ in 0 .. column_count {
tables.push(self.make_table(options, git, drender));
@ -234,17 +233,19 @@ impl<'a> Render<'a> {
rows.push(details_row);
}
let columns: Vec<_> = tables.into_iter().map(|(table, details_rows)| {
drender.iterate_with_table(table, details_rows).collect::<Vec<_>>()
}).collect();
let columns = tables
.into_iter()
.map(|(table, details_rows)| {
drender.iterate_with_table(table, details_rows)
.collect::<Vec<_>>()
})
.collect::<Vec<_>>();
let direction = if self.grid.across { grid::Direction::LeftToRight }
else { grid::Direction::TopToBottom };
let mut grid = grid::Grid::new(grid::GridOptions {
direction,
filling: grid::Filling::Spaces(4),
});
let filling = grid::Filling::Spaces(4);
let mut grid = grid::Grid::new(grid::GridOptions { direction, filling });
if self.grid.across {
for row in 0 .. height {
@ -280,7 +281,11 @@ impl<'a> Render<'a> {
fn divide_rounding_up(a: usize, b: usize) -> usize {
let mut result = a / b;
if a % b != 0 { result += 1; }
if a % b != 0 {
result += 1;
}
result
}

View File

@ -4,10 +4,12 @@ use crate::fs::File;
use crate::info::filetype::FileExtensions;
use crate::output::file_name::FileStyle;
pub trait FileIcon {
fn icon_file(&self, file: &File) -> Option<char>;
}
#[derive(Copy, Clone)]
pub enum Icons {
Audio,
@ -25,6 +27,7 @@ impl Icons {
}
}
pub fn painted_icon(file: &File, style: &FileStyle) -> String {
let file_icon = icon(file).to_string();
let painted = style.exts
@ -33,20 +36,32 @@ pub fn painted_icon(file: &File, style: &FileStyle) -> String {
// Remove underline from icon
if c.is_underline {
match c.foreground {
Some(color) => Style::from(color).paint(file_icon).to_string(),
None => Style::default().paint(file_icon).to_string(),
Some(color) => {
Style::from(color).paint(file_icon).to_string()
}
} else {
None => {
Style::default().paint(file_icon).to_string()
}
}
}
else {
c.paint(file_icon).to_string()
}
});
format!("{} ", painted)
}
fn icon(file: &File) -> char {
let extensions = Box::new(FileExtensions);
if file.points_to_directory() { '\u{f115}' }
else if let Some(icon) = extensions.icon_file(file) { icon }
if file.points_to_directory() {
'\u{f115}'
}
else if let Some(icon) = extensions.icon_file(file) {
icon
}
else if let Some(ext) = file.ext.as_ref() {
match ext.as_str() {
"ai" => '\u{e7b4}',
@ -189,7 +204,8 @@ fn icon(file: &File) -> char {
"nix" => '\u{f313}',
_ => '\u{f016}'
}
} else {
}
else {
'\u{f016}'
}
}

View File

@ -3,14 +3,15 @@ use std::io::{Write, Result as IOResult};
use ansi_term::{ANSIStrings, ANSIGenericString};
use crate::fs::File;
use crate::output::file_name::{FileName, FileStyle};
use crate::style::Colours;
use crate::output::icons::painted_icon;
use crate::output::cell::TextCell;
use crate::output::file_name::{FileName, FileStyle};
use crate::output::icons::painted_icon;
use crate::style::Colours;
#[derive(PartialEq, Debug, Copy, Clone)]
pub struct Options {
pub icons: bool
pub icons: bool,
}
/// The lines view literally just displays each file, line-by-line.
@ -32,7 +33,8 @@ impl<'a> Render<'a> {
cell.push(ANSIGenericString::from(icon), 2);
cell.append(name_cell.promote());
writeln!(w, "{}", ANSIStrings(&cell))?;
} else {
}
else {
writeln!(w, "{}", ANSIStrings(&name_cell))?;
}
}

View File

@ -6,8 +6,8 @@ pub use self::escape::escape;
pub mod details;
pub mod file_name;
pub mod grid_details;
pub mod grid;
pub mod grid_details;
pub mod icons;
pub mod lines;
pub mod render;

View File

@ -1,7 +1,7 @@
use ansi_term::Style;
use crate::output::cell::TextCell;
use crate::fs::fields as f;
use crate::output::cell::TextCell;
impl f::Blocks {

View File

@ -65,7 +65,7 @@ pub mod test {
fn renamed(&self) -> Style { Fixed(94).normal() }
fn type_change(&self) -> Style { Fixed(95).normal() }
fn ignored(&self) -> Style { Fixed(96).normal() }
fn conflicted(&self) -> Style { Fixed(93).normal() }
fn conflicted(&self) -> Style { Fixed(97).normal() }
}

View File

@ -18,8 +18,10 @@ impl f::Group {
let current_uid = users.get_current_uid();
if let Some(current_user) = users.get_user_by_uid(current_uid) {
if current_user.primary_group_id() == group.gid()
|| group.members().iter().any(|u| u == current_user.name()) {
|| group.members().iter().any(|u| u == current_user.name())
{
style = colours.yours();
}
}

View File

@ -1,7 +1,7 @@
use ansi_term::Style;
use crate::output::cell::TextCell;
use crate::fs::fields as f;
use crate::output::cell::TextCell;
impl f::Inode {

View File

@ -1,8 +1,8 @@
use ansi_term::Style;
use locale::Numeric as NumericLocale;
use crate::output::cell::TextCell;
use crate::fs::fields as f;
use crate::output::cell::TextCell;
impl f::Links {

View File

@ -1,7 +1,8 @@
use ansi_term::Style;
use crate::output::cell::TextCell;
use crate::fs::fields as f;
use crate::output::cell::TextCell;
impl f::OctalPermissions {
fn bits_to_octal(r: bool, w: bool, x: bool) -> u8 {
@ -9,7 +10,6 @@ impl f::OctalPermissions {
}
pub fn render(&self, style: Style) -> TextCell {
let perm = &self.permissions;
let octal_sticky = Self::bits_to_octal(perm.setuid, perm.setgid, perm.sticky);
let octal_owner = Self::bits_to_octal(perm.user_read, perm.user_write, perm.user_execute);
@ -18,9 +18,9 @@ impl f::OctalPermissions {
TextCell::paint(style, format!("{}{}{}{}", octal_sticky, octal_owner, octal_group, octal_other))
}
}
#[cfg(test)]
pub mod test {
use crate::output::cell::TextCell;

View File

@ -25,12 +25,12 @@ impl f::PermissionsPlus {
}
impl f::Permissions {
pub fn render<C: Colours>(&self, colours: &C, is_regular_file: bool) -> Vec<ANSIString<'static>> {
let bit = |bit, chr: &'static str, style: Style| {
if bit { style.paint(chr) } else { colours.dash().paint("-") }
if bit { style.paint(chr) }
else { colours.dash().paint("-") }
};
vec![

View File

@ -21,20 +21,23 @@ impl f::Size {
SizeFormat::DecimalBytes => NumberPrefix::decimal(size as f64),
SizeFormat::BinaryBytes => NumberPrefix::binary(size as f64),
SizeFormat::JustBytes => {
// Use the binary prefix to select a style.
let prefix = match NumberPrefix::binary(size as f64) {
NumberPrefix::Standalone(_) => None,
NumberPrefix::Prefixed(p, _) => Some(p),
};
// But format the number directly using the locale.
let string = numerics.format_int(size);
return TextCell::paint(colours.size(prefix), string);
},
}
};
let (prefix, n) = match result {
NumberPrefix::Standalone(b) => return TextCell::paint(colours.size(None), b.to_string()),
NumberPrefix::Prefixed(p, n) => (p, n)
NumberPrefix::Prefixed(p, n) => (p, n),
};
let symbol = prefix.symbol();
@ -72,6 +75,7 @@ impl f::DeviceIDs {
}
}
pub trait Colours {
fn size(&self, prefix: Option<Prefix>) -> Style;
fn unit(&self, prefix: Option<Prefix>) -> Style;

View File

@ -1,3 +1,5 @@
use std::time::SystemTime;
use datetime::TimeZone;
use ansi_term::Style;
@ -6,23 +8,20 @@ use crate::output::time::TimeFormat;
pub trait Render {
fn render(self, style: Style,
tz: &Option<TimeZone>,
format: &TimeFormat) -> TextCell;
fn render(self, style: Style, tz: &Option<TimeZone>, format: &TimeFormat) -> TextCell;
}
impl Render for Option<std::time::SystemTime> {
fn render(self, style: Style,
tz: &Option<TimeZone>,
format: &TimeFormat) -> TextCell {
impl Render for Option<SystemTime> {
fn render(self, style: Style, tz: &Option<TimeZone>, format: &TimeFormat) -> TextCell {
let datestamp = if let Some(time) = self {
if let Some(ref tz) = tz {
format.format_zoned(time, tz)
} else {
}
else {
format.format_local(time)
}
} else {
}
else {
String::from("-")
};

View File

@ -5,7 +5,6 @@ use crate::fs::fields as f;
use crate::output::cell::TextCell;
impl f::User {
pub fn render<C: Colours, U: Users>(self, colours: &C, users: &U) -> TextCell {
let user_name = match users.get_user_by_uid(self.0) {

View File

@ -11,12 +11,12 @@ use log::*;
use users::UsersCache;
use crate::style::Colours;
use crate::fs::{File, fields as f};
use crate::fs::feature::git::GitCache;
use crate::output::cell::TextCell;
use crate::output::render::TimeRender;
use crate::output::time::TimeFormat;
use crate::fs::{File, fields as f};
use crate::fs::feature::git::GitCache;
use crate::style::Colours;
/// Options for displaying a table.
@ -136,7 +136,8 @@ pub enum Column {
/// right-aligned, and text is left-aligned.
#[derive(Copy, Clone)]
pub enum Alignment {
Left, Right,
Left,
Right,
}
impl Column {
@ -199,6 +200,7 @@ impl Default for SizeFormat {
/// across most (all?) operating systems.
#[derive(PartialEq, Debug, Copy, Clone)]
pub enum TimeType {
/// The files modified time (`st_mtime`).
Modified,
@ -229,7 +231,7 @@ impl TimeType {
/// Fields for which of a files time fields should be displayed in the
/// columns output.
///
/// There should always be at least one of these--there's no way to disable
/// There should always be at least one of these — theres no way to disable
/// the time columns entirely (yet).
#[derive(PartialEq, Debug, Copy, Clone)]
pub struct TimeTypes {
@ -244,15 +246,18 @@ impl Default for TimeTypes {
/// By default, display just the modified time. This is the most
/// common option, which is why it has this shorthand.
fn default() -> Self {
Self { modified: true, changed: false, accessed: false, created: false }
Self {
modified: true,
changed: false,
accessed: false,
created: false,
}
}
}
/// The **environment** struct contains any data that could change between
/// running instances of exa, depending on the user's computer's configuration.
/// running instances of exa, depending on the users computers configuration.
///
/// Any environment field should be able to be mocked up for test runs.
pub struct Environment {
@ -260,8 +265,8 @@ pub struct Environment {
/// Localisation rules for formatting numbers.
numeric: locale::Numeric,
/// The computer's current time zone. This gets used to determine how to
/// offset files' timestamps.
/// The computers current time zone. This gets used to determine how to
/// offset files timestamps.
tz: Option<TimeZone>,
/// Mapping cache of user IDs to usernames.
@ -275,7 +280,9 @@ impl Environment {
pub fn load_all() -> Self {
let tz = match determine_time_zone() {
Ok(t) => Some(t),
Ok(t) => {
Some(t)
}
Err(ref e) => {
println!("Unable to determine time zone: {}", e);
None
@ -294,7 +301,8 @@ impl Environment {
fn determine_time_zone() -> TZResult<TimeZone> {
if let Ok(file) = env::var("TZ") {
TimeZone::from_file(format!("/usr/share/zoneinfo/{}", file))
} else {
}
else {
TimeZone::from_file("/etc/localtime")
}
}
@ -321,7 +329,10 @@ impl<'a, 'f> Table<'a> {
let widths = TableWidths::zero(columns.len());
Table {
colours, widths, columns, git,
colours,
widths,
columns,
git,
env: &options.env,
time_format: &options.time_format,
size_format: options.size_format,
@ -368,25 +379,52 @@ impl<'a, 'f> Table<'a> {
fn display(&self, file: &File, column: Column, xattrs: bool) -> TextCell {
match column {
Column::Permissions => self.permissions_plus(file, xattrs).render(self.colours),
Column::FileSize => file.size().render(self.colours, self.size_format, &self.env.numeric),
Column::HardLinks => file.links().render(self.colours, &self.env.numeric),
Column::Inode => file.inode().render(self.colours.inode),
Column::Blocks => file.blocks().render(self.colours),
Column::User => file.user().render(self.colours, &*self.env.lock_users()),
Column::Group => file.group().render(self.colours, &*self.env.lock_users()),
Column::GitStatus => self.git_status(file).render(self.colours),
Column::Octal => self.octal_permissions(file).render(self.colours.octal),
Column::Permissions => {
self.permissions_plus(file, xattrs).render(self.colours)
}
Column::FileSize => {
file.size().render(self.colours, self.size_format, &self.env.numeric)
}
Column::HardLinks => {
file.links().render(self.colours, &self.env.numeric)
}
Column::Inode => {
file.inode().render(self.colours.inode)
}
Column::Blocks => {
file.blocks().render(self.colours)
}
Column::User => {
file.user().render(self.colours, &*self.env.lock_users())
}
Column::Group => {
file.group().render(self.colours, &*self.env.lock_users())
}
Column::GitStatus => {
self.git_status(file).render(self.colours)
}
Column::Octal => {
self.octal_permissions(file).render(self.colours.octal)
}
Column::Timestamp(TimeType::Modified) => file.modified_time().render(self.colours.date, &self.env.tz, self.time_format),
Column::Timestamp(TimeType::Changed) => file.changed_time() .render(self.colours.date, &self.env.tz, self.time_format),
Column::Timestamp(TimeType::Created) => file.created_time() .render(self.colours.date, &self.env.tz, self.time_format),
Column::Timestamp(TimeType::Accessed) => file.accessed_time().render(self.colours.date, &self.env.tz, self.time_format),
Column::Timestamp(TimeType::Modified) => {
file.modified_time().render(self.colours.date, &self.env.tz, self.time_format)
}
Column::Timestamp(TimeType::Changed) => {
file.changed_time().render(self.colours.date, &self.env.tz, self.time_format)
}
Column::Timestamp(TimeType::Created) => {
file.created_time().render(self.colours.date, &self.env.tz, self.time_format)
}
Column::Timestamp(TimeType::Accessed) => {
file.accessed_time().render(self.colours.date, &self.env.tz, self.time_format)
}
}
}
fn git_status(&self, file: &File) -> f::Git {
debug!("Getting Git status for file {:?}", file.path);
self.git
.map(|g| g.get(&file.path, file.is_directory()))
.unwrap_or_default()
@ -395,12 +433,22 @@ impl<'a, 'f> Table<'a> {
pub fn render(&self, row: Row) -> TextCell {
let mut cell = TextCell::default();
for (n, (this_cell, width)) in row.cells.into_iter().zip(self.widths.iter()).enumerate() {
let iter = row.cells.into_iter()
.zip(self.widths.iter())
.enumerate();
for (n, (this_cell, width)) in iter {
let padding = width - *this_cell.width;
match self.columns[n].alignment() {
Alignment::Left => { cell.append(this_cell); cell.add_spaces(padding); }
Alignment::Right => { cell.add_spaces(padding); cell.append(this_cell); }
Alignment::Left => {
cell.append(this_cell);
cell.add_spaces(padding);
}
Alignment::Right => {
cell.add_spaces(padding);
cell.append(this_cell);
}
}
cell.add_spaces(1);
@ -411,7 +459,6 @@ impl<'a, 'f> Table<'a> {
}
pub struct TableWidths(Vec<usize>);
impl Deref for TableWidths {

View File

@ -119,10 +119,7 @@ impl DefaultFormat {
Self { current_year, locale, date_and_time, date_and_year }
}
}
impl DefaultFormat {
fn is_recent(&self, date: LocalDateTime) -> bool {
date.year() == self.current_year
}
@ -217,7 +214,6 @@ fn long_zoned(time: SystemTime, zone: &TimeZone) -> String {
date.hour(), date.minute())
}
#[allow(trivial_numeric_casts)]
fn full_local(time: SystemTime) -> String {
let date = LocalDateTime::at(systemtime_epoch(time));
@ -240,7 +236,6 @@ fn full_zoned(time: SystemTime, zone: &TimeZone) -> String {
}
#[derive(Debug, Copy, Clone)]
pub struct ISOFormat {

View File

@ -111,18 +111,21 @@ impl TreeTrunk {
// If this isnt our first iteration, then update the tree parts thus
// far to account for there being another row after it.
if let Some(last) = self.last_params {
self.stack[last.depth.0] = if last.last { TreePart::Blank } else { TreePart::Line };
self.stack[last.depth.0] = if last.last { TreePart::Blank }
else { TreePart::Line };
}
// Make sure the stack has enough space, then add or modify another
// part into it.
self.stack.resize(params.depth.0 + 1, TreePart::Edge);
self.stack[params.depth.0] = if params.last { TreePart::Corner } else { TreePart::Edge };
self.stack[params.depth.0] = if params.last { TreePart::Corner }
else { TreePart::Edge };
self.last_params = Some(params);
// Return the tree parts as a slice of the stack.
//
// Ignore the first element here to prevent a 'zeroth level' from
// Ignore the first element here to prevent a zeroth level from
// appearing before the very first directory. This level would
// join unrelated directories without connecting to anything:
//
@ -159,7 +162,8 @@ impl TreeDepth {
/// Creates an iterator that, as well as yielding each value, yields a
/// `TreeParams` with the current depth and last flag filled in.
pub fn iterate_over<I, T>(self, inner: I) -> Iter<I>
where I: ExactSizeIterator+Iterator<Item=T> {
where I: ExactSizeIterator + Iterator<Item = T>
{
Iter { current_depth: self, inner }
}
}
@ -171,14 +175,16 @@ pub struct Iter<I> {
}
impl<I, T> Iterator for Iter<I>
where I: ExactSizeIterator+Iterator<Item=T> {
where I: ExactSizeIterator + Iterator<Item = T>
{
type Item = (TreeParams, T);
fn next(&mut self) -> Option<Self::Item> {
self.inner.next().map(|t| {
// use exact_size_is_empty API soon
(TreeParams::new(self.current_depth, self.inner.len() == 0), t)
})
let t = self.inner.next()?;
// TODO: use exact_size_is_empty API soon
let params = TreeParams::new(self.current_depth, self.inner.len() == 0);
Some((params, t))
}
}
@ -240,7 +246,6 @@ mod trunk_test {
}
#[cfg(test)]
mod iter_test {
use super::*;

View File

@ -1,9 +1,8 @@
use ansi_term::Style;
use ansi_term::Colour::{Red, Green, Yellow, Blue, Cyan, Purple, Fixed};
use ansi_term::Style;
use crate::output::render;
use crate::output::file_name::Colours as FileNameColours;
use crate::output::render;
use crate::style::lsc::Pair;
@ -190,11 +189,8 @@ impl Colours {
impl Size {
pub fn colourful(scale: bool) -> Self {
if scale {
Self::colourful_scale()
} else {
Self::colourful_plain()
}
if scale { Self::colourful_scale() }
else { Self::colourful_plain() }
}
fn colourful_plain() -> Self {
@ -233,7 +229,6 @@ impl Size {
unit_giga: Green.normal(),
unit_huge: Green.normal(),
}
}
}
@ -352,6 +347,7 @@ impl Colours {
_ => return false,
}
true
}

View File

@ -1,3 +1,4 @@
use std::iter::Peekable;
use std::ops::FnMut;
use ansi_term::{Colour, Style};
@ -21,7 +22,6 @@ use ansi_term::Colour::*;
// just not worth doing, and there should really be a way to just use slices
// of the LS_COLORS string without having to parse them.
pub struct LSColors<'var>(pub &'var str);
impl<'var> LSColors<'var> {
@ -40,14 +40,10 @@ impl<'var> LSColors<'var> {
}
}
pub struct Pair<'var> {
pub key: &'var str,
pub value: &'var str,
}
use std::iter::Peekable;
fn parse_into_high_colour<'a, I>(iter: &mut Peekable<I>) -> Option<Colour>
where I: Iterator<Item=&'a str> {
where I: Iterator<Item = &'a str>
{
match iter.peek() {
Some(&"5") => {
let _ = iter.next();
@ -57,11 +53,12 @@ where I: Iterator<Item=&'a str> {
}
}
}
Some(&"2") => {
let _ = iter.next();
if let Some(hexes) = iter.next() {
// Some terminals support R:G:B instead of R;G;B
// but this clashes with splitting on ':' in each_pair above.
// but this clashes with splitting on : in each_pair above.
/*if hexes.contains(':') {
let rgb = hexes.splitn(3, ':').collect::<Vec<_>>();
if rgb.len() != 3 {
@ -79,11 +76,19 @@ where I: Iterator<Item=&'a str> {
}
}
}
_ => {},
}
None
}
pub struct Pair<'var> {
pub key: &'var str,
pub value: &'var str,
}
impl<'var> Pair<'var> {
pub fn to_style(&self) -> Style {
let mut style = Style::default();
@ -185,7 +190,6 @@ mod ansi_test {
}
#[cfg(test)]
mod test {
use super::*;