From d7e7ddd01e38a8cec914d93c80f414c011d0e182 Mon Sep 17 00:00:00 2001 From: t-mangoe Date: Sat, 2 Oct 2021 08:30:59 +0900 Subject: [PATCH] add --time-style option in long style format --- lib/colorls/core.rb | 5 +++-- lib/colorls/flags.rb | 6 +++++- spec/color_ls/flags_spec.rb | 8 ++++++++ 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/lib/colorls/core.rb b/lib/colorls/core.rb index 5d90fcc..40b43f1 100644 --- a/lib/colorls/core.rb +++ b/lib/colorls/core.rb @@ -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 diff --git a/lib/colorls/flags.rb b/lib/colorls/flags.rb index 248c8c0..9ab44d3 100644 --- a/lib/colorls/flags.rb +++ b/lib/colorls/flags.rb @@ -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) diff --git a/spec/color_ls/flags_spec.rb b/spec/color_ls/flags_spec.rb index 7d50d46..8a9b9c3 100644 --- a/spec/color_ls/flags_spec.rb +++ b/spec/color_ls/flags_spec.rb @@ -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