Skip to content

Commit

Permalink
Merge pull request #53 from ypresto/add-faint
Browse files Browse the repository at this point in the history
Add faint/dark method and fix missing bold alias in NullPresenter
  • Loading branch information
ku1ik committed Apr 5, 2017
2 parents f9785ab + e364abd commit 3e8064c
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 2 deletions.
3 changes: 2 additions & 1 deletion README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ Rainbow presenter adds the following methods to presented string:
* `blink`
* `inverse`
* `hide`
* `italic` (not well supported by terminal emulators).
* `faint` (not well supported by terminal emulators)
* `italic` (not well supported by terminal emulators)

Text color can also be changed by calling a method named by a color:

Expand Down
4 changes: 4 additions & 0 deletions lib/rainbow/ext/string.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ def bright
Rainbow(self).bright
end

def faint
Rainbow(self).faint
end

def italic
Rainbow(self).italic
end
Expand Down
3 changes: 3 additions & 0 deletions lib/rainbow/null_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ def color(*values); self; end
def background(*values); self; end
def reset; self; end
def bright; self; end
def faint; self; end
def italic; self; end
def underline; self; end
def blink; self; end
Expand All @@ -32,6 +33,8 @@ def method_missing(method_name,*args)
alias_method :foreground, :color
alias_method :fg, :color
alias_method :bg, :background
alias_method :bold, :bright
alias_method :dark, :faint

end

Expand Down
9 changes: 9 additions & 0 deletions lib/rainbow/presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class Presenter < ::String
TERM_EFFECTS = {
reset: 0,
bright: 1,
faint: 2,
italic: 3,
underline: 4,
blink: 5,
Expand Down Expand Up @@ -46,6 +47,14 @@ def bright

alias_method :bold, :bright

# Turns on faint/dark for this text (not well supported by terminal
# emulators).
def faint
wrap_with_sgr(TERM_EFFECTS[:faint])
end

alias_method :dark, :faint

# Turns on italic style for this text (not well supported by terminal
# emulators).
def italic
Expand Down
13 changes: 12 additions & 1 deletion spec/integration/rainbow_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@
expect(result).to eq("\e[1mhello\e[0m")
end

it 'allows making text faint' do
result = Rainbow('hello').faint
expect(result).to eq("\e[2mhello\e[0m")
end

it 'allows making text italic' do
result = Rainbow('hello').italic
expect(result).to eq("\e[3mhello\e[0m")
Expand Down Expand Up @@ -111,6 +116,9 @@
result = Rainbow('hello').
foreground(:red).
bright.
bold.
faint.
dark.
italic.
background('#ff8040').
underline.
Expand All @@ -120,7 +128,7 @@
hide

expect(result).to eq(
"\e[31m\e[1m\e[3m\e[48;5;215m\e[4m\e[34m\e[5m\e[7m\e[8mhello\e[0m"
"\e[31m\e[1m\e[1m\e[2m\e[2m\e[3m\e[48;5;215m\e[4m\e[34m\e[5m\e[7m\e[8mhello\e[0m"
)
end

Expand All @@ -134,6 +142,9 @@
result = Rainbow('hello').
foreground(:red).
bright.
bold.
faint.
dark.
italic.
background('#ff8040').
underline.
Expand Down
4 changes: 4 additions & 0 deletions spec/integration/string_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@
expect('hello'.bright).to eq(Rainbow('hello').bright)
end

it 'proxies faint to Rainbow().faint' do
expect('hello'.faint).to eq(Rainbow('hello').faint)
end

it 'proxies italic to Rainbow().italic' do
expect('hello'.italic).to eq(Rainbow('hello').italic)
end
Expand Down
18 changes: 18 additions & 0 deletions spec/unit/null_presenter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,24 @@ module Rainbow
it_behaves_like "rainbow null string method"
end

describe '#bold' do
subject { presenter.bold }

it_behaves_like "rainbow null string method"
end

describe '#faint' do
subject { presenter.faint }

it_behaves_like "rainbow null string method"
end

describe '#dark' do
subject { presenter.dark }

it_behaves_like "rainbow null string method"
end

describe '#italic' do
subject { presenter.italic }

Expand Down
22 changes: 22 additions & 0 deletions spec/unit/presenter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,28 @@ module Rainbow
end
end

describe '#faint' do
subject { presenter.faint }

it_behaves_like "rainbow string method"

it 'wraps with 2 code' do
subject
expect(StringUtils).to have_received(:wrap_with_sgr).with('hello', [2])
end
end

describe '#dark' do
subject { presenter.dark }

it_behaves_like "rainbow string method"

it 'wraps with 2 code' do
subject
expect(StringUtils).to have_received(:wrap_with_sgr).with('hello', [2])
end
end

describe '#italic' do
subject { presenter.italic }

Expand Down

0 comments on commit 3e8064c

Please sign in to comment.