mirror of
https://github.com/athityakumar/colorls.git
synced 2024-09-19 05:31:23 +02:00
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:
parent
8d08194262
commit
ac9734d879
1 changed files with 17 additions and 13 deletions
|
@ -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
|
||||
|
||||
|
|
Loading…
Reference in a new issue