Просмотр исходного кода

Merge branch 'main' into feature/add-recursive-dir-size

Signed-off-by: Robert Minsk <38872275+cfxegbert@users.noreply.github.com>
Robert Minsk 2 лет назад
Родитель
Сommit
423f4b9bb9

+ 21 - 20
src/fs/file.rs

@@ -461,12 +461,18 @@ impl<'dir> File<'dir> {
     /// This actual size the file takes up on disk, in bytes.
     #[cfg(unix)]
     pub fn blocksize(&self) -> f::Blocksize {
-        if self.is_file() || self.is_link() {
+        if self.deref_links && self.is_link() {
+            match self.link_target() {
+                FileTarget::Ok(f) => f.blocksize(),
+                _ => f::Blocksize::None,
+            }
+        } else if self.is_file() {
             // Note that metadata.blocks returns the number of blocks
             // for 512 byte blocks according to the POSIX standard
             // even though the physical block size may be different.
             f::Blocksize::Some(self.metadata.blocks() * 512)
         } else {
+            // directory or symlinks
             f::Blocksize::None
         }
     }
@@ -498,25 +504,23 @@ impl<'dir> File<'dir> {
 
     /// This file’s size, if it’s a regular file.
     ///
-    /// For directories, no size is given. Although they do have a size on
-    /// some filesystems, I’ve never looked at one of those numbers and gained
-    /// any information from it. So it’s going to be hidden instead.
+    /// For directories, the recursive size or no size is given depending on
+    /// flags. Although they do have a size on some filesystems, I’ve never
+    /// looked at one of those numbers and gained any information from it.
     ///
     /// Block and character devices return their device IDs, because they
     /// usually just have a file size of zero.
     ///
     /// Links will return the size of their target (recursively through other
-    /// links) if dereferencing is enabled, otherwise the size of the link
-    /// itself.
+    /// links) if dereferencing is enabled, otherwise None.
     #[cfg(unix)]
     pub fn size(&self) -> f::Size {
-        if self.is_link() {
-            let target = self.link_target();
-            if let FileTarget::Ok(target) = target {
-                return target.size();
+        if self.deref_links && self.is_link() {
+            match self.link_target() {
+                FileTarget::Ok(f) => f.size(),
+                _ => f::Size::None,
             }
-        }
-        if self.is_directory() {
+        } else if self.is_directory() {
             self.recursive_size
                 .map_or(f::Size::None, |s| f::Size::Some(s))
         } else if self.is_char_device() || self.is_block_device() {
@@ -527,20 +531,17 @@ impl<'dir> File<'dir> {
             // the "as u32" cast are not needed.  We turn off the warning to
             // allow it to compile cleanly on Linux.
             #[allow(trivial_numeric_casts)]
-            #[allow(clippy::unnecessary_cast)]
-            #[allow(clippy::useless_conversion)]
+            #[allow(clippy::unnecessary_cast, clippy::useless_conversion)]
             f::Size::DeviceIDs(f::DeviceIDs {
                 // SAFETY: Calling libc function to decompose the device_id
                 major: unsafe { libc::major(device_id.try_into().unwrap()) } as u32,
                 minor: unsafe { libc::minor(device_id.try_into().unwrap()) } as u32,
             })
-        } else if self.is_link() && self.deref_links {
-            match self.link_target() {
-                FileTarget::Ok(f) => f.size(),
-                _ => f::Size::None,
-            }
-        } else {
+        } else if self.is_file() {
             f::Size::Some(self.metadata.len())
+        } else {
+            // symlink
+            f::Size::None
         }
     }
 

+ 15 - 11
src/output/grid.rs

@@ -56,22 +56,26 @@ impl<'a> Render<'a> {
                     0
                 };
 
+            let space_filename_offset = if file.name.contains(' ') || file.name.contains('\'') {
+                2
+            } else {
+                0
+            };
+
             let contents = filename.paint();
+            #[rustfmt::skip]
             let width = match (
                 filename.options.embed_hyperlinks,
                 filename.options.show_icons,
             ) {
-                #[rustfmt::skip]
-                (EmbedHyperlinks::On, ShowIcons::Always(spacing)
-                | ShowIcons::Automatic(spacing))                  => filename.bare_width() + classification_width + 1 + (spacing as usize),
-                (EmbedHyperlinks::On, ShowIcons::Never) => {
-                    filename.bare_width() + classification_width
-                }
-                (
-                    EmbedHyperlinks::Off,
-                    ShowIcons::Always(spacing) | ShowIcons::Automatic(spacing),
-                ) => filename.bare_width() + 1 + (spacing as usize),
-                (EmbedHyperlinks::Off, _) => *contents.width(),
+                ( EmbedHyperlinks::On, ShowIcons::Always(spacing) | ShowIcons::Automatic(spacing) )
+                    => filename.bare_width() + classification_width + 1 + (spacing as usize) + space_filename_offset,
+                ( EmbedHyperlinks::On, ShowIcons::Never )
+                    => filename.bare_width() + classification_width + space_filename_offset,
+                ( EmbedHyperlinks::Off, ShowIcons::Always(spacing) | ShowIcons::Automatic(spacing) )
+                    => filename.bare_width() + 1 + (spacing as usize) + space_filename_offset,
+                ( EmbedHyperlinks::Off, _ )
+                    => *contents.width(),
             };
 
             grid.add(tg::Cell {

+ 8 - 3
src/output/grid_details.rs

@@ -162,11 +162,16 @@ impl<'a> Render<'a> {
             .map(|file| {
                 let filename = self.file_style.for_file(file, self.theme);
                 let contents = filename.paint();
+                let space_filename_offset = if file.name.contains(' ') || file.name.contains('\'') {
+                    2
+                } else {
+                    0
+                };
                 #[rustfmt::skip]
                 let width = match (filename.options.embed_hyperlinks, filename.options.show_icons) {
-                    (EmbedHyperlinks::On, ShowIcons::Automatic(spacing)) => filename.bare_width() + 1 + (spacing as usize),
-                    (EmbedHyperlinks::On, ShowIcons::Always(spacing)) => filename.bare_width() + 1 + (spacing as usize),
-                    (EmbedHyperlinks::On, ShowIcons::Never) => filename.bare_width(),
+                    (EmbedHyperlinks::On, ShowIcons::Automatic(spacing)) => filename.bare_width() + 1 + (spacing as usize) + space_filename_offset,
+                    (EmbedHyperlinks::On, ShowIcons::Always(spacing)) => filename.bare_width() + 1 + (spacing as usize) + space_filename_offset,
+                    (EmbedHyperlinks::On, ShowIcons::Never) => filename.bare_width() + space_filename_offset,
                     (EmbedHyperlinks::Off, _) => *contents.width(),
                 };
 

+ 2 - 0
tests/cmd/long_blocksize_dereference_symlink.toml

@@ -0,0 +1,2 @@
+bin.name = "eza"
+args = "tests/itest/vagrant/debug --long --blocksize --dereference"

+ 0 - 0
tests/cmd/long_blocksize_dereference_symlink_nix.stderr


+ 3 - 0
tests/cmd/long_blocksize_dereference_symlink_nix.stdout

@@ -0,0 +1,3 @@
+.rw-r--r-- 0 ariasuni 20 oct.  15:38 a
+lrw-r--r-- 0 ariasuni 20 oct.  15:38 symlink -> a
+---------- - -        -              symlink-broken -> ./b

+ 2 - 0
tests/cmd/long_blocksize_symlink.toml

@@ -0,0 +1,2 @@
+bin.name = "eza"
+args = "tests/itest/vagrant/debug --long --blocksize"

+ 0 - 0
tests/cmd/long_blocksize_symlink_nix.stderr


+ 3 - 0
tests/cmd/long_blocksize_symlink_nix.stdout

@@ -0,0 +1,3 @@
+.rw-r--r-- 0 ariasuni 20 oct.  15:38 a
+lrwxrwxrwx - ariasuni 17 oct.  18:35 symlink -> a
+lrwxrwxrwx - ariasuni 20 oct.  15:38 symlink-broken -> ./b

+ 3 - 1
tests/cmd/recursive_long_unix.stdout

@@ -33,7 +33,9 @@ dev
 log
 
 tests/itest/vagrant/debug:
-symlinking -> a
+a
+symlink -> a
+symlink-broken -> ./b
 
 tests/itest/vagrant/dev:
 main.bf

+ 3 - 1
tests/cmd/recursive_unix.stdout

@@ -33,7 +33,9 @@ dev
 log
 
 tests/itest/vagrant/debug:
-symlinking -> a
+a
+symlink -> a
+symlink-broken -> ./b
 
 tests/itest/vagrant/dev:
 main.bf

+ 3 - 1
tests/cmd/tree_long_unix.stdout

@@ -24,7 +24,9 @@ tests/itest
 ├── q
 └── vagrant
    ├── debug
-   │  └── symlinking -> a
+   │  ├── a
+   │  ├── symlink -> a
+   │  └── symlink-broken -> ./b
    ├── dev
    │  └── main.bf
    └── log

+ 3 - 1
tests/cmd/tree_unix.stdout

@@ -24,7 +24,9 @@ tests/itest
 ├── q
 └── vagrant
    ├── debug
-   │  └── symlinking -> a
+   │  ├── a
+   │  ├── symlink -> a
+   │  └── symlink-broken -> ./b
    ├── dev
    │  └── main.bf
    └── log

+ 0 - 0
tests/itest/vagrant/debug/a


+ 0 - 0
tests/itest/vagrant/debug/symlinking → tests/itest/vagrant/debug/symlink


+ 1 - 0
tests/itest/vagrant/debug/symlink-broken

@@ -0,0 +1 @@
+./b