Skip to content

Commit

Permalink
Add strip_ascii_escape method
Browse files Browse the repository at this point in the history
  • Loading branch information
ypresto committed Mar 29, 2017
1 parent f9785ab commit fa8d602
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/rainbow/global.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ def self.enabled=(value)
global.enabled = value
end

def self.strip_ascii_escape(string)
StringUtils.strip_ascii_escape(string)
end

end

def Rainbow(string)
Expand Down
5 changes: 5 additions & 0 deletions lib/rainbow/string_utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,10 @@ def self.wrap_with_sgr(string, codes)
string
end

def self.strip_ascii_escape(string)
# See http://www.commandlinefu.com/commands/view/3584/remove-color-codes-special-characters-with-sed
string.gsub(/\e\[[0-9;]*[a-zA-Z]/, '')
end

end
end
24 changes: 24 additions & 0 deletions spec/unit/string_utils_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,29 @@ class Stringgg < ::String; end
end

end

describe '.strip_ascii_escape' do
subject { described_class.strip_ascii_escape(string) }

let(:string) do
rainbow = Rainbow.new
rainbow.enabled = true
rainbow.wrap('hello').
foreground(:red).
bright.
bold.
italic.
background('#ff8040').
underline.
color(:blue).
blink.
inverse.
hide
end

it "removes color codes" do
expect(subject).to eq 'hello'
end
end
end
end

0 comments on commit fa8d602

Please sign in to comment.