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

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 години
родител
ревизия
d3926e7a1a
променени са 2 файла, в които са добавени 25 реда и са изтрити 7 реда
  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 character to be displayed after a file when classifying is on, if
     /// the file’s type has one associated with it.
     /// the file’s type has one associated with it.
     #[cfg(unix)]
     #[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() {
         if file.is_executable_file() {
             Some("*")
             Some("*")
         } else if file.is_directory() {
         } else if file.is_directory() {
@@ -331,7 +331,7 @@ impl<'a, 'dir, C: Colours> FileName<'a, 'dir, C> {
     }
     }
 
 
     #[cfg(windows)]
     #[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() {
         if file.is_directory() {
             Some("/")
             Some("/")
         } else if file.is_link() {
         } 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::filter::FileFilter;
 use crate::fs::File;
 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::output::file_name::{EmbedHyperlinks, ShowIcons};
 use crate::theme::Theme;
 use crate::theme::Theme;
 
 
@@ -44,11 +44,29 @@ impl<'a> Render<'a> {
         self.filter.sort_files(&mut self.files);
         self.filter.sort_files(&mut self.files);
         for file in &self.files {
         for file in &self.files {
             let filename = self.file_style.for_file(file, self.theme);
             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();
             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(),
                 (EmbedHyperlinks::Off, _) => *contents.width(),
             };
             };