Translate month names into the user's locale

This has been mostly done with changes in the datetime crate's suddenly
supporting locales.

It's still important that the user's locale is touched only once and
cached from that point on, so a struct in output::details has been made
public, along with that module. This will change later as that object
gains more and more uses thoughout the codes.
This commit is contained in:
Ben S 2015-02-10 18:14:56 +00:00
parent e39a20a5d4
commit 2906b8676a
6 changed files with 47 additions and 51 deletions

31
Cargo.lock generated
View File

@ -3,11 +3,10 @@ name = "exa"
version = "0.1.0"
dependencies = [
"ansi_term 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)",
"datetime 0.1.2 (git+https://github.com/ogham/rust-datetime.git)",
"datetime_macros 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
"datetime 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)",
"getopts 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)",
"git2 0.1.17 (registry+https://github.com/rust-lang/crates.io-index)",
"locale 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
"locale 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
"natord 1.0.7 (registry+https://github.com/rust-lang/crates.io-index)",
"number_prefix 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)",
"pad 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
@ -30,33 +29,15 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "datetime"
version = "0.1.2"
source = "git+https://github.com/ogham/rust-datetime.git#c108628eb3519535821bc7907f2305643465acf5"
version = "0.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"locale 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
"pad 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
"regex 0.1.14 (registry+https://github.com/rust-lang/crates.io-index)",
"regex_macros 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "datetime"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"pad 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
"regex 0.1.14 (registry+https://github.com/rust-lang/crates.io-index)",
"regex_macros 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "datetime_macros"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"datetime 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
"pad 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "gcc"
version = "0.1.7"
@ -124,7 +105,7 @@ dependencies = [
[[package]]
name = "locale"
version = "0.1.1"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]

View File

@ -8,9 +8,10 @@ name = "exa"
[dependencies]
ansi_term = "0.4.5"
datetime_macros = "0.1.2"
datetime = "0.1.3"
#datetime_macros = "0.1.2"
getopts = "0.2.1"
locale = "0.1.1"
locale = "0.1.2"
natord = "1.0.7"
number_prefix = "0.2.3"
pad = "0.1.1"
@ -23,6 +24,3 @@ git = [ "git2" ]
[dependencies.git2]
version = "0.1.13"
optional = true
[dependencies.datetime]
git = "https://github.com/ogham/rust-datetime.git"

View File

@ -8,14 +8,13 @@ use ansi_term::Colour::{Red, Green, Yellow, Blue, Purple, Cyan, Fixed};
use users::Users;
use pad::Alignment;
use locale;
use output::details::UserLocale;
use number_prefix::{binary_prefix, decimal_prefix, Prefixed, Standalone, PrefixNames};
use datetime;
use datetime::local::{LocalDateTime, DatePiece};
use datetime::format::{DateFormat};
use column::{Column, Cell};
use column::Column::*;
@ -99,14 +98,14 @@ impl<'a> File<'a> {
}
/// Get the data for a column, formatted as a coloured string.
pub fn display<U: Users>(&self, column: &Column, users_cache: &mut U, locale: &locale::Numeric) -> Cell {
pub fn display<U: Users>(&self, column: &Column, users_cache: &mut U, locale: &UserLocale) -> Cell {
match *column {
Permissions => self.permissions_string(),
FileSize(f) => self.file_size(f, locale),
Timestamp(t, y) => self.timestamp(t, y),
HardLinks => self.hard_links(locale),
FileSize(f) => self.file_size(f, &locale.numeric),
Timestamp(t, y) => self.timestamp(t, y, &locale.time),
HardLinks => self.hard_links(&locale.numeric),
Inode => self.inode(),
Blocks => self.blocks(locale),
Blocks => self.blocks(&locale.numeric),
User => self.user(users_cache),
Group => self.group(users_cache),
GitStatus => self.git_status(),
@ -305,7 +304,7 @@ impl<'a> File<'a> {
}
}
fn timestamp(&self, time_type: TimeType, current_year: i64) -> Cell {
fn timestamp(&self, time_type: TimeType, current_year: i64, locale: &locale::Time) -> Cell {
// Need to convert these values from milliseconds into seconds.
let time_in_seconds = match time_type {
@ -317,13 +316,13 @@ impl<'a> File<'a> {
let date = LocalDateTime::at(time_in_seconds);
let format = if date.year() == current_year {
date_format!("{2>:D} {:M} {2>:h}:{02>:m}")
DateFormat::parse("{2>:D} {:M} {2>:h}:{02>:m}").unwrap()
}
else {
date_format!("{2>:D} {:M} {4>:Y}")
DateFormat::parse("{2>:D} {:M} {4>:Y}").unwrap()
};
Cell::paint(Blue.normal(), format.format(date).as_slice())
Cell::paint(Blue.normal(), format.format(date, locale).as_slice())
}
/// This file's type, represented by a coloured character.
@ -447,7 +446,7 @@ pub mod test {
pub use ansi_term::Style::Plain;
pub use ansi_term::Colour::Yellow;
pub use locale;
pub use output::details::UserLocale;
#[test]
fn extension() {
@ -491,8 +490,8 @@ pub mod test {
}
}
pub fn dummy_locale() -> locale::Numeric {
locale::Numeric::default()
pub fn dummy_locale() -> UserLocale {
UserLocale::default()
}
mod users {

View File

@ -1,8 +1,5 @@
#![feature(collections, core, env, io, libc, os, path, plugin, std_misc)]
#[plugin] #[no_link]
extern crate datetime_macros;
extern crate ansi_term;
extern crate datetime;
extern crate getopts;

View File

@ -25,7 +25,7 @@ impl Details {
// padding the fields during output.
let columns = self.columns.for_dir(dir);
let locale = locale::Numeric::load_user_locale().unwrap_or_else(|_| locale::Numeric::default());
let locale = UserLocale::new();
let mut cache = OSUsers::empty_cache();
let mut table = Vec::new();
self.get_files(&columns[], &mut cache, &locale, &mut table, files, 0);
@ -75,7 +75,7 @@ impl Details {
}
}
fn get_files(&self, columns: &[Column], cache: &mut OSUsers, locale: &locale::Numeric, dest: &mut Vec<Row>, src: &[File], depth: usize) {
fn get_files(&self, columns: &[Column], cache: &mut OSUsers, locale: &UserLocale, dest: &mut Vec<Row>, src: &[File], depth: usize) {
for (index, file) in src.iter().enumerate() {
let row = Row {
@ -106,3 +106,24 @@ struct Row {
pub last: bool,
pub children: bool,
}
pub struct UserLocale {
pub time: locale::Time,
pub numeric: locale::Numeric,
}
impl UserLocale {
pub fn new() -> UserLocale {
UserLocale {
time: locale::Time::load_user_locale().unwrap_or_else(|_| locale::Time::default()),
numeric: locale::Numeric::load_user_locale().unwrap_or_else(|_| locale::Numeric::default()),
}
}
pub fn default() -> UserLocale {
UserLocale {
time: locale::Time::default(),
numeric: locale::Numeric::default(),
}
}
}

View File

@ -1,5 +1,5 @@
mod grid;
mod details;
pub mod details;
mod lines;
pub use self::grid::Grid;