-
Notifications
You must be signed in to change notification settings - Fork 29
/
lookup.pl
executable file
·134 lines (119 loc) · 3.45 KB
/
lookup.pl
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
#!/usr/bin/env perl
use strict;
use warnings;
use Term::ANSIColor;
use v5.10.0;
no warnings 'experimental';
my $composurePath = ($ENV{'XDG_DATA_HOME'} || $ENV{'HOME'} . '/.local') . '/composure';
my $referenceColor = $ENV{'COMPOSURE_REFERENCE_COLOR'} || 'bold bright_red';
my @funcs = glob($composurePath . '/*.inc') or die "Cannot find composure files: $!";
my %groupsSeen = ();
my $groupFilter;
sub main {
my $command = shift;
my %actions = (
glossary => \&glossary,
reference => \&reference
);
$actions{$command}(@_) || die "Unknown command: $command";
}
sub glossary {
my $groupFilter = shift;
foreach my $func (@funcs) {
my ($name, $about, $groupsRef, undef, undef) = extract_metadata_from_file($func);
my %groupsHash = map { $_ => 1 } @$groupsRef;
if ($groupFilter && !exists($groupsHash{$groupFilter})) {
next;
}
if (!$about) {
$about = "No description provided.";
}
print colorize($referenceColor, $name) . ": " . colorize('bright_white', $about) . "\n";
}
print colorize('bold bright_white', "\ngroups defined: ") . join(', ', sort(keys(%groupsSeen))) . "\n";
return 1;
}
sub reference {
my $func = shift;
my $funcPath = $composurePath . '/' . $func . '.inc';
my ($name, $about, $groupsRef, $examplesRef, $paramsRef) = extract_metadata_from_file($funcPath);
say colorize($referenceColor, $name) . ": " . colorize('bright_white', $about);
if (scalar @$paramsRef > 0) {
say colorize('dark white', 'params:');
foreach my $param (@$paramsRef) {
say $param;
}
}
if (scalar @$examplesRef > 0) {
say colorize('dark white', 'examples:');
foreach my $example (@$examplesRef) {
say $example;
}
}
if (scalar @$groupsRef > 0) {
say colorize('dark white', 'groups') . ": " . join(' ', sort(@$groupsRef)) . "\n";
}
return 1;
}
sub rm_quotes {
my $str = shift;
return substr($str, 1, -1); # chop leading/trailing quote chars
}
sub extract_metadata_from_file {
my $file = shift;
open my $info, $file or die "Could not open $file: $!";
my $fname = (split('/', $file))[-1];
my $name = (split '.inc', $fname)[0];
my $about;
my @groups = ();
my @params = ();
my @examples = ();
my @chunks = ();
while (my $line = <$info>) {
chomp($line);
SWITCH: {
if ($line =~ /^\s*about /) {
@chunks = split /\s+/, $line;
$about = rm_quotes(join(' ', @chunks[2 .. $#chunks]));
last SWITCH;
}
if ($line =~ /^\s*group /) {
@chunks = split /\s+/, $line;
my $group = rm_quotes($chunks[2]);
if ($group =~ /\w+/) {
push(@groups, $group);
$groupsSeen{$group}++;
}
last SWITCH;
}
if ($line =~ /^\s*example /) {
@chunks = split /\s+/, $line;
my $example = rm_quotes(join(' ', @chunks[2 .. $#chunks]));
if ($example =~ /\w+/) {
push(@examples, $example);
}
last SWITCH;
}
if ($line =~ /^\s*param /) {
@chunks = split /\s+/, $line;
my $param = rm_quotes(join(' ', @chunks[2 .. $#chunks]));
if ($param =~ /\w+/) {
push(@params, $param);
}
last SWITCH;
}
}
}
close $info;
return ($name, $about, \@groups, \@examples, \@params);
}
sub colorize {
my $colorstr = shift;
my $text = shift;
# don't colorize if not attached to a terminal
if (! -t STDOUT) {
return $text;
}
return color($colorstr) . $text . color('reset');
}
main @ARGV