Class: Hobostove::Panel

Inherits:
Struct
  • Object
show all
Defined in:
lib/hobostove/panel.rb

Direct Known Subclasses

InputPanel, UserPanel

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Panel

Returns a new instance of Panel.



3
4
5
6
7
8
9
10
11
12
13
14
# File 'lib/hobostove/panel.rb', line 3

def initialize(*args)
  super

  @win = Curses::Window.new(height, width, starty, startx)
  @win.box(0, 0)

  Curses.refresh
  @win.refresh

  @strings = []
  @scroll = 0
end

Instance Attribute Details

#heightObject

Returns the value of attribute height

Returns:

  • (Object)

    the current value of height



2
3
4
# File 'lib/hobostove/panel.rb', line 2

def height
  @height
end

#optionsObject

Returns the value of attribute options

Returns:

  • (Object)

    the current value of options



2
3
4
# File 'lib/hobostove/panel.rb', line 2

def options
  @options
end

#startxObject

Returns the value of attribute startx

Returns:

  • (Object)

    the current value of startx



2
3
4
# File 'lib/hobostove/panel.rb', line 2

def startx
  @startx
end

#startyObject

Returns the value of attribute starty

Returns:

  • (Object)

    the current value of starty



2
3
4
# File 'lib/hobostove/panel.rb', line 2

def starty
  @starty
end

#widthObject

Returns the value of attribute width

Returns:

  • (Object)

    the current value of width



2
3
4
# File 'lib/hobostove/panel.rb', line 2

def width
  @width
end

Instance Method Details

#<<(string, do_update = true) ⇒ Object



24
25
26
27
28
29
30
31
32
# File 'lib/hobostove/panel.rb', line 24

def <<(string, do_update = true)
  if wrap_lines?
    @strings << Line(string.first(width - 4))
  else
    @strings << Line(string)
  end

  refresh if do_update
end

#refreshObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/hobostove/panel.rb', line 52

def refresh
  @win.clear

  @win.box(0, 0)

  printable_lines.each_with_index do |line, i|
    @win.setpos(i + 1, 2)

    line.segments.each do |segment|
      color = case segment.color
              when :cyan
                Curses::COLOR_CYAN
              else
                Curses::COLOR_WHITE
              end

      @win.attron(Curses.color_pair(color) | Curses::A_NORMAL) do
        @win.addstr(segment.body)
      end
    end
  end

  refresh!
end

#refresh!Object



48
49
50
# File 'lib/hobostove/panel.rb', line 48

def refresh!
  @win.refresh
end

#scroll_downObject



41
42
43
44
45
46
# File 'lib/hobostove/panel.rb', line 41

def scroll_down
  if @scroll > 0
    @scroll -= 1
  end
  refresh
end

#scroll_upObject



34
35
36
37
38
39
# File 'lib/hobostove/panel.rb', line 34

def scroll_up
  if @strings.size > printable_area + @scroll
    @scroll += 1
  end
  refresh
end

#wrap_lines?Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/hobostove/panel.rb', line 20

def wrap_lines?
  !options[:nowrap]
end