ソースを参照

Merge branch 'main' into improve-gen-test-dir

Signed-off-by: MartinFillon <114775771+MartinFillon@users.noreply.github.com>
MartinFillon 2 年 前
コミット
79cffaaf51
5 ファイル変更107 行追加60 行削除
  1. 0 7
      Cargo.lock
  2. 0 1
      Cargo.toml
  3. 91 33
      src/output/render/groups.rs
  4. 3 4
      src/output/table.rs
  5. 13 15
      src/output/time.rs

+ 0 - 7
Cargo.lock

@@ -363,7 +363,6 @@ dependencies = [
  "criterion",
  "git2",
  "glob",
- "lazy_static",
  "libc",
  "locale",
  "log",
@@ -555,12 +554,6 @@ dependencies = [
  "wasm-bindgen",
 ]
 
-[[package]]
-name = "lazy_static"
-version = "1.4.0"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
-
 [[package]]
 name = "libc"
 version = "0.2.149"

+ 0 - 1
Cargo.toml

@@ -74,7 +74,6 @@ name = "eza"
 ansiterm = "0.12.2"
 chrono = { version = "0.4.31", default-features = false, features = ["clock"] }
 glob = "0.3"
-lazy_static = "1.3"
 libc = "0.2"
 locale = "0.2"
 log = "0.4"

+ 91 - 33
src/output/render/groups.rs

@@ -2,6 +2,7 @@ use ansiterm::Style;
 use uzers::{Groups, Users};
 
 use crate::fs::fields as f;
+use crate::fs::fields::User;
 use crate::output::cell::TextCell;
 use crate::output::table::{GroupFormat, UserFormat};
 
@@ -12,6 +13,7 @@ pub trait Render {
         users: &U,
         user_format: UserFormat,
         group_format: GroupFormat,
+        file_user: Option<User>,
     ) -> TextCell;
 }
 
@@ -22,6 +24,7 @@ impl Render for Option<f::Group> {
         users: &U,
         user_format: UserFormat,
         group_format: GroupFormat,
+        file_user: Option<User>,
     ) -> TextCell {
         use uzers::os::unix::GroupExt;
 
@@ -53,20 +56,15 @@ impl Render for Option<f::Group> {
             UserFormat::Numeric => group.gid().to_string(),
         };
 
-        group_name = match group_format {
-            GroupFormat::Smart => {
-                if let Some(current_user) = users.get_user_by_uid(current_uid) {
-                    if current_user.name() == group.name() {
-                        ":".to_string()
-                    } else {
-                        group_name
+        if let GroupFormat::Smart = group_format {
+            if let Some(file_uid) = file_user {
+                if let Some(file_user) = users.get_user_by_uid(file_uid.0) {
+                    if file_user.name().to_string_lossy() == group.name().to_string_lossy() {
+                        group_name = ":".to_string();
                     }
-                } else {
-                    group_name
                 }
             }
-            GroupFormat::Regular => group_name,
-        };
+        }
 
         TextCell::paint(style, group_name)
     }
@@ -109,20 +107,28 @@ pub mod test {
         users.add_group(Group::new(100, "folk"));
 
         let group = Some(f::Group(100));
-        let expected = TextCell::paint_str(Fixed(81).normal(), "folk");
+        let file_user = Some(f::User(1000));
+        let expected = TextCell::paint_str(TestColours.not_yours(), "folk");
         assert_eq!(
             expected,
-            group.render(&TestColours, &users, UserFormat::Name, GroupFormat::Regular)
+            group.render(
+                &TestColours,
+                &users,
+                UserFormat::Name,
+                GroupFormat::Regular,
+                file_user
+            )
         );
 
-        let expected = TextCell::paint_str(Fixed(81).normal(), "100");
+        let expected = TextCell::paint_str(TestColours.not_yours(), "100");
         assert_eq!(
             expected,
             group.render(
                 &TestColours,
                 &users,
                 UserFormat::Numeric,
-                GroupFormat::Regular
+                GroupFormat::Regular,
+                file_user
             )
         );
     }
@@ -132,10 +138,17 @@ pub mod test {
         let users = MockUsers::with_current_uid(1000);
 
         let group = Some(f::Group(100));
-        let expected = TextCell::paint_str(Fixed(81).normal(), "100");
+        let file_user = Some(f::User(1000));
+        let expected = TextCell::paint_str(TestColours.not_yours(), "100");
         assert_eq!(
             expected,
-            group.render(&TestColours, &users, UserFormat::Name, GroupFormat::Regular)
+            group.render(
+                &TestColours,
+                &users,
+                UserFormat::Name,
+                GroupFormat::Regular,
+                file_user
+            )
         );
         assert_eq!(
             expected,
@@ -143,7 +156,8 @@ pub mod test {
                 &TestColours,
                 &users,
                 UserFormat::Numeric,
-                GroupFormat::Regular
+                GroupFormat::Regular,
+                file_user
             )
         );
     }
@@ -155,10 +169,17 @@ pub mod test {
         users.add_group(Group::new(100, "folk"));
 
         let group = Some(f::Group(100));
-        let expected = TextCell::paint_str(Fixed(80).normal(), "folk");
+        let file_user = Some(f::User(2));
+        let expected = TextCell::paint_str(TestColours.yours(), "folk");
         assert_eq!(
             expected,
-            group.render(&TestColours, &users, UserFormat::Name, GroupFormat::Regular)
+            group.render(
+                &TestColours,
+                &users,
+                UserFormat::Name,
+                GroupFormat::Regular,
+                file_user
+            )
         )
     }
 
@@ -171,24 +192,33 @@ pub mod test {
         users.add_group(test_group);
 
         let group = Some(f::Group(100));
-        let expected = TextCell::paint_str(Fixed(80).normal(), "folk");
+        let file_user = Some(f::User(2));
+        let expected = TextCell::paint_str(TestColours.yours(), "folk");
         assert_eq!(
             expected,
-            group.render(&TestColours, &users, UserFormat::Name, GroupFormat::Regular)
+            group.render(
+                &TestColours,
+                &users,
+                UserFormat::Name,
+                GroupFormat::Regular,
+                file_user
+            )
         )
     }
 
     #[test]
     fn overflow() {
         let group = Some(f::Group(2_147_483_648));
-        let expected = TextCell::paint_str(Fixed(81).normal(), "2147483648");
+        let file_user = Some(f::User(1000));
+        let expected = TextCell::paint_str(TestColours.not_yours(), "2147483648");
         assert_eq!(
             expected,
             group.render(
                 &TestColours,
                 &MockUsers::with_current_uid(0),
                 UserFormat::Numeric,
-                GroupFormat::Regular
+                GroupFormat::Regular,
+                file_user
             )
         );
     }
@@ -196,33 +226,61 @@ pub mod test {
     #[test]
     fn smart() {
         let mut users = MockUsers::with_current_uid(1000);
-        users.add_user(User::new(1000, "user", 110));
+        users.add_user(User::new(1000, "user", 100));
+        users.add_user(User::new(1001, "http", 101));
         users.add_group(Group::new(100, "user"));
         users.add_group(Group::new(101, "http"));
 
-        let same_group = Some(f::Group(100));
-        let expected = TextCell::paint_str(Fixed(81).normal(), ":");
+        let user_group = Some(f::Group(100));
+        let user_file = Some(f::User(1000));
+        let expected = TextCell::paint_str(TestColours.yours(), ":");
         assert_eq!(
             expected,
-            same_group.render(&TestColours, &users, UserFormat::Name, GroupFormat::Smart)
+            user_group.render(
+                &TestColours,
+                &users,
+                UserFormat::Name,
+                GroupFormat::Smart,
+                user_file
+            )
         );
 
-        let expected = TextCell::paint_str(Fixed(81).normal(), ":");
+        let expected = TextCell::paint_str(TestColours.yours(), ":");
         assert_eq!(
             expected,
-            same_group.render(
+            user_group.render(
                 &TestColours,
                 &users,
                 UserFormat::Numeric,
-                GroupFormat::Smart
+                GroupFormat::Smart,
+                user_file
             )
         );
 
         let http_group = Some(f::Group(101));
-        let expected = TextCell::paint_str(Fixed(81).normal(), "http");
+        let expected = TextCell::paint_str(TestColours.not_yours(), "http");
         assert_eq!(
             expected,
-            http_group.render(&TestColours, &users, UserFormat::Name, GroupFormat::Smart)
+            http_group.render(
+                &TestColours,
+                &users,
+                UserFormat::Name,
+                GroupFormat::Smart,
+                user_file
+            )
+        );
+
+        let http_file = Some(f::User(1001));
+        let expected = TextCell::paint_str(TestColours.not_yours(), ":");
+        assert_eq!(
+            expected,
+            http_group.render(
+                &TestColours,
+                &users,
+                UserFormat::Name,
+                GroupFormat::Smart,
+                http_file
+            )
         );
     }
 }

+ 3 - 4
src/output/table.rs

@@ -5,8 +5,8 @@ use std::sync::{Mutex, MutexGuard};
 
 use chrono::prelude::*;
 
-use lazy_static::lazy_static;
 use log::*;
+use once_cell::sync::Lazy;
 #[cfg(unix)]
 use uzers::UsersCache;
 
@@ -352,9 +352,7 @@ impl Environment {
     }
 }
 
-lazy_static! {
-    static ref ENVIRONMENT: Environment = Environment::load_all();
-}
+static ENVIRONMENT: Lazy<Environment> = Lazy::new(Environment::load_all);
 
 pub struct Table<'a> {
     columns: Vec<Column>,
@@ -476,6 +474,7 @@ impl<'a> Table<'a> {
                 &*self.env.lock_users(),
                 self.user_format,
                 self.group_format,
+                file.user(),
             ),
             #[cfg(unix)]
             Column::SecurityContext => file.security_context().render(self.theme),

+ 13 - 15
src/output/time.rs

@@ -2,7 +2,7 @@
 
 use chrono::prelude::*;
 use core::cmp::max;
-use lazy_static::lazy_static;
+use once_cell::sync::Lazy;
 use std::time::Duration;
 use unicode_width::UnicodeWidthStr;
 
@@ -119,22 +119,20 @@ fn custom(time: &DateTime<FixedOffset>, fmt: &str) -> String {
     time.format(fmt).to_string()
 }
 
-lazy_static! {
+static CURRENT_YEAR: Lazy<i32> = Lazy::new(|| Local::now().year());
 
-    static ref CURRENT_YEAR: i32 = Local::now().year();
+static LOCALE: Lazy<locale::Time> =
+    Lazy::new(|| locale::Time::load_user_locale().unwrap_or_else(|_| locale::Time::english()));
 
-    static ref LOCALE: locale::Time = {
-        locale::Time::load_user_locale()
-               .unwrap_or_else(|_| locale::Time::english())
-    };
-
-    static ref MAX_MONTH_WIDTH: usize = {
-        // Some locales use a three-character wide month name (Jan to Dec);
-        // others vary between three to four (1月 to 12月, juil.). We check each month width
-        // to detect the longest and set the output format accordingly.
-        (0..11).map(|i| UnicodeWidthStr::width(&*LOCALE.short_month_name(i))).max().unwrap()
-    };
-}
+static MAX_MONTH_WIDTH: Lazy<usize> = Lazy::new(|| {
+    // Some locales use a three-character wide month name (Jan to Dec);
+    // others vary between three to four (1月 to 12月, juil.). We check each month width
+    // to detect the longest and set the output format accordingly.
+    (0..11)
+        .map(|i| UnicodeWidthStr::width(&*LOCALE.short_month_name(i)))
+        .max()
+        .unwrap()
+});
 
 #[cfg(test)]
 mod test {