Class: Fugit::Diff
- Inherits:
-
Panel
- Object
- Panel
- Fugit::Diff
- Defined in:
- lib/fugit/diff.rb
Instance Method Summary collapse
- #apply_diff(diff, type) ⇒ Object
- #change_value(value) ⇒ Object
- #clear ⇒ Object
-
#initialize(parent) ⇒ Diff
constructor
A new instance of Diff.
- #on_double_click(event) ⇒ Object
- #on_item_menu_request(event) ⇒ Object
- #on_menu_stage_chunk(event) ⇒ Object
- #on_menu_stage_line(event) ⇒ Object
- #set_diff(value, type) ⇒ Object
Constructor Details
#initialize(parent) ⇒ Diff
Returns a new instance of Diff.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/fugit/diff.rb', line 5 def initialize(parent) super(parent, ID_ANY) self.set_font(Font.new(8, FONTFAMILY_TELETYPE, FONTSTYLE_NORMAL, FONTWEIGHT_NORMAL)) @list = TreeCtrl.new(self, ID_ANY, nil, nil, NO_BORDER|TR_MULTIPLE|TR_HIDE_ROOT|TR_FULL_ROW_HIGHLIGHT|TR_NO_LINES) @root = @list.add_root("root") @list.hide @list_menu = Menu.new @menu_stage_chunk = @list_menu.append('Stage this chunk') @menu_stage_line = @list_menu.append('Stage this line') (@menu_stage_chunk, :on_menu_stage_chunk) (@menu_stage_line, :on_menu_stage_line) @text = TextCtrl.new(self, ID_ANY, nil, nil, nil, TE_MULTILINE|TE_DONTWRAP|TE_READONLY) @text.hide @box = BoxSizer.new(VERTICAL) @box.add(@list, 1, EXPAND) @box.add(@text, 1, EXPAND) self.set_sizer(@box) #~ evt_tree_sel_changed(@list.get_id, :on_click) (@list.get_id, :on_item_menu_request) evt_tree_item_activated(@list.get_id, :on_double_click) (:commit_saved, :clear) (:diff_clear, :clear) (:diff_set, :set_diff) (:diff_raw, :change_value) (:exiting) {self.hide} # Things seem to run smoother if we hide before destruction end |
Instance Method Details
#apply_diff(diff, type) ⇒ Object
137 138 139 140 141 142 143 144 |
# File 'lib/fugit/diff.rb', line 137 def apply_diff(diff, type) return if !diff or diff.empty? reverse = (type == :staged ? "--reverse" : "") diff_file = File.join(Dir.pwd, ".git", "fugit_partial.diff") File.open(diff_file, "wb") {|f| f << diff} # Write out in binary mode to preserve newlines, otherwise git freaks out `git apply --cached #{reverse} .git/fugit_partial.diff` (:index_changed) end |
#change_value(value) ⇒ Object
101 102 103 104 105 106 107 |
# File 'lib/fugit/diff.rb', line 101 def change_value(value) @list.hide @text.set_value(value) @text.show @text.set_focus @box.layout end |
#clear ⇒ Object
96 97 98 99 |
# File 'lib/fugit/diff.rb', line 96 def clear @list.hide @text.hide end |
#on_double_click(event) ⇒ Object
129 130 131 132 133 134 135 |
# File 'lib/fugit/diff.rb', line 129 def on_double_click(event) i = event.get_item unless @root == i = @list.get_item_data(i) apply_diff([1], [2]) if end end |
#on_item_menu_request(event) ⇒ Object
109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/fugit/diff.rb', line 109 def (event) i = event.get_item @menu_data = nil unless @root == i @menu_data = @list.get_item_data(i) @list_menu.set_label(@menu_stage_chunk.get_id, (@menu_data[2] == :staged ? "Unstage chunk" : "Stage chunk")) @list_menu.set_label(@menu_stage_line.get_id, (@menu_data[2] == :staged ? "Unstage line" : "Stage line")) @menu_stage_line.enable(!@menu_data[1].empty?) @list.(@list_menu) end end |
#on_menu_stage_chunk(event) ⇒ Object
121 122 123 |
# File 'lib/fugit/diff.rb', line 121 def (event) apply_diff(@menu_data[0], @menu_data[2]) if @menu_data end |
#on_menu_stage_line(event) ⇒ Object
125 126 127 |
# File 'lib/fugit/diff.rb', line 125 def (event) apply_diff(@menu_data[1], @menu_data[2]) if @menu_data end |
#set_diff(value, type) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/fugit/diff.rb', line 38 def set_diff(value, type) chunks = value.split("\n@@") header = chunks.slice!(0) chunks.map! {|line| "@@"+line} @text.hide @list.hide @list.delete_children(@root) chunks.each do |chunk| chunk_diff = header + "\n" + chunk chunk_diff += "\n" if chunk_diff[-1..-1] != "\n" # git bitches if we don't have a proper newline at the end of the diff lines = chunk.split("\n") lines.each_index do |i| line = lines[i] line_diff = case line[0..0] when "+", "-" chunk_lines = chunk.split("\n") diff_val = chunk_lines.first.match(/\A@@ -\d+,(\d+)/)[1].to_i + (line[0..0] == "+" ? 1 : -1) chunk_lines[0] = chunk_lines.first.gsub(/\+(\d+),\d+/, '+\1,' + diff_val.to_s) chunk_lines.delete_at(i) chunk_lines.map! {|l| l.gsub(/\A-/, " ")} chunk_lines.insert(i, line) chunk_lines.reject! {|l| l[0..0] == "+" && l != line} header + "\n" + chunk_lines.join("\n") + "\n" else "" end id = @list.append_item(@root, line.gsub("\t", " "), -1, -1, [chunk_diff, line_diff, type]) color = case line[0..0] when "+" Colour.new(0, 96, 0) when "-" Colour.new(160, 0, 0) when "@" Colour.new(0, 0, 150) end bgcolor = case line[0..0] when "+" Colour.new(220, 255, 220) when "-" Colour.new(255, 220, 220) when "@" Colour.new(220, 220, 225) end @list.set_item_text_colour(id, color) if color @list.set_item_background_colour(id, bgcolor) if bgcolor end end @list.show @list.set_focus @box.layout end |