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
This commit is contained in:
Chris Gorski 2023-10-03 14:20:32 -04:00
parent b1440bdf75
commit d3926e7a1a
2 changed files with 25 additions and 7 deletions

View file

@ -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 files 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() {

View file

@ -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(),
};