Skip to content

Commit

Permalink
Change say, ok, warn and error prompts to use keyword arguments and f…
Browse files Browse the repository at this point in the history
…ix color overriding
  • Loading branch information
piotrmurach committed Jun 8, 2020
1 parent 70ff1c3 commit 6287633
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 48 deletions.
22 changes: 11 additions & 11 deletions lib/tty/prompt.rb
Original file line number Diff line number Diff line change
Expand Up @@ -423,11 +423,11 @@ def slider(question, *args, &block)
# @return [String]
#
# @api public
def say(message = '', options = {})
def say(message = "", **options)
message = message.to_s
return if message.empty?

statement = Statement.new(self, options)
statement = Statement.new(self, **options)
statement.call(message)
end

Expand All @@ -442,9 +442,9 @@ def say(message = '', options = {})
# @return [Array] messages
#
# @api public
def ok(*args)
options = Utils.extract_options!(args)
args.each { |message| say message, options.merge(color: :green) }
def ok(*args, **options)
opts = { color: :green }.merge(options)
args.each { |message| say(message, **opts) }
end

# Print statement(s) out in yellow color.
Expand All @@ -458,9 +458,9 @@ def ok(*args)
# @return [Array] messages
#
# @api public
def warn(*args)
options = Utils.extract_options!(args)
args.each { |message| say message, options.merge(color: :yellow) }
def warn(*args, **options)
opts = { color: :yellow }.merge(options)
args.each { |message| say(message, **opts) }
end

# Print statement(s) out in red color.
Expand All @@ -474,9 +474,9 @@ def warn(*args)
# @return [Array] messages
#
# @api public
def error(*args)
options = Utils.extract_options!(args)
args.each { |message| say message, options.merge(color: :red) }
def error(*args, **options)
opts = { color: :red }.merge(options)
args.each { |message| say(message, **opts) }
end

# Print debug information in terminal top right corner
Expand Down
6 changes: 3 additions & 3 deletions lib/tty/prompt/statement.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ class Statement
# change the message display to color
#
# @api public
def initialize(prompt, options = {})
def initialize(prompt, newline: true, color: false)
@prompt = prompt
@newline = options.fetch(:newline) { true }
@color = options.fetch(:color) { false }
@newline = newline
@color = color
end

# Output the message to the prompt
Expand Down
16 changes: 11 additions & 5 deletions spec/unit/error_spec.rb
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
# frozen_string_literal: true

RSpec.describe TTY::Prompt, '.error' do
RSpec.describe TTY::Prompt, "#error" do
subject(:prompt) { TTY::TestPrompt.new }

it 'displays one message' do
it "displays one message" do
prompt.error "Nothing is fine!"
expect(prompt.output.string).to eql "\e[31mNothing is fine!\e[0m\n"
end

it 'displays many messages' do
it "displays many messages" do
prompt.error "Nothing is fine!", "All is broken!"
expect(prompt.output.string).to eql "\e[31mNothing is fine!\e[0m\n\e[31mAll is broken!\e[0m\n"
expect(prompt.output.string).to eq(
"\e[31mNothing is fine!\e[0m\n\e[31mAll is broken!\e[0m\n")
end

it 'displays message with option' do
it "displays message with option" do
prompt.error "Nothing is fine!", newline: false
expect(prompt.output.string).to eql "\e[31mNothing is fine!\e[0m"
end

it "changes default red color to cyan" do
prompt.error("All is fine", color: :cyan)
expect(prompt.output.string).to eq("\e[36mAll is fine\e[0m\n")
end
end
15 changes: 13 additions & 2 deletions spec/unit/ok_spec.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
# frozen_string_literal: true

RSpec.describe TTY::Prompt, 'ok' do
RSpec.describe TTY::Prompt, "#ok" do
subject(:prompt) { TTY::TestPrompt.new }

it 'prints text in green' do
it "prints text in green" do
prompt.ok("All is fine")
expect(prompt.output.string).to eq("\e[32mAll is fine\e[0m\n")
end

it "prints multiple lines in green" do
prompt.ok("All is fine", "All is good")
expect(prompt.output.string).to eq(
"\e[32mAll is fine\e[0m\n\e[32mAll is good\e[0m\n")
end

it "changes color to cyan" do
prompt.ok("All is fine", color: :cyan)
expect(prompt.output.string).to eq("\e[36mAll is fine\e[0m\n")
end
end
41 changes: 20 additions & 21 deletions spec/unit/say_spec.rb
Original file line number Diff line number Diff line change
@@ -1,65 +1,64 @@
# frozen_string_literal: true

RSpec.describe TTY::Prompt, '#say' do

RSpec.describe TTY::Prompt, "#say" do
subject(:prompt) { TTY::TestPrompt.new }

it 'prints an empty message' do
prompt.say('')
expect(prompt.output.string).to eq('')
it "prints an empty message" do
prompt.say("")
expect(prompt.output.string).to eq("")
end

context 'with new line' do
it 'prints a message with newline' do
context "with new line" do
it "prints a message with newline" do
prompt.say("Hell yeah!\n")
expect(prompt.output.string).to eq("Hell yeah!\n")
end

it 'prints a message with implicit newline' do
it "prints a message with implicit newline" do
prompt.say("Hell yeah!\n")
expect(prompt.output.string).to eq("Hell yeah!\n")
end

it 'prints a message with newline within text' do
it "prints a message with newline within text" do
prompt.say("Hell\n yeah!")
expect(prompt.output.string).to eq("Hell\n yeah!\n")
end

it 'prints a message with newline within text and blank space' do
it "prints a message with newline within text and blank space" do
prompt.say("Hell\n yeah! ")
expect(prompt.output.string).to eq("Hell\n yeah! ")
end

it 'prints a message without newline' do
it "prints a message without newline" do
prompt.say("Hell yeah!", newline: false)
expect(prompt.output.string).to eq("Hell yeah!")
end
end

context 'with tab or space' do
it 'prints ' do
context "with tab or space" do
it "prints " do
prompt.say("Hell yeah!\t")
expect(prompt.output.string).to eq("Hell yeah!\t")
end
end

context 'with color' do
it 'prints message with ansi color' do
prompt.say('Hell yeah!', color: :green)
context "with color" do
it "prints message with ansi color" do
prompt.say("Hell yeah!", color: :green)
expect(prompt.output.string).to eq("\e[32mHell yeah!\e[0m\n")
end

it 'prints message with ansi color without newline' do
prompt.say('Hell yeah! ', color: :green)
it "prints message with ansi color without newline" do
prompt.say("Hell yeah! ", color: :green)
expect(prompt.output.string).to eq("\e[32mHell yeah! \e[0m")
end
end

context 'without color' do
it 'prints message without ansi' do
context "without color" do
it "prints message without ansi" do
prompt = TTY::TestPrompt.new(enable_color: false)

prompt.say('Hell yeah!', color: :green)
prompt.say("Hell yeah!", color: :green)

expect(prompt.output.string).to eq("Hell yeah!\n")
end
Expand Down
17 changes: 11 additions & 6 deletions spec/unit/warn_spec.rb
Original file line number Diff line number Diff line change
@@ -1,21 +1,26 @@
# frozen_string_literal: true

RSpec.describe TTY::Prompt, '#warn' do

RSpec.describe TTY::Prompt, "#warn" do
subject(:prompt) { TTY::TestPrompt.new }

it 'displays one message' do
it "displays one message" do
prompt.warn "Careful young apprentice!"
expect(prompt.output.string).to eql "\e[33mCareful young apprentice!\e[0m\n"
end

it 'displays many messages' do
it "displays many messages" do
prompt.warn "Careful there!", "It's dangerous!"
expect(prompt.output.string).to eql "\e[33mCareful there!\e[0m\n\e[33mIt's dangerous!\e[0m\n"
expect(prompt.output.string).to eq(
"\e[33mCareful there!\e[0m\n\e[33mIt's dangerous!\e[0m\n")
end

it 'displays message with option' do
it "displays message with option" do
prompt.warn "Careful young apprentice!", newline: false
expect(prompt.output.string).to eql "\e[33mCareful young apprentice!\e[0m"
end

it "changes default yellow color to cyan" do
prompt.warn("All is fine", color: :cyan)
expect(prompt.output.string).to eq("\e[36mAll is fine\e[0m\n")
end
end

0 comments on commit 6287633

Please sign in to comment.