Class: Engine

Inherits:
Object
  • Object
show all
Defined in:
lib/tabscroll/engine.rb

Overview

The main interface with Curses.

This acts as a middleman, abstracting away Curses’ details.

Constant Summary collapse

Colors =

All possible colors.

{
  :black   => 1,
  :white   => 2,
  :red     => 3,
  :yellow  => 4,
  :magenta => 5,
  :blue    => 6,
  :green   => 7,
  :cyan    => 8
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(min_width = nil, min_height = nil) ⇒ Engine

Initializes Ncurses with minimal width and height.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/tabscroll/engine.rb', line 23

def initialize(min_width=nil, min_height=nil)
  @has_colors = nil

  @screen = Curses::init_screen
  return nil if not @screen

  if min_width and min_height
    cur_width  = @screen.maxx
    cur_height = @screen.maxy

    if cur_width < @width or cur_height < @height
      self.exit
      $stderr << "Error: Screen size too small (#{cur_width}x#{cur_height})\n"
      $stderr << "Please resize your terminal to at least #{@width}x#{@height}\n"
      return nil
    end
  end

  @has_colors = Curses.has_colors?
  if @has_colors
    Curses.start_color
    Curses.use_default_colors # will use default background

    # Initializes:   constant          foreground             bg
    Curses.init_pair(Colors[:white],   Curses::COLOR_BLACK,   -1)
    Curses.init_pair(Colors[:blue],    Curses::COLOR_BLUE,    -1)
    Curses.init_pair(Colors[:red],     Curses::COLOR_RED,     -1)
    Curses.init_pair(Colors[:green],   Curses::COLOR_GREEN,   -1)
    Curses.init_pair(Colors[:magenta], Curses::COLOR_MAGENTA, -1)
    Curses.init_pair(Colors[:yellow],  Curses::COLOR_YELLOW,  -1)
    Curses.init_pair(Colors[:cyan],    Curses::COLOR_CYAN,    -1)
  end

  Curses::cbreak
  Curses::curs_set 0
  Curses::noecho
  Curses::nonl
  Curses::stdscr.keypad = true # extra keys
end

Instance Method Details

#exitObject



71
72
73
74
# File 'lib/tabscroll/engine.rb', line 71

def exit
  Curses::refresh
  Curses::close_screen
end

#getcharObject



85
86
87
# File 'lib/tabscroll/engine.rb', line 85

def getchar
  return Curses::getch
end

#heightObject



67
68
69
# File 'lib/tabscroll/engine.rb', line 67

def height
  return Curses::lines
end

#set_color(color) ⇒ Object



76
77
78
79
80
81
82
83
# File 'lib/tabscroll/engine.rb', line 76

def set_color color
  if @has_colors
    @screen.attron Curses::color_pair(color)
    return self
  else
    return nil
  end
end

#timeout(timeout) ⇒ Object

timeout says how many milliseconds we wait for a key to be pressed.



91
92
93
# File 'lib/tabscroll/engine.rb', line 91

def timeout timeout
  Curses::timeout = timeout
end

#widthObject



63
64
65
# File 'lib/tabscroll/engine.rb', line 63

def width
  return Curses::cols
end