Class: TextWindow
- Inherits:
-
Curses::Window
- Object
- Curses::Window
- TextWindow
- Defined in:
- lib/profanity_fe/text_window.rb
Constant Summary collapse
- @@list =
Array.new
Instance Attribute Summary collapse
-
#buffer ⇒ Object
readonly
Returns the value of attribute buffer.
-
#color_stack ⇒ Object
readonly
Returns the value of attribute color_stack.
-
#indent_word_wrap ⇒ Object
Returns the value of attribute indent_word_wrap.
-
#layout ⇒ Object
Returns the value of attribute layout.
-
#scrollbar ⇒ Object
Returns the value of attribute scrollbar.
Class Method Summary collapse
Instance Method Summary collapse
- #add_line(line, line_colors = Array.new) ⇒ Object
- #add_string(string, string_colors = Array.new) ⇒ Object
- #clear_scrollbar ⇒ Object
-
#initialize(*args) ⇒ TextWindow
constructor
A new instance of TextWindow.
- #max_buffer_size ⇒ Object
- #max_buffer_size=(val) ⇒ Object
- #resize_buffer ⇒ Object
- #scroll(scroll_num) ⇒ Object
- #update_scrollbar ⇒ Object
Constructor Details
#initialize(*args) ⇒ TextWindow
Returns a new instance of TextWindow.
10 11 12 13 14 15 16 17 |
# File 'lib/profanity_fe/text_window.rb', line 10 def initialize(*args) @buffer = Array.new @buffer_pos = 0 @max_buffer_size = 250 @indent_word_wrap = true @@list.push(self) super(*args) end |
Instance Attribute Details
#buffer ⇒ Object (readonly)
Returns the value of attribute buffer.
2 3 4 |
# File 'lib/profanity_fe/text_window.rb', line 2 def buffer @buffer end |
#color_stack ⇒ Object (readonly)
Returns the value of attribute color_stack.
2 3 4 |
# File 'lib/profanity_fe/text_window.rb', line 2 def color_stack @color_stack end |
#indent_word_wrap ⇒ Object
Returns the value of attribute indent_word_wrap.
3 4 5 |
# File 'lib/profanity_fe/text_window.rb', line 3 def indent_word_wrap @indent_word_wrap end |
#layout ⇒ Object
Returns the value of attribute layout.
3 4 5 |
# File 'lib/profanity_fe/text_window.rb', line 3 def layout @layout end |
#scrollbar ⇒ Object
Returns the value of attribute scrollbar.
3 4 5 |
# File 'lib/profanity_fe/text_window.rb', line 3 def @scrollbar end |
Class Method Details
.list ⇒ Object
6 7 8 |
# File 'lib/profanity_fe/text_window.rb', line 6 def TextWindow.list @@list end |
Instance Method Details
#add_line(line, line_colors = Array.new) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/profanity_fe/text_window.rb', line 25 def add_line(line, line_colors=Array.new) part = [ 0, line.length ] line_colors.each { |h| part.push(h[:start]); part.push(h[:end]) } part.uniq! part.sort! for i in 0...(part.length-1) str = line[part[i]...part[i+1]] color_list = line_colors.find_all { |h| (h[:start] <= part[i]) and (h[:end] >= part[i+1]) } if color_list.empty? addstr str else # shortest length highlight takes precedence when multiple highlights cover the same substring # fixme: allow multiple highlights on a substring when one specifies fg and the other specifies bg color_list = color_list.sort_by { |h| h[:end] - h[:start] } #log("line: #{line}, list: #{color_list}") fg = color_list.map { |h| h[:fg] }.find { |fg| !fg.nil? } bg = color_list.map { |h| h[:bg] }.find { |bg| !bg.nil? } ul = color_list.map { |h| h[:ul] == "true" }.find { |ul| ul } attron(color_pair(get_color_pair_id(fg, bg))|(ul ? Curses::A_UNDERLINE : Curses::A_NORMAL)) { addstr str } end end end |
#add_string(string, string_colors = Array.new) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/profanity_fe/text_window.rb', line 49 def add_string(string, string_colors=Array.new) # # word wrap string, split highlights if needed so each wrapped line is independent, update buffer, update window if needed # while (line = string.slice!(/^.{2,#{maxx-1}}(?=\s|$)/)) or (line = string.slice!(0,(maxx-1))) line_colors = Array.new for h in string_colors line_colors.push(h.dup) if (h[:start] < line.length) h[:end] -= line.length h[:start] = [(h[:start] - line.length), 0].max end string_colors.delete_if { |h| h[:end] < 0 } line_colors.each { |h| h[:end] = [h[:end], line.length].min } @buffer.unshift([line,line_colors]) @buffer.pop if @buffer.length > @max_buffer_size if @buffer_pos == 0 addstr "\n" add_line(line, line_colors) else @buffer_pos += 1 scroll(1) if @buffer_pos > (@max_buffer_size - maxy) end break if string.chomp.empty? if @indent_word_wrap if string[0,1] == ' ' string = " #{string}" string_colors.each { |h| h[:end] += 1; # Never let the highlighting hang off the edge -- it looks weird h[:start] += h[:start] == 0 ? 2 : 1 } else string = " #{string}" string_colors.each { |h| h[:end] += 2; h[:start] += 2 } end else if string[0,1] == ' ' string = string[1,string.length] string_colors.each { |h| h[:end] -= 1; h[:start] -= 1 } end end end if @buffer_pos == 0 noutrefresh end end |
#clear_scrollbar ⇒ Object
165 166 167 168 169 |
# File 'lib/profanity_fe/text_window.rb', line 165 def @scrollbar_pos = nil @scrollbar.clear @scrollbar.noutrefresh end |
#max_buffer_size ⇒ Object
18 19 20 |
# File 'lib/profanity_fe/text_window.rb', line 18 def max_buffer_size @max_buffer_size end |
#max_buffer_size=(val) ⇒ Object
21 22 23 24 |
# File 'lib/profanity_fe/text_window.rb', line 21 def max_buffer_size=(val) # fixme: minimum size? Curses.lines? @max_buffer_size = val.to_i end |
#resize_buffer ⇒ Object
170 171 172 |
# File 'lib/profanity_fe/text_window.rb', line 170 def resize_buffer # fixme end |
#scroll(scroll_num) ⇒ Object
96 97 98 99 100 101 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 130 131 132 133 134 135 |
# File 'lib/profanity_fe/text_window.rb', line 96 def scroll(scroll_num) if scroll_num < 0 if (@buffer_pos + maxy + scroll_num.abs) >= @buffer.length scroll_num = 0 - (@buffer.length - @buffer_pos - maxy) end if scroll_num < 0 @buffer_pos += scroll_num.abs scrl(scroll_num) setpos(0,0) pos = @buffer_pos + maxy - 1 scroll_num.abs.times { add_line(@buffer[pos][0], @buffer[pos][1]) addstr "\n" pos -=1 } noutrefresh end elsif scroll_num > 0 if @buffer_pos == 0 nil else if (@buffer_pos - scroll_num) < 0 scroll_num = @buffer_pos end @buffer_pos -= scroll_num scrl(scroll_num) setpos(maxy - scroll_num, 0) pos = @buffer_pos + scroll_num - 1 (scroll_num - 1).times { add_line(@buffer[pos][0], @buffer[pos][1]) addstr "\n" pos -= 1 } add_line(@buffer[pos][0], @buffer[pos][1]) noutrefresh end end end |
#update_scrollbar ⇒ Object
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/profanity_fe/text_window.rb', line 136 def if @scrollbar = @scrollbar_pos @scrollbar_pos = maxy - ((@buffer_pos/[(@buffer.length - maxy), 1].max.to_f) * (maxy - 1)).round - 1 if unless == @scrollbar_pos @scrollbar.setpos(, 0) @scrollbar.addch '|' @scrollbar.setpos(@scrollbar_pos, 0) @scrollbar.attron(Curses::A_REVERSE) { @scrollbar.addch ' ' } @scrollbar.noutrefresh end else for num in 0...maxy @scrollbar.setpos(num, 0) if num == @scrollbar_pos @scrollbar.attron(Curses::A_REVERSE) { @scrollbar.addch ' ' } else @scrollbar.addch '|' end end @scrollbar.noutrefresh end end end |