-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
scat
executable file
·59 lines (50 loc) · 1.36 KB
/
scat
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
#!/usr/bin/perl
# vim:ft=perl:
# abstract: a cat with syntax highlighting powers
=scat
A cat with syntax highlighting powers
scat is a wrapper around GNU Source Highlight that might figure out
what kind of syntax highlighting you'd want by looking at the extension OR
the shebang, if any. If all fails, it'll use the --infer-lang option that
might or might not succeed.
=cut
use strict;
use Carp;
print "I need a file to scat!\n" and exit(1) unless @ARGV;
my $f = shift;
my %lang = (
pl => 'perl',
);
sub whatlang {
my $file = shift;
my $ext;
chomp(my @supported = lc(`source-highlight --lang-list`));
if($file =~ /.*\.(.+)$/) { #foo.(pl)
$ext = $lang{$1};
}
else {
open(my $fh,'<',$file) or croak "Cant open $file: $!";
my @foo = <$fh>;
if($foo[0] =~ /(?:[A-Za-z0-9]+)\/([A-Za-z0-9]+)\//i) { # /usr/bin/(perl)
$ext = $'; # everything after /usr/bin/
}
elsif($foo[0] =~ /(?:[A-Za-z0-9]+)\//) { # /bin/(sh)
$ext = $';
}
}
for my $ft(@supported) {
if($ft =~ /^(.+)\s=/) {
$ft = $1;
}
}
if($ext ~~ @supported) {
system("source-highlight -o STDOUT -f esc -n -s $ext -i $file") == 0
or croak("Dont do like that!");
}
else {
system("source-highlight -o STDOUT -f esc -n --infer-lang -i $file") == 0
or croak("That didnt went very well, dear sir...");
}
print "\n";
}
whatlang($f);