add --time-style option in long style format

This commit is contained in:
t-mangoe 2021-10-02 08:30:59 +09:00
parent a503848e67
commit d7e7ddd01e
3 changed files with 16 additions and 3 deletions

View file

@ -28,7 +28,7 @@ module ColorLS
def initialize(all: false, sort: false, show: false,
mode: nil, git_status: false, almost_all: false, colors: [], group: nil,
reverse: false, hyperlink: false, tree_depth: nil, show_group: true, show_user: true,
indicator_style: 'slash')
indicator_style: 'slash', time_style: '')
@count = {folders: 0, recognized_files: 0, unrecognized_files: 0}
@all = all
@almost_all = almost_all
@ -42,6 +42,7 @@ module ColorLS
@tree = {mode: mode == :tree, depth: tree_depth}
@horizontal = mode == :horizontal
@git_status = init_git_status(git_status)
@time_style = time_style
@indicator_style = indicator_style
init_colors colors
@ -249,7 +250,7 @@ module ColorLS
end
def mtime_info(file_mtime)
mtime = file_mtime.asctime
mtime = @time_style.empty? ? file_mtime.asctime : file_mtime.strftime(@time_style)
now = Time.now
return mtime.colorize(@colors[:hour_old]) if now - file_mtime < 60 * 60
return mtime.colorize(@colors[:day_old]) if now - file_mtime < 24 * 60 * 60

View file

@ -107,7 +107,8 @@ module ColorLS
tree_depth: 3,
show_group: true,
show_user: true,
indicator_style: 'slash'
indicator_style: 'slash',
time_style: ''
}
end
@ -183,6 +184,9 @@ module ColorLS
@opts[:show_user] = false
end
options.on('-G', '--no-group', 'show no group information in a long listing') { @opts[:show_group] = false }
options.on('--time-style=FORMAT', String, 'use time display format') do |time_style|
@opts[:time_style] = time_style
end
end
def add_general_options(options)

View file

@ -550,4 +550,12 @@ RSpec.describe ColorLS::Flags do
it { expect { subject }.to output(/.+second-level \n.+symlinks \n/).to_stdout }
end
context 'with --time-style option' do
let(:args) { ['-l', '--time-style=%y-%m-%d %k:%M', "#{FIXTURES}/a.txt"] }
mtime = File.mtime("#{FIXTURES}/a.txt")
it { expect { subject }.to output(/#{mtime.strftime("%y-%m-%d %k:%M")}/).to_stdout }
end
end