From d400231de5312d07081ec57efdb039615052db66 Mon Sep 17 00:00:00 2001 From: Ben S Date: Mon, 5 Jan 2015 14:41:43 +0000 Subject: [PATCH] Upgrade to latest Rust Also, remove dependency on the Regex library by replacing the one place it was used with standard code that should hopefully be faster anyway. --- Cargo.lock | 37 +++++++++++++++++++++++++++---------- src/exa.rs | 9 ++++----- src/file.rs | 42 ++++++++++++++++++++---------------------- 3 files changed, 51 insertions(+), 37 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 06f2c60..21c5e40 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,21 +2,25 @@ name = "exa" version = "0.1.0" dependencies = [ - "ansi_term 0.4.0 (git+https://github.com/ogham/rust-ansi-term.git)", - "natord 1.0.2 (git+https://github.com/lifthrasiir/rust-natord.git)", + "ansi_term 0.4.1 (git+https://github.com/ogham/rust-ansi-term.git)", + "natord 1.0.3 (git+https://github.com/lifthrasiir/rust-natord.git)", "number_prefix 0.2.0 (git+https://github.com/ogham/rust-number-prefix.git)", - "users 0.1.1 (git+https://github.com/ogham/rust-users.git)", + "users 0.2.0 (git+https://github.com/ogham/rust-users.git)", ] [[package]] name = "ansi_term" -version = "0.4.0" -source = "git+https://github.com/ogham/rust-ansi-term.git#df6fcf773bae488c2c3a1456295531d6803e638a" +version = "0.4.1" +source = "git+https://github.com/ogham/rust-ansi-term.git#6db0b81cf4517e482293351c133cf07cced8c703" +dependencies = [ + "regex 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)", + "regex_macros 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", +] [[package]] name = "natord" -version = "1.0.2" -source = "git+https://github.com/lifthrasiir/rust-natord.git#6a42c8c168646c9de390af734a2e9dbe48bfdb91" +version = "1.0.3" +source = "git+https://github.com/lifthrasiir/rust-natord.git#b3a70271270effd7233fc36aef97215015d53af4" [[package]] name = "number_prefix" @@ -24,7 +28,20 @@ version = "0.2.0" source = "git+https://github.com/ogham/rust-number-prefix.git#e4b56f7661c7d1b414b62af36a0e592f77911e4f" [[package]] -name = "users" -version = "0.1.1" -source = "git+https://github.com/ogham/rust-users.git#c2911ab96a2b2459333029dde3728bfb5847ef04" +name = "regex" +version = "0.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "regex_macros" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "regex 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "users" +version = "0.2.0" +source = "git+https://github.com/ogham/rust-users.git#63d2760c52ee0a8c1e5592dea088dc90fc069f26" diff --git a/src/exa.rs b/src/exa.rs index 0b7aef5..67c8b9e 100644 --- a/src/exa.rs +++ b/src/exa.rs @@ -1,6 +1,5 @@ -#![feature(phase, globs)] -extern crate regex; -#[phase(plugin)] extern crate regex_macros; +#![feature(globs)] + extern crate ansi_term; extern crate number_prefix; extern crate unicode; @@ -116,11 +115,11 @@ fn lines_view(files: Vec) { fn fit_into_grid(across: bool, console_width: uint, files: &Vec) -> Option<(uint, Vec)> { // TODO: this function could almost certainly be optimised... // surely not *all* of the numbers of lines are worth searching through! - + // Instead of numbers of columns, try to find the fewest number of *lines* // that the output will fit in. for num_lines in range(1, files.len()) { - + // The number of columns is the number of files divided by the number // of lines, *rounded up*. let mut num_columns = files.len() / num_lines; diff --git a/src/file.rs b/src/file.rs index 74018ac..9fb283c 100644 --- a/src/file.rs +++ b/src/file.rs @@ -1,6 +1,5 @@ use std::io::{fs, IoResult}; use std::io; -use std::str::CowString; use ansi_term::{ANSIString, Colour, Style}; use ansi_term::Style::Plain; @@ -49,16 +48,15 @@ impl<'a> File<'a> { dir: parent, stat: stat, name: filename.to_string(), - ext: File::ext(filename), + ext: File::ext(filename.as_slice()), } } - fn ext(name: CowString) -> Option { + fn ext(name: &'a str) -> Option { // The extension is the series of characters after a dot at // the end of a filename. This deliberately also counts // dotfiles - the ".git" folder has the extension "git". - let re = regex!(r"\.([^.]+)$"); - re.captures(name.as_slice()).map(|caps| caps.at(1).to_string()) + name.rfind('.').map(|pos| name.slice_from(pos + 1).to_string()) } pub fn is_dotfile(&self) -> bool { @@ -209,7 +207,7 @@ impl<'a> File<'a> { dir: self.dir, stat: stat, name: filename.to_string(), - ext: File::ext(filename.clone()), + ext: File::ext(filename.as_slice()), }); // Statting a path usually fails because the file at the @@ -232,19 +230,19 @@ impl<'a> File<'a> { GREY.paint("-").to_string() } else { - let result = match size_format { - SizeFormat::DecimalBytes => decimal_prefix(self.stat.size as f64), - SizeFormat::BinaryBytes => binary_prefix(self.stat.size as f64), - SizeFormat::JustBytes => return Green.bold().paint(self.stat.size.to_string().as_slice()).to_string(), - }; - - match result { - Standalone(bytes) => Green.bold().paint(bytes.to_string().as_slice()).to_string(), - Prefixed(prefix, n) => { - let number = if n < 10f64 { format!("{:.1}", n) } else { format!("{:.0}", n) }; - format!("{}{}", Green.bold().paint(number.as_slice()), Green.paint(prefix.symbol())) - } - } + let result = match size_format { + SizeFormat::DecimalBytes => decimal_prefix(self.stat.size as f64), + SizeFormat::BinaryBytes => binary_prefix(self.stat.size as f64), + SizeFormat::JustBytes => return Green.bold().paint(self.stat.size.to_string().as_slice()).to_string(), + }; + + match result { + Standalone(bytes) => Green.bold().paint(bytes.to_string().as_slice()).to_string(), + Prefixed(prefix, n) => { + let number = if n < 10f64 { format!("{:.1}", n) } else { format!("{:.0}", n) }; + format!("{}{}", Green.bold().paint(number.as_slice()), Green.paint(prefix.symbol())) + } + } } } @@ -270,10 +268,10 @@ impl<'a> File<'a> { fn permissions_string(&self) -> String { let bits = self.stat.perm; let executable_colour = match self.stat.kind { - io::FileType::RegularFile => Green.bold().underline(), - _ => Green.bold(), + io::FileType::RegularFile => Green.bold().underline(), + _ => Green.bold(), }; - + return format!("{}{}{}{}{}{}{}{}{}{}", self.type_char(),