Skip to content

Commit

Permalink
Merge pull request #30 from duffyjp/uncolor
Browse files Browse the repository at this point in the history
Add uncolor String method to remove ANSI color codes
  • Loading branch information
HarlemSquirrel authored May 18, 2020
2 parents c345744 + e3d86a7 commit 0e44d75
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
5 changes: 5 additions & 0 deletions lib/amazing_print/core_ext/string.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
7 changes: 7 additions & 0 deletions spec/colors_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 0e44d75

Please sign in to comment.