Class: Ruvi::DocView

Inherits:
Widget show all
Defined in:
lib/widgets.rb

Defined Under Namespace

Classes: HLSpan

Constant Summary collapse

BGC_NORMAL =
Curses::COLOR_BLACK

Instance Attribute Summary collapse

Attributes inherited from Widget

#buffer, #cursor, #parent

Instance Method Summary collapse

Constructor Details

#initialize(*k) ⇒ DocView

Returns a new instance of DocView.



197
198
199
200
# File 'lib/widgets.rb', line 197

def initialize *k
    super(*k)
    @absolute_cursor = Point.new 0, 0
end

Instance Attribute Details

#absolute_cursorObject

Returns the value of attribute absolute_cursor.



196
197
198
# File 'lib/widgets.rb', line 196

def absolute_cursor
  @absolute_cursor
end

Instance Method Details

#bg_spans_for_selection_at_y(selc, y, line, hilite_colour, width) ⇒ Object



206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/widgets.rb', line 206

def bg_spans_for_selection_at_y selc, y, line, hilite_colour, width
    line_filler = [HLSpan.new(Curses::COLOR_WHITE, BGC_NORMAL, true, " " * (width - 1))]
    return line_filler if selc.nil? || selc.invalid?
    selc = selc.right_way_up
    hlls = EditorApp.lineslice_for_selection_intersect_with_y selc, y, line.length
    return line_filler if hlls.nil?
    temp_str = line.dup + (" " * [hlls.x2 - line.length, 0].max)
    bg_spans = []
    bg_spans << HLSpan.new(Curses::COLOR_WHITE, BGC_NORMAL,    true, temp_str.slice!(0, hlls.x1) || "")
    bg_spans << HLSpan.new(Curses::COLOR_WHITE, hilite_colour, true, temp_str.slice!(0, hlls.x2 - hlls.x1) || "")
    bg_spans << HLSpan.new(Curses::COLOR_WHITE, BGC_NORMAL,    true, temp_str || "")
    visual_selc_after_end = (selc.mode == :selc_lined)
    needed = ((width - 1) - line.length)
    needed = 0 if needed < 0
    bg_spans << HLSpan.new(Curses::COLOR_WHITE, (visual_selc_after_end ? hilite_colour : BGC_NORMAL), true, " " * needed)
end

#canvasObject



262
263
264
# File 'lib/widgets.rb', line 262

def canvas
    Curses.begin_draw WinDescs::instance.descs[self]
end

#heightObject



202
# File 'lib/widgets.rb', line 202

def height; nil; end

#merge_bgspans_with_spans(merged_spans, hl_bg_spans) ⇒ Object



249
250
251
252
253
254
255
256
257
258
259
260
261
# File 'lib/widgets.rb', line 249

def merge_bgspans_with_spans merged_spans, hl_bg_spans
    normalized_merged_spans, normalized_hl_bg_spans = normalize_span_lengths merged_spans, hl_bg_spans
    
    hl_merged_spans = []
    normalized_hl_bg_spans.zip(normalized_merged_spans) {
        |b,a| 
        c = a.dup
        c.bgcolor = b.bgcolor if c.bgcolor == BGC_NORMAL
        hl_merged_spans << c
    }
    
    hl_merged_spans 
end

#normalize_span_lengths(spans1, spans2) ⇒ Object

overlays spans1 lengths over spans2, thusly extras in spans2 are discarded



222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/widgets.rb', line 222

def normalize_span_lengths spans1, spans2 # overlays spans1 lengths over spans2, thusly extras in spans2 are discarded
    tmp_spans1, tmp_spans2 = [], []
    while true
        span1, span2 = spans1.first, spans2.first
        break if (span1.nil? and span2.nil?) or (span1.nil?)
        if span2.nil?
            dbg(:dbg_render) { "render argh!!!!" }
            break
        end
        if  span1.text.length < span2.text.length
            span2 = span2.dup
            span2.text = spans2.first.text.slice!(0, span1.text.length)
            spans1.shift
        elsif span1.text.length > span2.text.length
            # test - TODO - when does this happen?
            span1 = span1.dup
            span1.text = spans1.first.text.slice!(0, span2.text.length)
            spans2.shift
        else
            spans2.shift
            spans1.shift
        end
        tmp_spans1 << span1
        tmp_spans2 << span2
    end
    return tmp_spans1, tmp_spans2
end

#render(y) ⇒ Object



265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
# File 'lib/widgets.rb', line 265

def render y
    a_tab = " " * @app.config_get_tab_size
    buf = @buffer
    width = WinDescs::instance.descs[self].sx
    line = buf.lines[buf.top + y]
    # TODO render empty lines..
    line = "" if line.nil?
    spans = []
    if buf.is_paste_buffer
        # test - switch to paste buffer - needs a render check really...
        linetype = (line.newline_at_start ? "S" : "-") + (line.newline_at_end ? "E" : "-") + ":"
        spans << HLSpan.new(Curses::COLOR_YELLOW, BGC_NORMAL, true, linetype)
    end
    
    xpos = 0
    max_len = width - 1
    # TODO - test the expanded_str stuff
    # FIXME  a final half off screen tab will be rendered incorrectly atm
    buf.iterate_line_highlight(buf.top+y) {
        |color, bold, word|
        str = word.to_str.dup
        expanded_str = str.gsub("\t", a_tab)
        overend = ((xpos + expanded_str.length) >= max_len)
        if overend and (xpos <= max_len)
            expanded_str = expanded_str.slice 0, max_len
        elsif overend
            next
        end
        spans << HLSpan.new(color, BGC_NORMAL, bold, str)
        xpos += expanded_str.length
    }
    
    if max_len > xpos or spans.empty?
        len = (max_len - xpos)
        len = 0 if len < 0
        spans << HLSpan.new(Curses::COLOR_WHITE, BGC_NORMAL, false, " " * len)
    end

    merged_spans = spans
    
    if !@app.selection.nil?
        bg_spans = bg_spans_for_selection_at_y @app.selection, buf.top+y, line, Curses::COLOR_YELLOW, width
        merged_spans = merge_bgspans_with_spans merged_spans, bg_spans.dup
    end
    
    if !@app.hl_selection.nil?
        hl_bg_spans = bg_spans_for_selection_at_y @app.hl_selection, buf.top+y, line, Curses::COLOR_BLUE, width
        merged_spans = merge_bgspans_with_spans merged_spans, hl_bg_spans
    end
    
    cx, cy = @absolute_cursor.x, @absolute_cursor.y + buf.top
    if cy == (y + buf.top)
        cursor_selection = Selection.new Point.new(cx, cy), Point.new(cx, cy), :selc_normal
        curs_spans = bg_spans_for_selection_at_y cursor_selection, buf.top+y, line, Curses::COLOR_GREEN, width
        merged_spans = merge_bgspans_with_spans merged_spans, curs_spans
    end
    
    screen = canvas
    screen.setpos y, 0 # y, x
    merged_spans.each {
        |span|
        next if span.text.nil?
        screen.set_attr span.bold, span.color, span.bgcolor
        screen.addstr span.text.gsub("\t", a_tab)
    }
end

#watch_bufferObject



203
204
205
# File 'lib/widgets.rb', line 203

def watch_buffer
    @buffer
end

#widthObject



201
# File 'lib/widgets.rb', line 201

def width;  nil; end