Class: Ruco::Window

Inherits:
Object show all
Defined in:
lib/ruco/window.rb

Constant Summary collapse

OFFSET =
5

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(lines, columns, options = {}) ⇒ Window

Returns a new instance of Window.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/ruco/window.rb', line 8

def initialize(lines, columns, options={})
  @options = options

  @options[:line_scroll_threshold] ||= 1
  @options[:line_scroll_offset] ||= 1
  @options[:column_scroll_threshold] ||= 1
  @options[:column_scroll_offset] ||= 5

  @lines = lines
  @columns = columns
  @top = 0
  @left = 0
  @cursor = Position.new(0,0)
end

Instance Attribute Details

#columnsObject

Returns the value of attribute columns.



5
6
7
# File 'lib/ruco/window.rb', line 5

def columns
  @columns
end

#cursorObject (readonly)

Returns the value of attribute cursor.



6
7
8
# File 'lib/ruco/window.rb', line 6

def cursor
  @cursor
end

#leftObject

Returns the value of attribute left.



5
6
7
# File 'lib/ruco/window.rb', line 5

def left
  @left
end

#linesObject

Returns the value of attribute lines.



5
6
7
# File 'lib/ruco/window.rb', line 5

def lines
  @lines
end

#topObject (readonly)

Returns the value of attribute top.



6
7
8
# File 'lib/ruco/window.rb', line 6

def top
  @top
end

Instance Method Details

#add_selection_styles(map, selection) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/ruco/window.rb', line 61

def add_selection_styles(map, selection)
  lines.times do |line|
    visible = visible_area(line)
    next unless selection.overlap?(visible)

    first = [selection.first, visible.first].max
    first = first[1] - left
    last = [selection.last, visible.last].min
    last = last[1] - left

    map.add(:reverse, line, first...last)
  end
  map
end

#crop(lines) ⇒ Object



33
34
35
36
37
38
39
40
# File 'lib/ruco/window.rb', line 33

def crop(lines)
  lines_to_display = lines[visible_lines] || []
  lines_to_display[@lines-1] ||= nil
  lines_to_display.map do |line|
    line ||= ''
    line[visible_columns] || ''
  end
end

#position=(x) ⇒ Object



23
24
25
# File 'lib/ruco/window.rb', line 23

def position=(x)
  set_position(x)
end

#scroll_column_into_view(column) ⇒ Object



47
48
49
50
# File 'lib/ruco/window.rb', line 47

def scroll_column_into_view(column)
  result = adjustment(column, visible_columns, @options[:column_scroll_threshold], @options[:column_scroll_offset])
  self.left = result if result
end

#scroll_line_into_view(line, total_lines) ⇒ Object



42
43
44
45
# File 'lib/ruco/window.rb', line 42

def scroll_line_into_view(line, total_lines)
  result = adjustment(line, visible_lines, @options[:line_scroll_threshold], @options[:line_scroll_offset])
  set_top result, total_lines if result
end

#set_position(position, options = {}) ⇒ Object



27
28
29
30
31
# File 'lib/ruco/window.rb', line 27

def set_position(position, options={})
  scroll_line_into_view position.line, (options[:max_lines] || 9999)
  scroll_column_into_view position.column
  @cursor = Position.new(position.line - @top, position.column - @left)
end

#set_top(line, total_lines) ⇒ Object



80
81
82
83
# File 'lib/ruco/window.rb', line 80

def set_top(line, total_lines)
  max_top = total_lines - lines + 1 + @options[:line_scroll_offset]
  @top = [[line, max_top].min, 0].max
end

#style_map(selection) ⇒ Object



52
53
54
55
56
57
58
59
# File 'lib/ruco/window.rb', line 52

def style_map(selection)
  map = Dispel::StyleMap.new(lines)
  if selection
    add_selection_styles(map, selection)
  else
    map
  end
end

#visible_columnsObject



89
90
91
# File 'lib/ruco/window.rb', line 89

def visible_columns
  @left..(@left+@columns-1)
end

#visible_linesObject



85
86
87
# File 'lib/ruco/window.rb', line 85

def visible_lines
  @top..(@top+@lines-1)
end