Sfoglia il codice sorgente

Merge branch 'localisation'

Ben S 11 anni fa
parent
commit
113df66408
7 ha cambiato i file con 90 aggiunte e 80 eliminazioni
  1. 21 34
      Cargo.lock
  2. 4 5
      Cargo.toml
  3. 33 26
      src/file.rs
  4. 3 9
      src/main.rs
  5. 1 1
      src/options.rs
  6. 27 4
      src/output/details.rs
  7. 1 1
      src/output/mod.rs

+ 21 - 34
Cargo.lock

@@ -2,11 +2,11 @@
 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)",
- "getopts 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)",
+ "ansi_term 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
+ "datetime 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)",
+ "getopts 0.2.4 (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.3 (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)",
@@ -15,7 +15,7 @@ dependencies = [
 
 [[package]]
 name = "ansi_term"
-version = "0.4.5"
+version = "0.4.6"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 dependencies = [
  "regex 0.1.14 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -29,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"
-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.4"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 dependencies = [
+ "locale 0.1.3 (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_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"
@@ -63,7 +45,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
 
 [[package]]
 name = "getopts"
-version = "0.2.3"
+version = "0.2.4"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 dependencies = [
  "log 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -75,7 +57,7 @@ version = "0.1.17"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 dependencies = [
  "bitflags 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
- "libgit2-sys 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)",
+ "libgit2-sys 0.1.13 (registry+https://github.com/rust-lang/crates.io-index)",
  "url 0.2.20 (registry+https://github.com/rust-lang/crates.io-index)",
 ]
 
@@ -86,13 +68,13 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
 
 [[package]]
 name = "libgit2-sys"
-version = "0.1.12"
+version = "0.1.13"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 dependencies = [
  "libssh2-sys 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)",
  "libz-sys 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
- "openssl-sys 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)",
- "pkg-config 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)",
+ "openssl-sys 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)",
+ "pkg-config 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
 ]
 
 [[package]]
@@ -109,7 +91,7 @@ version = "0.1.5"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 dependencies = [
  "libz-sys 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
- "openssl-sys 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)",
+ "openssl-sys 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)",
  "pkg-config 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)",
 ]
 
@@ -121,6 +103,11 @@ dependencies = [
  "pkg-config 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)",
 ]
 
+[[package]]
+name = "locale"
+version = "0.1.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+
 [[package]]
 name = "log"
 version = "0.2.3"
@@ -143,12 +130,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
 
 [[package]]
 name = "openssl-sys"
-version = "0.3.3"
+version = "0.3.6"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 dependencies = [
  "gcc 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)",
  "libressl-pnacl-sys 2.1.4 (registry+https://github.com/rust-lang/crates.io-index)",
- "pkg-config 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
+ "pkg-config 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
 ]
 
 [[package]]
@@ -163,7 +150,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
 
 [[package]]
 name = "pkg-config"
-version = "0.2.0"
+version = "0.2.1"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 
 [[package]]

+ 4 - 5
Cargo.toml

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

+ 33 - 26
src/file.rs

@@ -8,12 +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::*;
@@ -97,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) -> 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),
-            Timestamp(t, y) => self.timestamp(t, y),
-            HardLinks       => self.hard_links(),
+            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(),
+            Blocks          => self.blocks(&locale.numeric),
             User            => self.user(users_cache),
             Group           => self.group(users_cache),
             GitStatus       => self.git_status(),
@@ -202,9 +203,9 @@ impl<'a> File<'a> {
     }
 
     /// This file's number of hard links as a coloured string.
-    fn hard_links(&self) -> Cell {
+    fn hard_links(&self, locale: &locale::Numeric) -> Cell {
         let style = if self.has_multiple_links() { Red.on(Yellow) } else { Red.normal() };
-        Cell::paint(style, &*self.stat.unstable.nlink.to_string())
+        Cell::paint(style, &locale.format_int(self.stat.unstable.nlink as isize)[])
     }
 
     /// Whether this is a regular file with more than one link.
@@ -222,9 +223,9 @@ impl<'a> File<'a> {
     }
 
     /// This file's number of filesystem blocks (if available) as a coloured string.
-    fn blocks(&self) -> Cell {
+    fn blocks(&self, locale: &locale::Numeric) -> Cell {
         if self.stat.kind == io::FileType::RegularFile || self.stat.kind == io::FileType::Symlink {
-            Cell::paint(Cyan.normal(), &*self.stat.unstable.blocks.to_string())
+            Cell::paint(Cyan.normal(), &locale.format_int(self.stat.unstable.blocks as isize)[])
         }
         else {
             Cell { text: GREY.paint("-").to_string(), length: 1 }
@@ -277,7 +278,7 @@ impl<'a> File<'a> {
     /// some filesystems, I've never looked at one of those numbers and gained
     /// any information from it, so by emitting "-" instead, the table is less
     /// cluttered with numbers.
-    fn file_size(&self, size_format: SizeFormat) -> Cell {
+    fn file_size(&self, size_format: SizeFormat, locale: &locale::Numeric) -> Cell {
         if self.stat.kind == io::FileType::Directory {
             Cell { text: GREY.paint("-").to_string(), length: 1 }
         }
@@ -285,13 +286,13 @@ impl<'a> File<'a> {
             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 Cell::paint(Green.bold(), &*self.stat.size.to_string())
+                SizeFormat::JustBytes    => return Cell::paint(Green.bold(), &locale.format_int(self.stat.size as isize)[]),
             };
 
             match result {
                 Standalone(bytes) => Cell::paint(Green.bold(), &*bytes.to_string()),
                 Prefixed(prefix, n) => {
-                    let number = if n < 10f64 { format!("{:.1}", n) } else { format!("{:.0}", n) };
+                    let number = if n < 10f64 { locale.format_float(n, 1) } else { locale.format_int(n as isize) };
                     let symbol = prefix.symbol();
 
                     Cell {
@@ -303,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 {
@@ -315,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.
@@ -445,6 +446,8 @@ pub mod test {
     pub use ansi_term::Style::Plain;
     pub use ansi_term::Colour::Yellow;
 
+    pub use output::details::UserLocale;
+
     #[test]
     fn extension() {
         assert_eq!(Some("dat".to_string()), super::ext("fester.dat"))
@@ -487,6 +490,10 @@ pub mod test {
         }
     }
 
+    pub fn dummy_locale() -> UserLocale {
+        UserLocale::default()
+    }
+
     mod users {
         use super::*;
 
@@ -501,7 +508,7 @@ pub mod test {
             users.add_user(User { uid: 1000, name: "enoch".to_string(), primary_group: 100 });
 
             let cell = Cell::paint(Yellow.bold(), "enoch");
-            assert_eq!(cell, file.display(&Column::User, &mut users))
+            assert_eq!(cell, file.display(&Column::User, &mut users, &dummy_locale()))
         }
 
         #[test]
@@ -514,7 +521,7 @@ pub mod test {
             let mut users = MockUsers::with_current_uid(1000);
 
             let cell = Cell::paint(Yellow.bold(), "1000");
-            assert_eq!(cell, file.display(&Column::User, &mut users))
+            assert_eq!(cell, file.display(&Column::User, &mut users, &dummy_locale()))
         }
 
         #[test]
@@ -528,7 +535,7 @@ pub mod test {
             users.add_user(User { uid: 1000, name: "enoch".to_string(), primary_group: 100 });
 
             let cell = Cell::paint(Plain, "enoch");
-            assert_eq!(cell, file.display(&Column::User, &mut users))
+            assert_eq!(cell, file.display(&Column::User, &mut users, &dummy_locale()))
         }
 
         #[test]
@@ -541,7 +548,7 @@ pub mod test {
             let mut users = MockUsers::with_current_uid(3);
 
             let cell = Cell::paint(Plain, "1000");
-            assert_eq!(cell, file.display(&Column::User, &mut users))
+            assert_eq!(cell, file.display(&Column::User, &mut users, &dummy_locale()))
         }
     }
 
@@ -559,7 +566,7 @@ pub mod test {
             users.add_group(Group { gid: 100, name: "folk".to_string(), members: vec![] });
 
             let cell = Cell::paint(Plain, "folk");
-            assert_eq!(cell, file.display(&Column::Group, &mut users))
+            assert_eq!(cell, file.display(&Column::Group, &mut users, &dummy_locale()))
         }
 
         #[test]
@@ -572,7 +579,7 @@ pub mod test {
             let mut users = MockUsers::with_current_uid(3);
 
             let cell = Cell::paint(Plain, "100");
-            assert_eq!(cell, file.display(&Column::Group, &mut users))
+            assert_eq!(cell, file.display(&Column::Group, &mut users, &dummy_locale()))
         }
 
         #[test]
@@ -587,7 +594,7 @@ pub mod test {
             users.add_group(Group { gid: 100, name: "folk".to_string(), members: vec![] });
 
             let cell = Cell::paint(Yellow.bold(), "folk");
-            assert_eq!(cell, file.display(&Column::Group, &mut users))
+            assert_eq!(cell, file.display(&Column::Group, &mut users, &dummy_locale()))
         }
 
         #[test]
@@ -602,7 +609,7 @@ pub mod test {
             users.add_group(Group { gid: 100, name: "folk".to_string(), members: vec![ "eve".to_string() ] });
 
             let cell = Cell::paint(Yellow.bold(), "folk");
-            assert_eq!(cell, file.display(&Column::Group, &mut users))
+            assert_eq!(cell, file.display(&Column::Group, &mut users, &dummy_locale()))
         }
     }
 }

+ 3 - 9
src/main.rs

@@ -1,11 +1,9 @@
-#![feature(collections, core, env, io, libc, os, path, plugin, std_misc)]
-
-#[plugin] #[no_link]
-extern crate datetime_macros;
+#![feature(collections, core, env, io, libc, path, plugin, std_misc)]
 
 extern crate ansi_term;
 extern crate datetime;
 extern crate getopts;
+extern crate locale;
 extern crate natord;
 extern crate number_prefix;
 extern crate pad;
@@ -140,7 +138,7 @@ impl<'a> Exa<'a> {
 }
 
 fn main() {
-    let args = args();
+    let args: Vec<String> = env::args().collect();
 
     match Options::getopts(args.tail()) {
         Ok((options, paths)) => {
@@ -155,7 +153,3 @@ fn main() {
         },
     };
 }
-
-fn args() -> Vec<String> {
-    env::args().map(|arg| arg.to_string_lossy().into_owned()).collect()
-}

+ 1 - 1
src/options.rs

@@ -32,7 +32,7 @@ pub struct FileFilter {
     sort_field: SortField,
 }
 
-#[derive(PartialEq, Copy, Debug)]
+#[derive(PartialEq, Debug, Copy)]
 pub enum View {
     Details(Details),
     Lines,

+ 27 - 4
src/output/details.rs

@@ -4,6 +4,7 @@ use file::{File, GREY};
 use options::{Columns, FileFilter};
 use users::OSUsers;
 
+use locale;
 use ansi_term::Style::Plain;
 
 #[derive(PartialEq, Debug, Copy)]
@@ -24,9 +25,10 @@ impl Details {
         // padding the fields during output.
 
         let columns = self.columns.for_dir(dir);
+        let locale = UserLocale::new();
         let mut cache = OSUsers::empty_cache();
         let mut table = Vec::new();
-        self.get_files(&columns[], &mut cache, &mut table, files, 0);
+        self.get_files(&columns[], &mut cache, &locale, &mut table, files, 0);
 
         if self.header {
             let row = Row {
@@ -73,12 +75,12 @@ impl Details {
         }
     }
 
-    fn get_files(&self, columns: &[Column], cache: &mut OSUsers, 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 {
                 depth: depth,
-                cells: columns.iter().map(|c| file.display(c, cache)).collect(),
+                cells: columns.iter().map(|c| file.display(c, cache, locale)).collect(),
                 name:  file.file_name_view(),
                 last:  index == src.len() - 1,
                 children: file.this.is_some(),
@@ -90,7 +92,7 @@ impl Details {
                 if let Some(ref dir) = file.this {
                     let mut files = dir.files(true);
                     self.filter.transform_files(&mut files);
-                    self.get_files(columns, cache, dest, files.as_slice(), depth + 1);
+                    self.get_files(columns, cache, locale, dest, files.as_slice(), depth + 1);
                 }
             }
         }
@@ -104,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::english()),
+            numeric: locale::Numeric::load_user_locale().unwrap_or_else(|_| locale::Numeric::english()),
+        }
+    }
+
+    pub fn default() -> UserLocale {
+        UserLocale {
+            time: locale::Time::english(),
+            numeric: locale::Numeric::english(),
+        }
+    }
+}

+ 1 - 1
src/output/mod.rs

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