Patchwork [6,of,8,STABLE] encoding: add 'headside' argument into 'trim' to switch trimming side

login
register
mail settings
Submitter Katsunori FUJIWARA
Date June 13, 2014, 4:22 p.m.
Message ID <0d4c198fa1eac9824eb6.1402676554@feefifofum>
Download mbox | patch
Permalink /patch/4992/
State Superseded
Headers show

Comments

Katsunori FUJIWARA - June 13, 2014, 4:22 p.m.
# HG changeset patch
# User FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
# Date 1402675901 -32400
#      Sat Jun 14 01:11:41 2014 +0900
# Branch stable
# Node ID 0d4c198fa1eac9824eb691c7e89d59ef08b36720
# Parent  409d0eb0fdcffabd1526d46a3c907d317b30fd5d
encoding: add 'headside' 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='', headside=False):
     """Trim string 's' to at most 'width' columns (including 'ellipsis').
 
+    If 'headside' is True, string 's' is trimmed at head
+    side. '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, headside=True)
+    +++67890
     >>> print trim(t, 8)
     12345678
+    >>> print trim(t, 8, headside=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, headside=True)
+    +++\xe3\x81\x88\xe3\x81\x8a
     >>> print trim(t, 5)
     \xe3\x81\x82\xe3\x81\x84
+    >>> print trim(t, 5, headside=True)
+    \xe3\x81\x88\xe3\x81\x8a
     >>> print trim(t, 4, ellipsis=ellipsis)
     +++
+    >>> print trim(t, 4, ellipsis=ellipsis, headside=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, headside=True)
+    +++\x66\x77\x88\x99\xaa
     >>> print trim(t, 8)
     \x11\x22\x33\x44\x55\x66\x77\x88
+    >>> print trim(t, 8, headside=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 headside:
+            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 headside:
+        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: