CodeText/Theme.pm

filter: Statementstmt 28.5% 32 / 112 Branchbran 0.0% 0 / 40 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 50.0% 8 / 16 Podpod 84.6% 11 / 13 totaltotal 27.9% 52 / 186 CC 27 SCAR 32.0
CC
27
Cov
21%
load
72.0
put
30.0
get
20.0
CRAP 24.6
« XText.pm ↑ Summary CodeText.pm »
1 package Tk::CodeText::Theme;
2  
3 =head1 NAME
4  
5 Tk:CodeText::Theme - Theme object for highlight colors in L<Tk::CodeText>.
6  
7 =cut
8  
9 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 $VERSION = '0.40';
13  
14 1 my %Attributes = (
15 Alert => 1,
16 Annotation => 1,
17 Attribute => 1,
18 BaseN => 1,
19 BuiltIn => 1,
20 Char => 1,
21 Comment => 1,
22 CommentVar => 1,
23 Constant => 1,
24 ControlFlow => 1,
25 DataType => 1,
26 DecVal => 1,
27 Documentation => 1,
28 Error => 1,
29 Extension => 1,
30 Float => 1,
31 Function => 1,
32 Import => 1,
33 Information => 1,
34 Keyword => 1,
35 Normal => 1,
36 Operator => 1,
37 Others => 1,
38 Preprocessor => 1,
39 RegionMarker => 1,
40 SpecialChar => 1,
41 SpecialString => 1,
42 String => 1,
43 Variable => 1,
44 VerbatimString => 1,
45 Warning => 1,
46 );
47  
48 1 my $IdString = "Tk::CodeText theme file";
49  
50 1 my %Options = (
51 -background => 1,
52 -foreground => 1,
53 -slant => 1,
54 -weight => 1,
55 );
56  
57 =head1 SYNOPSIS
58  
59 require Tk::CodeText::Theme;
60 my $theme= new Tk::CodeText::Theme;
61 $theme->load($file);
62 $theme->save($file);
63  
64 =head1 METHODS
65  
66 =over 4
67  
68 =cut
69  
70 1 ▶sub new {
Subroutine new: called CC=2 SCAR=7.1
Pod undocumented
71 1 my $proto = shift;
72 1 ▶ my $class = ref($proto) || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
010
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
73 1 my $self = {
74 POOL => {}
75 };
76 1 bless ($self, $class);
77 1 $self->clear;
78 1 return $self;
79 }
80  
81 =item B<clear>
82  
83 Clears all tag definitions.
84  
85 =cut
86  
87 1 ▶sub clear {
Subroutine clear: called CC=4 SCAR=13.9
Pod documented
88 1 my $self = shift;
89 1 my $pool = $self->Pool;
90 0 for (keys %$pool) { delete $pool->{$_} };
91 1 for ($self->tagList) {
92 31 my $tag = $_;
93 31 my %options = ();
94 31 for ($self->optionList) {
95 124 $options{$_} = ''
96 }
97 31 $pool->{$tag} = \%options
98 }
99 }
100  
101 =item B<getItem>I<($tag, $option)>
102  
103 Returns the value of $option assigned to $tag.
104  
105 =cut
106  
107 0 ▶sub getItem {
Subroutine getItem: not called CC=3 SCAR=24.8
Pod documented
108 0 my ($self, $tag, $option) = @_;
109 0 my $pool = $self->Pool;
110 0 ▶ if ($self->validTag($tag)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->validTag($tag)) { } 0 0 0
111 0 ▶ if ($self->validOption($option)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->validOption($option)) { } 0 0 0
112 0 return $self->Pool->{$tag}->{$option}
113 } else {
114 0 warn "invalid option '$option' in getItem"
115 }
116 } else {
117 0 warn "invalid tag name '$tag' in getItem"
118 }
119 }
120  
121 =item B<get>
122  
123 Returns a list of tag/options pairs.
124  
125 =cut
126  
127 0 ▶sub get {
Subroutine get: not called CC=4 SCAR=30.0
Pod documented
128 0 my $self = shift;
129 0 my $pool = $self->Pool;
130 0 my @result = ();
131 0 for ($self->tagList) {
132 0 my $tag = $_;
133 0 push @result, $tag;
134 0 my @options = ();
135 0 for ($self->optionList) {
136 0 my $val = $pool->{$tag}->{$_};
137 0 ▶ push @options, $_, $val unless $val eq '';
Branch: 1 branch
BranchTrueFalseTotal
unless $val eq "" 0 0 0
138 }
139 0 push @result => \@options
140 }
141 return @result
142 0 }
143  
144 =item B<load>I<($file)>
145  
146 Loads a CodeText theme definition file.
147  
148 =cut
149  
150 0 ▶sub load {
Subroutine load: not called CC=8 SCAR=42.8
Pod documented
151 0 my ($self, $file) = @_;
152 0 ▶ if (open(OFILE, "<", $file)) {
Branch: 1 branch
BranchTrueFalseTotal
if (open OFILE, '<', $file) { } 0 0 0
153 0 my $id = <OFILE>;
154 0 chomp $id;
155 0 ▶ unless ($id eq $IdString) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($id eq $IdString) 0 0 0
156 0 warn "$file is not a $IdString";
157 0 close OFILE;
158 return
159 0 }
160 0 my @values = ();
161 0 my $section;
162 0 my @inf = ();
163 0 ▶ while (<OFILE>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline OFILE)) 0 0 0
164 0 my $line = $_;
165 0 chomp $line;
166 0 ▶ if ($line =~ /^\[([^\]]+)\]/) { #new section
Branch: 2 branches
BranchTrueFalseTotal
if ($line =~ /^\[([^\]]+)\]/) { } 0 0 0
elsif ($line =~ s/^([^=]+)=//) { } 0 0 0
167 0 ▶ push @values, $section, [ @inf ] if defined $section;
Branch: 1 branch
BranchTrueFalseTotal
if defined $section 0 0 0
168 0 $section = $1;
169 0 @inf = ();
170 } elsif ($line =~ s/^([^=]+)=//) {#new key
171 0 push @inf, $1, $line;
172 }
173 }
174 0 ▶ push @values, $section, [ @inf ] if defined $section;
Branch: 1 branch
BranchTrueFalseTotal
if defined $section 0 0 0
175 0 close OFILE;
176 0 $self->put(@values);
177 } else {
178 0 warn "Cannot open '$file'"
179 }
180 }
181  
182 =item B<optionList>
183  
184 Returns a list of available options to use.
185 They are:
186  
187 -background
188 -foreground
189 -slant
190 -weight
191  
192 =cut
193  
194 31 ▶sub optionList {
Subroutine optionList: called CC=1 SCAR=0.0
Pod documented
195 31 return sort keys %Options;
196 }
197  
198 1 ▶sub Pool {
Subroutine Pool: called CC=1 SCAR=0.0
Pod undocumented
199 1 return $_[0]->{POOL};
200 }
201  
202 =item B<put>I<(@list)>
203  
204 Assigns a @list of tag/option pairs.
205  
206 =cut
207  
208 0 ▶sub put {
Subroutine put: not called CC=5 SCAR=34.0
Pod documented
209 0 my $self = shift;
210 0 $self->clear;
211 0 my $pool = $self->Pool;
212 0 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
213 0 my $tag = shift;
214 0 my $opt = shift;
215 0 ▶ next unless $self->validTag($tag);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->validTag($tag) 0 0 0
216 0 my @options = @$opt;
217 0 ▶ while (@options) {
Branch: 1 branch
BranchTrueFalseTotal
if (@options) 0 0 0
218 0 my $key = shift @options;
219 0 my $value = shift @options;
220 0 ▶ $pool->{$tag}->{$key} = $value if $self->validOption($key);
Branch: 1 branch
BranchTrueFalseTotal
if $self->validOption($key) 0 0 0
221 }
222 }
223 }
224  
225 =item B<save>I<($file)>
226  
227 Saves a CodeText theme definition file.
228  
229 =cut
230  
231 0 ▶sub save {
Subroutine save: not called CC=4 SCAR=30.0
Pod documented
232 0 my ($self, $file) = @_;
233 0 ▶ if (open(OFILE, ">", $file)) {
Branch: 1 branch
BranchTrueFalseTotal
if (open OFILE, '>', $file) { } 0 0 0
234 0 print OFILE "$IdString\n";
235 0 my @values = $self->get;
236 0 ▶ while (@values) {
Branch: 1 branch
BranchTrueFalseTotal
if (@values) 0 0 0
237 0 my $tag = shift @values;
238 0 print OFILE "[$tag]\n";
239 0 my $options = shift @values;
240 0 ▶ while (@$options) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$options) 0 0 0
241 0 my $key = shift @$options;
242 0 my $value = shift @$options;
243 0 print OFILE "$key=$value\n";
244 }
245 }
246 0 close OFILE
247 } else {
248 0 warn "Cannot open '$file'"
249 }
250 }
251  
252 =item B<setItem>I<($tag, $option, $value)>
253  
254 Assigns $value to $option in $tag.
255  
256 =cut
257  
258 0 ▶sub setItem {
Subroutine setItem: not called CC=4 SCAR=30.0
Pod documented
259 0 my ($self, $tag, $option, $value) = @_;
260 0 my $pool = $self->Pool;
261 0 ▶ if ($self->validTag($tag)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->validTag($tag)) { } 0 0 0
262 0 ▶ if ($self->validOption($option)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->validOption($option)) { } 0 0 0
263 0 ▶ $self->Pool->{$tag}->{$option} = $value if defined $value
Branch: 1 branch
BranchTrueFalseTotal
if defined $value 0 0 0
264 } else {
265 0 warn "invalid option '$option' in setItem"
266 }
267 } else {
268 0 warn "invalid tag name '$tag' in setItem"
269 }
270 }
271  
272 =item B<tagList>
273  
274 Returns a list of available tags.
275 They are:
276  
277 Alert
278 Annotation
279 Attribute
280 BaseN
281 BuiltIn
282 Char
283 Comment
284 CommentVar
285 Constant
286 ControlFlow
287 DataType
288 DecVal
289 Documentation
290 Error
291 Extension
292 Float
293 Function
294 Import
295 Information
296 Keyword
297 Normal
298 Operator
299 Others
300 Preprocessor
301 RegionMarker
302 SpecialChar
303 SpecialString
304 String
305 Variable
306 VerbatimString
307 Warning
308  
309 =cut
310  
311 1 ▶sub tagList {
Subroutine tagList: called CC=1 SCAR=0.0
Pod documented
312 1 return sort keys %Attributes;
313 }
314  
315 =item B<validOption>I<($option)>
316  
317 Returns true if $option is in the list of available options.
318  
319 =cut
320  
321 0 ▶sub validOption {
Subroutine validOption: not called CC=1 SCAR=6.9
Pod documented
322 0 my ($self, $option) = @_;
323 0 return exists $Options{$option};
324 }
325  
326 =item B<validTag>I<($tag)>
327  
328 Returns true if $tag is in the list of available tags.
329  
330 =cut
331  
332 0 ▶sub validTag {
Subroutine validTag: not called CC=1 SCAR=6.9
Pod documented
333 0 my ($self, $tag) = @_;
334 0 return exists $Attributes{$tag};
335 }
336  
337 =back
338  
339 =head1 AUTHOR
340  
341 Hans Jeuken (hanje at cpan dot org)
342  
343 =head1 BUGS
344  
345 Unknown. If you find any, please contact the author.
346  
347 =head1 SEE ALSO
348  
349 =over 4
350  
351 =item L<Tk::CodeText>
352  
353 =back
354  
355 =cut
356  
357 1 1;
358  
359 __END__
« XText.pm ↑ Summary CodeText.pm »