Class: IGMarkets::CLI::CursesWindow

Inherits:
Object
  • Object
show all
Defined in:
lib/ig_markets/cli/curses_window.rb

Overview

This helper class supports the display of text in a fullscreen curses window.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCursesWindow

Initializes this fullscreen curses window.



8
9
10
11
12
13
# File 'lib/ig_markets/cli/curses_window.rb', line 8

def initialize
  self.class.prepare

  @window = Curses::Window.new 0, 0, 0, 0
  @position = [0, 0]
end

Class Method Details

.available?Boolean

Returns whether curses support is available. On Windows the ‘curses’ gem is optional and so this class may not be usable.

Returns:



45
46
47
48
49
50
# File 'lib/ig_markets/cli/curses_window.rb', line 45

def available?
  require 'curses'
  true
rescue LoadError
  false
end

.prepareObject

Raises:



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/ig_markets/cli/curses_window.rb', line 52

def prepare
  raise IGMarketsError, 'curses gem is not installed' unless available?

  return if @prepared

  Curses.noecho
  Curses.nonl
  Curses.stdscr.nodelay = 1
  Curses.init_screen
  Curses.start_color

  8.times { |color| Curses.init_pair (30 + color), color, 0 }

  @prepared = true
end

Instance Method Details

#clearObject

Clears the contents of this curses window and resets the cursor to the top left.



16
17
18
19
# File 'lib/ig_markets/cli/curses_window.rb', line 16

def clear
  @window.clear
  @position = [0, 0]
end

Prints the specified lines in this fullscreen curses window.

Parameters:

  • lines (Array<String>)

    The lines to print.



29
30
31
32
33
34
35
36
37
38
# File 'lib/ig_markets/cli/curses_window.rb', line 29

def print_lines(*lines)
  change_foreground_color nil

  lines.flatten.each do |line|
    print_next_line_segment line until line.empty?

    @position[0] += 1
    @position[1] = 0
  end
end

#refreshObject

Refreshes this curses window so its content is updated on the screen.



22
23
24
# File 'lib/ig_markets/cli/curses_window.rb', line 22

def refresh
  @window.refresh
end