1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589
|
# frozen_string_literal: true
require "forwardable"
require "pastel"
require "tty-cursor"
require "tty-reader"
require "tty-screen"
require_relative "prompt/answers_collector"
require_relative "prompt/confirm_question"
require_relative "prompt/errors"
require_relative "prompt/expander"
require_relative "prompt/enum_list"
require_relative "prompt/keypress"
require_relative "prompt/list"
require_relative "prompt/multi_list"
require_relative "prompt/multiline"
require_relative "prompt/mask_question"
require_relative "prompt/question"
require_relative "prompt/slider"
require_relative "prompt/statement"
require_relative "prompt/suggestion"
require_relative "prompt/symbols"
require_relative "prompt/utils"
require_relative "prompt/version"
module TTY
# A main entry for asking prompt questions.
class Prompt
extend Forwardable
# @api private
attr_reader :input
# @api private
attr_reader :output
attr_reader :reader
attr_reader :cursor
# Prompt prefix
#
# @example
# prompt = TTY::Prompt.new(prefix: [?])
#
# @return [String]
#
# @api private
attr_reader :prefix
# Theme colors
#
# @api private
attr_reader :active_color, :help_color, :error_color, :enabled_color
# Quiet mode
#
# @api private
attr_reader :quiet
# The collection of display symbols
#
# @example
# prompt = TTY::Prompt.new(symbols: {marker: ">"})
#
# @return [Hash]
#
# @api private
attr_reader :symbols
def_delegators :@pastel, :strip
def_delegators :@cursor, :clear_lines, :clear_line,
:show, :hide
def_delegators :@reader, :read_char, :read_keypress, :read_line,
:read_multiline, :on, :subscribe, :unsubscribe, :trigger,
:count_screen_lines
def_delegators :@output, :print, :puts, :flush
def self.messages
{
range?: "Value %{value} must be within the range %{in}",
valid?: "Your answer is invalid (must match %{valid})",
required?: "Value must be provided",
convert?: "Cannot convert `%{value}` to '%{type}' type"
}
end
# Initialize a Prompt
#
# @param [IO] :input
# the input stream
# @param [IO] :output
# the output stream
# @param [Hash] :env
# the environment variables
# @param [Hash] :symbols
# the symbols displayed in prompts such as :marker, :cross
# @param options [Boolean] :quiet
# enable quiet mode, don't re-echo the question
# @param [String] :prefix
# the prompt prefix, by default empty
# @param [Symbol] :interrupt
# handling of Ctrl+C key out of :signal, :exit, :noop
# @param [Boolean] :track_history
# disable line history tracking, true by default
# @param [Boolean] :enable_color
# enable color support, true by default
# @param [String,Proc] :active_color
# the color used for selected option
# @param [String,Proc] :help_color
# the color used for help text
# @param [String] :error_color
# the color used for displaying error messages
#
# @api public
def initialize(input: $stdin, output: $stdout, env: ENV, symbols: {},
prefix: "", interrupt: :error, track_history: true,
quiet: false, enable_color: nil, active_color: :green,
help_color: :bright_black, error_color: :red)
@input = input
@output = output
@env = env
@prefix = prefix
@enabled_color = enable_color
@active_color = active_color
@help_color = help_color
@error_color = error_color
@interrupt = interrupt
@track_history = track_history
@symbols = Symbols.symbols.merge(symbols)
@quiet = quiet
@cursor = TTY::Cursor
@pastel = enabled_color.nil? ? Pastel.new : Pastel.new(enabled: enabled_color)
@reader = TTY::Reader.new(
input: input,
output: output,
interrupt: interrupt,
track_history: track_history,
env: env
)
end
# Decorate a string with colors
#
# @param [String] :string
# the string to color
# @param [Array<Proc|Symbol>] :colors
# collection of color symbols or callable object
#
# @api public
def decorate(string, *colors)
if Utils.blank?(string) || @enabled_color == false || colors.empty?
return string
end
coloring = colors.first
if coloring.respond_to?(:call)
coloring.call(string)
else
@pastel.decorate(string, *colors)
end
end
# Invoke a question type of prompt
#
# @example
# prompt = TTY::Prompt.new
# prompt.invoke_question(Question, "Your name? ")
#
# @return [String]
#
# @api public
def invoke_question(object, message, **options, &block)
options[:messages] = self.class.messages
question = object.new(self, **options)
question.(message, &block)
end
# Ask a question.
#
# @example
# propmt = TTY::Prompt.new
# prompt.ask("What is your name?")
#
# @param [String] message
# the question to be asked
#
# @yieldparam [TTY::Prompt::Question] question
# further configure the question
#
# @yield [question]
#
# @return [TTY::Prompt::Question]
#
# @api public
def ask(message = "", **options, &block)
invoke_question(Question, message, **options, &block)
end
# Ask a question with a keypress answer
#
# @see #ask
#
# @api public
def keypress(message = "", **options, &block)
invoke_question(Keypress, message, **options, &block)
end
# Ask a question with a multiline answer
#
# @example
# prompt.multiline("Description?")
#
# @return [Array[String]]
#
# @api public
def multiline(message = "", **options, &block)
invoke_question(Multiline, message, **options, &block)
end
# Invoke a list type of prompt
#
# @example
# prompt = TTY::Prompt.new
# editors = %w(emacs nano vim)
# prompt.invoke_select(EnumList, "Select editor: ", editors)
#
# @return [String]
#
# @api public
def invoke_select(object, question, *args, &block)
options = Utils.extract_options!(args)
choices = if args.empty? && !block
possible = options.dup
options = {}
possible
elsif args.size == 1 && args[0].is_a?(Hash)
Utils.extract_options!(args)
else
args.flatten
end
list = object.new(self, **options)
list.(question, choices, &block)
end
# Ask masked question
#
# @example
# propmt = TTY::Prompt.new
# prompt.mask("What is your secret?")
#
# @return [TTY::Prompt::MaskQuestion]
#
# @api public
def mask(message = "", **options, &block)
invoke_question(MaskQuestion, message, **options, &block)
end
# Ask a question with a list of options
#
# @example
# prompt = TTY::Prompt.new
# prompt.select("What size?", %w(large medium small))
#
# @example
# prompt = TTY::Prompt.new
# prompt.select("What size?") do |menu|
# menu.choice :large
# menu.choices %w(:medium :small)
# end
#
# @param [String] question
# the question to ask
#
# @param [Array[Object]] choices
# the choices to select from
#
# @api public
def select(question, *args, &block)
invoke_select(List, question, *args, &block)
end
# Ask a question with multiple attributes activated
#
# @example
# prompt = TTY::Prompt.new
# choices = %w(Scorpion Jax Kitana Baraka Jade)
# prompt.multi_select("Choose your destiny?", choices)
#
# @param [String] question
# the question to ask
#
# @param [Array[Object]] choices
# the choices to select from
#
# @return [String]
#
# @api public
def multi_select(question, *args, &block)
invoke_select(MultiList, question, *args, &block)
end
# Ask a question with indexed list
#
# @example
# prompt = TTY::Prompt.new
# editors = %w(emacs nano vim)
# prompt.enum_select(EnumList, "Select editor: ", editors)
#
# @param [String] question
# the question to ask
#
# @param [Array[Object]] choices
# the choices to select from
#
# @return [String]
#
# @api public
def enum_select(question, *args, &block)
invoke_select(EnumList, question, *args, &block)
end
# A shortcut method to ask the user positive question and return
# true for "yes" reply, false for "no".
#
# @example
# prompt = TTY::Prompt.new
# prompt.yes?("Are you human?")
# # => Are you human? (Y/n)
#
# @return [Boolean]
#
# @api public
def yes?(message, **options, &block)
opts = { default: true }.merge(options)
question = ConfirmQuestion.new(self, **opts)
question.call(message, &block)
end
# A shortcut method to ask the user negative question and return
# true for "no" reply.
#
# @example
# prompt = TTY::Prompt.new
# prompt.no?("Are you alien?") # => true
# # => Are you human? (y/N)
#
# @return [Boolean]
#
# @api public
def no?(message, **options, &block)
opts = { default: false }.merge(options)
question = ConfirmQuestion.new(self, **opts)
!question.call(message, &block)
end
# Expand available options
#
# @example
# prompt = TTY::Prompt.new
# choices = [{
# key: "Y",
# name: "Overwrite",
# value: :yes
# }, {
# key: "n",
# name: "Skip",
# value: :no
# }]
# prompt.expand("Overwirte Gemfile?", choices)
#
# @return [Object]
# the user specified value
#
# @api public
def expand(message, *args, &block)
invoke_select(Expander, message, *args, &block)
end
# Ask a question with a range slider
#
# @example
# prompt = TTY::Prompt.new
# prompt.slider("What size?", min: 32, max: 54, step: 2)
# prompt.slider("What size?", [ 'xs', 's', 'm', 'l', 'xl' ])
#
# @param [String] question
# the question to ask
#
# @param [Array] choices
# the choices to display
#
# @return [String]
#
# @api public
def slider(question, choices = nil, **options, &block)
slider = Slider.new(self, **options)
slider.call(question, choices, &block)
end
# Print statement out. If the supplied message ends with a space or
# tab character, a new line will not be appended.
#
# @example
# say("Simple things.", color: :red)
#
# @param [String] message
#
# @return [String]
#
# @api public
def say(message = "", **options)
message = message.to_s
return if message.empty?
statement = Statement.new(self, **options)
statement.call(message)
end
# Print statement(s) out in red green.
#
# @example
# prompt.ok "Are you sure?"
# prompt.ok "All is fine!", "This is fine too."
#
# @param [Array] messages
#
# @return [Array] messages
#
# @api public
def ok(*args, **options)
opts = { color: :green }.merge(options)
args.each { |message| say(message, **opts) }
end
# Print statement(s) out in yellow color.
#
# @example
# prompt.warn "This action can have dire consequences"
# prompt.warn "Carefull young apprentice", "This is potentially dangerous"
#
# @param [Array] messages
#
# @return [Array] messages
#
# @api public
def warn(*args, **options)
opts = { color: :yellow }.merge(options)
args.each { |message| say(message, **opts) }
end
# Print statement(s) out in red color.
#
# @example
# prompt.error "Shutting down all systems!"
# prompt.error "Nothing is fine!", "All is broken!"
#
# @param [Array] messages
#
# @return [Array] messages
#
# @api public
def error(*args, **options)
opts = { color: :red }.merge(options)
args.each { |message| say(message, **opts) }
end
# Print debug information in terminal top right corner
#
# @example
# prompt.debug "info1", "info2"
#
# @param [Array] messages
#
# @retrun [nil]
#
# @api public
def debug(*messages)
longest = messages.max_by(&:length).size
width = TTY::Screen.width - longest
print cursor.save
messages.reverse_each do |msg|
print cursor.column(width) + cursor.up + cursor.clear_line_after
print msg
end
ensure
print cursor.restore
end
# Takes the string provided by the user and compare it with other possible
# matches to suggest an unambigous string
#
# @example
# prompt.suggest("sta", ["status", "stage", "commit", "branch"])
# # => "status, stage"
#
# @param [String] message
#
# @param [Array] possibilities
#
# @param [Hash] options
# @option options [String] :indent
# The number of spaces for indentation
# @option options [String] :single_text
# The text for a single suggestion
# @option options [String] :plural_text
# The text for multiple suggestions
#
# @return [String]
#
# @api public
def suggest(message, possibilities, **options)
suggestion = Suggestion.new(**options)
say(suggestion.suggest(message, possibilities))
end
# Gathers more than one aswer
#
# @example
# prompt.collect do
# key(:name).ask("Name?")
# end
#
# @return [Hash]
# the collection of answers
#
# @api public
def collect(**options, &block)
collector = AnswersCollector.new(self, **options)
collector.call(&block)
end
# Check if outputing to terminal
#
# @return [Boolean]
#
# @api public
def tty?
stdout.tty?
end
# Return standard in
#
# @api private
def stdin
$stdin
end
# Return standard out
#
# @api private
def stdout
$stdout
end
# Return standard error
#
# @api private
def stderr
$stderr
end
# Inspect this instance public attributes
#
# @return [String]
#
# @api public
def inspect
attributes = [
:prefix,
:quiet,
:enabled_color,
:active_color,
:error_color,
:help_color,
:input,
:output,
]
name = self.class.name
"#<#{name}#{attributes.map { |attr| " #{attr}=#{send(attr).inspect}" }.join}>"
end
end # Prompt
end # TTY
|