Class: Potty::Surfaces::CursesSurface

Inherits:
Potty::Surface show all
Defined in:
lib/potty/surfaces/curses_surface.rb

Overview

The default render target: a full-screen curses display. Wraps the WindowManager / stdscr. It resolves a Style to a curses colour pair + attributes (allocating pairs on demand); a raw integer is passed through unchanged, so a host that draws to the surface with its own curses attrs still works.

Instance Method Summary collapse

Constructor Details

#initialize(window_manager, theme, tick_interval: nil) ⇒ CursesSurface

Returns a new instance of CursesSurface.



16
17
18
19
20
21
22
23
# File 'lib/potty/surfaces/curses_surface.rb', line 16

def initialize(window_manager, theme, tick_interval: nil)
  super()
  @wm = window_manager
  @theme = theme
  @tick_interval = tick_interval
  @next_pair = 1
  @pairs = {}
end

Instance Method Details

#addstr(str) ⇒ Object



58
59
60
# File 'lib/potty/surfaces/curses_surface.rb', line 58

def addstr(str)
  stdscr.addstr(str)
end

#attron(style_or_attr, &block) ⇒ Object



62
63
64
# File 'lib/potty/surfaces/curses_surface.rb', line 62

def attron(style_or_attr, &block)
  stdscr.attron(curses_attr(style_or_attr), &block)
end

#eraseObject



50
51
52
# File 'lib/potty/surfaces/curses_surface.rb', line 50

def erase
  stdscr.erase
end

#finalizeObject



42
43
44
# File 'lib/potty/surfaces/curses_surface.rb', line 42

def finalize
  ::Curses.close_screen
end

#presentObject



66
67
68
# File 'lib/potty/surfaces/curses_surface.rb', line 66

def present
  @wm.refresh_all
end

#read_keyObject



70
71
72
# File 'lib/potty/surfaces/curses_surface.rb', line 70

def read_key
  Keys.code(stdscr.getch)
end

#setpos(row, col) ⇒ Object



54
55
56
# File 'lib/potty/surfaces/curses_surface.rb', line 54

def setpos(row, col)
  stdscr.setpos(row, col)
end

#sizeObject



46
47
48
# File 'lib/potty/surfaces/curses_surface.rb', line 46

def size
  [@wm.max_y, @wm.max_x]
end

#startObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/potty/surfaces/curses_surface.rb', line 25

def start
  # See ESCDELAY notes in Theme/Application history: the env var is only
  # honoured by newer ncurses, so also set it via Curses.ESCDELAY=.
  ENV['ESCDELAY'] ||= '250'
  @wm.setup(::Curses.init_screen)
  ::Curses.ESCDELAY = 250 if ::Curses.respond_to?(:ESCDELAY=)
  ::Curses.curs_set(0)
  ::Curses.noecho
  ::Curses.cbreak
  stdscr.keypad(true)
  stdscr.timeout = @tick_interval if @tick_interval
  if ::Curses.has_colors?
    ::Curses.start_color
    ::Curses.use_default_colors # enables -1 = terminal default
  end
end