Class: Dispel::Screen

Inherits:
Object
  • Object
show all
Defined in:
lib/dispel/screen.rb

Constant Summary collapse

COLOR_SOURCE_VALUES =
256
COLOR_TARGET_VALUES =
5
COLOR_DIVIDE =
COLOR_SOURCE_VALUES / COLOR_TARGET_VALUES
TERM_COLOR_BASE =
16

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Screen

Returns a new instance of Screen.



7
8
9
10
# File 'lib/dispel/screen.rb', line 7

def initialize(options)
  @options = options
  @cache = []
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



5
6
7
# File 'lib/dispel/screen.rb', line 5

def options
  @options
end

Class Method Details

.color_id(foreground, background) ⇒ Object

create a new color from foreground+background or reuse old and return color-id



133
134
135
136
137
138
139
140
141
142
143
# File 'lib/dispel/screen.rb', line 133

def color_id(foreground, background)
  Tools.memoize(:color_id, foreground, background) do
    # make a new pair with a unique id
    @@max_color_id ||= 0
    id = (@@max_color_id += 1)
    unless defined? RSpec # stops normal text-output, do not use in tests
      Curses::init_pair(id, foreground, background)
    end
    Curses.color_pair(id)
  end
end

.curses_style(style, colors, options = {}) ⇒ Object

TODO maybe instance and simpler caching…



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/dispel/screen.rb', line 102

def curses_style(style, colors, options={})
  Tools.memoize(:curses_style, style, colors) do
    if colors
      foreground = options[:foreground] || '#ffffff'
      background = options[:background] || '#000000'

      foreground, background = if style == :normal
        [foreground, background]
      elsif style == :reverse
        ['#000000', '#ffffff']
      else
        # :red or [:red, :blue]
        f,b = style
        [f || foreground, b || background]
      end

      foreground = html_to_terminal_color(foreground)
      background = html_to_terminal_color(background)
      color_id(foreground, background)
    else # no colors
      if style == :reverse
        Curses::A_REVERSE
      else
        Curses::A_NORMAL
      end
    end
  end
end

.html_to_terminal_color(html_color) ⇒ Object



150
151
152
153
154
155
156
# File 'lib/dispel/screen.rb', line 150

def html_to_terminal_color(html_color)
  return unless html_color
  r = (html_color[1..2].to_i(16) / COLOR_DIVIDE) * 36
  g = (html_color[3..4].to_i(16) / COLOR_DIVIDE) * 6
  b = (html_color[5..6].to_i(16) / COLOR_DIVIDE) * 1
  TERM_COLOR_BASE + r + g + b
end

.open(options = {}, &block) ⇒ Object



12
13
14
# File 'lib/dispel/screen.rb', line 12

def self.open(options={}, &block)
  new(options).open(&block)
end

Instance Method Details

#clear_cacheObject



38
39
40
# File 'lib/dispel/screen.rb', line 38

def clear_cache
  @cache.clear
end

#color?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/dispel/screen.rb', line 53

def color?
  @options[:colors] and Curses.has_colors?
end

#columnsObject



30
31
32
# File 'lib/dispel/screen.rb', line 30

def columns
  Curses.stdscr.maxx
end

#debug_key(key) ⇒ Object



47
48
49
50
51
# File 'lib/dispel/screen.rb', line 47

def debug_key(key)
  @key_line ||= -1
  @key_line = (@key_line + 1) % lines
  write(@key_line, 0, "#{key.inspect}---")
end

#draw(view, style_map = [], cursor = nil) ⇒ Object



42
43
44
45
# File 'lib/dispel/screen.rb', line 42

def draw(view, style_map=[], cursor=nil)
  draw_view(view, style_map)
  Curses.setpos(*cursor) if cursor
end

#linesObject



34
35
36
# File 'lib/dispel/screen.rb', line 34

def lines
  Curses.stdscr.maxy
end

#open(&block) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/dispel/screen.rb', line 16

def open(&block)
  Curses.noecho # do not show typed chars
  Curses.nonl # turn off newline translation
  Curses.stdscr.keypad(true) # enable arrow keys
  Curses.raw # give us all other keys
  Curses.stdscr.nodelay = 1 # do not block -> we can use timeouts
  Curses.init_screen
  Curses.start_color if color?
  yield self
ensure
  Curses.clear # needed to clear the menu/status bar on windows
  Curses.close_screen
end