Based on the responses here and in referenced posts, I made the following function and added it to my ~/.bash_profile file:
# Set Finder label color
label(){
if [ $# -lt 2 ]; then
echo "USAGE: label [0-7] file1 [file2] ..."
echo "Sets the Finder label (color) for files"
echo "Default colors:"
echo " 0 No color"
echo " 1 Orange"
echo " 2 Red"
echo " 3 Yellow"
echo " 4 Blue"
echo " 5 Purple"
echo " 6 Green"
echo " 7 Gray"
else
osascript - "$@" << EOF
on run argv
set labelIndex to (item 1 of argv as number)
repeat with i from 2 to (count of argv)
tell application "Finder"
set theFile to POSIX file (item i of argv) as alias
set label index of theFile to labelIndex
end tell
end repeat
end run
EOF
fi
}
>
By chance, I happened to find the config wiki for mintty
. It has a lot of great suggestions, one of which is how to set various options using escape sequences meant for xterm
. So this works by not reading the config again but instead by interpreting escape sequences to override the existing color selections.
We can use this from URxvt by binding a key to a long chain of commands, each of which changes one of the 16 default colors.
For example, here I set alt+ctrl+l
to change every color to C0C0C0:
# This stupidly changes every color to grey.
URxvt.keysym.M-C-l: command:\033]11;#C0C0C0\007\033]10;#C0C0C0\007\033]4;0;#C0C0C0\007\033]4;1;#C0C0C0\007\033]4;2;#C0C0C0\007\033]4;3;#C0C0C0\007\033]4;4;#C0C0C0\007\033]4;5;#C0C0C0\007\033]4;6;#C0C0C0\007\033]4;7;#C0C0C0\007\033]4;8;#C0C0C0\007\033]4;9;#C0C0C0\007\033]4;10;#C0C0C0\007\033]4;11;#C0C0C0\007\033]4;12;#C0C0C0\007\033]4;13;#C0C0C0\007\033]4;14;#C0C0C0\007\033]4;15;#C0C0C0\007
Perl Plugin
I have it "working" but not really, because it seems that there is a fundamental
difference between resources defined as
URxvt.keysym.M-1: command:\033].......
and any attempt to do the same with $term->cmd_parse()
.
Is there anything that can be done about this? So far I have hardcoded entire
light and dark color sets using (very long) escape sequences:
URxvt.keysym.M-C-l: command:\033]4;12;#72729F9FCFCF\007\033]4;1;#CCCC00000000\007\033]4;15;#EEEEEEEEECEC\007\033]4;14;#3434E2E2E2E2\007\033]4;5;#757550507B7B\007\033]4;3;#C4C4A0A00000\007\033]4;7;#D3D3D7D7CFCF\007\033]4;4;#34346565A4A4\007\033]4;10;#8A8AE2E23434\007\033]4;13;#ADAD7F7FA8A8\007\033]4;8;#555557575353\007\033]11;#FFFFFFFFFFFF\007\033]4;9;#EFEF29292929\007\033]4;2;#4E4E9A9A0606\007\033]4;0;#2E2E34343636\007\033]4;11;#FCFCE9E94F4F\007\033]10;#000000000000\007\033]4;6;#060698209A9A\007
URxvt.keysym.M-C-d: command:\033]4;12;#9090FF\007\033]4;1;#AA0000\007\033]4;15;#FFFFFF\007\033]4;14;#55FFFF\007\033]4;5;#AA00AA\007\033]4;3;#AA5500\007\033]4;7;#AAAAAA\007\033]4;10;#55FF55\007\033]4;13;#FF55FF\007\033]4;4;#0000AD\007\033]4;8;#555555\007\033]11;#000000\007\033]4;9;#FF5555\007\033]4;2;#00AA00\007\033]\007\033]4;0;#000000\007\033]4;11;#FFFF55\007\033]10;#00FF00\007\033]5;0;#00FF00\007\033]4;6;#00AAAA\007
This works exactly how I'd hoped it would and can be toggled at runtime so I'm marking this as answered, but why can't this be done dynamically from Perl? Here is what I have so far, I'm not a great Perl coder so please excuse the undoubtedly bad style.
Hopefully someone can chime in on what the issue is. This will be a nice plugin. Upstream is at github.
#! /usr/bin/env perl -w
# Author: John Tyree
# Website: http://github.com/johntyree/urxvt-perls/blob/master/rotate-colors
# License: CCBYNC
# Use keyboard shortcuts to load colors of the form *.colorN:XXXXXX from a file
# This gives us "on demand" theme switching.
# Usage: put the following lines in your .Xdefaults/.Xresources:
# URxvt.perl-ext-common: ...,rotate-colors
# URxvt.colorFiles: ~/.Xresources,~/light.txt,~/dark.txt
# URxvt.keysym.M-C-n: perl:rotate-colors:forward
# URxvt.keysym.M-C-p: perl:rotate-colors:backward
use strict;
sub on_start {
my ($self) = @_;
$self->{current_index} = -1;
my @arr = split(/,/, $self->x_resource('colorFiles') || '');
$self->{color_files} = \@arr;
()
}
sub read_colors {
my $fn = shift;
open my $fin, $fn or print STDERR "Unable to open $fn for reading";
my %colors;
while (my $line = <$fin>) {
if ($line =~ /(\w+)\s*:\s*(#[0-9a-fA-F]+)/) {
$colors{$1} = $2;
}
}
return %colors
}
sub escape_seq {
my ($k, $v) = @_;
my $cmd = "";
if ($k =~ /^color(\d+)$/) {
$cmd = "4;$1;$v";
} elsif ($k =~ /^colorBD$/) {
$cmd = "5;0;$v";
} elsif ($k =~ /^colorUL$/) {
$cmd = "5;1;$v";
} elsif ($k =~ /^colorBL$/) {
$cmd = "5;2;$v";
} elsif ($k =~ /^colorRV$/) {
$cmd = "5;3;$v";
} elsif ($k =~ /^foreground$/) {
$cmd = "10;$v";
} elsif ($k =~ /^background$/) {
$cmd = "11;$v";
} elsif ($k =~ /^cursorColor$/) {
$cmd = "12;$v";
} elsif ($k =~ /^pointerColor$/) {
$cmd = "13;$v";
}
return "\033]".$cmd."\007"
}
sub build_cmd {
my $fn = shift;
my %colors = read_colors($fn);
my $s = join("", map {escape_seq($_, $colors{$_})} keys %colors);
return $s # was implicit anyway
}
sub on_user_command {
my ($self, $cmd) = @_;
my @fs = @{$self->{color_files}};
my $len = @fs;
if ($cmd eq "rotate-colors:forward") {
my $idx = $self->{current_index}++;
my $fn = $fs[$idx % scalar(@fs)];
$self->cmd_parse(build_cmd($fn));
} elsif ($cmd eq "rotate-colors:backward") {
my $idx = $self->{current_index}--;
my $fn = $fs[$idx % scalar(@fs)];
$self->cmd_parse(build_cmd($fn));
}
()
}
Best Answer
Those are used in
xterm
. They define a color palette. Colors 0-7 are the eight "ANSI" colors. For colors 8-15, it is more complicated:boldColors
resource—see manual). It does this as an alternative to using bold fonts because some bitmap fonts are available as bold only by overstriking the font offset by one pixel -- not always readable (this is configurable using theboldFont
resource).The manual also lists the default values for the colors as names. X maps the names to RGB values. Those hex-color codes are one way of representing RGB (red/green/blue) using two hexadecimal digits for each color. You may also see that represented in the format
rbg:rr/gg/bb
.Some programs assume that color0 is the same as the background color and color7 is the same as the foreground (text) color. Actually, xterm maintains those colors with a separate set of default values (the
-fg
and-bg
command-line options for instance).