+contractLeftTo:maxLen
authorClaus Gittinger <cg@exept.de>
Mon, 09 Jun 2008 19:25:17 +0200
changeset 11049 2ee7b3d263c3
parent 11048 feed33f1f796
child 11050 226b25e4bb97
+contractLeftTo:maxLen
CharacterArray.st
--- a/CharacterArray.st	Thu Jun 05 12:17:59 2008 +0200
+++ b/CharacterArray.st	Mon Jun 09 19:25:17 2008 +0200
@@ -4003,6 +4003,37 @@
     "Modified: / 24-10-2006 / 12:32:26 / cg"
 !
 
+contractLeftTo:maxLen
+    "if the receivers size is less or equal to maxLen, return it.
+     Otherwise, return a copy of the receiver, where some characters
+     near the first quarter have been replaced by '...' for a total string length
+     of maxLen. 
+     Very similar to contractTo:, but better to abbreviate long filename entries, 
+     where the right part is of more use than the left."
+
+    |sz "{ SmallInteger }"
+     halfSize quarterSize "{ SmallInteger }"
+     leftEnd rightEnd rightStart|
+
+    (sz := self size) <= maxLen ifTrue:[ ^ self ].
+
+    halfSize := maxLen // 2.
+    quarterSize := maxLen // 4.
+    leftEnd := quarterSize-1.
+    rightEnd := maxLen - leftEnd - 3.
+    rightStart := sz - rightEnd + 1.
+    ^ (self copyTo:leftEnd),'...',(self copyFrom:rightStart)
+
+    "
+     '12345678901234' contractLeftTo:15  
+     '123456789012345' contractLeftTo:15     
+     '1234567890123456' contractLeftTo:15   
+     'aShortString' contractLeftTo:15             
+     'aVeryLongNameForAStringThatShouldBeShortened' contractLeftTo:15  
+     'C:\Dokumente und Einstellungen\cg\work\bosch\dapas\hw_schnittstellen\DAPAS__HpibDLL.st' contractLeftTo:40
+    "
+!
+
 contractTo:maxLen
     "if the receivers size is less or equal to maxLen, return it.
      Otherwise, return a copy of the receiver, where some characters
@@ -5455,7 +5486,7 @@
 !CharacterArray class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libbasic/CharacterArray.st,v 1.375 2008-05-28 09:41:59 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic/CharacterArray.st,v 1.376 2008-06-09 17:25:17 cg Exp $'
 ! !
 
 CharacterArray initialize!