Class: Downup::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/downup/base.rb

Overview

The main interface to use Downup

initialize a Downup::Base object with all your desired options and call #prompt to retrieve the user selection.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options:, flash_message: nil, flash_color: :green, default_color: :brown, selected_color: :magenta, multi_select_selector: "√", selector: "‣", type: :default, stdin: $stdin, stdout: $stdout, header_proc: Proc.new{}) ⇒ Base

Returns a new instance of Base.

Examples:

array of options

Downup::Base.new(options: ["option 1", "option 2"])

hash of options

Downup::Base.new(options: {"a" => "option 1", "b" => "option 2"})

hash with “value” and “display” keys

Downup::Base.new(options: {"a" => {"value" => "option 1", "display" => "Option 1"}})

header_proc example

Downup::Base.new(options: [], header_proc: Proc.new {puts "Hello"})


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
# File 'lib/downup/base.rb', line 38

def initialize(options:,
               flash_message:         nil,
               flash_color:           :green,
               default_color:         :brown,
               selected_color:        :magenta,
               multi_select_selector: "",
               selector:              "",
               type:                  :default,
               stdin:                 $stdin,
               stdout:                $stdout,
               header_proc:           Proc.new{})

  @options                  = options
  @flash_color              = flash_color
  @flash_message            = flash_message
  @default_color            = default_color
  @selected_color           = selected_color
  @selector                 = selector
  @type                     = type
  @header_proc              = header_proc
  @stdin                    = stdin
  @stdout                   = stdout
  @colonel                  = Kernel
  @multi_select_selector    = multi_select_selector
  @multi_selected_positions = []
end

Instance Attribute Details

#selected_positionObject (readonly)

Returns the value of attribute selected_position.



15
16
17
# File 'lib/downup/base.rb', line 15

def selected_position
  @selected_position
end

Instance Method Details

#prompt(position = 0) ⇒ String

Prompts the user to make selection from the options the object with initialized with.



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/downup/base.rb', line 69

def prompt(position = 0)
  @selected_position = position_selector(position)
  colonel.system("clear")
  header_proc.call
  print_flash
  Downup::OptionsPrinter.new(
    options: options,
    selected_position: @selected_position,
    multi_selected_positions: @multi_selected_positions,
    multi_select_selector: multi_select_selector,
    default_color: default_color,
    selected_color: selected_color,
    selector: selector,
    stdin: stdin,
    stdout: stdout,
  ).print_options
  stdout.print "\n> "
  input = read_char
  process_input input
end