summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorBen S <ogham@bsago.me>2015-11-15 19:26:58 +0000
committerBen S <ogham@bsago.me>2015-11-15 19:26:58 +0000
commite07992d08c22898ca723fcb2383c545226d360ce (patch)
tree484110571b81bd627914cfaa0d2c03a0320ab625 /src
parent021655faecc24b4845afebb7cd61b3d1466a12c7 (diff)
Use lazy_static to cache datetime formats
One of those two date formats was re-compiled before any date was displayed. Now they are compiled only the first time they're used, and cached versions are used thereafter, resulting in a speedup.
Diffstat (limited to 'src')
-rw-r--r--src/main.rs7
-rw-r--r--src/output/details.rs25
2 files changed, 20 insertions, 12 deletions
diff --git a/src/main.rs b/src/main.rs
index 8c4761a..1e9f76c 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,5 +1,5 @@
#![warn(trivial_casts, trivial_numeric_casts)]
-#![warn(unused_extern_crates, unused_qualifications)]
+#![warn(unused_qualifications)]
#![warn(unused_results)]
extern crate ansi_term;
@@ -15,9 +15,8 @@ extern crate term_grid;
extern crate unicode_width;
extern crate users;
-#[cfg(feature="git")]
-extern crate git2;
-
+#[cfg(feature="git")] extern crate git2;
+#[macro_use] extern crate lazy_static;
use std::env;
use std::path::{Component, Path};
diff --git a/src/output/details.rs b/src/output/details.rs
index 7e9775f..a7d1da9 100644
--- a/src/output/details.rs
+++ b/src/output/details.rs
@@ -129,13 +129,11 @@ use output::column::{Alignment, Column, Columns, Cell, SizeFormat};
use ansi_term::{ANSIString, ANSIStrings, Style};
use datetime::local::{LocalDateTime, DatePiece};
-use datetime::format::{DateFormat};
-use datetime::zoned::{TimeZone};
+use datetime::format::DateFormat;
+use datetime::zoned::TimeZone;
use locale;
-use number_prefix::{binary_prefix, decimal_prefix, Prefixed, Standalone, PrefixNames};
-
use users::{OSUsers, Users};
use users::mock::MockUsers;
@@ -562,6 +560,8 @@ impl<U> Table<U> where U: Users {
}
fn render_size(&self, size: f::Size, size_format: SizeFormat) -> Cell {
+ use number_prefix::{binary_prefix, decimal_prefix, Prefixed, Standalone, PrefixNames};
+
if let f::Size::Some(offset) = size {
let result = match size_format {
SizeFormat::DecimalBytes => decimal_prefix(offset as f64),
@@ -591,14 +591,14 @@ impl<U> Table<U> where U: Users {
fn render_time(&self, timestamp: f::Time) -> Cell {
let date = self.tz.at(LocalDateTime::at(timestamp.0 as i64));
- let format = if date.year() == self.current_year {
- DateFormat::parse("{2>:D} {:M} {2>:h}:{02>:m}").unwrap()
+ let datestamp = if date.year() == self.current_year {
+ DATE_AND_TIME.format(&date, &self.time)
}
else {
- DateFormat::parse("{2>:D} {:M} {5>:Y}").unwrap()
+ DATE_AND_YEAR.format(&date, &self.time)
};
- Cell::paint(self.colours.date, &format.format(&date, &self.time))
+ Cell::paint(self.colours.date, &datestamp)
}
fn render_git_status(&self, git: f::Git) -> Cell {
@@ -753,6 +753,15 @@ impl TreePart {
}
+lazy_static! {
+ static ref DATE_AND_TIME: DateFormat<'static> =
+ DateFormat::parse("{2>:D} {:M} {2>:h}:{02>:m}").unwrap();
+
+ static ref DATE_AND_YEAR: DateFormat<'static> =
+ DateFormat::parse("{2>:D} {:M} {5>:Y}").unwrap();
+}
+
+
#[cfg(test)]
pub mod test {
pub use super::Table;