Merge pull request #589 from ayushpoddar/size-in-bytes

add support for --non-human-readable flag
This commit is contained in:
Claudio Bley 2023-04-26 21:39:30 +02:00 committed by GitHub
commit d1e28edcd0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 52 additions and 7 deletions

View file

@ -113,6 +113,11 @@ Man pages have been added. Checkout `man colorls`.
![image](https://user-images.githubusercontent.com/17109060/32149084-9eb2a416-bd25-11e7-8fb7-a9d336c6e038.png) ![image](https://user-images.githubusercontent.com/17109060/32149084-9eb2a416-bd25-11e7-8fb7-a9d336c6e038.png)
- Using `--non-human-readable` with `-l` :
- This will print the file sizes in bytes (non-human readable format)
![image](https://user-images.githubusercontent.com/19269206/234581461-1e1fdd90-a362-4cea-ab82-5ca360746be8.png)
# Installation # Installation
[(Back to top)](#table-of-contents) [(Back to top)](#table-of-contents)

View file

@ -24,7 +24,9 @@ module ColorLS
@screen_width @screen_width
end end
class Core class Core # rubocop:disable Metrics/ClassLength
MIN_SIZE_CHARS = 4
def initialize(all: false, sort: false, show: false, def initialize(all: false, sort: false, show: false,
mode: nil, show_git: false, almost_all: false, colors: [], group: nil, mode: nil, show_git: false, almost_all: false, colors: [], group: nil,
reverse: false, hyperlink: false, tree_depth: nil, show_inode: false, reverse: false, hyperlink: false, tree_depth: nil, show_inode: false,
@ -120,6 +122,7 @@ module ColorLS
layout.each_line do |line, widths| layout.each_line do |line, widths|
ls_line(line, widths) ls_line(line, widths)
end end
clear_chars_for_size
end end
def init_colors(colors) def init_colors(colors)
@ -140,6 +143,8 @@ module ColorLS
@show_group = long_style_options.key?(:show_group) ? long_style_options[:show_group] : true @show_group = long_style_options.key?(:show_group) ? long_style_options[:show_group] : true
@show_user = long_style_options.key?(:show_user) ? long_style_options[:show_user] : true @show_user = long_style_options.key?(:show_user) ? long_style_options[:show_user] : true
@show_symbol_dest = long_style_options.key?(:show_symbol_dest) ? long_style_options[:show_symbol_dest] : false @show_symbol_dest = long_style_options.key?(:show_symbol_dest) ? long_style_options[:show_symbol_dest] : false
@show_human_readable_size =
long_style_options.key?(:human_readable_size) ? long_style_options[:human_readable_size] : true
end end
def init_git_status(show_git) def init_git_status(show_git)
@ -254,14 +259,36 @@ module ColorLS
end end
def size_info(filesize) def size_info(filesize)
size = Filesize.new(filesize).pretty.split filesize = Filesize.new(filesize)
size = "#{size[0][0..-4].rjust(4,' ')} #{size[1].ljust(3,' ')}" size = @show_human_readable_size ? filesize.pretty : filesize.to_s
size = size.split
size = justify_size_info(size)
return size.colorize(@colors[:file_large]) if filesize >= 512 * (1024 ** 2) return size.colorize(@colors[:file_large]) if filesize >= 512 * (1024 ** 2)
return size.colorize(@colors[:file_medium]) if filesize >= 128 * (1024 ** 2) return size.colorize(@colors[:file_medium]) if filesize >= 128 * (1024 ** 2)
size.colorize(@colors[:file_small]) size.colorize(@colors[:file_small])
end end
def chars_for_size
@chars_for_size ||= if @show_human_readable_size
MIN_SIZE_CHARS
else
max_size = @contents.max_by(&:size).size
reqd_chars = max_size.to_s.length
[reqd_chars, MIN_SIZE_CHARS].max
end
end
def justify_size_info(size)
size_num = size[0][0..-4].rjust(chars_for_size, ' ')
size_unit = @show_human_readable_size ? size[1].ljust(3, ' ') : size[1]
"#{size_num} #{size_unit}"
end
def clear_chars_for_size
@chars_for_size = nil
end
def mtime_info(file_mtime) def mtime_info(file_mtime)
mtime = @time_style.start_with?('+') ? file_mtime.strftime(@time_style.delete_prefix('+')) : file_mtime.asctime mtime = @time_style.start_with?('+') ? file_mtime.strftime(@time_style.delete_prefix('+')) : file_mtime.asctime
now = Time.now now = Time.now

View file

@ -184,7 +184,8 @@ module ColorLS
show_user: true, show_user: true,
time_style: '', time_style: '',
hard_links_count: true, hard_links_count: true,
show_symbol_dest: false show_symbol_dest: false,
human_readable_size: true
} }
end end
@ -199,6 +200,9 @@ module ColorLS
long_style_options[:hard_links_count] = false long_style_options[:hard_links_count] = false
end end
long_style_options = get_long_style_symlink_options(options, long_style_options) long_style_options = get_long_style_symlink_options(options, long_style_options)
options.on('--non-human-readable', 'show file sizes in bytes only') do
long_style_options[:human_readable_size] = false
end
@opts[:long_style_options] = long_style_options @opts[:long_style_options] = long_style_options
end end

View file

@ -1,5 +1,5 @@
# frozen_string_literal: true # frozen_string_literal: true
module ColorLS module ColorLS
VERSION = '1.4.6' VERSION = '1.5.0'
end end

View file

@ -30,7 +30,7 @@ RSpec.describe ColorLS::Flags do
it 'displays multiple files per line' do it 'displays multiple files per line' do
allow($stdout).to receive(:tty?).and_return(true) allow($stdout).to receive(:tty?).and_return(true)
expect { subject }.not_to output(/(.*\n){3}/).to_stdout expect { subject }.not_to output(/(.*\n){4}/).to_stdout
end end
it('does not display ./ or ../') { expect { subject }.not_to output(%r(\.{1,2}/)).to_stdout } it('does not display ./ or ../') { expect { subject }.not_to output(%r(\.{1,2}/)).to_stdout }
@ -140,6 +140,14 @@ RSpec.describe ColorLS::Flags do
end end
end end
context 'with --long and --non-human-readable flag for `2MB file`' do
let(:args) { ['--long', '--non-human-readable', "#{FIXTURES}/two_megabyte_file.txt"] }
it 'shows the file size in bytes' do
expect { subject }.to output(/#{2 * 1024 * 1024}\sB/).to_stdout
end
end
context 'with --long flag on windows' do context 'with --long flag on windows' do
let(:args) { ['--long', "#{FIXTURES}/a.txt"] } let(:args) { ['--long', "#{FIXTURES}/a.txt"] }
@ -360,7 +368,7 @@ RSpec.describe ColorLS::Flags do
let(:args) { ['--report', '--report=long', FIXTURES] } let(:args) { ['--report', '--report=long', FIXTURES] }
it 'shows a report with recognized and unrecognized files' do it 'shows a report with recognized and unrecognized files' do
expect { subject }.to output(/Recognized files\s+: 3\n.+Unrecognized files\s+: 3/).to_stdout expect { subject }.to output(/Recognized files\s+: 4\n.+Unrecognized files\s+: 3/).to_stdout
end end
end end

BIN
spec/fixtures/two_megabyte_file.txt vendored Normal file

Binary file not shown.

View file

@ -39,6 +39,7 @@ OK colorls --report=long
OK colorls --report=short OK colorls --report=short
OK colorls --inode OK colorls --inode
OK colorls -l --no-hardlinks OK colorls -l --no-hardlinks
OK colorls -l --non-human-readable
LC_ALL=C OK colorls spec/fixtures/ LC_ALL=C OK colorls spec/fixtures/
LC_ALL=C OK colorls --git spec/fixtures/ LC_ALL=C OK colorls --git spec/fixtures/