forked from wofr06/lesspipe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtarcolor
executable file
·197 lines (155 loc) · 4.75 KB
/
tarcolor
1
2
3
4
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
37
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#!/usr/bin/env perl
# tarcolor
#
# by Marc Abramowitz <marc at marc-abramowitz dot com>
#
# https://github.com/msabramo/tarcolor
#
# Colors output of `tar tvf` similarly to the way GNU ls (in GNU
# coreutils) would color a directory listing.
#
# Colors can be customized using an environment variable:
#
# TAR_COLORS='di=01;34:ln=01;36:ex=01;32:so=01;40:pi=01;40:bd=40;33:cd=40;33:su=0;41:sg=0;46'
#
# The format for TAR_COLORS is similar to the format used by LS_COLORS
# Check out the online LSCOLORS generator at http://geoff.greer.fm/lscolors/
use warnings;
use strict;
my $RESET = "\033[0m";
sub get_file_type {
return if (length($_) < 10);
if (substr($_, 0, 1) eq 'l') {
return 'ln';
} elsif (substr($_, 0, 1) eq 'd') {
return 'di';
} elsif (substr($_, 0, 1) eq 's') {
return 'so';
} elsif (substr($_, 3, 1) eq 'S') {
return 'su';
} elsif (substr($_, 6, 1) eq 'S') {
return 'sg';
} elsif (substr($_, 0, 1) eq 'p') {
return 'pi';
} elsif (substr($_, 0, 1) eq 'c') {
return 'cd';
} elsif (substr($_, 0, 1) eq 'b') {
return 'bd';
} elsif (substr($_, 0, 1) eq 'D') {
return 'do';
} elsif (substr($_, 3, 1) eq 'x') {
return 'ex';
} elsif (/\.\w{1,3}$/) {
return '*' . $&;
}
}
sub get_filename {
my $suntar_date = m{
(?: Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) # Month
\s+
\d{1,2} # Day
\s+
\d{2}:\d{2} # Time
\s+
\d{4} # Year
[\s,]+
(.+?) # Capture group 1: filename
(?=\s->|$)
}gx;
if ($suntar_date) {
return $1, pos();
}
my $bsdtar_date = m{
(?: Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) # Month
\s+
\d{1,2} # Day
\s+
(?: (?: \d{4}) | (?: \d{2}:\d{2})) # Year or time
\s+
(.+?) # Capture group 1: filename
(?=\s->|$)
}gx;
if ($bsdtar_date) {
return $1, pos();
}
my $bsdtar_date_dmy = m{
\d{1,2} # Day
\s+
(?: Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) # Month
\s+
(?: (?: \d{4}) | (?: \d{2}:\d{2})) # Year or time
\s+
(.+?) # Capture group 1: filename
(?=\s->|$)
}gx;
if ($bsdtar_date_dmy) {
return $1, pos();
}
my $gnutar_date = m{
\d{4}-\d{2}-\d{2} # Date (%Y-%m-%d)
\s
\d{2}:\d{2} # Time (%H:%M)
(?: :\d{2})? # [Optional] seconds in time
\s+
(.+?) # Capture group 1: filename
(?=\s->|$)
}gx;
if ($gnutar_date) {
return $1, pos();
}
}
sub color_filename {
my ($color) = @_;
my ($filename, $pos) = get_filename();
if ($filename && $pos) {
substr($_, $pos - length($filename), length($filename)) = $color . $filename . $RESET;
}
}
sub default_ls_colors {
return '';
}
if ( -t STDIN ) {
print "Example: tar tvzf some_tarball.tar.gz | tarcolor\n";
exit(0);
}
my %FILE_TYPE_TO_COLOR = (
"di" => "\033[01;34m",
"ln" => "\033[01;36m",
"ex" => "\033[01;32m",
"so" => "\033[01;35m",
"pi" => "\033[40;33m",
"bd" => "\033[40;33;01m",
"cd" => "\033[40;33;01m",
"su" => "\033[37;41m",
"sg" => "\033[30;43m",
);
my $tar_colors = $ENV{'TAR_COLORS'} || $ENV{'LS_COLORS'} || default_ls_colors();
foreach (split(':', $tar_colors)) {
my ($type, $codes) = split('=');
$FILE_TYPE_TO_COLOR{$type} = "\033[" . $codes . "m";
}
while (<>) {
my $type = get_file_type();
if ($type && $FILE_TYPE_TO_COLOR{$type}) {
color_filename($FILE_TYPE_TO_COLOR{$type});
}
print;
}
# ABSTRACT: colors output of `tar tvf`
# PODNAME: tarcolor
=pod
=head1 SYNOPSIS
tar tvzf <tarball.tar.gz> | tarcolor
=head1 DESCRIPTION
Tarcolor colors the output of `tar tvf` similarly to how ls does it.
Colors output of `tar tvf` similarly to the way GNU ls (in GNU coreutils) would
color a directory listing.
Colors can be customized using an environment variable:
TAR_COLORS='di=01;34:ln=01;36:ex=01;32:so=01;40:pi=01;40:bd=40;33:cd=40;33:su=0;41:sg=0;46'
The format for TAR_COLORS is similar to the format used by LS_COLORS Check out
the online LSCOLORS generator at http://geoff.greer.fm/lscolors/
=head1 SEE ALSO
tarcolorauto(1)
=head1 SOURCE CODE
https://github.com/msabramo/tarcolor
=cut