-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcolor.pl
148 lines (140 loc) · 3.94 KB
/
color.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# Copyright 2012 - 2013, Steve Rader
# Copyright 2013 - 2014, Scott Kostyshak
sub parse_report_line {
my ($l,$str) = @_;
&parse_line($l,$str);
if ( $current_command eq 'summary' && $parsed_tokens[0] =~ /^(\d+) project/ ) {
$num_projects = $1;
return;
}
push @{ $report_tokens[$l] }, (@parsed_tokens);
push @{ $report_colors_fg[$l] }, (@parsed_colors_fg);
push @{ $report_colors_bg[$l] }, (@parsed_colors_bg);
push @{ $report_attrs[$l] }, (@parsed_attrs);
}
#------------------------------------------------------------------
sub parse_line {
my ($l,$str) = @_;
my $fg = 999999;
my $bg = 999999;
my $attr = '';
@parsed_tokens = ();
@parsed_colors_fg = ();
@parsed_colors_bg = ();
@parsed_attrs = ();
my @toks = split(/\x1B/,$str);
my $t = 0;
#debug("PARSE IN $str");
for my $tok (@toks) {
if ( $tok eq '' ) { next; }
$attr = '';
CASE: {
# ANSI 16 color attr pairs...
if ( $tok =~ s/\[(\d+);(\d+)m// ) {
my ($a,$b) = ($1,$2);
if ( $a > 30 ) {
$fg = $a - 30;
$bg = $b - 40;
} else {
if ( $a eq '1' ) { $attr .= 'bold '; }
if ( $b eq '4' ) { $attr .= 'underline' ; }
if ( $a eq '7' ) { $attr .= 'inverse '; }
if ( $b < 38 ) {
$fg = $b - 30;
} else {
$bg = $b - 40;
}
}
last CASE;
}
# ANSI 16 color single colors or single attrs or attrs off...
if ( $tok =~ s/\[(\d+)m// ) {
my $a = $1;
if ( $a eq '0' ) {
$fg = $bg = 999999;
$attr .= 'none ';
} elsif ( $a eq '1' ) {
$attr .= 'bold ';
} elsif ( $a eq '4' ) {
$attr .= 'underline ';
} elsif ( $a < 38 ) {
$fg = $a - 30;
$bg = 999999;
} elsif ( $a > 99 ) {
$attr .= 'standout '; # "bright" in taskwarrior
$bg = $a - 100;
$fg = 999999;
} else {
$bg = $a - 40;
$fg = 999999;
}
last CASE;
}
# ANSI 16 color bold...
if ( $tok =~ s/\[1;(\d+);(\d+)m// ) {
my ($a,$b) = ($1,$2);
$attr .= 'bold ';
$fg = $a - 30;
$bg = $b - 40;
last CASE;
}
# ANSI 16 color underline...
if ( $tok =~ s/\[4;(\d+);(\d+)m// ) {
my ($a,$b) = ($1,$2);
$attr .= 'underline ';
$fg = $a - 30;
$bg = $b - 40;
last CASE;
}
# ANSI 16 color inverse...
if ( $tok =~ s/\[7;(\d+);(\d+)m// ) {
my ($a,$b) = ($1,$2);
$attr .= 'inverse ';
$fg = $a - 30;
$bg = $b - 40;
last CASE;
}
# 256 color xterm foreground...
if ( $tok =~ s/\[38;5;(\d+)m// ) {
$fg = $1;
last CASE;
}
# 256 color xterm background...
if ( $tok =~ s/\[48;5;(\d+)m// ) {
$bg = $1;
last CASE;
}
}
# FIXME summary mode...
# if ( $tok =~ /0%\s+100%/ ) { debug("summary graph tok=\"$tok\" column=$t"); }
if ( $tok ne '' ) {
$parsed_tokens[$t] = $tok;
$parsed_colors_fg[$t] = $fg;
$parsed_colors_bg[$t] = $bg;
if ( $attr eq '' ) { $attr = 'none'; }
$parsed_attrs[$t] = $attr;
#if ( $t == 0 ) { debug("PARSE OUT tok=\"$tok\" pos=$l.$t cp=$fg,$bg attr=$attr"); }
$t++;
}
}
}
#------------------------------------------------------------------
sub extract_color {
my ($s,$t) = @_;
$parsed_colors_fg[1] = -1;
$parsed_colors_bg[1] = -1;
$parsed_attrs[1] = '';
&audit("EXEC $task rc._forcecolor=on color $s 2>&1");
open(IN2,"$task rc._forcecolor=on color $s 2>&1 |");
while(<IN2>){
if ( $_ =~ /Your sample:/ ) {
$_ = <IN2>; $_ = <IN2>;
&parse_line(0,$_);
if ( $parsed_colors_fg[1] eq '999999' ) { $parsed_colors_fg[1] = -1; }
if ( $parsed_colors_bg[1] eq '999999' ) { $parsed_colors_bg[1] = -1; }
}
}
close(IN2);
}
#------------------------------------------------------------------
return 1;