Skip to content

Commit

Permalink
Merge pull request #48 from gromnitsky/mswin-file-formatter
Browse files Browse the repository at this point in the history
Add File/Dir formatters for mswin platform
  • Loading branch information
paddor authored Oct 26, 2022
2 parents 30a79be + cdedf7c commit 8c0ae33
Show file tree
Hide file tree
Showing 14 changed files with 203 additions and 167 deletions.
3 changes: 3 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,6 @@ Style/HashTransformValues:
Style/StringLiterals:
Exclude:
- gemfiles/*

Style/FetchEnvVar:
Enabled: false
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
## Unreleased
- Drop support for Ruby 2.3 and 2.4 as well as JRuby 9.1
- Add File/Dir formatters for mswin platform

## v1.4.0
- Support loading config from `$XDG_CONFIG_HOME/aprc` - #63
Expand Down
1 change: 0 additions & 1 deletion amazing_print.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ Gem::Specification.new do |s|
s.license = 'MIT'

s.files = Dir['[A-Z]*[^~]'] + Dir['lib/**/*.rb'] + Dir['spec/**/*'] + ['.gitignore']
s.test_files = Dir['spec/**/*']
s.executables = []
s.require_paths = ['lib']

Expand Down
3 changes: 1 addition & 2 deletions lib/amazing_print/core_ext/string.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
# frozen_string_literal: true

# Copyright (c) 2010-2016 Michael Dvorkin and contributors
#
# AmazingPrint is freely distributable under the terms of MIT license.
# See LICENSE file or http://www.opensource.org/licenses/mit-license.php
#------------------------------------------------------------------------------

# frozen_string_literal: true

class String
#
# ANSI color codes:
Expand Down
11 changes: 10 additions & 1 deletion lib/amazing_print/formatters/dir_formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require 'shellwords'

require_relative 'base_formatter'
require_relative 'mswin_helper' if RUBY_PLATFORM.include?('mswin')

module AmazingPrint
module Formatters
Expand All @@ -17,9 +18,17 @@ def initialize(dir, inspector)
end

def format
ls = `ls -alF #{dir.path.shellescape}`
ls = info
colorize(ls.empty? ? dir.inspect : "#{dir.inspect}\n#{ls.chop}", :dir)
end

def info
if RUBY_PLATFORM.include?('mswin')
"#{GetChildItem.new(@dir.path)}\n"
else
`ls -alF #{dir.path.shellescape}`
end
end
end
end
end
11 changes: 10 additions & 1 deletion lib/amazing_print/formatters/file_formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require 'shellwords'

require_relative 'base_formatter'
require_relative 'mswin_helper' if RUBY_PLATFORM.include?('mswin')

module AmazingPrint
module Formatters
Expand All @@ -17,9 +18,17 @@ def initialize(file, inspector)
end

def format
ls = File.directory?(file) ? `ls -adlF #{file.path.shellescape}` : `ls -alF #{file.path.shellescape}`
ls = info
colorize(ls.empty? ? file.inspect : "#{file.inspect}\n#{ls.chop}", :file)
end

def info
if RUBY_PLATFORM.include?('mswin')
"#{GetChildItem.new(@file.path)}\n"
else
File.directory?(file) ? `ls -adlF #{file.path.shellescape}` : `ls -alF #{file.path.shellescape}`
end
end
end
end
end
63 changes: 63 additions & 0 deletions lib/amazing_print/formatters/mswin_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# frozen_string_literal: true

require 'fiddle'
require 'fiddle/import'

module AmazingPrint
module Formatters
module Kernel32
extend Fiddle::Importer
dlload 'kernel32'
extern 'unsigned long GetFileAttributesA(const char*)'
end

class GetChildItem
def initialize(fname)
@fname = fname
@stat = File.send(File.symlink?(@fname) ? :lstat : :stat, @fname)
@attrs = Kernel32::GetFileAttributesA @fname
end

# docs.microsoft.com/en-us/windows/win32/fileio/file-attribute-constants
FILE_ATTRIBUTE_ARCHIVE = 0x20
FILE_ATTRIBUTE_READONLY = 0x1
FILE_ATTRIBUTE_HIDDEN = 0x2
FILE_ATTRIBUTE_SYSTEM = 0x4

def mode
r = ['-'] * 6
r[0] = 'd' if @stat.directory?
r[1] = 'a' unless (@attrs & FILE_ATTRIBUTE_ARCHIVE).zero?
r[2] = 'r' unless (@attrs & FILE_ATTRIBUTE_READONLY).zero?
r[3] = 'h' unless (@attrs & FILE_ATTRIBUTE_HIDDEN).zero?
r[4] = 's' unless (@attrs & FILE_ATTRIBUTE_SYSTEM).zero?
r[5] = 'l' if File.symlink? @fname
r.join
end

def last_write_time
@stat.mtime.strftime '%Y-%m-%d %H:%M'
end

def length
@stat.file? ? @stat.size.to_s : ''
end

def name
@fname
end

def to_s
format '%-12<Mode>s %<LastWriteTime>s %14<Length>s %<Name>s',
{
Mode: mode,
LastWriteTime: last_write_time,
Length: length,
Name: name
}
end
end
end
end

puts AmazingPrint::Formatters::GetChildItem.new ARGV[0] if __FILE__ == $PROGRAM_NAME
2 changes: 1 addition & 1 deletion lib/amazing_print/inspector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ def find_dotfile
return xdg_config_path if File.exist?(xdg_config_path)

# default to ~/.aprc
File.join(ENV['HOME'], '.aprc')
File.join(Dir.home, '.aprc')
end

# This method needs to be mocked during testing so that it always loads
Expand Down
6 changes: 3 additions & 3 deletions spec/ext/action_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
let(:expected_output) do
<<~OUTPUT
{
\"post\"\e[0;37m => \e[0m{
\"id\"\e[0;37m => \e[0m\e[1;34m1\e[0m,
\"content\"\e[0;37m => \e[0m\e[0;33m\"Some\"\e[0m
"post"\e[0;37m => \e[0m{
"id"\e[0;37m => \e[0m\e[1;34m1\e[0m,
"content"\e[0;37m => \e[0m\e[0;33m"Some"\e[0m
}
}
OUTPUT
Expand Down
80 changes: 21 additions & 59 deletions spec/ext/active_record_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,7 @@
:rank => 1
}
EOS
if RUBY_VERSION < '1.9'
str.sub!('?', 'Sat Oct 10 12:30:00 UTC 1992')
else
str.sub!('?', '1992-10-10 12:30:00 UTC')
end
str.sub!('?', '1992-10-10 12:30:00 UTC')
expect(out).to be_similar_to(str)
end

Expand All @@ -51,13 +47,8 @@
}
]
EOS
if RUBY_VERSION < '1.9'
str.sub!('??', 'Sat Oct 10 12:30:00 UTC 1992')
str.sub!('?!', 'Mon May 26 14:15:00 UTC 2003')
else
str.sub!('??', '1992-10-10 12:30:00 UTC')
str.sub!('?!', '2003-05-26 14:15:00 UTC')
end
str.sub!('??', '1992-10-10 12:30:00 UTC')
str.sub!('?!', '2003-05-26 14:15:00 UTC')
expect(out).to be_similar_to(str)
end

Expand All @@ -83,13 +74,8 @@
}
]
EOS
if RUBY_VERSION < '1.9'
str.sub!('??', 'Sat Oct 10 12:30:00 UTC 1992')
str.sub!('?!', 'Mon May 26 14:15:00 UTC 2003')
else
str.sub!('??', '1992-10-10 12:30:00 UTC')
str.sub!('?!', '2003-05-26 14:15:00 UTC')
end
str.sub!('??', '1992-10-10 12:30:00 UTC')
str.sub!('?!', '2003-05-26 14:15:00 UTC')
expect(out).to be_similar_to(str)
end
end
Expand Down Expand Up @@ -139,21 +125,13 @@
elsif activerecord_5_0?
ActiveRecordData.raw_5_0_diana
elsif activerecord_4_2?
if RUBY_VERSION > '1.9.3'
ActiveRecordData.raw_4_2_diana
else
ActiveRecordData.raw_4_2_diana_legacy
end
ActiveRecordData.raw_4_2_diana
elsif activerecord_4_1?
ActiveRecordData.raw_4_1_diana
elsif activerecord_4_0?
ActiveRecordData.raw_4_0_diana
elsif activerecord_3_2?
if RUBY_VERSION > '1.9.3'
ActiveRecordData.raw_3_2_diana
else
ActiveRecordData.raw_3_2_diana_legacy
end
ActiveRecordData.raw_3_2_diana
end

if RUBY_PLATFORM == 'java' && !activerecord_6_1?
Expand Down Expand Up @@ -181,21 +159,13 @@
elsif activerecord_5_0?
ActiveRecordData.raw_5_0_multi
elsif activerecord_4_2?
if RUBY_VERSION > '1.9.3'
ActiveRecordData.raw_4_2_multi
else
ActiveRecordData.raw_4_2_multi_legacy
end
ActiveRecordData.raw_4_2_multi
elsif activerecord_4_1?
ActiveRecordData.raw_4_1_multi
elsif activerecord_4_0?
ActiveRecordData.raw_4_0_multi
elsif activerecord_3_2?
if RUBY_VERSION > '1.9.3'
ActiveRecordData.raw_3_2_multi
else
ActiveRecordData.raw_3_2_multi_legacy
end
ActiveRecordData.raw_3_2_multi
end

if RUBY_PLATFORM == 'java' && !activerecord_6_1?
Expand Down Expand Up @@ -266,28 +236,24 @@ class SubUser < User {
expect(out).to match(
/\s+first\(\*args,\s&block\)\s+#<Class:\w+>\s+\(ActiveRecord::Querying\)/
)
elsif RUBY_VERSION >= '3.1.0'
elsif Gem::Version.new(RUBY_VERSION) >= Gem::Version.new('3.1.0')
expect(out).to match(
/\s*first\(\*(\*|args),\s+\?,\s+&(&|block)\)\s+#<Class:User> \(ActiveRecord::Querying\)/
)
elsif RUBY_VERSION >= '3.0.0'
elsif Gem::Version.new(RUBY_VERSION) >= Gem::Version.new('3.0.0')
expect(out).to match(
/\s*first\(\*(\*|args),\s+&(&|block)\)\s+#<Class:User> \(ActiveRecord::Querying\)/
)
elsif RUBY_VERSION >= '2.7.2'
elsif Gem::Version.new(RUBY_VERSION) >= Gem::Version.new('2.7.2')
expect(out).to match(
/\s*first\(\*(\*|args),\s+&(&|block)\)\s+User/
)
elsif RUBY_VERSION >= '2.6.7'
elsif Gem::Version.new(RUBY_VERSION) >= Gem::Version.new('2.6.7')
expect(out).to match(
/\s*first\(\*(\*|args),\s+&(&|block)\)\s+#<Class:ActiveRecord::Base> \(ActiveRecord::Querying\)/
)
elsif RUBY_VERSION =~ /^2\.4\.([4-9]|[1-9][0-9])|^2\.[56]\./
elsif Gem::Version.new(RUBY_VERSION) >= Gem::Version.new('2.4.4')
expect(out).to match(/\sfirst\(\*arg.*?\)\s+User/)
elsif RUBY_VERSION >= '1.9'
expect(out).to match(/\sfirst\(\*args,\s&block\)\s+Class \(ActiveRecord::Querying\)/)
else
expect(out).to match(/\sfirst\(\*arg1\)\s+Class \(ActiveRecord::Querying\)/)
end
else
expect(out).to match(/\sfirst\(\*arg.*?\)\s+User \(ActiveRecord::Base\)/)
Expand All @@ -299,18 +265,16 @@ class SubUser < User {
expect(out).to match(
/\sprimary_key\(.*?\)\s+#<Class:\w+>\s\(ActiveRecord::AttributeMethods::PrimaryKey::ClassMethods\)/
)
elsif RUBY_VERSION >= '3.0.0'
elsif Gem::Version.new(RUBY_VERSION) >= Gem::Version.new('3.0.0')
expect(out).to match(/\sprimary_key\(.*?\)\s+#<Class:User> \(ActiveRecord::AttributeMethods::PrimaryKey::ClassMethods\)/)
elsif RUBY_VERSION >= '2.6.7' && RUBY_VERSION < '2.7.0'
elsif Gem::Version.new(RUBY_VERSION) >= Gem::Version.new('2.6.7') && Gem::Version.new(RUBY_VERSION) < Gem::Version.new('2.7.0')
expect(out).to match(/\sprimary_key\(.*?\)\s+#<Class:ActiveRecord::Base> \(ActiveRecord::AttributeMethods::PrimaryKey::ClassMethods\)/)
elsif RUBY_VERSION =~ /^2\.4\.([4-9]|[1-9][0-9])|^2\.[56]\./ || RUBY_VERSION >= '2.7.2'
elsif Gem::Version.new(RUBY_VERSION) >= Gem::Version.new('2.4.4') || Gem::Version.new(RUBY_VERSION) >= Gem::Version.new('2.7.2')
expect(out).to match(/\sprimary_key\(.*?\)\s+User/)
elsif RUBY_VERSION >= '2.7.0'
elsif Gem::Version.new(RUBY_VERSION) >= Gem::Version.new('2.7.0')
expect(out).to match(
/\sprimary_key\(.*?\)\s+.+Class.+\(ActiveRecord::AttributeMethods::PrimaryKey::ClassMethods\)/
)
else
expect(out).to match(/\sprimary_key\(.*?\)\s+Class \(ActiveRecord::AttributeMethods::PrimaryKey::ClassMethods\)/)
end

# spec 3
Expand All @@ -320,16 +284,14 @@ class SubUser < User {
expect(out).to match(/\svalidate\(\*arg.*?\)\s+User \(ActiveRecord::Base\)/)
elsif RUBY_PLATFORM == 'java'
expect(out).to match(/\svalidate\(\*arg.*?\)\s+#<Class:\w+> \(ActiveModel::Validations::ClassMethods\)/)
elsif RUBY_VERSION >= '3.0.0'
elsif Gem::Version.new(RUBY_VERSION) >= Gem::Version.new('3.0.0')
expect(out).to match(/\svalidate\(\*arg.*?\)\s+#<Class:User> \(ActiveModel::Validations::ClassMethods\)/)
elsif RUBY_VERSION =~ /2\.7\.(0|1)/ || (RUBY_VERSION >= '2.6.7' && RUBY_VERSION < '2.7.0')
elsif (Gem::Version.new('2.6.7')..Gem::Version.new('2.7.1')).cover? Gem::Version.new(RUBY_VERSION)
expect(out).to match(
/\svalidate\(\*args.*?\)\s+#<Class:ActiveRecord::Base> \(ActiveModel::Validations::ClassMethods\)/
)
elsif RUBY_VERSION =~ /^2\.4\.([4-9]|[1-9][0-9])|^2\.[56]\./ || RUBY_VERSION >= '2.7.2'
elsif Gem::Version.new(RUBY_VERSION) >= Gem::Version.new('2.4.4') || Gem::Version.new(RUBY_VERSION) >= Gem::Version.new('2.7.2')
expect(out).to match(/\svalidate\(\*arg.*?\)\s+User/)
else
expect(out).to match(/\svalidate\(\*arg.*?\)\s+Class \(ActiveModel::Validations::ClassMethods\)/)
end
end
end
Expand Down
8 changes: 4 additions & 4 deletions spec/ext/nokogiri_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
# FIXME: Due to something strange with Nokogiri and JRuby, we need to remove extra blank lines.
output = xml.ai.gsub(/\n\n/, "\n")
expect(output).to eq <<~EOS
<?xml version=\"1.0\"?>\e[1;32m
<?xml version="1.0"?>\e[1;32m
\e[0m<\e[1;36mhtml\e[0m>\e[1;32m
\e[0m<\e[1;36mbody\e[0m>\e[1;32m
\e[0m<\e[1;36mh1\e[0m/>\e[1;32m
Expand All @@ -20,7 +20,7 @@
it 'colorizes contents' do
xml = Nokogiri::XML('<html><body><h1>Hello</h1></body></html>')
expect(xml.ai).to eq <<~EOS
<?xml version=\"1.0\"?>\e[1;32m
<?xml version="1.0"?>\e[1;32m
\e[0m<\e[1;36mhtml\e[0m>\e[1;32m
\e[0m<\e[1;36mbody\e[0m>\e[1;32m
\e[0m<\e[1;36mh1\e[0m>\e[1;32mHello\e[0m<\e[1;36m/h1\e[0m>\e[1;32m
Expand All @@ -34,11 +34,11 @@
# FIXME: Due to something strange with Nokogiri and JRuby, we need to remove extra blank lines.
output = xml.ai.gsub(/\n\n/, "\n")
expect(output).to eq <<~EOS
<?xml version=\"1.0\"?>\e[1;32m
<?xml version="1.0"?>\e[1;32m
\e[0m<\e[1;36mhtml\e[0m>\e[1;32m
\e[0m<\e[1;36mbody\e[0m>\e[1;32m
\e[0m<\e[1;36mh1\e[0m>\e[1;32m
\e[0m<\e[1;36mspan\e[0m \e[1;33mclass=\"world\"\e[0m \e[1;33mid=\"hello\"\e[0m/>\e[1;32m
\e[0m<\e[1;36mspan\e[0m \e[1;33mclass="world"\e[0m \e[1;33mid="hello"\e[0m/>\e[1;32m
\e[0m<\e[1;36m/h1\e[0m>\e[1;32m
\e[0m<\e[1;36m/body\e[0m>\e[1;32m
\e[0m<\e[1;36m/html\e[0m>
Expand Down
Loading

0 comments on commit 8c0ae33

Please sign in to comment.