diff_highlight 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +11 -0
- data/.rspec +3 -0
- data/.travis.yml +5 -0
- data/Gemfile +6 -0
- data/LICENSE.txt +21 -0
- data/README.md +46 -0
- data/Rakefile +6 -0
- data/bin/setup +8 -0
- data/diff_highlight.gemspec +30 -0
- data/exe/diff_highlight +5 -0
- data/lib/diff_highlight.rb +97 -0
- data/lib/diff_highlight/version.rb +3 -0
- metadata +113 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 7be1aa8bf190a0e49ca40866def43fe39b1453ae4427e492c241c16ba08a15d3
|
4
|
+
data.tar.gz: bd7dead99e89a70ec86d252827b741e7a884ec84b1e6432efb7204ab366b9574
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 0bef56b53401b4cfa602f2cec427aed6a6965d3e4d890ce1a944f11b1ba0818483f2f27c912bab4337aa54495e605cd0fbf8cfbe66135e4e83c5f88ad0eab97e
|
7
|
+
data.tar.gz: 1dd908e32a709b8d926b5fb2d5ebe84b5f8180d040a85eb140379db5c942869f573af29c4b878717eec81944630e9306de1df17f8cc5254bff543adfb41f2903
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2018 John Mair
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
|
2
|
+
# DiffHighlight
|
3
|
+
|
4
|
+
(C) John Mair (banisterfiend)
|
5
|
+
|
6
|
+
Applies syntax highlighting to a `git diff`. Use as follows:
|
7
|
+
|
8
|
+
```
|
9
|
+
git diff | diff_highlight | less
|
10
|
+
```
|
11
|
+
|
12
|
+
Output:
|
13
|
+
|
14
|
+
![img](https://i.imgur.com/DcO4txF.png)
|
15
|
+
|
16
|
+
|
17
|
+
## Features
|
18
|
+
|
19
|
+
* Makes its best guess at the language appearing in the diff and uses appropriate highlighter
|
20
|
+
* Supports Ruby, Javascript, C, YAML, Rakefiles, CSS, HTML, etc
|
21
|
+
* Supports multiple languages in the same diff
|
22
|
+
* Supports streaming input
|
23
|
+
|
24
|
+
## How to use
|
25
|
+
|
26
|
+
This gem provides the `diff_highlight` executable. Just install the gem and the executable should be available :)
|
27
|
+
Use as follows: `git diff | diff_highlight | less`
|
28
|
+
|
29
|
+
Or setup a git alias:
|
30
|
+
|
31
|
+
In `~/.gitconfig`:
|
32
|
+
|
33
|
+
```
|
34
|
+
[alias]
|
35
|
+
diffc="!f() { git diff $@ | diff_highlight | less; }; f"
|
36
|
+
```
|
37
|
+
|
38
|
+
And use like so:
|
39
|
+
|
40
|
+
```
|
41
|
+
git diffc HEAD^2
|
42
|
+
```
|
43
|
+
|
44
|
+
## License
|
45
|
+
|
46
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/setup
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "diff_highlight/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "diff_highlight"
|
8
|
+
spec.version = DiffHighlight::VERSION
|
9
|
+
spec.authors = ["John Mair"]
|
10
|
+
spec.email = ["jrmair@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = "Syntax highlights content from a git diff"
|
13
|
+
spec.description = "git diff | diff_highlight"
|
14
|
+
spec.homepage = "https://github.com/banister/diff_highlight"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
18
|
+
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
19
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
20
|
+
f.match(%r{^(test|spec|features)/})
|
21
|
+
end
|
22
|
+
spec.bindir = "exe"
|
23
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
24
|
+
spec.require_paths = ["lib"]
|
25
|
+
|
26
|
+
spec.add_dependency "coderay", "~> 1.1.0"
|
27
|
+
spec.add_development_dependency "bundler", "~> 1.16"
|
28
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
29
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
30
|
+
end
|
data/exe/diff_highlight
ADDED
@@ -0,0 +1,97 @@
|
|
1
|
+
require "diff_highlight/version"
|
2
|
+
require "coderay"
|
3
|
+
|
4
|
+
class DiffHighlight
|
5
|
+
Signal.trap("PIPE") { exit(0) }
|
6
|
+
|
7
|
+
EXTENSIONS = {
|
8
|
+
%w(.py) => :python,
|
9
|
+
%w(.js) => :javascript,
|
10
|
+
%w(.css) => :css,
|
11
|
+
%w(.xml) => :xml,
|
12
|
+
%w(.php) => :php,
|
13
|
+
%w(.html) => :html,
|
14
|
+
%w(.diff) => :diff,
|
15
|
+
%w(.java) => :java,
|
16
|
+
%w(.json) => :json,
|
17
|
+
%w(.c .h) => :c,
|
18
|
+
%w(.rhtml) => :rhtml,
|
19
|
+
%w(.yaml .yml) => :yaml,
|
20
|
+
%w(.cpp .hpp .cc .h .cxx) => :cpp,
|
21
|
+
%w(.rb .ru .irbrc .gemspec .pryrc .rake) => :ruby,
|
22
|
+
}
|
23
|
+
|
24
|
+
FILES = {
|
25
|
+
%w(Gemfile Rakefile Guardfile Capfile) => :ruby
|
26
|
+
}
|
27
|
+
|
28
|
+
def initialize
|
29
|
+
@buffer = ""
|
30
|
+
end
|
31
|
+
|
32
|
+
def execute
|
33
|
+
ARGF.each_line do |line|
|
34
|
+
if line =~ /^diff --git/
|
35
|
+
flush_buffer
|
36
|
+
@language = type_from_filename(line.split.last)
|
37
|
+
end
|
38
|
+
|
39
|
+
if line =~ /^\+[^\+]/
|
40
|
+
write_added_line(line)
|
41
|
+
else
|
42
|
+
flush_buffer
|
43
|
+
write_other_line(line)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
flush_buffer
|
48
|
+
end
|
49
|
+
|
50
|
+
private
|
51
|
+
|
52
|
+
def type_from_filename(filename, default = :unknown)
|
53
|
+
_, language = EXTENSIONS.find do |k, _|
|
54
|
+
k.any? { |ext| ext == File.extname(filename) }
|
55
|
+
end || FILES.find do |k, _|
|
56
|
+
k.any? { |file_name| file_name == File.basename(filename) }
|
57
|
+
end
|
58
|
+
|
59
|
+
language || default
|
60
|
+
end
|
61
|
+
|
62
|
+
def flush_buffer
|
63
|
+
if !@buffer.empty?
|
64
|
+
write(CodeRay.highlight(@buffer, @language, {}, :term))
|
65
|
+
@buffer.clear
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
def write_added_line(line)
|
70
|
+
if @language == :unknown
|
71
|
+
write(green(line))
|
72
|
+
else
|
73
|
+
@buffer += line
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
# when a line is not an addition (e.g begins with a +)
|
78
|
+
def write_other_line(line)
|
79
|
+
if line =~ /^-/
|
80
|
+
write(red(line))
|
81
|
+
else
|
82
|
+
write(line)
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
def write(str)
|
87
|
+
$stdout.write(str) unless $stdout.closed?
|
88
|
+
end
|
89
|
+
|
90
|
+
def red(str)
|
91
|
+
"\e[31m#{str}\e[0m"
|
92
|
+
end
|
93
|
+
|
94
|
+
def green(str)
|
95
|
+
"\e[32m#{str}\e[0m"
|
96
|
+
end
|
97
|
+
end
|
metadata
ADDED
@@ -0,0 +1,113 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: diff_highlight
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- John Mair
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-01-22 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: coderay
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 1.1.0
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 1.1.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.16'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.16'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '3.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '3.0'
|
69
|
+
description: git diff | diff_highlight
|
70
|
+
email:
|
71
|
+
- jrmair@gmail.com
|
72
|
+
executables:
|
73
|
+
- diff_highlight
|
74
|
+
extensions: []
|
75
|
+
extra_rdoc_files: []
|
76
|
+
files:
|
77
|
+
- ".gitignore"
|
78
|
+
- ".rspec"
|
79
|
+
- ".travis.yml"
|
80
|
+
- Gemfile
|
81
|
+
- LICENSE.txt
|
82
|
+
- README.md
|
83
|
+
- Rakefile
|
84
|
+
- bin/setup
|
85
|
+
- diff_highlight.gemspec
|
86
|
+
- exe/diff_highlight
|
87
|
+
- lib/diff_highlight.rb
|
88
|
+
- lib/diff_highlight/version.rb
|
89
|
+
homepage: https://github.com/banister/diff_highlight
|
90
|
+
licenses:
|
91
|
+
- MIT
|
92
|
+
metadata: {}
|
93
|
+
post_install_message:
|
94
|
+
rdoc_options: []
|
95
|
+
require_paths:
|
96
|
+
- lib
|
97
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
98
|
+
requirements:
|
99
|
+
- - ">="
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: '0'
|
102
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
103
|
+
requirements:
|
104
|
+
- - ">="
|
105
|
+
- !ruby/object:Gem::Version
|
106
|
+
version: '0'
|
107
|
+
requirements: []
|
108
|
+
rubyforge_project:
|
109
|
+
rubygems_version: 2.7.3
|
110
|
+
signing_key:
|
111
|
+
specification_version: 4
|
112
|
+
summary: Syntax highlights content from a git diff
|
113
|
+
test_files: []
|