f11dc3402935b752f98cc524f50f6098204036d6
[catagits/Gitalist.git] / lib / Gitalist / Utils.pm
1 package Gitalist::Utils;
2 use strict;
3 use warnings;
4 use Exporter qw/import/;
5
6 our @EXPORT_OK = qw/
7     age_string
8 /;
9
10 sub 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
48 sub is_binary {
49   # Crappy heuristic - does the first line or so look printable?
50   return $_[0] !~ /^[[:print:]]+$ (?: \s ^[[:print:]]+$ )?/mx;
51 }
52
53 1;
54
55 __END__
56
57 =head1 NAME
58
59 Gitalist::Utils - trivial utils for Gitalist
60
61 =head2 FUNCTIONS
62
63 =head2 age_string
64
65 Turns an integer number of seconds into a string.
66
67 =head2 is_binary
68
69 Check whether a string is binary according to C<-B>.
70
71 =head1 AUTHORS
72
73 See L<Gitalist> for authors.
74
75 =head1 LICENSE
76
77 See L<Gitalist> for the license.
78
79 =cut