Jelajahi Sumber

fix: adjust change width calculations for hyperlink and classify

This fix adjusts the calculations used when hyperlink and classify
options are both specified on the command line, correcting
behavior that would cause columns to be misaligned.

Resolves #267
Chris Gorski 2 tahun lalu
induk
melakukan
d3926e7a1a
2 mengubah file dengan 25 tambahan dan 7 penghapusan
  1. 2 2
      src/output/file_name.rs
  2. 23 5
      src/output/grid.rs

+ 2 - 2
src/output/file_name.rs

@@ -314,7 +314,7 @@ impl<'a, 'dir, C: Colours> FileName<'a, 'dir, C> {
     /// The character to be displayed after a file when classifying is on, if
     /// the file’s type has one associated with it.
     #[cfg(unix)]
-    fn classify_char(&self, file: &File<'_>) -> Option<&'static str> {
+    pub(crate) fn classify_char(&self, file: &File<'_>) -> Option<&'static str> {
         if file.is_executable_file() {
             Some("*")
         } else if file.is_directory() {
@@ -331,7 +331,7 @@ impl<'a, 'dir, C: Colours> FileName<'a, 'dir, C> {
     }
 
     #[cfg(windows)]
-    fn classify_char(&self, file: &File<'_>) -> Option<&'static str> {
+    pub(crate) fn classify_char(&self, file: &File<'_>) -> Option<&'static str> {
         if file.is_directory() {
             Some("/")
         } else if file.is_link() {

+ 23 - 5
src/output/grid.rs

@@ -4,7 +4,7 @@ use term_grid as tg;
 
 use crate::fs::filter::FileFilter;
 use crate::fs::File;
-use crate::output::file_name::Options as FileStyle;
+use crate::output::file_name::{Classify, Options as FileStyle};
 use crate::output::file_name::{EmbedHyperlinks, ShowIcons};
 use crate::theme::Theme;
 
@@ -44,11 +44,29 @@ impl<'a> Render<'a> {
         self.filter.sort_files(&mut self.files);
         for file in &self.files {
             let filename = self.file_style.for_file(file, self.theme);
+
+            // Calculate classification width
+            let classification_width =
+                if let Classify::AddFileIndicators = filename.options.classify {
+                    match filename.classify_char(file) {
+                        Some(s) => s.len(),
+                        None => 0,
+                    }
+                } else {
+                    0
+                };
+
             let contents = filename.paint();
-            #[rustfmt::skip]
-            let width = match (filename.options.embed_hyperlinks, filename.options.show_icons) {
-                (EmbedHyperlinks::On, ShowIcons::On(spacing)) => filename.bare_width() + 1 + spacing,
-                (EmbedHyperlinks::On, ShowIcons::Off) => filename.bare_width(),
+            let width = match (
+                filename.options.embed_hyperlinks,
+                filename.options.show_icons,
+            ) {
+                (EmbedHyperlinks::On, ShowIcons::On(spacing)) => {
+                    filename.bare_width() + 1 + spacing + classification_width
+                }
+                (EmbedHyperlinks::On, ShowIcons::Off) => {
+                    filename.bare_width() + classification_width
+                }
                 (EmbedHyperlinks::Off, _) => *contents.width(),
             };