Class: GitCrecord::UI::HunksWindow
- Inherits:
-
Object
- Object
- GitCrecord::UI::HunksWindow
- Defined in:
- lib/git_crecord/ui/hunks_window.rb
Instance Method Summary collapse
- #addstr(str, y_pos = nil, x_pos = 0, attr: 0, fill: false) ⇒ Object
- #collapse ⇒ Object
- #commit ⇒ Object
- #content_height(width) ⇒ Object
- #expand ⇒ Object
- #getch ⇒ Object
- #help_window ⇒ Object
- #highlight_first ⇒ Object
- #highlight_last ⇒ Object
- #highlight_next ⇒ Object
- #highlight_next_hunk ⇒ Object
- #highlight_previous ⇒ Object
- #highlight_previous_hunk ⇒ Object
-
#initialize(win, files) ⇒ HunksWindow
constructor
A new instance of HunksWindow.
- #move_highlight(to) ⇒ Object
- #print_list(list, line_number = -1)) ⇒ Object
- #quit ⇒ Object
- #redraw ⇒ Object
- #refresh ⇒ Object
- #resize ⇒ Object
- #scroll_position ⇒ Object
- #stage ⇒ Object
- #toggle_all_selections ⇒ Object
- #toggle_fold ⇒ Object
- #toggle_selection ⇒ Object
- #update_visibles ⇒ Object
- #width ⇒ Object
Constructor Details
#initialize(win, files) ⇒ HunksWindow
Returns a new instance of HunksWindow.
12 13 14 15 16 17 18 19 20 |
# File 'lib/git_crecord/ui/hunks_window.rb', line 12 def initialize(win, files) @win = win @files = files @visibles = @files @highlighted = @files[0] @scroll_position = 0 resize end |
Instance Method Details
#addstr(str, y_pos = nil, x_pos = 0, attr: 0, fill: false) ⇒ Object
73 74 75 76 77 78 79 80 81 |
# File 'lib/git_crecord/ui/hunks_window.rb', line 73 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 ⇒ Object
148 149 150 |
# File 'lib/git_crecord/ui/hunks_window.rb', line 148 def collapse toggle_fold if !@highlighted.subs.empty? && @highlighted. end |
#commit ⇒ Object
114 115 116 |
# File 'lib/git_crecord/ui/hunks_window.rb', line 114 def commit QuitAction.new { |reverse| Git.stage(@files, reverse) && Git.commit } end |
#content_height(width) ⇒ Object
49 50 51 |
# File 'lib/git_crecord/ui/hunks_window.rb', line 49 def content_height(width) @files.reduce(@files.size) { |a, e| a + e.max_height(width) } end |
#expand ⇒ Object
152 153 154 |
# File 'lib/git_crecord/ui/hunks_window.rb', line 152 def toggle_fold if !@highlighted.subs.empty? && !@highlighted. end |
#getch ⇒ Object
22 23 24 |
# File 'lib/git_crecord/ui/hunks_window.rb', line 22 def getch @win.getch end |
#help_window ⇒ Object
173 174 175 176 |
# File 'lib/git_crecord/ui/hunks_window.rb', line 173 def help_window HelpWindow.show refresh end |
#highlight_first ⇒ Object
126 127 128 |
# File 'lib/git_crecord/ui/hunks_window.rb', line 126 def highlight_first move_highlight(@visibles[0]) end |
#highlight_last ⇒ Object
130 131 132 |
# File 'lib/git_crecord/ui/hunks_window.rb', line 130 def highlight_last move_highlight(@visibles[-1]) end |
#highlight_next ⇒ Object
118 119 120 |
# File 'lib/git_crecord/ui/hunks_window.rb', line 118 def highlight_next move_highlight(@visibles[@visibles.index(@highlighted) + 1]) end |
#highlight_next_hunk ⇒ Object
134 135 136 137 138 139 |
# File 'lib/git_crecord/ui/hunks_window.rb', line 134 def highlight_next_hunk index = @visibles.index(@highlighted) move_highlight( @visibles[(index + 1)..-1].find { |entry| !entry.subs.empty? } ) end |
#highlight_previous ⇒ Object
122 123 124 |
# File 'lib/git_crecord/ui/hunks_window.rb', line 122 def highlight_previous move_highlight(@visibles[[@visibles.index(@highlighted) - 1, 0].max]) end |
#highlight_previous_hunk ⇒ Object
141 142 143 144 145 146 |
# File 'lib/git_crecord/ui/hunks_window.rb', line 141 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
63 64 65 66 67 68 69 70 71 |
# File 'lib/git_crecord/ui/hunks_window.rb', line 63 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)) ⇒ Object
83 84 85 86 87 88 89 90 91 92 |
# File 'lib/git_crecord/ui/hunks_window.rb', line 83 def print_list(list, line_number = -1) list.each do |entry| line_number = entry.print(self, line_number, entry == @highlighted) next unless entry. line_number = print_list(entry.subs, line_number) addstr('', line_number += 1, fill: '_') if entry.is_a?(Diff::File) end line_number end |
#quit ⇒ Object
106 107 108 |
# File 'lib/git_crecord/ui/hunks_window.rb', line 106 def quit :quit end |
#redraw ⇒ Object
34 35 36 37 38 |
# File 'lib/git_crecord/ui/hunks_window.rb', line 34 def redraw @win.clear print_list(@files) refresh end |
#refresh ⇒ Object
30 31 32 |
# File 'lib/git_crecord/ui/hunks_window.rb', line 30 def refresh @win.refresh(scroll_position, 0, 0, 0, Curses.lines - 1, width) end |
#resize ⇒ Object
40 41 42 43 44 45 46 47 |
# File 'lib/git_crecord/ui/hunks_window.rb', line 40 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_position ⇒ Object
53 54 55 56 57 58 59 60 61 |
# File 'lib/git_crecord/ui/hunks_window.rb', line 53 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 ⇒ Object
110 111 112 |
# File 'lib/git_crecord/ui/hunks_window.rb', line 110 def stage QuitAction.new { |reverse| Git.stage(@files, reverse) } end |
#toggle_all_selections ⇒ Object
167 168 169 170 171 |
# File 'lib/git_crecord/ui/hunks_window.rb', line 167 def toggle_all_selections new_selected = @files[0].selected == false @files.each { |file| file.selected = new_selected } redraw end |
#toggle_fold ⇒ Object
156 157 158 159 160 |
# File 'lib/git_crecord/ui/hunks_window.rb', line 156 def toggle_fold @highlighted. = !@highlighted. update_visibles redraw end |
#toggle_selection ⇒ Object
162 163 164 165 |
# File 'lib/git_crecord/ui/hunks_window.rb', line 162 def toggle_selection @highlighted.selected = !@highlighted.selected redraw end |
#update_visibles ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/git_crecord/ui/hunks_window.rb', line 94 def update_visibles @visibles = @files.each_with_object([]) do |entry, vs| vs << entry next unless entry. entry.selectable_subs.each do |entryy| vs << entryy vs.concat(entryy.selectable_subs) if entryy. end end end |
#width ⇒ Object
26 27 28 |
# File 'lib/git_crecord/ui/hunks_window.rb', line 26 def width @win.maxx end |