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
- .html_to_terminal_color(html_color) ⇒ Object
- .open(options = {}, &block) ⇒ Object
Instance Method Summary collapse
- #color? ⇒ Boolean
- #columns ⇒ Object
- #debug_key(key) ⇒ Object
- #draw(view, style_map = [], cursor = [0,0]) ⇒ 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() @options = @cache = [] end |
Instance Attribute Details
#options ⇒ Object
Returns the value of attribute options.
5 6 7 |
# File 'lib/dispel/screen.rb', line 5 def @options end |
Class Method Details
.color_id(foreground, background) ⇒ Object
create a new color from foreground+background or reuse old and return color-id
137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/dispel/screen.rb', line 137 def color_id(foreground, background) @color_ids ||= {} @color_ids[[foreground, background]] ||= begin # 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
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/dispel/screen.rb', line 108 def curses_style(style, colors, ={}) 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 |
.html_to_terminal_color(html_color) ⇒ Object
155 156 157 158 159 160 161 |
# File 'lib/dispel/screen.rb', line 155 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
#color? ⇒ Boolean
50 51 52 |
# File 'lib/dispel/screen.rb', line 50 def color? @options[: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
44 45 46 47 48 |
# File 'lib/dispel/screen.rb', line 44 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 = [0,0]) ⇒ Object
38 39 40 41 42 |
# File 'lib/dispel/screen.rb', line 38 def draw(view, style_map=[], cursor=[0,0]) @cache.clear if dimensions_changed? draw_view(view, style_map) Curses.setpos(*cursor) # cursor has to always be set or it ends in random position 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 |