Align number of link column in long listing

This fixes the misalignment of files in a long listing when some files have a
different number of characters in the links column.

Instead of iterating three times over the @contents array, in order to compute
the sizes of the variant columns, use a single loop to compute the maximum size
for each column in a one pass.
This commit is contained in:
Claudio Bley 2019-07-07 21:36:23 +02:00
parent 8d08194262
commit ac9734d879

View file

@ -93,10 +93,8 @@ module ColorLS
end
@total_content_length = @contents.length
return @contents unless @long
init_column_lengths
init_user_lengths
init_group_lengths
@contents
end
@ -105,16 +103,20 @@ module ColorLS
@contents.keep_if { |x| !x.start_with? '.' } unless @all || @almost_all
end
def init_user_lengths
@userlength = @contents.map do |c|
c.owner.length
end.max
end
def init_column_lengths
return unless @long
def init_group_lengths
@grouplength = @contents.map do |c|
c.group.length
end.max
maxlink = maxuser = maxgroup = 0
@contents.each do |c|
maxlink = c.nlink if c.nlink > maxlink
maxuser = c.owner.length if c.owner.length > maxuser
maxgroup = c.group.length if c.group.length > maxgroup
end
@linklength = maxlink.digits.length
@userlength = maxuser
@grouplength = maxgroup
end
def filter_contents
@ -257,7 +259,9 @@ module ColorLS
def long_info(content)
return '' unless @long
[mode_info(content.stats), content.nlink, user_info(content), group_info(content.group),
links = content.nlink.to_s.rjust(@linklength)
[mode_info(content.stats), links, user_info(content), group_info(content.group),
size_info(content.size), mtime_info(content.mtime)].join(' ')
end