Skip to content

Allow CLI::UI.ask to work without a question parameter #594

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .claude/settings.local.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"permissions": {
"allow": [
"Bash(git add:*)"
],
"deny": []
}
}
5 changes: 4 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
cli-ui (2.3.1)
cli-ui (2.4.0)

GEM
remote: https://rubygems.org/
Expand All @@ -14,6 +14,7 @@ GEM
docile (1.4.0)
erubi (1.13.0)
json (2.9.1)
json (2.9.1-java)
language_server-protocol (3.17.0.4)
method_source (1.1.0)
minitest (5.25.4)
Expand All @@ -31,6 +32,7 @@ GEM
racc
prism (1.2.0)
racc (1.8.1)
racc (1.8.1-java)
rainbow (3.1.1)
rake (13.2.1)
rbi (0.2.1)
Expand Down Expand Up @@ -66,6 +68,7 @@ GEM
sorbet (0.5.11781)
sorbet-static (= 0.5.11781)
sorbet-runtime (0.5.11781)
sorbet-static (0.5.11781-java)
sorbet-static (0.5.11781-universal-darwin)
sorbet-static (0.5.11781-x86_64-linux)
sorbet-static-and-runtime (0.5.11781)
Expand Down
4 changes: 2 additions & 2 deletions lib/cli/ui.rb
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ def any_key(prompt = 'Press any key to continue')
# Convenience Method for +CLI::UI::Prompt.ask+
sig do
params(
question: String,
question: T.nilable(String),
options: T.nilable(T::Array[String]),
default: T.nilable(T.any(String, T::Array[String])),
is_file: T::Boolean,
Expand All @@ -117,7 +117,7 @@ def any_key(prompt = 'Press any key to continue')
).returns(T.any(String, T::Array[String]))
end
def ask(
question,
question = nil,
options: nil,
default: nil,
is_file: false,
Expand Down
16 changes: 9 additions & 7 deletions lib/cli/ui/prompt.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def instructions_color=(color)
#
# ==== Attributes
#
# * +question+ - (required) The question to ask the user
# * +question+ - (optional) The question to ask the user
#
# ==== Options
#
Expand Down Expand Up @@ -104,7 +104,7 @@ def instructions_color=(color)
#
sig do
params(
question: String,
question: T.nilable(String),
options: T.nilable(T::Array[String]),
default: T.nilable(T.any(String, T::Array[String])),
is_file: T::Boolean,
Expand All @@ -116,7 +116,7 @@ def instructions_color=(color)
).returns(T.any(String, T::Array[String]))
end
def ask(
question,
question = nil,
options: nil,
default: nil,
is_file: false,
Expand Down Expand Up @@ -234,7 +234,7 @@ def read_char
private

sig do
params(question: String, default: T.nilable(String), is_file: T::Boolean, allow_empty: T::Boolean)
params(question: T.nilable(String), default: T.nilable(String), is_file: T::Boolean, allow_empty: T::Boolean)
.returns(String)
end
def ask_free_form(question, default, is_file, allow_empty)
Expand All @@ -245,7 +245,7 @@ def ask_free_form(question, default, is_file, allow_empty)
CLI::UI::StdoutRouter::Capture.in_alternate_screen do
if default
puts_question("#{question} (empty = #{default})")
else
elsif question
puts_question(question)
end

Expand All @@ -267,7 +267,7 @@ def ask_free_form(question, default, is_file, allow_empty)

sig do
params(
question: String,
question: T.nilable(String),
options: T.nilable(T::Array[String]),
multiple: T::Boolean,
default: T.nilable(T.any(String, T::Array[String])),
Expand Down Expand Up @@ -299,7 +299,9 @@ def ask_interactive(question, options = nil, multiple: false, default: nil, filt
resp = T.let([], T.any(String, T::Array[String]))

CLI::UI::StdoutRouter::Capture.in_alternate_screen do
puts_question("#{question} " + instructions_color.code + "(#{instructions})" + Color::RESET.code)
if question
puts_question("#{question} " + instructions_color.code + "(#{instructions})" + Color::RESET.code)
end
resp = interactive_prompt(options, multiple: multiple, default: default)

# Clear the line
Expand Down
2 changes: 1 addition & 1 deletion lib/cli/ui/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@

module CLI
module UI
VERSION = '2.3.1'
VERSION = '2.4.0'
end
end
6 changes: 6 additions & 0 deletions test/cli/ui/prompt_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,12 @@ def test_ask_free_form_default_nondefault
assert_output_includes('--zxcv--')
end

def test_ask_without_question_or_default
run_in_process('puts "--#{CLI::UI::Prompt.ask}--"')
write("user input\n")
assert_output_includes('--user input--')
end

def test_ask_invalid_kwargs
error = assert_raises(ArgumentError) { Prompt.ask('q', options: ['a'], is_file: true) }
assert_equal('conflicting arguments: is_file is only useful when options are not provided', error.message)
Expand Down