I have a client with a directory tree that has a number of high-level directories that I need to have on my local development machine. I rarely look at them unless something goes awry.
I've marked the "uninteresting" ones with the grey color label and the ones I use daily with a green color label in Finder. That works fine on the rare occasion when I'm using Finder.
Instead, I use Terminal.app, although I am not against other terminal emulators as long as they are fast and robust. When using ls
, even with the @
and G
flags, I don't have useful information that I can use to de-emphasize the uninteresting directory entries. (If I could pipe ls
through a simple awk
script to apply coloring, I'd be fine with that.)
I am aware that I can use osascript to get this attribute and then decorate the file output, but that will probably result in the slowest ls
implementation since Unicos. I also know I can change the default colors with ls, but that doesn't quite dig to the level that I need.
Is there a simple, fast, tool that already exists that will color ls output based on Finder label colors, and then fall back to $LSCOLORS
? Or is that my next GitHub project?
Best Answer
Color information is available from the
com.apple.FinderInfo
extended attribute.First row, tenth byte, bits 3-1 (i.e. those with binary values 2, 4 and 8). The example output is for a red file with no other values set.
If the attribute isn't available or the nibble is
0
, it's colorless.Possible values in context-menu order from left to right:
0
(colorless),C
(red),E
,A
,4
,8
,6
,2
(gray).Remember to check for possible
1
values for that last bit, i.e.3
would also be gray + some other attribute.One possible solution would be the following:
Define a function
ls
in~/.bash_profile
that does something slightly different if no parameters are given:colorize.sh
could look like the following, calling a Python script and printing the filename depending on its output:And
attrs.py
, which extracts relevant file attributes, in the same directory:I didn't have a large enough matching color selection here, so red and orange are both printed in red.
I added the
hidden
attribute here since I am interested in that part of modifyingls
output. Just remove theif
statement if you don't want it.