Class: LessCurse::Screen

Inherits:
Object
  • Object
show all
Defined in:
lib/less_curse/screen.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeScreen

Returns a new instance of Screen.



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/less_curse/screen.rb', line 11

def initialize
  # Need to initialize screen to access the terminal size
  FFI::NCurses.initscr

  height,width =  FFI::NCurses::getmaxyx(FFI::NCurses::stdscr)
  @size = LessCurse::Geometry::Size.new(width,height)
  @windows = {}
  @focused_widget = nil
  @grid    = LessCurse::Grid.new [[]]
  @popups  = {}
end

Instance Attribute Details

#focused_widgetObject

Returns the value of attribute focused_widget.



6
7
8
# File 'lib/less_curse/screen.rb', line 6

def focused_widget
  @focused_widget
end

Returns the value of attribute footer.



8
9
10
# File 'lib/less_curse/screen.rb', line 8

def footer
  @footer
end

#gridObject

Returns the value of attribute grid.



3
4
5
# File 'lib/less_curse/screen.rb', line 3

def grid
  @grid
end

#headerObject

Returns the value of attribute header.



7
8
9
# File 'lib/less_curse/screen.rb', line 7

def header
  @header
end

#popupsObject

Returns the value of attribute popups.



9
10
11
# File 'lib/less_curse/screen.rb', line 9

def popups
  @popups
end

#sizeObject

Returns the value of attribute size.



5
6
7
# File 'lib/less_curse/screen.rb', line 5

def size
  @size
end

#windowsObject

Returns the value of attribute windows.



4
5
6
# File 'lib/less_curse/screen.rb', line 4

def windows
  @windows
end

Instance Method Details

#add(widget_or_grid) ⇒ Object



23
24
25
26
27
28
29
30
# File 'lib/less_curse/screen.rb', line 23

def add widget_or_grid
  if widget_or_grid.is_a? LessCurse::Grid
    @grid = widget_or_grid
  else
    @grid.add widget_or_grid
  end
  recalc_window_sizes
end

#focus_nextObject

Focus next element in #widgets



89
90
91
# File 'lib/less_curse/screen.rb', line 89

def focus_next
  cycle_focus(+1)
end

#focus_previousObject

Focus next element in #widgets



94
95
96
# File 'lib/less_curse/screen.rb', line 94

def focus_previous
  cycle_focus(-1)
end

#repaintObject

Repaint the screen and all contained widgets



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
# File 'lib/less_curse/screen.rb', line 50

def repaint
  FFI::NCurses.refresh

  # 'Draw' header and/or footer
  if @header && !@header.empty?
    FFI::NCurses::mvaddstr 0, 0, @header
  end
  if @footer && !@footer.empty?
    FFI::NCurses::mvaddstr @size.height - 1, 0, @footer
  end

  # Let all Widgets redraw themselfes
  widgets.each  do |widget|
    window = @windows[widget]
    FFI::NCurses.wclear   window
    widget.draw           window
    FFI::NCurses.wrefresh window
  end

  @popups.each do |popup, window|
    FFI::NCurses.wclear   window
    popup.draw            window
    FFI::NCurses.wrefresh window
  end
end

#showObject



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/less_curse/screen.rb', line 36

def show
  @focused_widget = widgets.first
  @focused_widget.focus if @focused_widget
  # Note that FFI::NCurses.initscr is called in initialize
  FFI::NCurses.cbreak # can ctrl-c, not waiting for newlines to end input.
  #FFI::NCurses.raw    # TODO this overrides cbreak ... 
  FFI::NCurses.noecho # do not echo input in win.
  FFI::NCurses.keypad FFI::NCurses::stdscr, true # recognize KEY_UP etc.
  FFI::NCurses.clear
  FFI::NCurses.refresh
  repaint
end

#show_popup(type = :info, content = 'Popup') ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
# File 'lib/less_curse/screen.rb', line 76

def show_popup type=:info, content='Popup'
  # Lets take up quarter of screen and draw a boxed window
  popup_widget = LessCurse::Widgets::Button.new title: content.to_s
  popup_widget.focus = true
  area = LessCurse::Geometry::Rectangle.new @size.width / 4,
    @size.height / 4,
    @size.width  / 2,
    @size.height / 2

  @popups[popup_widget] = LessCurse.window area
end

#widgetsObject



32
33
34
# File 'lib/less_curse/screen.rb', line 32

def widgets
  @grid.widgets
end