Class: GitCrecord::Diff::Difference

Inherits:
Object
  • Object
show all
Defined in:
lib/git_crecord/diff/difference.rb

Direct Known Subclasses

File, Hunk, Line, PseudoLine

Constant Summary collapse

SELECTED_MAP =
{
  true => '[X]  ',
  false => '[ ]  ',
  :partly => '[~]  '
}.freeze
REVERSE_SELECTED_MAP =
{
  true => '[R]  ',
  false => '[X]  ',
  :partly => '[~]  '
}.freeze
SELECTION_MARKER_WIDTH =
SELECTED_MAP[true].size

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(reverse: false) ⇒ Difference

Returns a new instance of Difference.



26
27
28
29
30
# File 'lib/git_crecord/diff/difference.rb', line 26

def initialize(reverse: false)
  @reverse = reverse
  @selection_marker_map = reverse ? REVERSE_SELECTED_MAP : SELECTED_MAP
  @subs = []
end

Instance Attribute Details

#expandedObject

Returns the value of attribute expanded.



8
9
10
# File 'lib/git_crecord/diff/difference.rb', line 8

def expanded
  @expanded
end

#subsObject (readonly)

Returns the value of attribute subs.



10
11
12
# File 'lib/git_crecord/diff/difference.rb', line 10

def subs
  @subs
end

#y1Object

Returns the value of attribute y1.



9
10
11
# File 'lib/git_crecord/diff/difference.rb', line 9

def y1
  @y1
end

#y2Object

Returns the value of attribute y2.



9
10
11
# File 'lib/git_crecord/diff/difference.rb', line 9

def y2
  @y2
end

Instance Method Details

#content_width(width) ⇒ Object



43
44
45
# File 'lib/git_crecord/diff/difference.rb', line 43

def content_width(width)
  [1, width - x_offset - SELECTION_MARKER_WIDTH].max
end

#max_height(width) ⇒ Object



36
37
38
39
40
41
# File 'lib/git_crecord/diff/difference.rb', line 36

def max_height(width)
  width = content_width(width)
  ((to_s.size - 1).abs / width) + 1 + subs.reduce(0) do |a, e|
    a + e.max_height(width)
  end
end

#prefix(line_number) ⇒ Object



76
77
78
79
80
81
# File 'lib/git_crecord/diff/difference.rb', line 76

def prefix(line_number)
  show_selection_marker = line_number.zero? && selectable?
  return @selection_marker_map.fetch(selected) if show_selection_marker

  ' ' * SELECTION_MARKER_WIDTH
end

#prefix_style(_is_highlighted) ⇒ Object



72
73
74
# File 'lib/git_crecord/diff/difference.rb', line 72

def prefix_style(_is_highlighted)
  UI::Color.normal
end


83
84
85
86
87
88
89
90
91
92
93
# File 'lib/git_crecord/diff/difference.rb', line 83

def print(win, line_number, is_highlighted)
  @y1 = line_number + 1
  prefix_style = prefix_style(is_highlighted)
  style = style(is_highlighted)
  strings(win.width).each_with_index do |string, index|
    win.addstr(' ' * x_offset, line_number += 1, attr: prefix_style)
    win.addstr(prefix(index), attr: prefix_style)
    win.addstr(string, attr: style, fill: ' ')
  end
  @y2 = line_number
end

#selectable?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/git_crecord/diff/difference.rb', line 47

def selectable?
  true
end

#selectable_subsObject



51
52
53
# File 'lib/git_crecord/diff/difference.rb', line 51

def selectable_subs
  @selectable_subs ||= subs.select(&:selectable?)
end

#selectedObject



55
56
57
58
59
60
# File 'lib/git_crecord/diff/difference.rb', line 55

def selected
  s = selectable_subs.map(&:selected).uniq
  return s[0] if s.size == 1

  :partly
end

#selected=(value) ⇒ Object



62
63
64
# File 'lib/git_crecord/diff/difference.rb', line 62

def selected=(value)
  selectable_subs.each { |sub| sub.selected = value }
end

#strings(width) ⇒ Object



32
33
34
# File 'lib/git_crecord/diff/difference.rb', line 32

def strings(width)
  to_s.scan(/.{1,#{content_width(width)}}/)
end

#style(is_highlighted) ⇒ Object



66
67
68
69
70
# File 'lib/git_crecord/diff/difference.rb', line 66

def style(is_highlighted)
  return Curses::A_BOLD | UI::Color.hl if is_highlighted

  Curses::A_BOLD | UI::Color.normal
end