Class: GLW::Term

Inherits:
Object
  • Object
show all
Defined in:
lib/glw/term.rb

Overview

This class is responsible for handling all interactions with the terminal so that this thing can be half-ways frontend independent, though I have no intention of ever switching frontends.

Defined Under Namespace

Classes: Cell

Constant Summary collapse

DEFAULT_FG =
255
DEFAULT_BG =
16
DEFAULT_CELL =
Cell.new(" ", DEFAULT_FG, DEFAULT_BG)

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(window) ⇒ Term

Returns a new instance of Term.



28
29
30
31
32
33
# File 'lib/glw/term.rb', line 28

def initialize(window)
  @window = window
  @tiles = Hash.new { DEFAULT_CELL }
  @next_tiles = {}
  @colors = TermColors.new
end

Class Method Details

.with_termObject



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/glw/term.rb', line 14

def with_term
  Curses.init_screen
  Curses.start_color
  Curses.curs_set(0)
  Curses.noecho

  Curses.assume_default_colors(DEFAULT_FG, DEFAULT_BG)

  yield new(Curses::Window.new(0, 0, 0, 0))
ensure
  Curses.close_screen
end

Instance Method Details

#getchObject



49
50
51
# File 'lib/glw/term.rb', line 49

def getch
  window.getch
end

#heightObject



45
46
47
# File 'lib/glw/term.rb', line 45

def height
  Curses.lines
end

#refreshObject



53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/glw/term.rb', line 53

def refresh
  @next_tiles.each do |coords, cell|
    x, y = *coords
    window.setpos(y, x)

    window.attron(colors[cell.fg, cell.bg]) do
      window << cell.c
    end
  end

  @tiles = @tiles.merge(@next_tiles)

  window.refresh
end

#set(x:, y:, c:, fg: DEFAULT_FG, bg: DEFAULT_BG) ⇒ Object



35
36
37
38
39
# File 'lib/glw/term.rb', line 35

def set(x:, y:, c:, fg: DEFAULT_FG, bg: DEFAULT_BG)
  cell = Cell.new(c, fg, bg)

  @next_tiles[[x, y]] = cell unless @tiles[[x, y]] == cell
end

#widthObject



41
42
43
# File 'lib/glw/term.rb', line 41

def width
  Curses.cols
end