Fixed Gitalist::Utils::is_binary to work.
[catagits/Gitalist.git] / lib / Gitalist / Utils.pm
CommitLineData
c113db92 1package Gitalist::Utils;
2use strict;
3use warnings;
4use Exporter qw/import/;
5
6our @EXPORT_OK = qw/
7 age_string
8/;
9
10sub age_string {
11 my $age = shift;
12 my $age_str;
13
14 if ( $age > 60 * 60 * 24 * 365 * 2 ) {
15 $age_str = ( int $age / 60 / 60 / 24 / 365 );
16 $age_str .= " years ago";
17 }
18 elsif ( $age > 60 * 60 * 24 * ( 365 / 12 ) * 2 ) {
19 $age_str = int $age / 60 / 60 / 24 / ( 365 / 12 );
20 $age_str .= " months ago";
21 }
22 elsif ( $age > 60 * 60 * 24 * 7 * 2 ) {
23 $age_str = int $age / 60 / 60 / 24 / 7;
24 $age_str .= " weeks ago";
25 }
26 elsif ( $age > 60 * 60 * 24 * 2 ) {
27 $age_str = int $age / 60 / 60 / 24;
28 $age_str .= " days ago";
29 }
30 elsif ( $age > 60 * 60 * 2 ) {
31 $age_str = int $age / 60 / 60;
32 $age_str .= " hours ago";
33 }
34 elsif ( $age > 60 * 2 ) {
35 $age_str = int $age / 60;
36 $age_str .= " min ago";
37 }
38 elsif ( $age > 2 ) {
39 $age_str = int $age;
40 $age_str .= " sec ago";
41 }
42 else {
43 $age_str .= " right now";
44 }
45 return $age_str;
46}
47
53a9d6de 48sub is_binary {
32bc9fdb 49 # Crappy heuristic - does the first line or so look printable?
50 return $_[0] !~ /^[[:print:]]+$ (?: \s ^[[:print:]]+$ )?/mx;
53a9d6de 51}
52
c113db92 531;
54
55__END__
56
57=head1 NAME
58
59Gitalist::Utils - trivial utils for Gitalist
60
61=head2 FUNCTIONS
62
63=head2 age_string
64
53a9d6de 65Turns an integer number of seconds into a string.
66
67=head2 is_binary
68
69Check whether a string is binary according to C<-B>.
c113db92 70
71=head1 AUTHORS
72
73See L<Gitalist> for authors.
74
75=head1 LICENSE
76
77See L<Gitalist> for the license.
78
79=cut