Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix colorized 1.9 hash syntax #113

Merged
merged 5 commits into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions Appraisals
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ if RUBY_VERSION < '3'
appraise 'rails-5.2' do
gem 'activerecord-jdbcsqlite3-adapter', '~> 52.0', platform: :jruby
gem 'rails', '~> 5.2.0'
gem 'sqlite3', platform: :mri
gem 'sqlite3', '~> 1.4', platform: :mri
end

appraise 'rails-6.0' do
gem 'activerecord-jdbcsqlite3-adapter', '~> 60.0', platform: :jruby
gem 'rails', '~> 6.0.0'
gem 'sqlite3', platform: :mri
gem 'sqlite3', '~> 1.4', platform: :mri
end

appraise 'mongoid-4.0' do
Expand Down Expand Up @@ -41,7 +41,7 @@ if RUBY_VERSION >= '2.7'
appraise 'rails-7.0' do
gem 'activerecord-jdbcsqlite3-adapter', '~> 70.0', platform: :jruby
gem 'rails', '~> 7.0.0'
gem 'sqlite3', platform: :mri
gem 'sqlite3', '~> 1.4', platform: :mri
end

appraise 'mongoid-7.0' do
Expand All @@ -56,11 +56,11 @@ end
appraise 'rails-6.1' do
gem 'activerecord-jdbcsqlite3-adapter', '~> 61.0', platform: :jruby
gem 'rails', '~> 6.1.0'
gem 'sqlite3', platform: :mri
gem 'sqlite3', '~> 1.4', platform: :mri
end

appraise 'sequel-5.0' do
gem 'jdbc-sqlite3', platform: :jruby
gem 'sequel', '~> 5.0'
gem 'sqlite3', platform: :mri
gem 'sqlite3', '~> 1.4', platform: :mri
end
10 changes: 0 additions & 10 deletions gemfiles/mongo_mapper.gemfile

This file was deleted.

8 changes: 0 additions & 8 deletions gemfiles/mongoid_4.0.gemfile

This file was deleted.

8 changes: 0 additions & 8 deletions gemfiles/mongoid_5.0.gemfile

This file was deleted.

7 changes: 0 additions & 7 deletions gemfiles/mongoid_6.0.gemfile

This file was deleted.

15 changes: 15 additions & 0 deletions gemfiles/mongoid_7.0.gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# This file was generated by Appraisal

source "https://rubygems.org"

gem "appraisal", git: "https://github.com/thoughtbot/appraisal.git", branch: :main
gem "bigdecimal"
gem "fakefs", "~> 1.2"
gem "mongoid", "~> 7.0.0"
gem "nokogiri", "~> 1.10"
gem "pry"
gem "rspec", "~> 3.9"
gem "rubocop", "~> 1.20"
gem "rubocop-rspec", "~> 2.4"

gemspec path: "../"
15 changes: 15 additions & 0 deletions gemfiles/mongoid_8.0.gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# This file was generated by Appraisal

source "https://rubygems.org"

gem "appraisal", git: "https://github.com/thoughtbot/appraisal.git", branch: :main
gem "bigdecimal"
gem "fakefs", "~> 1.2"
gem "mongoid", "~> 8.0.0"
gem "nokogiri", "~> 1.10"
gem "pry"
gem "rspec", "~> 3.9"
gem "rubocop", "~> 1.20"
gem "rubocop-rspec", "~> 2.4"

gemspec path: "../"
9 changes: 0 additions & 9 deletions gemfiles/rails_5.2.gemfile

This file was deleted.

9 changes: 0 additions & 9 deletions gemfiles/rails_6.0.gemfile

This file was deleted.

3 changes: 2 additions & 1 deletion gemfiles/rails_6.1.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ source "https://rubygems.org"

gem "activerecord-jdbcsqlite3-adapter", "~> 61.0", platform: :jruby
gem "appraisal", git: "https://github.com/thoughtbot/appraisal.git", branch: :main
gem "bigdecimal"
gem "fakefs", "~> 1.2"
gem "nokogiri", "~> 1.10"
gem "pry"
gem "rails", "~> 6.1.0"
gem "rspec", "~> 3.9"
gem "rubocop", "~> 1.20"
gem "rubocop-rspec", "~> 2.4"
gem "sqlite3", platform: :mri
gem "sqlite3", "~> 1.4", platform: :mri

gemspec path: "../"
3 changes: 2 additions & 1 deletion gemfiles/rails_7.0.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ source "https://rubygems.org"

gem "activerecord-jdbcsqlite3-adapter", "~> 70.0", platform: :jruby
gem "appraisal", git: "https://github.com/thoughtbot/appraisal.git", branch: :main
gem "bigdecimal"
gem "fakefs", "~> 1.2"
gem "nokogiri", "~> 1.10"
gem "pry"
gem "rails", "~> 7.0.0"
gem "rspec", "~> 3.9"
gem "rubocop", "~> 1.20"
gem "rubocop-rspec", "~> 2.4"
gem "sqlite3", platform: :mri
gem "sqlite3", "~> 1.4", platform: :mri

gemspec path: "../"
3 changes: 2 additions & 1 deletion gemfiles/sequel_5.0.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
source "https://rubygems.org"

gem "appraisal", git: "https://github.com/thoughtbot/appraisal.git", branch: :main
gem "bigdecimal"
gem "fakefs", "~> 1.2"
gem "jdbc-sqlite3", platform: :jruby
gem "nokogiri", "~> 1.10"
Expand All @@ -11,6 +12,6 @@ gem "rspec", "~> 3.9"
gem "rubocop", "~> 1.20"
gem "rubocop-rspec", "~> 2.4"
gem "sequel", "~> 5.0"
gem "sqlite3", platform: :mri
gem "sqlite3", "~> 1.4", platform: :mri

gemspec path: "../"
6 changes: 5 additions & 1 deletion lib/amazing_print/formatters/base_formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,12 @@ def align(value, width)
end
end

def colorless(string)
string.gsub(/\e\[[\d;]+m/, '')
end

def colorless_size(string)
string.gsub(/\e\[[\d;]+m/, '').size
colorless(string).size
end
end
end
Expand Down
38 changes: 29 additions & 9 deletions lib/amazing_print/formatters/hash_formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,18 @@ def left_width(keys)
result
end

def key_size(key)
return key.inspect.size if symbol?(key)

if options[:html]
single_line { inspector.awesome(key) }.size
else
plain_single_line { inspector.awesome(key) }.size
end
end

def max_key_width(keys)
keys.map { |key, _value| colorless_size(key) }.max || 0
keys.map { |key, _value| key_size(key) }.max || 0
end

def printable_keys
Expand All @@ -75,27 +85,37 @@ def printable_keys
keys.sort! { |a, b| a.to_s <=> b.to_s } if options[:sort_keys]

keys.map! do |key|
plain_single_line do
[inspector.awesome(key), hash[key]]
single_line do
[key, hash[key]]
end
end
end

def symbol?(key)
key[0] == ':'
key.is_a?(Symbol)
end

def ruby19_syntax(key, value, width)
key[0] = ''
key << ':'
"#{align(key, width)} #{inspector.awesome(value)}"
# Move the colon to the right side of the symbol
awesome_key = inspector.awesome(key).sub(/#{key.inspect}/, "#{key}:")

"#{align(awesome_key, width)} #{inspector.awesome(value)}"
end

def pre_ruby19_syntax(key, value, width)
"#{align(key, width)}#{colorize(' => ', :hash)}#{inspector.awesome(value)}"
awesome_key = single_line { inspector.awesome(key) }
"#{align(awesome_key, width)}#{colorize(' => ', :hash)}#{inspector.awesome(value)}"
end

def plain_single_line(&block)
plain = options[:plain]
options[:plain] = true
single_line(&block)
ensure
options[:plain] = plain
end

def plain_single_line
def single_line
multiline = options[:multiline]
options[:multiline] = false
yield
Expand Down
4 changes: 2 additions & 2 deletions spec/formats_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -331,10 +331,10 @@
expect(@hash.ai(ruby19_syntax: true)).to eq <<~EOS.strip
{
\e[1;34m1\e[0m\e[0;37m => \e[0m{
\e[0;36m:sym\e[0m\e[0;37m => \e[0m{
\e[0;36msym:\e[0m {
\e[0;33m"str"\e[0m\e[0;37m => \e[0m{
[ \e[1;34m1\e[0m, \e[1;34m2\e[0m, \e[1;34m3\e[0m ]\e[0;37m => \e[0m{
{ \e[0;36m:k\e[0m\e[0;37m => \e[0m\e[0;36m:v\e[0m }\e[0;37m => \e[0m\e[1;33mHash < Object\e[0m
{ \e[0;36mk:\e[0m \e[0;36m:v\e[0m }\e[0;37m => \e[0m\e[1;33mHash < Object\e[0m
}
}
}
Expand Down
Loading