Class: Dispel::Screen
- Inherits:
-
Object
- Object
- Dispel::Screen
- 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
-
#options ⇒ Object
Returns the value of attribute options.
Class Method Summary collapse
-
.color_id(foreground, background) ⇒ Object
create a new color from foreground+background or reuse old and return color-id.
-
.curses_style(style, colors, options = {}) ⇒ Object
TODO maybe instance and simpler caching…
- .html_to_terminal_color(html_color) ⇒ Object
- .open(options = {}, &block) ⇒ Object
Instance Method Summary collapse
- #clear_cache ⇒ Object
- #color? ⇒ Boolean
- #columns ⇒ Object
- #debug_key(key) ⇒ Object
- #draw(view, style_map = [], cursor = nil) ⇒ Object
-
#initialize(options) ⇒ Screen
constructor
A new instance of Screen.
- #lines ⇒ Object
- #open(&block) ⇒ Object
Constructor Details
#initialize(options) ⇒ Screen
Returns a new instance of Screen.
7 8 9 10 |
# File 'lib/dispel/screen.rb', line 7 def initialize() = @cache = [] end |
Instance Attribute Details
#options ⇒ Object
Returns the value of attribute options.
5 6 7 |
# File 'lib/dispel/screen.rb', line 5 def 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, ={}) Tools.memoize(:curses_style, style, colors) do if colors foreground = [:foreground] || '#ffffff' background = [: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(={}, &block) new().open(&block) end |
Instance Method Details
#clear_cache ⇒ Object
38 39 40 |
# File 'lib/dispel/screen.rb', line 38 def clear_cache @cache.clear end |
#color? ⇒ Boolean
53 54 55 |
# File 'lib/dispel/screen.rb', line 53 def color? [:colors] and Curses.has_colors? end |
#columns ⇒ Object
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 |
#lines ⇒ Object
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 |