Patchwork [1,of,5] templatefilters: add short format for age formatting

login
register
mail settings
Submitter David Soria Parra
Date Sept. 17, 2013, 3:55 p.m.
Message ID <4732ba61dd562a85f251.1379433317@achird.localdomain>
Download mbox | patch
Permalink /patch/2506/
State Accepted
Commit f08e542ce9189b4c7611046a957be9983e402577
Headers show

Comments

David Soria Parra - Sept. 17, 2013, 3:55 p.m.
# HG changeset patch
# User David Soria Parra <dsp@experimentalworks.net>
# Date 1379432533 -7200
#      Tue Sep 17 17:42:13 2013 +0200
# Node ID 4732ba61dd562a85f2517a634b67f49ea3229f2e
# Parent  d0317889595e93f1892b279edcdffb2dc14a63c4
templatefilters: add short format for age formatting

Implements a short output format for ages e.g. "1 second ago" is
abbrevated as "1s ago".
Matt Mackall - Sept. 18, 2013, 6 p.m.
On Tue, 2013-09-17 at 17:55 +0200, David Soria Parra wrote:
> # HG changeset patch
> # User David Soria Parra <dsp@experimentalworks.net>
> # Date 1379432533 -7200
> #      Tue Sep 17 17:42:13 2013 +0200
> # Node ID 4732ba61dd562a85f2517a634b67f49ea3229f2e
> # Parent  d0317889595e93f1892b279edcdffb2dc14a63c4
> templatefilters: add short format for age formatting

This one is queued, thanks.

Patch

diff --git a/mercurial/templatefilters.py b/mercurial/templatefilters.py
--- a/mercurial/templatefilters.py
+++ b/mercurial/templatefilters.py
@@ -15,15 +15,15 @@ 
     """
     return text.replace('\n', '<br/>\n')
 
-agescales = [("year", 3600 * 24 * 365),
-             ("month", 3600 * 24 * 30),
-             ("week", 3600 * 24 * 7),
-             ("day", 3600 * 24),
-             ("hour", 3600),
-             ("minute", 60),
-             ("second", 1)]
+agescales = [("year", 3600 * 24 * 365, 'Y'),
+             ("month", 3600 * 24 * 30, 'M'),
+             ("week", 3600 * 24 * 7, 'W'),
+             ("day", 3600 * 24, 'd'),
+             ("hour", 3600, 'h'),
+             ("minute", 60, 'm'),
+             ("second", 1, 's')]
 
-def age(date):
+def age(date, abbrev=False):
     """:age: Date. Returns a human-readable date/time difference between the
     given date/time and the current date/time.
     """
@@ -32,7 +32,9 @@ 
         if c == 1:
             return t
         return t + "s"
-    def fmt(t, c):
+    def fmt(t, c, a):
+        if abbrev:
+            return "%d%s" % (c, a)
         return "%d %s" % (c, plural(t, c))
 
     now = time.time()
@@ -48,12 +50,12 @@ 
         if delta > agescales[0][1] * 2:
             return util.shortdate(date)
 
-    for t, s in agescales:
+    for t, s, a in agescales:
         n = delta // s
         if n >= 2 or s == 1:
             if future:
-                return '%s from now' % fmt(t, n)
-            return '%s ago' % fmt(t, n)
+                return '%s from now' % fmt(t, n, a)
+            return '%s ago' % fmt(t, n, a)
 
 def basename(path):
     """:basename: Any text. Treats the text as a path, and returns the last