Re-use CoreLS::Core instance

Only initialize colors and flags once.
This commit is contained in:
Claudio Bley 2020-11-25 22:35:58 +01:00 committed by Claudio Bley
parent 77b5135d97
commit d0df25d35d
3 changed files with 16 additions and 11 deletions

View file

@ -16,11 +16,10 @@ module ColorLS
end
class Core
def initialize(input, all: false, report: false, sort: false, show: false,
def initialize(all: false, report: 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)
@input = (+input).force_encoding(ColorLS.file_encoding)
@count = {folders: 0, recognized_files: 0, unrecognized_files: 0}
@count = nil
@all = all
@almost_all = almost_all
@hyperlink = hyperlink
@ -37,11 +36,13 @@ module ColorLS
init_colors colors
@contents = init_contents(@input)
init_icons
end
def ls
def ls(input)
@input = (+input).force_encoding(ColorLS.file_encoding)
@contents = init_contents(@input)
return print "\n Nothing to show here\n".colorize(@colors[:empty]) if @contents.empty?
layout = case
@ -92,6 +93,8 @@ module ColorLS
end
def init_contents(path)
@count = {folders: 0, recognized_files: 0, unrecognized_files: 0}
info = FileInfo.new(path, link_info: @long)
if info.directory?

View file

@ -28,6 +28,9 @@ module ColorLS
init_locale
@args = ['.'] if @args.empty?
core = Core.new(**@opts)
exit_status_code = 0
@args.sort!.each_with_index do |path, i|
unless File.exist?(path)
@ -38,7 +41,8 @@ module ColorLS
puts '' if i.positive?
puts "\n#{path}:" if Dir.exist?(path) && @args.size > 1
Core.new(path, **@opts).ls
core.ls(path)
rescue SystemCallError => e
$stderr.puts "#{path}: #{e}".colorize(:red)
end

View file

@ -2,11 +2,9 @@
require 'spec_helper'
RSpec.describe ColorLS::Core do
subject { described_class.new(*args) }
context 'initialize' do
let(:args) { 'Imágenes' }
subject { described_class.new(colors: Hash.new('black')) }
context 'ls' do
it 'works with Unicode characters' do
camera = 'Cámara'.force_encoding(ColorLS::file_encoding)
imagenes = 'Imágenes'.force_encoding(ColorLS::file_encoding)
@ -62,7 +60,7 @@ RSpec.describe ColorLS::Core do
allow(ColorLS::FileInfo).to receive(:new).and_return(dirInfo)
allow(ColorLS::FileInfo).to receive(:new).with(File.join(imagenes, camera), link_info: false) { fileInfo }
expect { subject }.not_to raise_error
expect { subject.ls('Imágenes') }.to output(/mara/).to_stdout
end
end
end