Class: Tuile::Component::PickerWindow

Inherits:
Window
  • Object
show all
Defined in:
lib/tuile/component/picker_window.rb,
sig/tuile.rbs

Overview

A Window that lists options identified by single keyboard keys, asks the user to pick one, and fires a callback with the picked key. Each row is "<key> <caption>":

PickerWindow.open("Sort by", [%w[n name], %w[s size]]) { sort_by(_1) }

┌Sort by───────┐
│n name        │
│s size        │
└──────────────┘

Usable tiled (just add to a Layout and read picks via the block) or as a popup via PickerWindow.open, which wraps it in a Popup that closes itself after a pick. ESC / q close without firing the callback.

Captions paint in the terminal's own foreground. To color them, hand in StyledString captions — the picker styles nothing itself, so an app's own token applies per option:

PickerWindow.open("File", [["o", StyledString.plain("Open")],
                         ["d", theme.fg(:danger, "Delete")]]) { … }

Defined Under Namespace

Classes: Option, PickEvent

Constant Summary collapse

MAX_ITEMS =

Scrolls the window when more items.

Returns:

  • (Integer)
10

Instance Attribute Summary

Attributes inherited from Window

#footer_text

Attributes included from HasContent

#content

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Window

#bottom_border, #caption, #caption=, #content_rect, #focusable?, #footer, #footer=, #handle_focus, #inspect_details, #layout_footer, #relayout, #repaint, #repaint_border, #scrollbar=, #top_border

Methods included from HasCaption

#caption, #caption=, #inspect_details

Methods included from HasContent

#handle_focus

Constructor Details

#initialize(caption, options, &block) ⇒ PickerWindow

@param caption — the window caption.

@param options — pairs of keyboard key and option caption. A caption goes through StyledString.parse, so a plain String, an ANSI-coded one (what Theme#fg returns) and a StyledString are all accepted.

Parameters:

  • caption (String)
  • options (::Array[[String, StyledString]])


51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/tuile/component/picker_window.rb', line 51

def initialize(caption, options, &block)
  raise ArgumentError, "block required" unless block
  raise ArgumentError, "options must not be empty" if options.empty?

  super(caption)
  @options = options.map { Option.new(_1[0], StyledString.parse(_1[1])) }
  @block = block
  list = Component::List.new
  list.renderer = ->(option, _text_width) { StyledString.plain("#{option.key} ") + option.caption }
  list.items = @options
  list.cursor = Component::List::Cursor.new
  list.on_item_chosen { |e| select_option(e.item.key) }
  self.content = list
end

Class Method Details

.open(caption, options, &block) ⇒ void

This method returns an undefined value.

Opens a picker as a popup. Picking an option fires block, then closes the popup; ESC / q close without firing block.

@param caption

@param options

@return — the wrapping popup.

Parameters:

  • caption (String)
  • options (::Array[[String, StyledString]])


101
102
103
104
105
106
# File 'lib/tuile/component/picker_window.rb', line 101

def self.open(caption, options, &block)
  picker = PickerWindow.new(caption, options, &block)
  popup = Popup.new(content: picker)
  picker.on_pick { popup.close }
  popup.open
end

Instance Method Details

#handle_key?(key) ⇒ Boolean

Handles an option-key press. Reached by bubbling: the inner List (the focused component) sees the key first and handles cursor/Enter picks; anything it declines bubbles up here, where a key matching an option's key picks that option.

@param key

Parameters:

  • key (String)

Returns:

  • (Boolean)


84
85
86
87
88
89
90
91
# File 'lib/tuile/component/picker_window.rb', line 84

def handle_key?(key)
  if @options.any? { _1.key == key }
    select_option(key)
    true
  else
    false
  end
end

#on_pick ⇒ Listeners

Fired with a PickEvent after the user picks an option, after the block fires. The Tuile::Component::Popup returned by open registers its own close.

Returns:



76
# File 'lib/tuile/component/picker_window.rb', line 76

listener :on_pick

#select_option(key) ⇒ void

This method returns an undefined value.

@param key

Parameters:

  • key (String)


112
113
114
115
# File 'lib/tuile/component/picker_window.rb', line 112

def select_option(key)
  @block.call(key)
  on_pick.fire(PickEvent.new(source: self))
end