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 so existing widgets draw exactly as before.

attron accepts EITHER a Potty::Style (resolved to a colour pair + attributes) OR a raw integer (a legacy curses attribute, passed through). That dual acceptance is what lets un-migrated widgets — and direct-to-window consumers like a host’s own paint code — keep working untouched while new widgets go render-target-agnostic.

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of CursesSurface.



19
20
21
22
23
24
25
26
# File 'lib/potty/surfaces/curses_surface.rb', line 19

def initialize(window_manager, theme, tick_interval: nil)
  super()
  @wm = window_manager
  @theme = theme
  @tick_interval = tick_interval
  @next_pair = theme.palette.size + 1 # leave 1..N for theme[]'s pairs
  @pairs = nil
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



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

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
  @theme.setup_colors if ::Curses.has_colors?
end