Patchwork [06,of,11,V2] encoding: add 'leftside' argument into 'trim' to switch trimming side

login
register
mail settings
Submitter Katsunori FUJIWARA
Date July 5, 2014, 6 p.m.
Message ID <280066d24ef9a639b0d9.1404583205@feefifofum>
Download mbox | patch
Permalink /patch/5110/
State Accepted
Commit b515c3a63e964ec21e39934d2d1c4dd12fabf38b
Headers show

Comments

Katsunori FUJIWARA - July 5, 2014, 6 p.m.
# HG changeset patch
# User FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
# Date 1404583001 -32400
#      Sun Jul 06 02:56:41 2014 +0900
# Node ID 280066d24ef9a639b0d93a92f291f853b43bb4da
# Parent  779dd32974bd625647980c0c8049c32add7ba29b
encoding: add 'leftside' argument into 'trim' to switch trimming side

Patch

diff --git a/mercurial/encoding.py b/mercurial/encoding.py
--- a/mercurial/encoding.py
+++ b/mercurial/encoding.py
@@ -165,9 +165,12 @@ 
         if colwidth(t) == c:
             return t
 
-def trim(s, width, ellipsis=''):
+def trim(s, width, ellipsis='', leftside=False):
     """Trim string 's' to at most 'width' columns (including 'ellipsis').
 
+    If 'leftside' is True, left side of string 's' is trimmed.
+    'ellipsis' is always placed at trimmed side.
+
     >>> ellipsis = '+++'
     >>> from mercurial import encoding
     >>> encoding.encoding = 'utf-8'
@@ -178,8 +181,12 @@ 
     1234567890
     >>> print trim(t, 8, ellipsis=ellipsis)
     12345+++
+    >>> print trim(t, 8, ellipsis=ellipsis, leftside=True)
+    +++67890
     >>> print trim(t, 8)
     12345678
+    >>> print trim(t, 8, leftside=True)
+    34567890
     >>> print trim(t, 3, ellipsis=ellipsis)
     +++
     >>> print trim(t, 1, ellipsis=ellipsis)
@@ -192,10 +199,16 @@ 
     \xe3\x81\x82\xe3\x81\x84\xe3\x81\x86\xe3\x81\x88\xe3\x81\x8a
     >>> print trim(t, 8, ellipsis=ellipsis)
     \xe3\x81\x82\xe3\x81\x84+++
+    >>> print trim(t, 8, ellipsis=ellipsis, leftside=True)
+    +++\xe3\x81\x88\xe3\x81\x8a
     >>> print trim(t, 5)
     \xe3\x81\x82\xe3\x81\x84
+    >>> print trim(t, 5, leftside=True)
+    \xe3\x81\x88\xe3\x81\x8a
     >>> print trim(t, 4, ellipsis=ellipsis)
     +++
+    >>> print trim(t, 4, ellipsis=ellipsis, leftside=True)
+    +++
     >>> t = '\x11\x22\x33\x44\x55\x66\x77\x88\x99\xaa' # invalid byte sequence
     >>> print trim(t, 12, ellipsis=ellipsis)
     \x11\x22\x33\x44\x55\x66\x77\x88\x99\xaa
@@ -203,8 +216,12 @@ 
     \x11\x22\x33\x44\x55\x66\x77\x88\x99\xaa
     >>> print trim(t, 8, ellipsis=ellipsis)
     \x11\x22\x33\x44\x55+++
+    >>> print trim(t, 8, ellipsis=ellipsis, leftside=True)
+    +++\x66\x77\x88\x99\xaa
     >>> print trim(t, 8)
     \x11\x22\x33\x44\x55\x66\x77\x88
+    >>> print trim(t, 8, leftside=True)
+    \x33\x44\x55\x66\x77\x88\x99\xaa
     >>> print trim(t, 3, ellipsis=ellipsis)
     +++
     >>> print trim(t, 1, ellipsis=ellipsis)
@@ -218,6 +235,8 @@ 
         width -= len(ellipsis)
         if width <= 0: # no enough room even for ellipsis
             return ellipsis[:width + len(ellipsis)]
+        if leftside:
+            return ellipsis + s[-width:]
         return s[:width] + ellipsis
 
     if ucolwidth(u) <= width: # trimming is not needed
@@ -227,8 +246,12 @@ 
     if width <= 0: # no enough room even for ellipsis
         return ellipsis[:width + len(ellipsis)]
 
-    uslice = lambda i: u[:-i]
-    concat = lambda s: s + ellipsis
+    if leftside:
+        uslice = lambda i: u[i:]
+        concat = lambda s: ellipsis + s
+    else:
+        uslice = lambda i: u[:-i]
+        concat = lambda s: s + ellipsis
     for i in xrange(1, len(u)):
         usub = uslice(i)
         if ucolwidth(usub) <= width: