Class: GitCrecord::UI::HunksWindow

Inherits:
Object
  • Object
show all
Defined in:
lib/git_crecord/ui/hunks_window.rb

Instance Method Summary collapse

Constructor Details

#initialize(win, files) ⇒ HunksWindow

Returns a new instance of HunksWindow.



10
11
12
13
14
15
16
17
18
# File 'lib/git_crecord/ui/hunks_window.rb', line 10

def initialize(win, files)
  @win = win
  @files = files
  @visibles = @files
  @highlighted = @files[0]
  @scroll_position = 0

  resize
end

Instance Method Details

#addstr(str, y = nil, x = 0, attr: 0, fill: false) ⇒ Object



69
70
71
72
73
74
75
76
# File 'lib/git_crecord/ui/hunks_window.rb', line 69

def addstr(str, y = nil, x = 0, attr: 0, fill: false)
  @win.setpos(y, x) unless y.nil?
  @win.attrset(attr)
  @win.addstr(str)
  fill_size = width - @win.curx
  return unless fill && fill_size > 0
  @win.addstr((fill * fill_size)[0..fill_size])
end

#collapseObject



141
142
143
# File 'lib/git_crecord/ui/hunks_window.rb', line 141

def collapse
  toggle_fold if !@highlighted.subs.empty? && @highlighted.expanded
end

#commitObject



107
108
109
# File 'lib/git_crecord/ui/hunks_window.rb', line 107

def commit
  QuitAction.new{ Git.stage(@files) && Git.commit }
end

#content_height(width) ⇒ Object



46
47
48
# File 'lib/git_crecord/ui/hunks_window.rb', line 46

def content_height(width)
  @files.reduce(@files.size){ |a, e| a + e.max_height(width) }
end

#expandObject



145
146
147
# File 'lib/git_crecord/ui/hunks_window.rb', line 145

def expand
  toggle_fold if !@highlighted.subs.empty? && !@highlighted.expanded
end

#getchObject



20
21
22
# File 'lib/git_crecord/ui/hunks_window.rb', line 20

def getch
  @win.getch
end

#help_windowObject



166
167
168
169
# File 'lib/git_crecord/ui/hunks_window.rb', line 166

def help_window
  HelpWindow.show
  refresh
end

#highlight_firstObject



119
120
121
# File 'lib/git_crecord/ui/hunks_window.rb', line 119

def highlight_first
  move_highlight(@visibles[0])
end

#highlight_lastObject



123
124
125
# File 'lib/git_crecord/ui/hunks_window.rb', line 123

def highlight_last
  move_highlight(@visibles[-1])
end

#highlight_nextObject



111
112
113
# File 'lib/git_crecord/ui/hunks_window.rb', line 111

def highlight_next
  move_highlight(@visibles[@visibles.index(@highlighted) + 1])
end

#highlight_next_hunkObject



127
128
129
130
131
132
# File 'lib/git_crecord/ui/hunks_window.rb', line 127

def highlight_next_hunk
  index = @visibles.index(@highlighted)
  move_highlight(
    @visibles[(index + 1)..-1].find{ |entry| !entry.subs.empty? }
  )
end

#highlight_previousObject



115
116
117
# File 'lib/git_crecord/ui/hunks_window.rb', line 115

def highlight_previous
  move_highlight(@visibles[[@visibles.index(@highlighted) - 1, 0].max])
end

#highlight_previous_hunkObject



134
135
136
137
138
139
# File 'lib/git_crecord/ui/hunks_window.rb', line 134

def highlight_previous_hunk
  index = @visibles.index(@highlighted)
  move_highlight(
    @visibles[0...index].reverse_each.find{ |entry| !entry.subs.empty? }
  )
end

#move_highlight(to) ⇒ Object



60
61
62
63
64
65
66
67
# File 'lib/git_crecord/ui/hunks_window.rb', line 60

def move_highlight(to)
  return if to == @highlighted || to.nil?
  from = @highlighted
  @highlighted = to
  from.print(self, from.y1 - 1, false)
  to.print(self, to.y1 - 1, true)
  refresh
end


78
79
80
81
82
83
84
85
86
# File 'lib/git_crecord/ui/hunks_window.rb', line 78

def print_list(list, line_number = -1)
  list.each do |entry|
    line_number = entry.print(self, line_number, entry == @highlighted)
    next unless entry.expanded
    line_number = print_list(entry.subs, line_number)
    addstr('', line_number += 1, fill: '_') if entry.is_a?(Diff::File)
  end
  line_number
end

#quitObject



99
100
101
# File 'lib/git_crecord/ui/hunks_window.rb', line 99

def quit
  :quit
end

#redrawObject



32
33
34
35
36
# File 'lib/git_crecord/ui/hunks_window.rb', line 32

def redraw
  @win.clear
  print_list(@files)
  refresh
end

#refreshObject



28
29
30
# File 'lib/git_crecord/ui/hunks_window.rb', line 28

def refresh
  @win.refresh(scroll_position, 0, 0, 0, Curses.lines - 1, width)
end

#resizeObject



38
39
40
41
42
43
44
# File 'lib/git_crecord/ui/hunks_window.rb', line 38

def resize
  new_width = Curses.cols
  new_height = [Curses.lines, content_height(new_width)].max
  return if width == new_width && @win.maxy == new_height
  @win.resize(new_height, new_width)
  redraw
end

#scroll_positionObject



50
51
52
53
54
55
56
57
58
# File 'lib/git_crecord/ui/hunks_window.rb', line 50

def scroll_position
  upper_position = @highlighted.y1 - 3
  if @scroll_position > upper_position
    @scroll_position = upper_position
  elsif @scroll_position <= @highlighted.y2 + 4 - Curses.lines
    @scroll_position = [@highlighted.y2 + 4, @win.maxy].min - Curses.lines
  end
  @scroll_position
end

#stageObject



103
104
105
# File 'lib/git_crecord/ui/hunks_window.rb', line 103

def stage
  QuitAction.new{ Git.stage(@files) }
end

#toggle_all_selectionsObject



160
161
162
163
164
# File 'lib/git_crecord/ui/hunks_window.rb', line 160

def toggle_all_selections
  new_selected = @files[0].selected == false
  @files.each{ |file| file.selected = new_selected }
  redraw
end

#toggle_foldObject



149
150
151
152
153
# File 'lib/git_crecord/ui/hunks_window.rb', line 149

def toggle_fold
  @highlighted.expanded = !@highlighted.expanded
  update_visibles
  redraw
end

#toggle_selectionObject



155
156
157
158
# File 'lib/git_crecord/ui/hunks_window.rb', line 155

def toggle_selection
  @highlighted.selected = !@highlighted.selected
  redraw
end

#update_visiblesObject



88
89
90
91
92
93
94
95
96
97
# File 'lib/git_crecord/ui/hunks_window.rb', line 88

def update_visibles
  @visibles = @files.each_with_object([]) do |entry, vs|
    vs << entry
    next unless entry.expanded
    entry.selectable_subs.each do |entryy|
      vs << entryy
      vs.concat(entryy.selectable_subs) if entryy.expanded
    end
  end
end

#widthObject



24
25
26
# File 'lib/git_crecord/ui/hunks_window.rb', line 24

def width
  @win.maxx
end