Skip to content

Commit 992ecf6

Browse files
committed
Various small perf tweaks in best-color-rule
1 parent ef3dd76 commit 992ecf6

File tree

1 file changed

+27
-38
lines changed

1 file changed

+27
-38
lines changed

lib/Color/DirColors.rakumod

Lines changed: 27 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -134,63 +134,52 @@ method !best-color-rule(::?CLASS:D: IO::Path:D $path --> Str:D) {
134134

135135
# First try rules based on mode (inode type info and permission bits);
136136
# failure to stat a mode at all indicates an orphan.
137-
my ($mtype, $type);
138-
my $stat = lstat($path);
139-
my $mode = $stat.mode;
137+
my $type;
138+
my $stat = lstat($path);
139+
my $mode = $stat.mode;
140140
if $mode.defined {
141-
$mtype = ($mode +& 0o170000) +> 12;
142-
$type = @mtypes[$mtype];
141+
$type = @mtypes[($mode +& 0o170000) +> 12];
143142
}
144143
else {
145-
$mode = 0;
146-
$mtype = 0;
147-
$type = 'orphan';
144+
$mode = 0;
145+
$type = 'orphan';
148146
}
149147

150148
# Specialize 'dir' type if non-empty rules for sticky/o+w and mode matches
151149
if $type eq 'dir' {
152150
my $ow = $mode +& 0o0002;
153151
my $st = $mode +& 0o1000;
154-
$type = 'dir_sticky' if $st && %.type-rules{'dir_sticky'};
155-
$type = 'dir_o+w' if $ow && %.type-rules{'dir_o+w'};
156-
$type = 'dir_o+w_sticky' if $ow && $st && %.type-rules{'dir_o+w_sticky'};
152+
$type = 'dir_sticky' if $st && %!type-rules{'dir_sticky'};
153+
$type = 'dir_o+w' if $ow && %!type-rules{'dir_o+w'};
154+
$type = 'dir_o+w_sticky' if $ow && $st && %!type-rules{'dir_o+w_sticky'};
157155
}
158156
# Check for orphaned symlinks
159157
elsif $type eq 'symlink' {
160-
$type = 'orphan' if !$path.readlink.e && %.type-rules<orphan>;
158+
$type = 'orphan' if %!type-rules<orphan> && !$path.readlink.e;
161159
}
162160

163-
# Check for multiple hardlinks
164-
unless %.type-rules{$type} {
165-
$type = 'multi_hardlink' if $stat.nlink > 1 && %.type-rules<multi_hardlink>;
161+
# Check for multiple hardlinks and executable bits
162+
my $exe = $mode +& 0o0111;
163+
unless %!type-rules{$type} {
164+
$type = 'multi_hardlink' if %!type-rules<multi_hardlink> && $stat.nlink > 1;
165+
$type = 'exe' if $exe && %!type-rules<exe>;
166166
}
167167

168168
# Check for setuid/setgid, and whether they are on something executable
169-
my $exe = $mode +& 0o0111;
170-
my $setuid = $mode +& 0o4000;
171-
my $setgid = $mode +& 0o2000;
172-
173-
$type = 'setgid' if $setgid && %.type-rules<setgid>;
174-
$type = 'setuid' if $setuid && %.type-rules<setuid>;
175-
if $exe {
176-
$type = 'exe_setgid' if $setgid && %.type-rules<exe_setgid>;
177-
$type = 'exe_setuid' if $setuid && %.type-rules<exe_setuid>;
169+
if $mode +& 0o6000 {
170+
my $setgid = $mode +& 0o2000;
171+
my $setuid = $mode +& 0o4000;
172+
173+
$type = 'setgid' if $setgid && %!type-rules<setgid>;
174+
$type = 'setuid' if $setuid && %!type-rules<setuid>;
175+
if $exe {
176+
$type = 'exe_setgid' if $setgid && %!type-rules<exe_setgid>;
177+
$type = 'exe_setuid' if $setuid && %!type-rules<exe_setuid>;
178+
}
178179
}
179180

180-
# If we've got a non-empty mode/type rule, choose that one
181-
return %.type-rules{$type} if %.type-rules{$type};
182-
183-
# Nothing special found in mode matches, try extension and return if found
184-
my $ext = $path.extension;
185-
my $ext-rule = %.ext-rules{$ext};
186-
return %.ext-rules{$ext} if %.ext-rules{$ext};
187-
188-
# Extension didn't match, try a general glob if any match
189-
my $basename = $path.basename;
190-
my $glob-rule; # = %glob-rules.keys.first({ ... }); # XXXX: Glob match basename
191-
192-
# Glob or bust
193-
$glob-rule // ''
181+
# Ignores glob rules for now
182+
%!type-rules{$type} || %!ext-rules{$path.extension} || '';
194183
}
195184

196185
# (PRIVATE) Convert a BSD two-letter code to a Terminal::ANSIColor color string

0 commit comments

Comments
 (0)