Class: Difftastic::Differ
- Inherits:
-
Object
- Object
- Difftastic::Differ
- Defined in:
- lib/difftastic/differ.rb
Constant Summary collapse
- DEFAULT_TAB_WIDTH =
2
Instance Method Summary collapse
- #diff_ada(old, new) ⇒ Object
- #diff_apex(old, new) ⇒ Object
- #diff_bash(old, new) ⇒ Object
- #diff_c(old, new) ⇒ Object
- #diff_clojure(old, new) ⇒ Object
- #diff_cmake(old, new) ⇒ Object
- #diff_commonlisp(old, new) ⇒ Object
- #diff_cpp(old, new) ⇒ Object
- #diff_csharp(old, new) ⇒ Object
- #diff_css(old, new) ⇒ Object
- #diff_dart(old, new) ⇒ Object
- #diff_devicetree(old, new) ⇒ Object
- #diff_elisp(old, new) ⇒ Object
- #diff_elixir(old, new) ⇒ Object
- #diff_elm(old, new) ⇒ Object
- #diff_elvish(old, new) ⇒ Object
- #diff_erlang(old, new) ⇒ Object
- #diff_files(old_file, new_file) ⇒ Object
- #diff_fsharp(old, new) ⇒ Object
- #diff_gleam(old, new) ⇒ Object
- #diff_go(old, new) ⇒ Object
- #diff_hack(old, new) ⇒ Object
- #diff_hare(old, new) ⇒ Object
- #diff_haskell(old, new) ⇒ Object
- #diff_hcl(old, new) ⇒ Object
- #diff_html(old, new) ⇒ Object
- #diff_janet(old, new) ⇒ Object
- #diff_java(old, new) ⇒ Object
- #diff_javascript(old, new) ⇒ Object
- #diff_json(old, new) ⇒ Object
- #diff_jsx(old, new) ⇒ Object
- #diff_julia(old, new) ⇒ Object
- #diff_kotlin(old, new) ⇒ Object
- #diff_latex(old, new) ⇒ Object
- #diff_lua(old, new) ⇒ Object
- #diff_make(old, new) ⇒ Object
- #diff_newick(old, new) ⇒ Object
- #diff_nix(old, new) ⇒ Object
- #diff_objc(old, new) ⇒ Object
- #diff_objects(old, new) ⇒ Object
- #diff_ocaml(old, new) ⇒ Object
- #diff_perl(old, new) ⇒ Object
- #diff_php(old, new) ⇒ Object
- #diff_python(old, new) ⇒ Object
- #diff_qml(old, new) ⇒ Object
- #diff_r(old, new) ⇒ Object
- #diff_racket(old, new) ⇒ Object
- #diff_ruby(old, new) ⇒ Object
- #diff_rust(old, new) ⇒ Object
- #diff_scala(old, new) ⇒ Object
- #diff_scheme(old, new) ⇒ Object
- #diff_scss(old, new) ⇒ Object
- #diff_smali(old, new) ⇒ Object
- #diff_solidity(old, new) ⇒ Object
- #diff_sql(old, new) ⇒ Object
- #diff_strings(old, new, file_extension: nil) ⇒ Object
- #diff_swift(old, new) ⇒ Object
- #diff_toml(old, new) ⇒ Object
- #diff_tsx(old, new) ⇒ Object
- #diff_typescript(old, new) ⇒ Object
- #diff_vhdl(old, new) ⇒ Object
- #diff_xml(old, new) ⇒ Object
- #diff_yaml(old, new) ⇒ Object
- #diff_zig(old, new) ⇒ Object
-
#initialize(background: nil, color: nil, syntax_highlight: nil, context: nil, width: nil, tab_width: nil, parse_error_limit: nil, underline_highlights: true, left_label: nil, right_label: nil, display: "side-by-side-show-both") ⇒ Differ
constructor
A new instance of Differ.
Constructor Details
#initialize(background: nil, color: nil, syntax_highlight: nil, context: nil, width: nil, tab_width: nil, parse_error_limit: nil, underline_highlights: true, left_label: nil, right_label: nil, display: "side-by-side-show-both") ⇒ Differ
Returns a new instance of Differ.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/difftastic/differ.rb', line 6 def initialize(background: nil, color: nil, syntax_highlight: nil, context: nil, width: nil, tab_width: nil, parse_error_limit: nil, underline_highlights: true, left_label: nil, right_label: nil, display: "side-by-side-show-both") @show_paths = false @background = background => :dark | :light | nil @color = color => :always | :never | :auto | nil @syntax_highlight = syntax_highlight => :on | :off | nil @context = context => Integer | nil @width = width => Integer | nil @tab_width = tab_width => Integer | nil @parse_error_limit = parse_error_limit => Integer | nil @underline_highlights = underline_highlights => true | false @left_label = left_label => String | nil @right_label = right_label => String | nil @display = display end |
Instance Method Details
#diff_ada(old, new) ⇒ Object
30 31 32 |
# File 'lib/difftastic/differ.rb', line 30 def diff_ada(old, new) diff_strings(old, new, file_extension: "ada") end |
#diff_apex(old, new) ⇒ Object
34 35 36 |
# File 'lib/difftastic/differ.rb', line 34 def diff_apex(old, new) diff_strings(old, new, file_extension: "apex") end |
#diff_bash(old, new) ⇒ Object
38 39 40 |
# File 'lib/difftastic/differ.rb', line 38 def diff_bash(old, new) diff_strings(old, new, file_extension: "sh") end |
#diff_c(old, new) ⇒ Object
42 43 44 |
# File 'lib/difftastic/differ.rb', line 42 def diff_c(old, new) diff_strings(old, new, file_extension: "c") end |
#diff_clojure(old, new) ⇒ Object
54 55 56 |
# File 'lib/difftastic/differ.rb', line 54 def diff_clojure(old, new) diff_strings(old, new, file_extension: "clj") end |
#diff_cmake(old, new) ⇒ Object
58 59 60 |
# File 'lib/difftastic/differ.rb', line 58 def diff_cmake(old, new) diff_strings(old, new, file_extension: "cmake") end |
#diff_commonlisp(old, new) ⇒ Object
62 63 64 |
# File 'lib/difftastic/differ.rb', line 62 def diff_commonlisp(old, new) diff_strings(old, new, file_extension: "lisp") end |
#diff_cpp(old, new) ⇒ Object
46 47 48 |
# File 'lib/difftastic/differ.rb', line 46 def diff_cpp(old, new) diff_strings(old, new, file_extension: "cpp") end |
#diff_csharp(old, new) ⇒ Object
50 51 52 |
# File 'lib/difftastic/differ.rb', line 50 def diff_csharp(old, new) diff_strings(old, new, file_extension: "cs") end |
#diff_css(old, new) ⇒ Object
234 235 236 |
# File 'lib/difftastic/differ.rb', line 234 def diff_css(old, new) diff_strings(old, new, file_extension: "css") end |
#diff_dart(old, new) ⇒ Object
66 67 68 |
# File 'lib/difftastic/differ.rb', line 66 def diff_dart(old, new) diff_strings(old, new, file_extension: "dart") end |
#diff_devicetree(old, new) ⇒ Object
70 71 72 |
# File 'lib/difftastic/differ.rb', line 70 def diff_devicetree(old, new) diff_strings(old, new, file_extension: "dts") end |
#diff_elisp(old, new) ⇒ Object
90 91 92 |
# File 'lib/difftastic/differ.rb', line 90 def diff_elisp(old, new) diff_strings(old, new, file_extension: "el") end |
#diff_elixir(old, new) ⇒ Object
74 75 76 |
# File 'lib/difftastic/differ.rb', line 74 def diff_elixir(old, new) diff_strings(old, new, file_extension: "ex") end |
#diff_elm(old, new) ⇒ Object
78 79 80 |
# File 'lib/difftastic/differ.rb', line 78 def diff_elm(old, new) diff_strings(old, new, file_extension: "elm") end |
#diff_elvish(old, new) ⇒ Object
82 83 84 |
# File 'lib/difftastic/differ.rb', line 82 def diff_elvish(old, new) diff_strings(old, new, file_extension: "elv") end |
#diff_erlang(old, new) ⇒ Object
86 87 88 |
# File 'lib/difftastic/differ.rb', line 86 def diff_erlang(old, new) diff_strings(old, new, file_extension: "erl") end |
#diff_files(old_file, new_file) ⇒ Object
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 |
# File 'lib/difftastic/differ.rb', line 290 def diff_files(old_file, new_file) = [ (file_to_path(old_file)), (file_to_path(new_file)), ("--color=#{@color}" if @color), ("--context=#{@context}" if @context), ("--background=#{@background}" if @background), ("--syntax-highlight=#{@syntax_highlight}" if @syntax_highlight), ("--tab-width=#{@tab_width}" if @tab_width), ("--display=#{@display}" if @display), ("--width=#{@width}" if @width), ].compact! result = Difftastic.execute(.join(" ")).lstrip.sub(/\n{2}\z/, "") unless @show_paths new_line_index = (result.index("\n") || 0) + 1 result = result.byteslice(new_line_index, result.bytesize - new_line_index) end if @left_label || @right_label # Get the first content line to calculate offset offset_line = @show_paths ? 1 : 0 first_line = result.split("\n")[offset_line] # Calculate padding needed between labels offset = right_label_offset(first_line) left_part = if @left_label Difftastic::ANSI.red(@left_label.to_s.ljust(offset)) else " " * offset end right_part = if @right_label Difftastic::ANSI.green(@right_label.to_s) else "" end # Insert formatted labels at the top result = "#{left_part}#{right_part}#{Difftastic::ANSI.reset}\n#{result}" end # Removed due to inconsistencies in the original output. Need to improve the pattern matching. # if @underline_highlights # result.gsub!(/\e\[([0-9;]*)m/) { # codes = $1 # if codes =~ /9[12];1|1;9[12]/ # Matches 91;1, 92;1, 1;91, or 1;92 # "\e[#{codes};4m" # else # "\e[#{codes}m" # end # } # end result end |
#diff_fsharp(old, new) ⇒ Object
94 95 96 |
# File 'lib/difftastic/differ.rb', line 94 def diff_fsharp(old, new) diff_strings(old, new, file_extension: "fs") end |
#diff_gleam(old, new) ⇒ Object
98 99 100 |
# File 'lib/difftastic/differ.rb', line 98 def diff_gleam(old, new) diff_strings(old, new, file_extension: "gleam") end |
#diff_go(old, new) ⇒ Object
102 103 104 |
# File 'lib/difftastic/differ.rb', line 102 def diff_go(old, new) diff_strings(old, new, file_extension: "go") end |
#diff_hack(old, new) ⇒ Object
106 107 108 |
# File 'lib/difftastic/differ.rb', line 106 def diff_hack(old, new) diff_strings(old, new, file_extension: "hack") end |
#diff_hare(old, new) ⇒ Object
110 111 112 |
# File 'lib/difftastic/differ.rb', line 110 def diff_hare(old, new) diff_strings(old, new, file_extension: "ha") end |
#diff_haskell(old, new) ⇒ Object
114 115 116 |
# File 'lib/difftastic/differ.rb', line 114 def diff_haskell(old, new) diff_strings(old, new, file_extension: "hs") end |
#diff_hcl(old, new) ⇒ Object
238 239 240 |
# File 'lib/difftastic/differ.rb', line 238 def diff_hcl(old, new) diff_strings(old, new, file_extension: "hcl") end |
#diff_html(old, new) ⇒ Object
242 243 244 |
# File 'lib/difftastic/differ.rb', line 242 def diff_html(old, new) diff_strings(old, new, file_extension: "html") end |
#diff_janet(old, new) ⇒ Object
118 119 120 |
# File 'lib/difftastic/differ.rb', line 118 def diff_janet(old, new) diff_strings(old, new, file_extension: "janet") end |
#diff_java(old, new) ⇒ Object
122 123 124 |
# File 'lib/difftastic/differ.rb', line 122 def diff_java(old, new) diff_strings(old, new, file_extension: "java") end |
#diff_javascript(old, new) ⇒ Object
126 127 128 |
# File 'lib/difftastic/differ.rb', line 126 def diff_javascript(old, new) diff_strings(old, new, file_extension: "js") end |
#diff_json(old, new) ⇒ Object
246 247 248 |
# File 'lib/difftastic/differ.rb', line 246 def diff_json(old, new) diff_strings(old, new, file_extension: "json") end |
#diff_jsx(old, new) ⇒ Object
130 131 132 |
# File 'lib/difftastic/differ.rb', line 130 def diff_jsx(old, new) diff_strings(old, new, file_extension: "jsx") end |
#diff_julia(old, new) ⇒ Object
134 135 136 |
# File 'lib/difftastic/differ.rb', line 134 def diff_julia(old, new) diff_strings(old, new, file_extension: "jl") end |
#diff_kotlin(old, new) ⇒ Object
138 139 140 |
# File 'lib/difftastic/differ.rb', line 138 def diff_kotlin(old, new) diff_strings(old, new, file_extension: "kt") end |
#diff_latex(old, new) ⇒ Object
250 251 252 |
# File 'lib/difftastic/differ.rb', line 250 def diff_latex(old, new) diff_strings(old, new, file_extension: "tex") end |
#diff_lua(old, new) ⇒ Object
142 143 144 |
# File 'lib/difftastic/differ.rb', line 142 def diff_lua(old, new) diff_strings(old, new, file_extension: "lua") end |
#diff_make(old, new) ⇒ Object
146 147 148 |
# File 'lib/difftastic/differ.rb', line 146 def diff_make(old, new) diff_strings(old, new, file_extension: "mk") end |
#diff_newick(old, new) ⇒ Object
254 255 256 |
# File 'lib/difftastic/differ.rb', line 254 def diff_newick(old, new) diff_strings(old, new, file_extension: "newick") end |
#diff_nix(old, new) ⇒ Object
150 151 152 |
# File 'lib/difftastic/differ.rb', line 150 def diff_nix(old, new) diff_strings(old, new, file_extension: "nix") end |
#diff_objc(old, new) ⇒ Object
154 155 156 |
# File 'lib/difftastic/differ.rb', line 154 def diff_objc(old, new) diff_strings(old, new, file_extension: "m") end |
#diff_objects(old, new) ⇒ Object
21 22 23 24 25 26 27 28 |
# File 'lib/difftastic/differ.rb', line 21 def diff_objects(old, new) tab_width = @tab_width || DEFAULT_TAB_WIDTH old = Difftastic.pretty(old, tab_width:) new = Difftastic.pretty(new, tab_width:) diff_strings(old, new, file_extension: "rb") end |
#diff_ocaml(old, new) ⇒ Object
158 159 160 |
# File 'lib/difftastic/differ.rb', line 158 def diff_ocaml(old, new) diff_strings(old, new, file_extension: "ml") end |
#diff_perl(old, new) ⇒ Object
162 163 164 |
# File 'lib/difftastic/differ.rb', line 162 def diff_perl(old, new) diff_strings(old, new, file_extension: "pl") end |
#diff_php(old, new) ⇒ Object
166 167 168 |
# File 'lib/difftastic/differ.rb', line 166 def diff_php(old, new) diff_strings(old, new, file_extension: "php") end |
#diff_python(old, new) ⇒ Object
170 171 172 |
# File 'lib/difftastic/differ.rb', line 170 def diff_python(old, new) diff_strings(old, new, file_extension: "py") end |
#diff_qml(old, new) ⇒ Object
174 175 176 |
# File 'lib/difftastic/differ.rb', line 174 def diff_qml(old, new) diff_strings(old, new, file_extension: "qml") end |
#diff_r(old, new) ⇒ Object
178 179 180 |
# File 'lib/difftastic/differ.rb', line 178 def diff_r(old, new) diff_strings(old, new, file_extension: "r") end |
#diff_racket(old, new) ⇒ Object
182 183 184 |
# File 'lib/difftastic/differ.rb', line 182 def diff_racket(old, new) diff_strings(old, new, file_extension: "rkt") end |
#diff_ruby(old, new) ⇒ Object
186 187 188 |
# File 'lib/difftastic/differ.rb', line 186 def diff_ruby(old, new) diff_strings(old, new, file_extension: "rb") end |
#diff_rust(old, new) ⇒ Object
190 191 192 |
# File 'lib/difftastic/differ.rb', line 190 def diff_rust(old, new) diff_strings(old, new, file_extension: "rs") end |
#diff_scala(old, new) ⇒ Object
194 195 196 |
# File 'lib/difftastic/differ.rb', line 194 def diff_scala(old, new) diff_strings(old, new, file_extension: "scala") end |
#diff_scheme(old, new) ⇒ Object
198 199 200 |
# File 'lib/difftastic/differ.rb', line 198 def diff_scheme(old, new) diff_strings(old, new, file_extension: "scm") end |
#diff_scss(old, new) ⇒ Object
258 259 260 |
# File 'lib/difftastic/differ.rb', line 258 def diff_scss(old, new) diff_strings(old, new, file_extension: "scss") end |
#diff_smali(old, new) ⇒ Object
202 203 204 |
# File 'lib/difftastic/differ.rb', line 202 def diff_smali(old, new) diff_strings(old, new, file_extension: "smali") end |
#diff_solidity(old, new) ⇒ Object
206 207 208 |
# File 'lib/difftastic/differ.rb', line 206 def diff_solidity(old, new) diff_strings(old, new, file_extension: "sol") end |
#diff_sql(old, new) ⇒ Object
210 211 212 |
# File 'lib/difftastic/differ.rb', line 210 def diff_sql(old, new) diff_strings(old, new, file_extension: "sql") end |
#diff_strings(old, new, file_extension: nil) ⇒ Object
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 |
# File 'lib/difftastic/differ.rb', line 274 def diff_strings(old, new, file_extension: nil) old_file = Tempfile.new(["old", ".#{file_extension}"]) new_file = Tempfile.new(["new", ".#{file_extension}"]) old_file.write(old) new_file.write(new) old_file.close new_file.close diff_files(old_file, new_file) ensure old_file.unlink new_file.unlink end |
#diff_swift(old, new) ⇒ Object
214 215 216 |
# File 'lib/difftastic/differ.rb', line 214 def diff_swift(old, new) diff_strings(old, new, file_extension: "swift") end |
#diff_toml(old, new) ⇒ Object
262 263 264 |
# File 'lib/difftastic/differ.rb', line 262 def diff_toml(old, new) diff_strings(old, new, file_extension: "toml") end |
#diff_tsx(old, new) ⇒ Object
222 223 224 |
# File 'lib/difftastic/differ.rb', line 222 def diff_tsx(old, new) diff_strings(old, new, file_extension: "tsx") end |
#diff_typescript(old, new) ⇒ Object
218 219 220 |
# File 'lib/difftastic/differ.rb', line 218 def diff_typescript(old, new) diff_strings(old, new, file_extension: "ts") end |
#diff_vhdl(old, new) ⇒ Object
226 227 228 |
# File 'lib/difftastic/differ.rb', line 226 def diff_vhdl(old, new) diff_strings(old, new, file_extension: "vhdl") end |
#diff_xml(old, new) ⇒ Object
266 267 268 |
# File 'lib/difftastic/differ.rb', line 266 def diff_xml(old, new) diff_strings(old, new, file_extension: "xml") end |
#diff_yaml(old, new) ⇒ Object
270 271 272 |
# File 'lib/difftastic/differ.rb', line 270 def diff_yaml(old, new) diff_strings(old, new, file_extension: "yaml") end |
#diff_zig(old, new) ⇒ Object
230 231 232 |
# File 'lib/difftastic/differ.rb', line 230 def diff_zig(old, new) diff_strings(old, new, file_extension: "zig") end |