Class: RubyAnything::Windows

Inherits:
BaseWindow show all
Defined in:
lib/ruby-anything/windows.rb

Constant Summary

Constants inherited from BaseWindow

BaseWindow::KEYS

Instance Attribute Summary collapse

Attributes inherited from BaseWindow

#cursor, #top

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseWindow

#before_down, #before_left, #before_right, #before_up, #change_focus_line, #collection, #down, #draw_at, #draw_at!, #enhansive_line, #in_color, #in_pos, #left, #method_missing, #normalize_line, #refresh, #right, #up, #update, #view_collection

Constructor Details

#initialize(items) ⇒ Windows

Returns a new instance of Windows.



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/ruby-anything/windows.rb', line 33

def initialize(items)
  super(screen, h: screen.maxy, w: screen.maxx, y: 0, x: 0)

  @active = true
  @text_window = TextWindow.new self, opt(:text)
  @items_window = ItemsWindow.new self, items, opt(:items)
  PromptWindow.new self, prompt, opt(:prompt)

  @screen.setpos @text_window.begy, @text_window.begx
  @screen.refresh
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class RubyAnything::BaseWindow

Instance Attribute Details

#items_windowObject

Returns the value of attribute items_window.



16
17
18
# File 'lib/ruby-anything/windows.rb', line 16

def items_window
  @items_window
end

#text_windowObject

Returns the value of attribute text_window.



16
17
18
# File 'lib/ruby-anything/windows.rb', line 16

def text_window
  @text_window
end

Class Method Details

.anything(items) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/ruby-anything/windows.rb', line 19

def anything(items)
  windows = new items.extend(Filterable)
  begin
    loop {
      break unless windows.active?
      windows.on_input Curses.getch
    }
    windows.selected_item
  ensure
    windows.close unless Curses.closed?
  end
end

Instance Method Details

#active?Boolean

Returns:

  • (Boolean)


45
# File 'lib/ruby-anything/windows.rb', line 45

def active?; @active end

#closeObject



81
82
83
84
85
86
# File 'lib/ruby-anything/windows.rb', line 81

def close
  items_window.close
  text_window.close
  @c_window.close
  Curses.close_screen
end

#configure_cursesObject



88
89
90
91
92
93
94
95
96
97
98
# File 'lib/ruby-anything/windows.rb', line 88

def configure_curses
  Curses.class_eval do
    init_screen
    raw
    noecho
    start_color
    init_pair ENHANSIVE_COLOR, Curses::COLOR_WHITE, Curses::COLOR_MAGENTA
    init_pair PROMPT_COLOR, Curses::COLOR_YELLOW, 0
    stdscr.keypad(true)
  end
end

#deactivateObject



46
# File 'lib/ruby-anything/windows.rb', line 46

def deactivate; @active = false end

#filter(*args) ⇒ Object



102
# File 'lib/ruby-anything/windows.rb', line 102

def filter(*args) items_window.filter(*args) end

#on_input(ch) ⇒ Object



62
63
64
65
66
67
68
69
70
71
# File 'lib/ruby-anything/windows.rb', line 62

def on_input(ch)
  case ch
  when *KEYS[:enter], *KEYS[:interrupt]
    deactivate
  when *KEYS[:up], *KEYS[:down]
    items_window.on_input ch
  else
    text_window.on_input ch
  end
end

#opt(window_type) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/ruby-anything/windows.rb', line 49

def opt(window_type)
  case window_type
  when :prompt
    { h: 1, w: prompt.size, y: 0, x: 0}
  when :text
    { h: 1, w: maxx - prompt.size, y: 0, x: 2 }
  when :items
    { h: maxy - 1, w: maxx, y: 1, x: 0 }
  else
    {}
  end
end

#promptObject



47
# File 'lib/ruby-anything/windows.rb', line 47

def prompt; '> ' end

#screenObject



73
74
75
76
77
78
79
# File 'lib/ruby-anything/windows.rb', line 73

def screen
  unless @screen
    configure_curses
    @screen = Curses.stdscr
  end
  @screen
end

#selected_itemObject

delegate to items_window



101
# File 'lib/ruby-anything/windows.rb', line 101

def selected_item; items_window.selected_item end