| 1 | package Smolder::Util; | ||
| 2 | use Smolder::Conf qw(HostName Port); | ||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | =head1 NAME | ||
| 7 | |||
| 8 | Smolder::Util | ||
| 9 | |||
| 10 | =head1 DESCRIPTION | ||
| 11 | |||
| 12 | Collection of various useful routines for Smolder | ||
| 13 | |||
| 14 | =head1 ROUTINES | ||
| 15 | |||
| 16 | =head2 pass_fail_color | ||
| 17 | |||
| 18 | Given a ratio, will return an RRGGBB color suitable for a web page | ||
| 19 | as a visual indicator of a test's success. Green == 100%, Red == 0%. | ||
| 20 | |||
| 21 | =cut | ||
| 22 | |||
| 23 | # adapted from Test::TAP::Model::Visual | ||
| 24 | sub pass_fail_color { | ||
| 25 | my $ratio = shift; | ||
| 26 | |||
| 27 | # handle percentages | ||
| 28 | $ratio = $ratio / 100 if $ratio > 1; | ||
| 29 | my @start_color = ('255', '00', '00'); # ff0000 | ||
| 30 | my @end_color = ('00', '255', '00'); # 00ff00 | ||
| 31 | my @color_pieces; | ||
| 32 | |||
| 33 | if ($ratio == 1) { | ||
| 34 | @color_pieces = @end_color; | ||
| 35 | } elsif ($ratio == 0) { | ||
| 36 | @color_pieces = @start_color; | ||
| 37 | } else { | ||
| 38 | @color_pieces = ('255', 100 + ((255 - 100) * $ratio), 100 - 20,); | ||
| 39 | } | ||
| 40 | return sprintf('%02x%02x%02x', @color_pieces); | ||
| 41 | } | ||
| 42 | |||
| 43 | =head2 format_time | ||
| 44 | |||
| 45 | Given a number of seconds, format it as string of the format | ||
| 46 | C<HH:MM:SS> or C<MM:SS> or C<SS>. | ||
| 47 | |||
| 48 | Smolder::Util::format_time(1000); # prints 16:40 | ||
| 49 | Smolder::Util::format_time(35); # prints 35 | ||
| 50 | Smolder::Util::format_time(100); # prints 1:40 | ||
| 51 | |||
| 52 | =cut | ||
| 53 | |||
| 54 | sub format_time { | ||
| 55 | my $secs = shift; | ||
| 56 | return $secs if $secs < 60; | ||
| 57 | my $hour = int($secs / 3600); | ||
| 58 | my $min = int(($secs % 3600) / 60); | ||
| 59 | my $sec = $secs % 60; | ||
| 60 | if ($hour) { | ||
| 61 | return sprintf('%i:%02i:%02i', $hour, $min, $sec); | ||
| 62 | } elsif ($min) { | ||
| 63 | return sprintf('%i:%02i', $min, $sec); | ||
| 64 | } | ||
| 65 | } | ||
| 66 | |||
| 67 | =head2 url_base | ||
| 68 | |||
| 69 | This method will return the base url for the installed version of | ||
| 70 | Smolder. | ||
| 71 | |||
| 72 | =cut | ||
| 73 | |||
| 74 | { | ||
| 75 | my $_base = 'http://' . HostName . (Port == 80 ? '' : ':' . Port); | ||
| 76 | sub url_base { $_base } | ||
| 77 | } | ||
| 78 | |||
| 79 | 1; | ||
| 80 |