Permalink
Browse files

Add AlignChop format.

  • Loading branch information...
spl committed Jun 26, 2009
1 parent c1c54c5 commit 84f56c4dfd3995b3838c2c883eb70591215a2f77
Showing with 22 additions and 8 deletions.
  1. +19 −8 src/Text/XFormat/Show.hs
  2. +3 −0 tests/Show.hs
View
@@ -64,6 +64,7 @@ module Text.XFormat.Show (
WrapF(..),
AlignF(..),
+ AlignChopF(..),
Dir(..),
-- ** Other Format Descriptors
@@ -327,21 +328,31 @@ instance (Format din fin, Format dout fout)
data AlignF a = Align Dir Int a
--- | Direction (left or right) used for 'AlignF'.
+-- | Same as 'AlignF' but chop off the output if it extends past the column
+-- width.
+
+data AlignChopF a = AlignChop Dir Int a
+
+-- | Direction (left or right) used for 'AlignF' and 'AlignChopF'.
data Dir = L | R
-align :: Dir -> Int -> ShowS -> ShowS
-align dir wid f =
+align :: Bool -> Dir -> Int -> ShowS -> ShowS
+align doChop dir wid input =
case dir of
- L -> f . g
- R -> g . f
+ L -> chop (take wid) . input . addSpaces
+ R -> chop (drop (len - wid)) . addSpaces . input
where
- len = length (f "")
- g = if len < wid then showString (replicate (wid - len) ' ') else id
+ len = length (input "")
+ spaces = replicate (wid - len) ' '
+ chop act = if doChop && len > wid then act else id
+ addSpaces = if len < wid then showString spaces else id
instance (Format d f) => Format (AlignF d) f where
- showsf' (Align dir wid d) = fmap (align dir wid) (showsf' d)
+ showsf' (Align dir wid d) = fmap (align False dir wid) (showsf' d)
+
+instance (Format d f) => Format (AlignChopF d) f where
+ showsf' (AlignChop dir wid d) = fmap (align True dir wid) (showsf' d)
--------------------------------------------------------------------------------
View
@@ -58,6 +58,9 @@ testRecursive = and
, showf (Align L 5 String) "abc" == "abc "
, showf (Align R 5 Int) 999 == " 999"
, showf (Align L 5 "1234567") == "1234567"
+ , showf (AlignChop L 5 "123") == "123 "
+ , showf (AlignChop L 3 "12345") == "123"
+ , showf (AlignChop R 3 "12345") == "345"
]
testTuples :: Bool

0 comments on commit 84f56c4

Please sign in to comment.