Skip to content

Commit 749adf2

Browse files
Merge branch 'next' of github.com:scottchiefbaker/diff-so-fancy into next
2 parents 77de3bd + c12b91a commit 749adf2

File tree

1 file changed

+211
-0
lines changed

1 file changed

+211
-0
lines changed
Lines changed: 211 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,211 @@
1+
#!/usr/bin/perl
2+
3+
use strict;
4+
5+
my $args = join(" ",@ARGV);
6+
my ($perl) = $args =~ /--perl/;
7+
my ($both) = $args =~ /--both/;
8+
9+
# If we want both, we set perl also
10+
if ($both) {
11+
$perl = 1;
12+
}
13+
14+
# Term::ANSIColor didn't get 256 color constants until 4.0
15+
if ($perl && has_term_ansicolor(4.0)) {
16+
require Term::ANSIColor;
17+
Term::ANSIColor->import(':constants','color','uncolor');
18+
19+
#print "TERM::ANSIColor constant names:\n";
20+
term_ansicolor();
21+
} else {
22+
my $cols = 120;
23+
my $rows = 24;
24+
if (-f '/bin/stty') {
25+
($rows,$cols) = split(/ /,`/bin/stty size`);
26+
}
27+
28+
my $section = 1;
29+
my $grouping = 8;
30+
31+
for (my $i=0;$i<256;$i++) {
32+
print set_bcolor($i); # Set the background color
33+
34+
if (needs_white($i)) {
35+
print set_fcolor(15); # White
36+
printf(" %03d ",$i); # Ouput the color number in white
37+
} else {
38+
print set_fcolor(0); # Black
39+
printf(" %03d ",$i); # Ouput the color number in black
40+
}
41+
42+
print set_fcolor(); # Reset both colors
43+
print " "; # Seperators
44+
45+
if ($i == 15 || $i == 231) {
46+
print set_bcolor(); # Reset
47+
print "\n\n";
48+
$section = 0;
49+
$grouping = 6;
50+
} elsif ($section > 0 && ($section % $grouping == 0)) {
51+
print set_bcolor(); # Reset
52+
print "\n";
53+
}
54+
55+
$section++;
56+
}
57+
}
58+
59+
END {
60+
print set_fcolor(); # Reset the colors
61+
print "\n";
62+
}
63+
64+
#################################################################################
65+
66+
sub has_term_ansicolor {
67+
my $version = shift();
68+
$version ||= 4;
69+
70+
eval {
71+
# Check if we have Term::ANSIColor version 4.0
72+
require Term::ANSIColor;
73+
Term::ANSIColor->VERSION($version);
74+
};
75+
76+
if ($@) {
77+
return 0;
78+
} else {
79+
return 1;
80+
}
81+
}
82+
83+
sub set_fcolor {
84+
my $c = shift();
85+
86+
my $ret = '';
87+
if (!defined($c)) { $ret = "\e[0m"; } # Reset the color
88+
else { $ret = "\e[38;5;${c}m"; }
89+
90+
return $ret;
91+
}
92+
93+
sub set_bcolor {
94+
my $c = shift();
95+
96+
my $ret = '';
97+
if (!defined($c)) { $ret = "\e[0m"; } # Reset the color
98+
else { $ret .= "\e[48;5;${c}m"; }
99+
100+
return $ret;
101+
}
102+
103+
sub highlight_string {
104+
my $needle = shift();
105+
my $haystack = shift();
106+
my $color = shift() || 2; # Green if they don't pass in a color
107+
108+
my $fc = set_fcolor($color);
109+
my $reset = set_fcolor();
110+
111+
$haystack =~ s/$needle/$fc.$needle.$reset/e;
112+
113+
return $haystack;
114+
}
115+
116+
sub get_color_mapping {
117+
my $map = {};
118+
119+
for (my $i = 0; $i < 256; $i++) {
120+
my $str = "\e[38;5;${i}m";
121+
my ($acc) = uncolor($str);
122+
123+
$map->{$acc} = int($i);
124+
}
125+
126+
return $map;
127+
}
128+
129+
sub term_ansicolor {
130+
my @colors = get_color_names();
131+
my $map = get_color_mapping();
132+
133+
my $absolute = 0;
134+
my $group = 0;
135+
my $grouping = 8;
136+
137+
print "Showing Term::ANSIColor constant names\n\n";
138+
139+
foreach my $name (@colors) {
140+
my $bg = "on_$name";
141+
my $map_num = int($map->{$name});
142+
my $perl_name = sprintf("%6s",$name);
143+
my $ansi_number = sprintf("#%03i",$map_num);
144+
145+
my $name_string = "";
146+
if ($both) {
147+
$name_string = "$perl_name / $ansi_number";
148+
} else {
149+
$name_string = "$perl_name";
150+
}
151+
152+
if (needs_white($map_num)) {
153+
print color($bg) . " " . color('bright_white') . $name_string . " ";
154+
} else {
155+
print color($bg) . " " . color("black") . $name_string . " ";
156+
}
157+
print color('reset') . " ";
158+
159+
$absolute++;
160+
$group++;
161+
162+
if ($absolute == 16 || $absolute == 232) {
163+
print "\n\n";
164+
$group = 0;
165+
$grouping = 6;
166+
} elsif ($group % $grouping == 0) {
167+
print "\n";
168+
}
169+
}
170+
}
171+
172+
sub get_color_names {
173+
my @colors = ();
174+
my ($r,$g,$b) = 0;
175+
176+
for (my $i = 0; $i < 16; $i++) {
177+
my $name = "ansi$i";
178+
push(@colors,$name);
179+
}
180+
181+
for ($r = 0; $r <= 5; $r++) {
182+
for ($g = 0; $g <= 5; $g++) {
183+
for ($b = 0; $b <= 5; $b++) {
184+
my $name = "rgb$r$g$b";
185+
push(@colors,$name);
186+
}
187+
}
188+
}
189+
190+
for (my $i = 0; $i < 24; $i++) {
191+
my $name = "grey$i";
192+
push(@colors,$name);
193+
}
194+
195+
return @colors;
196+
}
197+
198+
sub needs_white {
199+
my $num = shift();
200+
201+
# Sorta lame, but it's a hard coded list of which background colors need a white foreground
202+
my @white = qw(0 1 4 5 8 232 233 234 235 236 237 238 239 240 241 242 243 16 17 18
203+
19 20 21 22 28 52 53 54 55 25 56 57 58 59 60 88 89 90 91 92 93 124 125 29 30 31 26
204+
27 61 62 64 160 196 161 126 63 94 95 100 101 127 128 129 12 130 131 23 24);
205+
206+
if (grep(/\b$num\b/,@white)) {
207+
return 1,
208+
} else {
209+
return 0;
210+
}
211+
}

0 commit comments

Comments
 (0)