diff --git a/CHANGELOG.md b/CHANGELOG.md index b0268ad..c279169 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## unreleased + - Add `uncolor` method to remove ANSI color sequences. + + ## v1.1.0 - Print out method keyword arguments - Fix NoMethodError with Sequel diff --git a/lib/amazing_print/core_ext/string.rb b/lib/amazing_print/core_ext/string.rb index d479ac8..d08f50c 100644 --- a/lib/amazing_print/core_ext/string.rb +++ b/lib/amazing_print/core_ext/string.rb @@ -40,4 +40,9 @@ class String alias black grayish alias pale whiteish + + # Remove ANSI color codes. + def uncolor + gsub(/\e\[[0-9;]*m/, '') + end end diff --git a/spec/colors_spec.rb b/spec/colors_spec.rb index 93accea..0a971a6 100644 --- a/spec/colors_spec.rb +++ b/spec/colors_spec.rb @@ -112,5 +112,12 @@ def tty? end end end + + describe 'uncolor' do + it 'removes any ANSI color codes' do + expect('red'.red + 'blue'.blue).to eq "\e[1;31mred\e[0m\e[1;34mblue\e[0m" + expect(('red'.red + 'blue'.blue).uncolor).to eq 'redblue' + end + end end end