Class: RubyCurses::Bottomline::Question
- Defined in:
- lib/rbcurse/extras/bottomline.rb
Overview
do nothing, just creating a unique error type
Direct Known Subclasses
Defined Under Namespace
Classes: NoAutoCompleteMatch
Instance Attribute Summary collapse
-
#_case ⇒ Object
Used to control character case processing for the answer to this question.
-
#above ⇒ Object
Used to control range checks for answer.
-
#answer_type ⇒ Object
The type that will be used to convert this answer.
-
#below ⇒ Object
Used to control range checks for answer.
-
#change_proc ⇒ Object
Called when any character is pressed with the string.
-
#character ⇒ Object
Can be set to
trueto use HighLine’s cross-platform character reader instead of fetching an entire line of input. -
#color_pair ⇒ Object
Returns the value of attribute color_pair.
-
#completion_proc ⇒ Object
If the user presses tab in ask(), then this proc is used to fill in values.
-
#confirm ⇒ Object
Asks a yes or no confirmation question, to ensure a user knows what they have just agreed to.
-
#default ⇒ Object
Used to provide a default answer to this question.
-
#directory ⇒ Object
The directory from which a user will be allowed to select files, when File or Pathname is specified as an answer_type.
-
#echo ⇒ Object
Can be set to
trueorfalseto control whether or not input will be echoed back to the user. -
#first_answer ⇒ Object
Returns first_answer, which will be unset following this call.
-
#gather ⇒ Object
When set, the user will be prompted for multiple answers which will be collected into an Array or Hash and returned as the final answer.
-
#glob ⇒ Object
The glob pattern used to limit file selection when File or Pathname is specified as an answer_type.
-
#helptext ⇒ Object
text to be shown if user presses M-h.
-
#history ⇒ Object
Returns the value of attribute history.
-
#in ⇒ Object
If set, answer must pass an include?() check on this object.
-
#limit ⇒ Object
Allows you to set a character limit for input.
-
#overwrite ⇒ Object
When set to
truethe question is asked, but output does not progress to the next line. -
#question ⇒ Object
The ERb template of the question to be asked.
-
#readline ⇒ Object
Use the Readline library to fetch input.
-
#responses ⇒ Object
readonly
A Hash that stores the various responses used by HighLine to notify the user.
-
#validate ⇒ Object
If set to a Regexp, the answer must match (before type conversion).
-
#whitespace ⇒ Object
Used to control whitespace processing for the answer to this question.
Instance Method Summary collapse
-
#answer_or_default(answer_string) ⇒ Object
Returns the provided answer_string or the default answer for this Question if a default was set and the answer is empty.
-
#build_responses ⇒ Object
Called late in the initialization process to build intelligent responses based on the details of this Question object.
-
#change_case(answer_string) ⇒ Object
Returns the provided answer_string after changing character case by the rules of this Question.
-
#convert(answer_string) ⇒ Object
Transforms the given answer_string into the expected type for this Question.
-
#expected_range ⇒ Object
Returns a english explination of the current range settings.
-
#first_answer? ⇒ Boolean
Returns true if first_answer is set.
-
#in_range?(answer_object) ⇒ Boolean
Returns
trueif the answer_object is greater than the above attribute, less than the below attribute and included?()ed in the in attribute. -
#initialize(question, answer_type) {|_self| ... } ⇒ Question
constructor
Create an instance of HighLine::Question.
-
#remove_whitespace(answer_string) ⇒ Object
Returns the provided answer_string after processing whitespace by the rules of this Question.
-
#selection ⇒ Object
Returns an Array of valid answers to this question.
-
#to_str ⇒ Object
Stringifies the question to be asked.
-
#valid_answer?(answer_string) ⇒ Boolean
Returns
trueif the provided answer_string is accepted by the validate attribute orfalseif it’s not.
Constructor Details
#initialize(question, answer_type) {|_self| ... } ⇒ Question
Create an instance of HighLine::Question. Expects a question to ask (can be "") and an answer_type to convert the answer to. The answer_type parameter must be a type recognized by Question.convert(). If given, a block is yeilded the new Question object to allow custom initializaion.
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 |
# File 'lib/rbcurse/extras/bottomline.rb', line 123 def initialize( question, answer_type ) # initialize instance data @question = question @answer_type = answer_type @character = nil @limit = nil @echo = true @readline = false @whitespace = :strip @_case = nil @default = nil @validate = nil @above = nil @below = nil @in = nil @confirm = nil @gather = false @first_answer = nil @directory = Pathname.new(File.(File.dirname($0))) @glob = "*" @responses = Hash.new @overwrite = false @history = nil # allow block to override settings yield self if block_given? #$log.debug " XXX default #{@default}" if $log.debug? #$log.debug " XXX history #{@history}" if $log.debug? # finalize responses based on settings build_responses end |
Instance Attribute Details
#_case ⇒ Object
Used to control character case processing for the answer to this question. See HighLine::Question.change_case() for acceptable settings.
213 214 215 |
# File 'lib/rbcurse/extras/bottomline.rb', line 213 def _case @_case end |
#above ⇒ Object
Used to control range checks for answer.
223 224 225 |
# File 'lib/rbcurse/extras/bottomline.rb', line 223 def above @above end |
#answer_type ⇒ Object
The type that will be used to convert this answer.
161 162 163 |
# File 'lib/rbcurse/extras/bottomline.rb', line 161 def answer_type @answer_type end |
#below ⇒ Object
Used to control range checks for answer.
223 224 225 |
# File 'lib/rbcurse/extras/bottomline.rb', line 223 def below @below end |
#change_proc ⇒ Object
Called when any character is pressed with the string.
q.change_proc = Proc.new {|str| Dir.glob(str +"*") }
312 313 314 |
# File 'lib/rbcurse/extras/bottomline.rb', line 312 def change_proc @change_proc end |
#character ⇒ Object
Can be set to true to use HighLine’s cross-platform character reader instead of fetching an entire line of input. (Note: HighLine’s character reader ONLY supports STDIN on Windows and Unix.) Can also be set to :getc to use that method on the input stream.
WARNING: The echo and overwrite attributes for a question are ignored when using the :getc method.
171 172 173 |
# File 'lib/rbcurse/extras/bottomline.rb', line 171 def character @character end |
#color_pair ⇒ Object
Returns the value of attribute color_pair.
317 318 319 |
# File 'lib/rbcurse/extras/bottomline.rb', line 317 def color_pair @color_pair end |
#completion_proc ⇒ Object
If the user presses tab in ask(), then this proc is used to fill in values. Typically, for files. e.g.
q.completion_proc = Proc.new {|str| Dir.glob(str +"*") }
305 306 307 |
# File 'lib/rbcurse/extras/bottomline.rb', line 305 def completion_proc @completion_proc end |
#confirm ⇒ Object
Asks a yes or no confirmation question, to ensure a user knows what they have just agreed to. If set to true the question will be, “Are you sure? ” Any other true value for this attribute is assumed to be the question to ask. When false or nil (the default), answers are not confirmed.
233 234 235 |
# File 'lib/rbcurse/extras/bottomline.rb', line 233 def confirm @confirm end |
#default ⇒ Object
Used to provide a default answer to this question.
215 216 217 |
# File 'lib/rbcurse/extras/bottomline.rb', line 215 def default @default end |
#directory ⇒ Object
The directory from which a user will be allowed to select files, when File or Pathname is specified as an answer_type. Initially set to Pathname.new(File.expand_path(File.dirname($0))).
260 261 262 |
# File 'lib/rbcurse/extras/bottomline.rb', line 260 def directory @directory end |
#echo ⇒ Object
Can be set to true or false to control whether or not input will be echoed back to the user. A setting of true will cause echo to match input, but any other true value will be treated as to String to echo for each character typed.
This requires HighLine’s character reader. See the character attribute for details.
Note: When using HighLine to manage echo on Unix based systems, we recommend installing the termios gem. Without it, it’s possible to type fast enough to have letters still show up (when reading character by character only).
192 193 194 |
# File 'lib/rbcurse/extras/bottomline.rb', line 192 def echo @echo end |
#first_answer ⇒ Object
Returns first_answer, which will be unset following this call.
469 470 471 472 473 |
# File 'lib/rbcurse/extras/bottomline.rb', line 469 def first_answer( ) @first_answer ensure @first_answer = nil end |
#gather ⇒ Object
When set, the user will be prompted for multiple answers which will be collected into an Array or Hash and returned as the final answer.
You can set gather to an Integer to have an Array of exactly that many answers collected, or a String/Regexp to match an end input which will not be returned in the Array.
Optionally gather can be set to a Hash. In this case, the question will be asked once for each key and the answers will be returned in a Hash, mapped by key. The @key variable is set before each question is evaluated, so you can use it in your question.
247 248 249 |
# File 'lib/rbcurse/extras/bottomline.rb', line 247 def gather @gather end |
#glob ⇒ Object
The glob pattern used to limit file selection when File or Pathname is specified as an answer_type. Initially set to "*".
265 266 267 |
# File 'lib/rbcurse/extras/bottomline.rb', line 265 def glob @glob end |
#helptext ⇒ Object
text to be shown if user presses M-h
316 317 318 |
# File 'lib/rbcurse/extras/bottomline.rb', line 316 def helptext @helptext end |
#history ⇒ Object
Returns the value of attribute history.
318 319 320 |
# File 'lib/rbcurse/extras/bottomline.rb', line 318 def history @history end |
#in ⇒ Object
If set, answer must pass an include?() check on this object.
225 226 227 |
# File 'lib/rbcurse/extras/bottomline.rb', line 225 def in @in end |
#limit ⇒ Object
Allows you to set a character limit for input.
If not set, a default of 100 is used
177 178 179 |
# File 'lib/rbcurse/extras/bottomline.rb', line 177 def limit @limit end |
#overwrite ⇒ Object
When set to true the question is asked, but output does not progress to the next line. The Cursor is moved back to the beginning of the question line and it is cleared so that all the contents of the line disappear from the screen.
297 298 299 |
# File 'lib/rbcurse/extras/bottomline.rb', line 297 def overwrite @overwrite end |
#question ⇒ Object
The ERb template of the question to be asked.
159 160 161 |
# File 'lib/rbcurse/extras/bottomline.rb', line 159 def question @question end |
#readline ⇒ Object
Use the Readline library to fetch input. This allows input editing as well as keeping a history. In addition, tab will auto-complete within an Array of choices or a file listing.
WARNING: This option is incompatible with all of HighLine’s character reading modes and it causes HighLine to ignore the specified input stream.
this messes up in ncurses RK 2010-10-24 12:23
203 204 205 |
# File 'lib/rbcurse/extras/bottomline.rb', line 203 def readline @readline end |
#responses ⇒ Object (readonly)
A Hash that stores the various responses used by HighLine to notify the user. The currently used responses and their purpose are as follows:
:ambiguous_completion-
Used to notify the user of an ambiguous answer the auto-completion system cannot resolve.
:ask_on_error-
This is the question that will be redisplayed to the user in the event of an error. Can be set to
:questionto repeat the original question. :invalid_type-
The error message shown when a type conversion fails.
:no_completion-
Used to notify the user that their selection does not have a valid auto-completion match.
:not_in_range-
Used to notify the user that a provided answer did not satisfy the range requirement tests.
:not_valid-
The error message shown when validation checks fail.
290 291 292 |
# File 'lib/rbcurse/extras/bottomline.rb', line 290 def responses @responses end |
#validate ⇒ Object
If set to a Regexp, the answer must match (before type conversion). Can also be set to a Proc which will be called with the provided answer to validate with a true or false return.
221 222 223 |
# File 'lib/rbcurse/extras/bottomline.rb', line 221 def validate @validate end |
#whitespace ⇒ Object
Used to control whitespace processing for the answer to this question. See HighLine::Question.remove_whitespace() for acceptable settings.
208 209 210 |
# File 'lib/rbcurse/extras/bottomline.rb', line 208 def whitespace @whitespace end |
Instance Method Details
#answer_or_default(answer_string) ⇒ Object
Returns the provided answer_string or the default answer for this Question if a default was set and the answer is empty. NOTE: in our case, the user actually edits this value (in highline it is used if user enters blank)
326 327 328 329 330 331 332 |
# File 'lib/rbcurse/extras/bottomline.rb', line 326 def answer_or_default( answer_string ) if answer_string.length == 0 and not @default.nil? @default else answer_string end end |
#build_responses ⇒ Object
Called late in the initialization process to build intelligent responses based on the details of this Question object.
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 |
# File 'lib/rbcurse/extras/bottomline.rb', line 338 def build_responses( ) ### WARNING: This code is quasi-duplicated in ### ### Menu.update_responses(). Check there too when ### ### making changes! ### append_default unless default.nil? @responses = { :ambiguous_completion => "Ambiguous choice. " + "Please choose one of #{@answer_type.inspect}.", :ask_on_error => "? ", :invalid_type => "You must enter a valid #{@answer_type}.", :no_completion => "You must choose one of " + "#{@answer_type.inspect}.", :not_in_range => "Your answer isn't within the expected range " + "(#{expected_range}).", :not_valid => "Your answer isn't valid (must match " + "#{@validate.inspect})." }.merge(@responses) ### WARNING: This code is quasi-duplicated in ### ### Menu.update_responses(). Check there too when ### ### making changes! ### end |
#change_case(answer_string) ⇒ Object
Returns the provided answer_string after changing character case by the rules of this Question. Valid settings for whitespace are:
nil-
Do not alter character case. (Default.)
:up-
Calls upcase().
:upcase-
Calls upcase().
:down-
Calls downcase().
:downcase-
Calls downcase().
:capitalize-
Calls capitalize().
An unrecognized choice (like :none) is treated as nil.
378 379 380 381 382 383 384 385 386 387 388 |
# File 'lib/rbcurse/extras/bottomline.rb', line 378 def change_case( answer_string ) if [:up, :upcase].include?(@_case) answer_string.upcase elsif [:down, :downcase].include?(@_case) answer_string.downcase elsif @_case == :capitalize answer_string.capitalize else answer_string end end |
#convert(answer_string) ⇒ Object
Transforms the given answer_string into the expected type for this Question. Currently supported conversions are:
[...]-
Answer must be a member of the passed Array. Auto-completion is used to expand partial answers.
lambda {...}-
Answer is passed to lambda for conversion.
- Date
-
Date.parse() is called with answer.
- DateTime
-
DateTime.parse() is called with answer.
- File
-
The entered file name is auto-completed in terms of directory + glob, opened, and returned.
- Float
-
Answer is converted with Kernel.Float().
- Integer
-
Answer is converted with Kernel.Integer().
nil-
Answer is left in String format. (Default.)
- Pathname
-
Same as File, save that a Pathname object is returned.
- String
-
Answer is converted with Kernel.String().
- Regexp
-
Answer is fed to Regexp.new().
- Symbol
-
The method to_sym() is called on answer and the result returned.
- any other Class
-
The answer is passed on to
Class.parse().
This method throws ArgumentError, if the conversion cannot be completed for any reason.
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 |
# File 'lib/rbcurse/extras/bottomline.rb', line 418 def convert( answer_string ) if @answer_type.nil? answer_string elsif [Float, Integer, String].include?(@answer_type) Kernel.send(@answer_type.to_s.to_sym, answer_string) elsif @answer_type == Symbol answer_string.to_sym elsif @answer_type == Regexp Regexp.new(answer_string) elsif @answer_type.is_a?(Array) or [File, Pathname].include?(@answer_type) # cheating, using OptionParser's Completion module choices = selection #choices.extend(OptionParser::Completion) #answer = choices.complete(answer_string) answer = choices # bug in completion of optparse if answer.nil? raise NoAutoCompleteMatch end if @answer_type.is_a?(Array) #answer.last # we don't need this anylonger answer_string # we have already selected elsif @answer_type == File File.open(File.join(@directory.to_s, answer_string)) else #Pathname.new(File.join(@directory.to_s, answer.last)) Pathname.new(File.join(@directory.to_s, answer_string)) end elsif [Date, DateTime].include?(@answer_type) or @answer_type.is_a?(Class) @answer_type.parse(answer_string) elsif @answer_type.is_a?(Proc) @answer_type[answer_string] end end |
#expected_range ⇒ Object
Returns a english explination of the current range settings.
453 454 455 456 457 458 459 460 461 462 463 464 465 466 |
# File 'lib/rbcurse/extras/bottomline.rb', line 453 def expected_range( ) expected = [ ] expected << "above #{@above}" unless @above.nil? expected << "below #{@below}" unless @below.nil? expected << "included in #{@in.inspect}" unless @in.nil? case expected.size when 0 then "" when 1 then expected.first when 2 then expected.join(" and ") else expected[0..-2].join(", ") + ", and #{expected.last}" end end |
#first_answer? ⇒ Boolean
Returns true if first_answer is set.
476 477 478 |
# File 'lib/rbcurse/extras/bottomline.rb', line 476 def first_answer?( ) not @first_answer.nil? end |
#in_range?(answer_object) ⇒ Boolean
Returns true if the answer_object is greater than the above attribute, less than the below attribute and included?()ed in the in attribute. Otherwise, false is returned. Any nil attributes are not checked.
486 487 488 489 490 |
# File 'lib/rbcurse/extras/bottomline.rb', line 486 def in_range?( answer_object ) (@above.nil? or answer_object > @above) and (@below.nil? or answer_object < @below) and (@in.nil? or @in.include?(answer_object)) end |
#remove_whitespace(answer_string) ⇒ Object
Returns the provided answer_string after processing whitespace by the rules of this Question. Valid settings for whitespace are:
nil-
Do not alter whitespace.
:strip-
Calls strip(). (Default.)
:chomp-
Calls chomp().
:collapse-
Collapses all whitspace runs to a single space.
:strip_and_collapse-
Calls strip(), then collapses all whitspace runs to a single space.
:chomp_and_collapse-
Calls chomp(), then collapses all whitspace runs to a single space.
:remove-
Removes all whitespace.
An unrecognized choice (like :none) is treated as nil.
This process is skipped, for single character input.
511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 |
# File 'lib/rbcurse/extras/bottomline.rb', line 511 def remove_whitespace( answer_string ) if @whitespace.nil? answer_string elsif [:strip, :chomp].include?(@whitespace) answer_string.send(@whitespace) elsif @whitespace == :collapse answer_string.gsub(/\s+/, " ") elsif [:strip_and_collapse, :chomp_and_collapse].include?(@whitespace) result = answer_string.send(@whitespace.to_s[/^[a-z]+/]) result.gsub(/\s+/, " ") elsif @whitespace == :remove answer_string.gsub(/\s+/, "") else answer_string end end |
#selection ⇒ Object
Returns an Array of valid answers to this question. These answers are only known when answer_type is set to an Array of choices, File, or Pathname. Any other time, this method will return an empty Array.
533 534 535 536 537 538 539 540 541 542 543 |
# File 'lib/rbcurse/extras/bottomline.rb', line 533 def selection( ) if @answer_type.is_a?(Array) @answer_type elsif [File, Pathname].include?(@answer_type) Dir[File.join(@directory.to_s, @glob)].map do |file| File.basename(file) end else [ ] end end |
#to_str ⇒ Object
Stringifies the question to be asked.
546 547 548 |
# File 'lib/rbcurse/extras/bottomline.rb', line 546 def to_str( ) @question end |
#valid_answer?(answer_string) ⇒ Boolean
Returns true if the provided answer_string is accepted by the validate attribute or false if it’s not.
It’s important to realize that an answer is validated after whitespace and case handling.
557 558 559 560 561 |
# File 'lib/rbcurse/extras/bottomline.rb', line 557 def valid_answer?( answer_string ) @validate.nil? or (@validate.is_a?(Regexp) and answer_string =~ @validate) or (@validate.is_a?(Proc) and @validate[answer_string]) end |