summaryrefslogtreecommitdiff
path: root/lib/JWebmail/View/Webmail.pm
diff options
context:
space:
mode:
Diffstat (limited to 'lib/JWebmail/View/Webmail.pm')
-rw-r--r--lib/JWebmail/View/Webmail.pm88
1 files changed, 88 insertions, 0 deletions
diff --git a/lib/JWebmail/View/Webmail.pm b/lib/JWebmail/View/Webmail.pm
new file mode 100644
index 0000000..464c97e
--- /dev/null
+++ b/lib/JWebmail/View/Webmail.pm
@@ -0,0 +1,88 @@
+package JWebmail::View::Webmail;
+
+use Mojo::Base -base;
+
+use POSIX qw(floor round log);
+
+
+### template formatting functions
+
+sub print_sizes10 {
+ shift;
+ my $var = shift || return '0 Byte';
+
+ my $i = floor(((log($var)/log(10))+1e-5) / 3);
+ my $expo = $i * 3;
+
+ my @PREFIX;
+ $PREFIX[0] = 'Byte';
+ $PREFIX[1] = 'kByte';
+ $PREFIX[2] = 'MByte';
+ $PREFIX[3] = 'GByte';
+ $PREFIX[4] = 'TByte';
+ $PREFIX[5] = 'PByte';
+
+ return sprintf('%.0f %s', $var / (10**$expo), $PREFIX[$i]);
+}
+
+sub print_sizes2 {
+ shift;
+ my $var = shift || return '0 Byte';
+
+ my $i = floor(((log($var)/log(2))+1e-5) / 10);
+ my $expo = $i * 10;
+ my %PREFIX = (
+ 0 => 'Byte',
+ 1 => 'KiByte',
+ 2 => 'MiByte',
+ 3 => 'GiByte',
+ 4 => 'TiByte',
+ 5 => 'PiByte',
+ );
+ my $pref = $PREFIX{$i};
+ return round($var / (2**$expo)) . " $pref";
+}
+
+my sub dgt { "([[:digit:]]{$_[0]})" }
+
+sub parse_iso_date {
+ shift;
+ state $rx = do { my $re = dgt(4).'-'.dgt(2).'-'.dgt(2).'T'.dgt(2).':'.dgt(2).':'.dgt(2); qr/$re/a };
+ my @d = shift =~ /$rx/;
+ if (@d != 6) {
+ # TODO
+ warn "issue when parsing date";
+ }
+ return {
+ year => $d[0],
+ month => $d[1],
+ mday => $d[2],
+ hour => $d[3],
+ min => $d[4],
+ sec => $d[5],
+ };
+}
+
+1
+
+__END__
+
+=head1 VIEW METHODS
+
+=head2 print_sizes10
+
+A helper for templates used to format byte sizes.
+
+ $app->helper(print_sizes10 => sub { shift; JWebmail::Plugin::Helper::print_sizes10(@_) });
+
+ %= print_sizes10 12345 # => 12 kB
+
+=head2 print_sizes2
+
+A helper for templates used to format byte sizes.
+
+ %= print_sizes10 12345 # => 12 KiB
+
+This is not registered by default.
+
+=head2 parse_iso_date