class GitCrecord::UI::HunksWindow

Public Class Methods

new(win, files) click to toggle source
# File lib/git_crecord/ui/hunks_window.rb, line 15
def initialize(win, files)
  @win = win
  @files = files
  @visibles = @files
  @highlighted = @files[0]
  @scroll_position = 0

  resize
end

Public Instance Methods

addstr(str, y_pos = nil, x_pos = 0, attr: 0, fill: false) click to toggle source
# File lib/git_crecord/ui/hunks_window.rb, line 75
def addstr(str, y_pos = nil, x_pos = 0, attr: 0, fill: false)
  @win.setpos(y_pos, x_pos) unless y_pos.nil?
  @win.attrset(attr)
  @win.addstr(str)
  fill_size = width - @win.curx
  return unless fill && fill_size.positive?

  @win.addstr((fill * fill_size)[0..fill_size])
end
collapse() click to toggle source
# File lib/git_crecord/ui/hunks_window.rb, line 152
def collapse
  toggle_fold if !@highlighted.subs.empty? && @highlighted.expanded
end
commit() click to toggle source
# File lib/git_crecord/ui/hunks_window.rb, line 116
def commit
  QuitAction.new do |reverse|
    Git.stage_files(@files, reverse) && Git.commit
  end
end
content_height(width) click to toggle source
# File lib/git_crecord/ui/hunks_window.rb, line 51
def content_height(width)
  @files.reduce(@files.size) { |a, e| a + e.max_height(width) }
end
expand() click to toggle source
# File lib/git_crecord/ui/hunks_window.rb, line 156
def expand
  toggle_fold if !@highlighted.subs.empty? && !@highlighted.expanded
end
help_window() click to toggle source
# File lib/git_crecord/ui/hunks_window.rb, line 177
def help_window
  HelpWindow.show
  refresh
end
highlight_first() click to toggle source
# File lib/git_crecord/ui/hunks_window.rb, line 130
def highlight_first
  move_highlight(@visibles[0])
end
highlight_last() click to toggle source
# File lib/git_crecord/ui/hunks_window.rb, line 134
def highlight_last
  move_highlight(@visibles[-1])
end
highlight_next() click to toggle source
# File lib/git_crecord/ui/hunks_window.rb, line 122
def highlight_next
  move_highlight(@visibles[@visibles.index(@highlighted) + 1])
end
highlight_next_hunk() click to toggle source
# File lib/git_crecord/ui/hunks_window.rb, line 138
def highlight_next_hunk
  index = @visibles.index(@highlighted)
  move_highlight(
    @visibles[(index + 1)..-1].find { |entry| !entry.subs.empty? }
  )
end
highlight_position() click to toggle source
# File lib/git_crecord/ui/hunks_window.rb, line 28
def highlight_position
  "#{@visibles.index(@highlighted) + 1}/#{@visibles.size}"
end
highlight_previous() click to toggle source
# File lib/git_crecord/ui/hunks_window.rb, line 126
def highlight_previous
  move_highlight(@visibles[[@visibles.index(@highlighted) - 1, 0].max])
end
highlight_previous_hunk() click to toggle source
# File lib/git_crecord/ui/hunks_window.rb, line 145
def highlight_previous_hunk
  index = @visibles.index(@highlighted)
  move_highlight(
    @visibles[0...index].reverse_each.find { |entry| !entry.subs.empty? }
  )
end
move_highlight(to) click to toggle source
# File lib/git_crecord/ui/hunks_window.rb, line 65
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
print_list(list, line_number = -1) click to toggle source
quit() click to toggle source
# File lib/git_crecord/ui/hunks_window.rb, line 108
def quit
  :quit
end
redraw() click to toggle source
# File lib/git_crecord/ui/hunks_window.rb, line 36
def redraw
  @win.clear
  print_list(@files)
  refresh
end
refresh() click to toggle source
# File lib/git_crecord/ui/hunks_window.rb, line 32
def refresh
  @win.refresh(scroll_position, 0, 1, 0, Curses.lines - 1, width)
end
resize() click to toggle source
# File lib/git_crecord/ui/hunks_window.rb, line 42
def resize
  new_width = Curses.cols
  new_height = [Curses.lines - 1, content_height(new_width)].max
  return if width == new_width && @win.maxy == new_height

  @win.resize(new_height, new_width)
  redraw
end
scroll_position() click to toggle source
# File lib/git_crecord/ui/hunks_window.rb, line 55
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
stage() click to toggle source
# File lib/git_crecord/ui/hunks_window.rb, line 112
def stage
  QuitAction.new { |reverse| Git.stage_files(@files, reverse) }
end
toggle_all_selections() click to toggle source
# File lib/git_crecord/ui/hunks_window.rb, line 171
def toggle_all_selections
  new_selected = @files[0].selected == false
  @files.each { |file| file.selected = new_selected }
  redraw
end
toggle_fold() click to toggle source
# File lib/git_crecord/ui/hunks_window.rb, line 160
def toggle_fold
  @highlighted.expanded = !@highlighted.expanded
  update_visibles
  redraw
end
toggle_selection() click to toggle source
# File lib/git_crecord/ui/hunks_window.rb, line 166
def toggle_selection
  @highlighted.selected = !@highlighted.selected
  redraw
end
update_visibles() click to toggle source
# File lib/git_crecord/ui/hunks_window.rb, line 96
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