Преглед на файлове

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.
Ben S преди 11 години
родител
ревизия
2906b8676a
променени са 6 файла, в които са добавени 46 реда и са изтрити 50 реда
  1. 5 24
      Cargo.lock
  2. 3 5
      Cargo.toml
  3. 14 15
      src/file.rs
  4. 0 3
      src/main.rs
  5. 23 2
      src/output/details.rs
  6. 1 1
      src/output/mod.rs

+ 5 - 24
Cargo.lock

@@ -3,11 +3,10 @@ name = "exa"
 version = "0.1.0"
 version = "0.1.0"
 dependencies = [
 dependencies = [
  "ansi_term 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)",
  "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)",
  "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)",
  "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)",
  "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)",
  "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)",
  "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]]
 [[package]]
 name = "datetime"
 name = "datetime"
-version = "0.1.2"
-source = "git+https://github.com/ogham/rust-datetime.git#c108628eb3519535821bc7907f2305643465acf5"
-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"
-version = "0.1.2"
+version = "0.1.3"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 dependencies = [
 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)",
  "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 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)",
  "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]]
 [[package]]
 name = "gcc"
 name = "gcc"
 version = "0.1.7"
 version = "0.1.7"
@@ -124,7 +105,7 @@ dependencies = [
 
 
 [[package]]
 [[package]]
 name = "locale"
 name = "locale"
-version = "0.1.1"
+version = "0.1.2"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 
 
 [[package]]
 [[package]]

+ 3 - 5
Cargo.toml

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

+ 14 - 15
src/file.rs

@@ -8,14 +8,13 @@ use ansi_term::Colour::{Red, Green, Yellow, Blue, Purple, Cyan, Fixed};
 
 
 use users::Users;
 use users::Users;
 
 
-use pad::Alignment;
-
 use locale;
 use locale;
+use output::details::UserLocale;
 
 
 use number_prefix::{binary_prefix, decimal_prefix, Prefixed, Standalone, PrefixNames};
 use number_prefix::{binary_prefix, decimal_prefix, Prefixed, Standalone, PrefixNames};
 
 
-use datetime;
 use datetime::local::{LocalDateTime, DatePiece};
 use datetime::local::{LocalDateTime, DatePiece};
+use datetime::format::{DateFormat};
 
 
 use column::{Column, Cell};
 use column::{Column, Cell};
 use column::Column::*;
 use column::Column::*;
@@ -99,14 +98,14 @@ impl<'a> File<'a> {
     }
     }
 
 
     /// Get the data for a column, formatted as a coloured string.
     /// 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 {
         match *column {
             Permissions     => self.permissions_string(),
             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(),
             Inode           => self.inode(),
-            Blocks          => self.blocks(locale),
+            Blocks          => self.blocks(&locale.numeric),
             User            => self.user(users_cache),
             User            => self.user(users_cache),
             Group           => self.group(users_cache),
             Group           => self.group(users_cache),
             GitStatus       => self.git_status(),
             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.
         // Need to convert these values from milliseconds into seconds.
         let time_in_seconds = match time_type {
         let time_in_seconds = match time_type {
@@ -317,13 +316,13 @@ impl<'a> File<'a> {
         let date = LocalDateTime::at(time_in_seconds);
         let date = LocalDateTime::at(time_in_seconds);
 
 
         let format = if date.year() == current_year {
         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 {
             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.
     /// 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::Style::Plain;
     pub use ansi_term::Colour::Yellow;
     pub use ansi_term::Colour::Yellow;
 
 
-    pub use locale;
+    pub use output::details::UserLocale;
 
 
     #[test]
     #[test]
     fn extension() {
     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 {
     mod users {

+ 0 - 3
src/main.rs

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

+ 23 - 2
src/output/details.rs

@@ -25,7 +25,7 @@ impl Details {
         // padding the fields during output.
         // padding the fields during output.
 
 
         let columns = self.columns.for_dir(dir);
         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 cache = OSUsers::empty_cache();
         let mut table = Vec::new();
         let mut table = Vec::new();
         self.get_files(&columns[], &mut cache, &locale, &mut table, files, 0);
         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() {
         for (index, file) in src.iter().enumerate() {
 
 
             let row = Row {
             let row = Row {
@@ -106,3 +106,24 @@ struct Row {
     pub last: bool,
     pub last: bool,
     pub children: 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(),
+        }
+    }
+}

+ 1 - 1
src/output/mod.rs

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