]> ruderich.org/simon Gitweb - config/dotfiles.git/commitdiff
term2gui.pl: Add, convert 256-color terminal color scheme to GUI.
authorSimon Ruderich <simon@ruderich.org>
Thu, 15 Nov 2012 16:51:40 +0000 (17:51 +0100)
committerSimon Ruderich <simon@ruderich.org>
Thu, 15 Nov 2012 16:51:40 +0000 (17:51 +0100)
term2gui.pl [new file with mode: 0755]

diff --git a/term2gui.pl b/term2gui.pl
new file mode 100755 (executable)
index 0000000..c019326
--- /dev/null
@@ -0,0 +1,313 @@
+#!/usr/bin/perl
+
+# Convert a terminal color scheme for 256 color terminals to a GUI color
+# scheme by adding the appropriate gui* options.
+
+# Copyright (C) 2012  Simon Ruderich
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+
+use strict;
+use warnings;
+
+
+sub xterm2rgb {
+    my ($color) = @_;
+
+    # Taken from the urxvt-8.2-256color.patch in Debian's urxvt-unicode
+    # package 9.07-2.
+    my @colors = (
+        'rgb:00/00/00',
+        'rgb:00/00/5f',
+        'rgb:00/00/87',
+        'rgb:00/00/af',
+        'rgb:00/00/d7',
+        'rgb:00/00/ff',
+        'rgb:00/5f/00',
+        'rgb:00/5f/5f',
+        'rgb:00/5f/87',
+        'rgb:00/5f/af',
+        'rgb:00/5f/d7',
+        'rgb:00/5f/ff',
+        'rgb:00/87/00',
+        'rgb:00/87/5f',
+        'rgb:00/87/87',
+        'rgb:00/87/af',
+        'rgb:00/87/d7',
+        'rgb:00/87/ff',
+        'rgb:00/af/00',
+        'rgb:00/af/5f',
+        'rgb:00/af/87',
+        'rgb:00/af/af',
+        'rgb:00/af/d7',
+        'rgb:00/af/ff',
+        'rgb:00/d7/00',
+        'rgb:00/d7/5f',
+        'rgb:00/d7/87',
+        'rgb:00/d7/af',
+        'rgb:00/d7/d7',
+        'rgb:00/d7/ff',
+        'rgb:00/ff/00',
+        'rgb:00/ff/5f',
+        'rgb:00/ff/87',
+        'rgb:00/ff/af',
+        'rgb:00/ff/d7',
+        'rgb:00/ff/ff',
+        'rgb:5f/00/00',
+        'rgb:5f/00/5f',
+        'rgb:5f/00/87',
+        'rgb:5f/00/af',
+        'rgb:5f/00/d7',
+        'rgb:5f/00/ff',
+        'rgb:5f/5f/00',
+        'rgb:5f/5f/5f',
+        'rgb:5f/5f/87',
+        'rgb:5f/5f/af',
+        'rgb:5f/5f/d7',
+        'rgb:5f/5f/ff',
+        'rgb:5f/87/00',
+        'rgb:5f/87/5f',
+        'rgb:5f/87/87',
+        'rgb:5f/87/af',
+        'rgb:5f/87/d7',
+        'rgb:5f/87/ff',
+        'rgb:5f/af/00',
+        'rgb:5f/af/5f',
+        'rgb:5f/af/87',
+        'rgb:5f/af/af',
+        'rgb:5f/af/d7',
+        'rgb:5f/af/ff',
+        'rgb:5f/d7/00',
+        'rgb:5f/d7/5f',
+        'rgb:5f/d7/87',
+        'rgb:5f/d7/af',
+        'rgb:5f/d7/d7',
+        'rgb:5f/d7/ff',
+        'rgb:5f/ff/00',
+        'rgb:5f/ff/5f',
+        'rgb:5f/ff/87',
+        'rgb:5f/ff/af',
+        'rgb:5f/ff/d7',
+        'rgb:5f/ff/ff',
+        'rgb:87/00/00',
+        'rgb:87/00/5f',
+        'rgb:87/00/87',
+        'rgb:87/00/af',
+        'rgb:87/00/d7',
+        'rgb:87/00/ff',
+        'rgb:87/5f/00',
+        'rgb:87/5f/5f',
+        'rgb:87/5f/87',
+        'rgb:87/5f/af',
+        'rgb:87/5f/d7',
+        'rgb:87/5f/ff',
+        'rgb:87/87/00',
+        'rgb:87/87/5f',
+        'rgb:87/87/87',
+        'rgb:87/87/af',
+        'rgb:87/87/d7',
+        'rgb:87/87/ff',
+        'rgb:87/af/00',
+        'rgb:87/af/5f',
+        'rgb:87/af/87',
+        'rgb:87/af/af',
+        'rgb:87/af/d7',
+        'rgb:87/af/ff',
+        'rgb:87/d7/00',
+        'rgb:87/d7/5f',
+        'rgb:87/d7/87',
+        'rgb:87/d7/af',
+        'rgb:87/d7/d7',
+        'rgb:87/d7/ff',
+        'rgb:87/ff/00',
+        'rgb:87/ff/5f',
+        'rgb:87/ff/87',
+        'rgb:87/ff/af',
+        'rgb:87/ff/d7',
+        'rgb:87/ff/ff',
+        'rgb:af/00/00',
+        'rgb:af/00/5f',
+        'rgb:af/00/87',
+        'rgb:af/00/af',
+        'rgb:af/00/d7',
+        'rgb:af/00/ff',
+        'rgb:af/5f/00',
+        'rgb:af/5f/5f',
+        'rgb:af/5f/87',
+        'rgb:af/5f/af',
+        'rgb:af/5f/d7',
+        'rgb:af/5f/ff',
+        'rgb:af/87/00',
+        'rgb:af/87/5f',
+        'rgb:af/87/87',
+        'rgb:af/87/af',
+        'rgb:af/87/d7',
+        'rgb:af/87/ff',
+        'rgb:af/af/00',
+        'rgb:af/af/5f',
+        'rgb:af/af/87',
+        'rgb:af/af/af',
+        'rgb:af/af/d7',
+        'rgb:af/af/ff',
+        'rgb:af/d7/00',
+        'rgb:af/d7/5f',
+        'rgb:af/d7/87',
+        'rgb:af/d7/af',
+        'rgb:af/d7/d7',
+        'rgb:af/d7/ff',
+        'rgb:af/ff/00',
+        'rgb:af/ff/5f',
+        'rgb:af/ff/87',
+        'rgb:af/ff/af',
+        'rgb:af/ff/d7',
+        'rgb:af/ff/ff',
+        'rgb:d7/00/00',
+        'rgb:d7/00/5f',
+        'rgb:d7/00/87',
+        'rgb:d7/00/af',
+        'rgb:d7/00/d7',
+        'rgb:d7/00/ff',
+        'rgb:d7/5f/00',
+        'rgb:d7/5f/5f',
+        'rgb:d7/5f/87',
+        'rgb:d7/5f/af',
+        'rgb:d7/5f/d7',
+        'rgb:d7/5f/ff',
+        'rgb:d7/87/00',
+        'rgb:d7/87/5f',
+        'rgb:d7/87/87',
+        'rgb:d7/87/af',
+        'rgb:d7/87/d7',
+        'rgb:d7/87/ff',
+        'rgb:d7/af/00',
+        'rgb:d7/af/5f',
+        'rgb:d7/af/87',
+        'rgb:d7/af/af',
+        'rgb:d7/af/d7',
+        'rgb:d7/af/ff',
+        'rgb:d7/d7/00',
+        'rgb:d7/d7/5f',
+        'rgb:d7/d7/87',
+        'rgb:d7/d7/af',
+        'rgb:d7/d7/d7',
+        'rgb:d7/d7/ff',
+        'rgb:d7/ff/00',
+        'rgb:d7/ff/5f',
+        'rgb:d7/ff/87',
+        'rgb:d7/ff/af',
+        'rgb:d7/ff/d7',
+        'rgb:d7/ff/ff',
+        'rgb:ff/00/00',
+        'rgb:ff/00/5f',
+        'rgb:ff/00/87',
+        'rgb:ff/00/af',
+        'rgb:ff/00/d7',
+        'rgb:ff/00/ff',
+        'rgb:ff/5f/00',
+        'rgb:ff/5f/5f',
+        'rgb:ff/5f/87',
+        'rgb:ff/5f/af',
+        'rgb:ff/5f/d7',
+        'rgb:ff/5f/ff',
+        'rgb:ff/87/00',
+        'rgb:ff/87/5f',
+        'rgb:ff/87/87',
+        'rgb:ff/87/af',
+        'rgb:ff/87/d7',
+        'rgb:ff/87/ff',
+        'rgb:ff/af/00',
+        'rgb:ff/af/5f',
+        'rgb:ff/af/87',
+        'rgb:ff/af/af',
+        'rgb:ff/af/d7',
+        'rgb:ff/af/ff',
+        'rgb:ff/d7/00',
+        'rgb:ff/d7/5f',
+        'rgb:ff/d7/87',
+        'rgb:ff/d7/af',
+        'rgb:ff/d7/d7',
+        'rgb:ff/d7/ff',
+        'rgb:ff/ff/00',
+        'rgb:ff/ff/5f',
+        'rgb:ff/ff/87',
+        'rgb:ff/ff/af',
+        'rgb:ff/ff/d7',
+        'rgb:ff/ff/ff',
+        'rgb:08/08/08',
+        'rgb:12/12/12',
+        'rgb:1c/1c/1c',
+        'rgb:26/26/26',
+        'rgb:30/30/30',
+        'rgb:3a/3a/3a',
+        'rgb:44/44/44',
+        'rgb:4e/4e/4e',
+        'rgb:58/58/58',
+        'rgb:62/62/62',
+        'rgb:6c/6c/6c',
+        'rgb:76/76/76',
+        'rgb:80/80/80',
+        'rgb:8a/8a/8a',
+        'rgb:94/94/94',
+        'rgb:9e/9e/9e',
+        'rgb:a8/a8/a8',
+        'rgb:b2/b2/b2',
+        'rgb:bc/bc/bc',
+        'rgb:c6/c6/c6',
+        'rgb:d0/d0/d0',
+        'rgb:da/da/da',
+        'rgb:e4/e4/e4',
+        'rgb:ee/ee/ee',
+    );
+
+    return if $color < 16 or $color > 255; # undef
+
+    my $value = $colors[$color - 16];
+    $value =~ s{^rgb:}{};
+    $value =~ s{/}{}g;
+
+    return '#' . $value;
+}
+
+
+while (my $line = <>) {
+    # Only handle lines with highlight commands.
+    if ($line =~ /^\s*\bhi(?:ghlight)?\b/) {
+        foreach my $setting ($line =~ m/\bcterm(?:fg|bg)?=\S+/g) {
+            my ($name, $value) = split /=/, $setting;
+
+            my $gui_name = $name;
+            $gui_name =~ s/cterm/gui/;
+
+            my $new_value;
+            if ($name eq 'cterm') {
+                $new_value = $value;
+            } elsif ($value eq 'NONE') {
+                $new_value = 'NONE';
+            } else {
+                $new_value = xterm2rgb($value);
+            }
+
+            # Update existing value.
+            if ($line =~ /\b\Q$gui_name\E=/) {
+                $line =~ s/\Q$gui_name\E=\S+/$gui_name=$new_value/;
+            # Append at the end of the line.
+            } else {
+                $line =~ s/\n/ $gui_name=$new_value\n/;
+            }
+        }
+    }
+
+    print $line;
+}