File Coverage

blib/lib/Calendar/Plugin/Renderer/Text.pm
Criterion Covered Total %
statement 11 86 12.7
branch 0 16 0.0
condition 0 3 0.0
subroutine 4 11 36.3
pod 6 7 85.7
total 21 123 17.0


line stmt bran cond sub pod time code
1             package Calendar::Plugin::Renderer::Text;
2              
3             $Calendar::Plugin::Renderer::Text::VERSION = '0.11';
4             $Calendar::Plugin::Renderer::Text::AUTHORITY = 'cpan:MANWAR';
5              
6             =head1 NAME
7              
8             Calendar::Plugin::Renderer::Text - Interface to render calendar in text format.
9              
10             =head1 VERSION
11              
12             Version 0.11
13              
14             =cut
15              
16 3     3   60 use 5.006;
  3         8  
17 3     3   13 use Data::Dumper;
  3         5  
  3         155  
18              
19 3     3   1605 use Moo;
  3         35608  
  3         17  
20 3     3   5515 use namespace::clean;
  3         30461  
  3         16  
21              
22             has 'start_index' => (is => 'ro', required => 1);
23             has 'month_name' => (is => 'ro', required => 1);
24             has 'days' => (is => 'ro', required => 1);
25             has 'year' => (is => 'ro', required => 1);
26             has 'day_names' => (is => 'ro', required => 1);
27              
28             has 'max_days_name' => (is => 'rw');
29             has 'line_size' => (is => 'rw');
30             has 'f' => (is => 'rw');
31             has 's' => (is => 'rw');
32             has 'month_head' => (is => 'rw');
33              
34             =head1 DESCRIPTION
35              
36             Interface to render calendar in text format.
37              
38             =head1 CONSTRUCTOR
39              
40             It expects one parameter as a hash ref with keys mentioned in the below table.
41              
42             +-------------+-------------------------------------------------------------+
43             | Key | Description |
44             +-------------+-------------------------------------------------------------+
45             | start_index | Index (0-6) for the first day of the month, 0 for Sunday. |
46             | month_name | Name of the given month. |
47             | days | Total days count for the given month. |
48             | year | Given year. |
49             | day_names | Ref to a list of day name starting with Sunday. |
50             +-------------+-------------------------------------------------------------+
51              
52             =cut
53              
54             sub BUILD {
55 0     0 0   my ($self) = @_;
56              
57 0           my $day_names = $self->day_names;
58 0           my $month_name = $self->month_name;
59 0           my $year = $self->year;
60              
61 0           my $max_days_name = 0;
62 0           foreach my $day_name (@$day_names) {
63 0 0         if ($max_days_name < length($day_name)) {
64 0           $max_days_name = length($day_name);
65             }
66             }
67              
68 0           my $line_size = (7 * ($max_days_name + 2)) + 8;
69 0           my $month_head = sprintf("%s [%d BE]", $month_name, $year);
70 0           my $h = int($line_size/2);
71 0           my $m = int(length($month_head)/2);
72 0           my $f = $h - $m;
73 0           my $s = $line_size - ($f + length($month_head));
74              
75 0           $self->max_days_name($max_days_name);
76 0           $self->line_size($line_size);
77 0           $self->month_head($month_head);
78 0           $self->f($f);
79 0           $self->s($s);
80             }
81              
82             =head1 METHODS
83              
84             =head2 get_day_header()
85              
86             =cut
87              
88             sub get_day_header {
89 0     0 1   my ($self) = @_;
90              
91 0           my $max_length_day_name = $self->max_days_name;
92 0           my $day_names = $self->day_names;
93 0           my $line = '|';
94 0           my $i = 1;
95 0           foreach (@$day_names) {
96 0           my $x = length($_);
97 0           my $y = $max_length_day_name - $x;
98 0           my $z = $y + 1;
99 0 0         if ($i == 1) {
100 0           $line .= ((' ')x$z). "$_";
101 0           $i++;
102             }
103             else {
104 0           $line .= " |".((' ')x$z)."$_";
105             }
106              
107             }
108 0           $line .= " |";
109              
110 0           return $line;
111             }
112              
113             =head2 get_month_header()
114              
115             =cut
116              
117             sub get_month_header {
118 0     0 1   my ($self) = @_;
119              
120 0           my $f = $self->f;
121 0           my $s = $self->s;
122 0           my $h = $self->month_head;
123              
124 0           return '|'.
125             (' ')x($f-1).
126             ''.
127             $h.
128             ''.
129             (' ')x($s-1).
130             '|';
131             }
132              
133             =head2 get_dashed_line()
134              
135             =cut
136              
137             sub get_dashed_line {
138 0     0 1   my ($self) = @_;
139              
140 0           my $line_size = $self->line_size;
141 0           return '+'.('-')x($line_size-2).'+';
142             }
143              
144             =head2 get_blocked_line()
145              
146             =cut
147              
148             sub get_blocked_line {
149 0     0 1   my ($self) = @_;
150              
151 0           my $max_length_day_name = $self->max_days_name;
152 0           my $line = '+';
153 0           foreach (1..7) {
154 0           $line .= ('-')x($max_length_day_name+2).'+';
155             }
156              
157 0           $line .= '';
158              
159 0           return $line;
160             }
161              
162             =head2 get_empty_space()
163              
164             =cut
165              
166             sub get_empty_space {
167 0     0 1   my ($self) = @_;
168              
169 0           my $max_length_day_name = $self->max_days_name;
170 0           my $start_index = $self->start_index;
171 0           my $line = '';
172 0 0         if ($start_index % 7 != 0) {
173 0           $line .= '|'.(' ')x($max_length_day_name+2);
174 0           map { $line .= ' 'x($max_length_day_name+3) } (2..($start_index %= 7));
  0            
175             }
176              
177 0           return $line;
178             }
179              
180             =head2 get_dates()
181              
182             =cut
183              
184             sub get_dates {
185 0     0 1   my ($self) = @_;
186              
187 0           my $max_length_day_name = $self->max_days_name;
188 0           my $start_index = $self->start_index;
189 0           my $days = $self->days;
190 0           my $line = '';
191 0           my $blocked_line = $self->get_blocked_line;
192 0           foreach (1 .. $days) {
193 0           $line .= sprintf("|%".($max_length_day_name+1)."s ", $_);
194 0 0         if ($_ != $days) {
    0          
195 0 0         $line .= "|\n".$blocked_line."\n" unless (($start_index + $_) % 7);
196             }
197             elsif ($_ == $days) {
198 0           my $x = 7 - (($start_index + $_) % 7);
199 0 0 0       if (($x >= 2) && ($x != 7)) {
    0          
200 0           $line .= '|'. (' 'x($max_length_day_name+2));
201 0           map { $line .= ' 'x($max_length_day_name+3) } (1..$x-1);
  0            
202             }
203             elsif ($x != 7) {
204 0           $line .= '|'.' 'x($max_length_day_name+2);
205             }
206             }
207             }
208              
209 0           return sprintf("%s|\n%s\n", $line, $blocked_line);
210             }
211              
212              
213             =head1 AUTHOR
214              
215             Mohammad S Anwar, C<< >>
216              
217             =head1 REPOSITORY
218              
219             L
220              
221             =head1 BUGS
222              
223             Please report any bugs / feature requests to C,
224             or through the web interface at L.
225             I will be notified, and then you'll automatically be notified of progress on your
226             bug as I make changes.
227              
228             =head1 SUPPORT
229              
230             You can find documentation for this module with the perldoc command.
231              
232             perldoc Calendar::Plugin::Renderer::Text
233              
234             You can also look for information at:
235              
236             =over 4
237              
238             =item * RT: CPAN's request tracker
239              
240             L
241              
242             =item * AnnoCPAN: Annotated CPAN documentation
243              
244             L
245              
246             =item * CPAN Ratings
247              
248             L
249              
250             =item * Search CPAN
251              
252             L
253              
254             =back
255              
256             =head1 LICENSE AND COPYRIGHT
257              
258             Copyright (C) 2015 - 2016 Mohammad S Anwar.
259              
260             This program is free software; you can redistribute it and / or modify it under
261             the terms of the the Artistic License (2.0). You may obtain a copy of the full
262             license at:
263              
264             L
265              
266             Any use, modification, and distribution of the Standard or Modified Versions is
267             governed by this Artistic License.By using, modifying or distributing the Package,
268             you accept this license. Do not use, modify, or distribute the Package, if you do
269             not accept this license.
270              
271             If your Modified Version has been derived from a Modified Version made by someone
272             other than you,you are nevertheless required to ensure that your Modified Version
273             complies with the requirements of this license.
274              
275             This license does not grant you the right to use any trademark, service mark,
276             tradename, or logo of the Copyright Holder.
277              
278             This license includes the non-exclusive, worldwide, free-of-charge patent license
279             to make, have made, use, offer to sell, sell, import and otherwise transfer the
280             Package with respect to any patent claims licensable by the Copyright Holder that
281             are necessarily infringed by the Package. If you institute patent litigation
282             (including a cross-claim or counterclaim) against any party alleging that the
283             Package constitutes direct or contributory patent infringement,then this Artistic
284             License to you shall terminate on the date that such litigation is filed.
285              
286             Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER AND
287             CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES. THE IMPLIED
288             WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
289             NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY YOUR LOCAL LAW. UNLESS
290             REQUIRED BY LAW, NO COPYRIGHT HOLDER OR CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT,
291             INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE
292             OF THE PACKAGE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
293              
294             =cut
295              
296             1; # End of Calendar::Plugin::Renderer::Text