Skip to content

Commit

Permalink
Add ability to quiet output from ask prompt
Browse files Browse the repository at this point in the history
  • Loading branch information
piotrmurach committed Jul 2, 2020
1 parent fa02a81 commit fbfbb1c
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 19 deletions.
10 changes: 9 additions & 1 deletion lib/tty/prompt/question.rb
Expand Up @@ -54,6 +54,7 @@ def initialize(prompt, **options)
@help_color = options.fetch(:help_color) { @prompt.help_color }
@error_color = options.fetch(:error_color) { :red }
@value = options.fetch(:value) { UndefinedSetting }
@quiet = options.fetch(:quiet) { @prompt.quiet }
@messages = Utils.deep_copy(options.fetch(:messages) { { } })
@done = false
@first_render = true
Expand Down Expand Up @@ -129,7 +130,7 @@ def render
total_lines = @prompt.count_screen_lines(input_line)
@prompt.print(refresh(question.lines.count, total_lines))
end
@prompt.print(render_question)
@prompt.print(render_question) unless @quiet
result.value
end

Expand Down Expand Up @@ -362,6 +363,13 @@ def in?
@in != UndefinedSetting
end

# Set quiet mode.
#
# @api public
def quiet(value)
@quiet = value
end

# @api public
def to_s
message.to_s
Expand Down
54 changes: 36 additions & 18 deletions spec/unit/ask_spec.rb
@@ -1,19 +1,19 @@
# frozen_string_literal: true

RSpec.describe TTY::Prompt, '#ask' do
RSpec.describe TTY::Prompt, "#ask" do

subject(:prompt) { TTY::TestPrompt.new }

it 'asks question' do
prompt.ask('What is your name?')
it "asks question" do
prompt.ask("What is your name?")
expect(prompt.output.string).to eq([
"What is your name? ",
"\e[1A\e[2K\e[1G",
"What is your name? \n"
].join)
end

it 'asks an empty question ' do
it "asks an empty question " do
prompt = TTY::TestPrompt.new
prompt.input << "\r"
prompt.input.rewind
Expand All @@ -29,7 +29,7 @@
prompt.input << nil
prompt.input.rewind

answer = prompt.ask('')
answer = prompt.ask("")

expect(answer).to eql(nil)
expect(prompt.output.string).to eq("\e[1A\e[2K\e[1G\n")
Expand Down Expand Up @@ -71,7 +71,7 @@
prompt = TTY::TestPrompt.new(prefix: "[?] ")
prompt.input << "\r"
prompt.input.rewind
answer = prompt.ask 'Are you Polish?'
answer = prompt.ask "Are you Polish?"
expect(answer).to eq(nil)
expect(prompt.output.string).to eq([
"[?] Are you Polish? ",
Expand All @@ -81,13 +81,13 @@
].join)
end

it 'asks a question with block' do
prompt.input << ''
it "asks a question with block" do
prompt.input << ""
prompt.input.rewind
answer = prompt.ask "What is your name?" do |q|
q.default 'Piotr'
q.default "Piotr"
end
expect(answer).to eq('Piotr')
expect(answer).to eq("Piotr")
expect(prompt.output.string).to eq([
"What is your name? \e[90m(Piotr)\e[0m ",
"\e[1A\e[2K\e[1G",
Expand All @@ -96,11 +96,11 @@
end

it "changes question color" do
prompt.input << ''
prompt.input << ""
prompt.input.rewind
options = {default: 'Piotr', help_color: :red, active_color: :cyan}
options = {default: "Piotr", help_color: :red, active_color: :cyan}
answer = prompt.ask("What is your name?", **options)
expect(answer).to eq('Piotr')
expect(answer).to eq("Piotr")
expect(prompt.output.string).to eq([
"What is your name? \e[31m(Piotr)\e[0m ",
"\e[1A\e[2K\e[1G",
Expand All @@ -112,8 +112,8 @@
prompt.input << "\r"
prompt.input.rewind

answer = prompt.ask("What is your name?", default: '')
expect(answer).to eq('')
answer = prompt.ask("What is your name?", default: "")
expect(answer).to eq("")
expect(prompt.output.string).to eq([
"What is your name? ",
"\e[2K\e[1GWhat is your name? \n",
Expand All @@ -136,6 +136,24 @@
].join)
end

it "sets quiet mode" do
prompt.ask("What is your name?", quiet: true)
expect(prompt.output.string).to eq([
"What is your name? ",
"\e[1A\e[2K\e[1G"
].join)
end

it "sets quiet mode through DSL" do
prompt.ask("What is your name?") do |q|
q.quiet true
end
expect(prompt.output.string).to eq([
"What is your name? ",
"\e[1A\e[2K\e[1G"
].join)
end

it "overwrites global settings" do
active = ->(str) { Pastel.new.cyan(str) }
help = Pastel.new.red.detach
Expand All @@ -144,12 +162,12 @@

prompt.input << "Piotr\r"
prompt.input.rewind
prompt.ask('What is your name?')
prompt.ask("What is your name?")

prompt.input << "Piotr\r"
prompt.input.rewind
local_settings = {prefix: ':-) ', active_color: :blue, help_color: :magenta}
prompt.ask('What is your name?', **local_settings)
local_settings = {prefix: ":-) ", active_color: :blue, help_color: :magenta}
prompt.ask("What is your name?", **local_settings)

expect(prompt.output.string).to eq([
"[?] What is your name? ",
Expand Down
14 changes: 14 additions & 0 deletions spec/unit/multiline_spec.rb
Expand Up @@ -49,6 +49,20 @@
].join)
end

it "sets quiet mode" do
prompt = TTY::TestPrompt.new(quiet: true)
prompt.input << "\C-d"
prompt.input.rewind

answer = prompt.multiline("Description?", default: "A super sweet prompt")

expect(answer).to eq("A super sweet prompt")
expect(prompt.output.string).to eq([
"Description? \e[90m(Press Ctrl+D or Ctrl+Z to finish)\e[0m\n",
"\e[2K\e[1G\e[1A\e[2K\e[1G"
].join)
end

it "reads multiple lines with empty lines" do
prompt = TTY::TestPrompt.new
prompt.input << "aa\n\nbb\n\n\ncc\C-d"
Expand Down
21 changes: 21 additions & 0 deletions spec/unit/quiet_spec.rb
@@ -0,0 +1,21 @@
# frozen_string_literal: true

RSpec.describe TTY::Prompt::Question, "#default" do

subject(:prompt) { TTY::TestPrompt.new(quiet: true) }

it "sets quiet mode" do
name = "Anonymous"
prompt.input << "\n"
prompt.input.rewind

answer = prompt.ask("What is your name?", default: name)
expect(answer).to eq(name)

expect(prompt.output.string).to eq([
"What is your name? \e[90m(Anonymous)\e[0m ",
"\e[2K\e[1GWhat is your name? \e[90m(Anonymous)\e[0m \n",
"\e[1A\e[2K\e[1G",
].join)
end
end

0 comments on commit fbfbb1c

Please sign in to comment.