| 1 | #!/usr/bin/perl | ||||||||||||||||||||||||||||
| 2 | package Term::ExtendedColor::Xresources; | ||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | BEGIN { | ||||||||||||||||||||||||||||
| 7 | 2 | ▶ | use Exporter; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use vars qw($VERSION @ISA @EXPORT_OK); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 2 | ▶ | $VERSION = '0.080'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 2 | @ISA = qw(Exporter); | |||||||||||||||||||||||||||
| 12 | 2 | @EXPORT_OK = qw( | |||||||||||||||||||||||||||
| 13 | set_xterm_color | ||||||||||||||||||||||||||||
| 14 | get_xterm_color | ||||||||||||||||||||||||||||
| 15 | get_xterm_colors | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | set_background_color | ||||||||||||||||||||||||||||
| 18 | set_foreground_color | ||||||||||||||||||||||||||||
| 19 | ); | ||||||||||||||||||||||||||||
| 20 | } | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 2 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 23 | 2 | ▶ | use Term::ReadKey; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | { | ||||||||||||||||||||||||||||
| 26 | 2 | ▶ | no warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 27 | 2 | *get_xterm_color = *get_xterm_colors; | |||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | sub get_xterm_colors { | ||||||||||||||||||||||||||||
| 31 | 0 | ▶ | my $arg = shift; | ||||||||||||||||||||||||||
|
Subroutine
get_xterm_colors: not called CC=10 SCAR=47.0
Pod
documented
| |||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 0 | ▶ | my $index = (exists($arg->{index})) ? $arg->{index} : [0 .. 255]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 0 | my @indexes; | |||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 0 | ▶ | if(ref($index) eq 'ARRAY') { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 38 | 0 | push(@indexes, @{$index}); | |||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||
| 40 | elsif(ref($index) eq '') { | ||||||||||||||||||||||||||||
| 41 | 0 | push(@indexes, $index); | |||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 0 | ▶ | if( grep { $_ < 0 } @indexes ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 45 | 0 | croak("Index must be a value within 0 .. 255, inclusive\n"); | |||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 0 | ▶ | open(my $tty, '<', '/dev/tty') or croak("Can not open /dev/tty: $!\n"); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 0 | my $colors; | |||||||||||||||||||||||||||
| 51 | 0 | for my $i(@indexes) { | |||||||||||||||||||||||||||
| 52 | 0 | ▶ | next if not defined $i; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 0 | ReadMode('raw', $tty); | |||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 0 | print "\e]4;$i;?\a"; # the '?' indicates a query | |||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 0 | my $response = ''; | |||||||||||||||||||||||||||
| 59 | 0 | my ($r, $g, $b); | |||||||||||||||||||||||||||
| 60 | 0 | while(1) { | |||||||||||||||||||||||||||
| 61 | 0 | ▶ | if ($response =~ m[ rgb: (.{4}) / (.{4}) / (.{4}) ]x) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 62 | 0 | ($r, $g, $b) = map { substr $_, 0, 2 } ($1, $2, $3); | |||||||||||||||||||||||||||
| 63 | 0 | last; | |||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | else { | ||||||||||||||||||||||||||||
| 66 | 0 | $response .= ReadKey(0, $tty); | |||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 0 | ReadMode('normal'); | |||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 0 | $colors->{$i}->{raw} = $response; | |||||||||||||||||||||||||||
| 73 | # Return in base 10 by default | ||||||||||||||||||||||||||||
| 74 | 0 | ▶ | if($arg->{type} eq 'hex') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | 0 | $colors->{$i}->{red} = $r; # ff | |||||||||||||||||||||||||||
| 76 | 0 | $colors->{$i}->{green} = $g; | |||||||||||||||||||||||||||
| 77 | 0 | $colors->{$i}->{blue} = $b; | |||||||||||||||||||||||||||
| 78 | 0 | $colors->{$i}->{rgb} = "$r$g$b"; | |||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | else { | ||||||||||||||||||||||||||||
| 81 | 0 | ($r, $g, $b) = (hex($r), hex($g), hex($b)); | |||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 0 | $colors->{$i}->{red} = $r; # 255 | |||||||||||||||||||||||||||
| 84 | 0 | $colors->{$i}->{green} = $g; | |||||||||||||||||||||||||||
| 85 | 0 | $colors->{$i}->{blue} = $b; | |||||||||||||||||||||||||||
| 86 | 0 | $colors->{$i}->{rgb} = "$r/$g/$b"; # 255/255/0 | |||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 0 | $colors->{$i}{r} = $colors->{$i}{red}; | |||||||||||||||||||||||||||
| 90 | 0 | $colors->{$i}{g} = $colors->{$i}{green}; | |||||||||||||||||||||||||||
| 91 | 0 | $colors->{$i}{b} = $colors->{$i}{blue}; | |||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | 0 | return $colors; | |||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | sub set_xterm_color { | ||||||||||||||||||||||||||||
| 97 | # color => index | ||||||||||||||||||||||||||||
| 98 | 0 | ▶ | my $old_colors = shift; | ||||||||||||||||||||||||||
|
Subroutine
set_xterm_color: not called CC=9 SCAR=45.0
Pod
documented
| |||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 0 | ▶ | if(ref($old_colors) ne 'HASH') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 101 | 0 | croak("Hash reference expected"); | |||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 0 | my %new_colors; | |||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 0 | for my $index(keys(%{ $old_colors })) { | |||||||||||||||||||||||||||
| 107 | 0 | ▶ | if( ($index < 0) or ($index > 255) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$index < 0 or $index > 255
MC/DC0%0/2
$index < 0 or $index > 255
$index < 0
$index > 255
Truth table0%0/3
A or B
A= $index < 0B= $index > 255
| |||||||||||||||||||||||||||||
| 108 | 0 | next; | |||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | 0 | ▶ | if($old_colors->{$index} !~ /^[A-Fa-f0-9]{6}$/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 111 | 0 | next; | |||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 0 | my($r, $g, $b) = $old_colors->{$index} =~ m/(..)/g; | |||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 0 | my $term = $ENV{TERM}; | |||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | # This is for GNU Screen and Tmux | ||||||||||||||||||||||||||||
| 120 | # Tmux identifies itself as GNU Screen | ||||||||||||||||||||||||||||
| 121 | # "\eP\e]4;198;rgb:50/20/40\a\e\\" | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | # echo -e "\033]12;green\007" should change the cursor color, but inside of | ||||||||||||||||||||||||||||
| 124 | # screen, you have to wrap all that with "\eP" and "\a\e\\" | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | 0 | ▶ | if($term =~ m/^screen/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 127 | 0 | $new_colors{ $index } = "\eP\e]4;$index;rgb:$r/$g/$b\a\e\\"; | |||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||
| 129 | else { | ||||||||||||||||||||||||||||
| 130 | 0 | $new_colors{ $index } = "\e]4;$index;rgb:$r/$g/$b\e\\" | |||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | 0 | ▶ | if(!defined( wantarray() )) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 134 | 0 | print for values %new_colors; | |||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | else { | ||||||||||||||||||||||||||||
| 137 | 0 | return \%new_colors; | |||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | sub set_background_color { | ||||||||||||||||||||||||||||
| 142 | 0 | ▶ | my $color = shift; | ||||||||||||||||||||||||||
|
Subroutine
set_background_color: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 143 | 0 | $color =~ s/^#//g; | |||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | 0 | my $esc = "\e]11;#$color\007"; | |||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | 0 | ▶ | if(!defined( wantarray() )) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 148 | 0 | print $esc; | |||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||
| 150 | else { | ||||||||||||||||||||||||||||
| 151 | 0 | return $esc; | |||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | sub set_foreground_color { | ||||||||||||||||||||||||||||
| 156 | 0 | ▶ | my $color = shift; | ||||||||||||||||||||||||||
|
Subroutine
set_foreground_color: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 157 | 0 | $color =~ s/^#//g; | |||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | 0 | my $esc = "\e]10;#$color\007"; | |||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | 0 | ▶ | if(!defined( wantarray() )) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 162 | 0 | print $esc; | |||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||
| 164 | else { | ||||||||||||||||||||||||||||
| 165 | 0 | return $esc; | |||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | 2 | 1; | |||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | __END__ | ||||||||||||||||||||||||||||