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
31
# 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 = []
  @selected = true
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



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

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

#max_height(width) ⇒ Object



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

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



83
84
85
86
87
88
# File 'lib/git_crecord/diff/difference.rb', line 83

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



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

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


90
91
92
93
94
95
96
97
98
99
100
# File 'lib/git_crecord/diff/difference.rb', line 90

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)


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

def selectable?
  true
end

#selectable_subsObject



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

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

#selectedObject



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

def selected
  return @selected if selectable_subs.empty?

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

  :partly
end

#selected=(value) ⇒ Object



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

def selected=(value)
  if selectable_subs.empty?
    @selected = value
  else
    selectable_subs.each { |sub| sub.selected = value }
  end
end

#strings(width) ⇒ Object



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

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

#style(is_highlighted) ⇒ Object



73
74
75
76
77
# File 'lib/git_crecord/diff/difference.rb', line 73

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

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