Permalink
Browse files

Added stringRect and Rect. Useful for calculating the number of rows …

…a textarea will need to display a string.
  • Loading branch information...
1 parent 3fd782f commit a2faa7e82f81328c7e6769e7c907c594ccc35820 @stepcut stepcut committed Jan 6, 2010
Showing with 11 additions and 0 deletions.
  1. +11 −0 Text/Formlets.hs
View
@@ -37,6 +37,17 @@ data ContentType = ContentType { ctType :: String
, ctParameters :: [(String, String)]
}
deriving (Eq, Show, Read, Data, Typeable)
+
+data Rect = Rect {rectCols :: Int, rectRows :: Int}
+ deriving (Eq, Ord, Show, Read, Data, Typeable)
+
+-- |Choose a good number of rows for a textarea input. Uses the
+-- number of newlines in the string and the number of lines that
+-- are too long for the desired width.
+stringRect :: Int -> String -> Rect
+stringRect cols s =
+ Rect {rectCols = cols,
+ rectRows = foldr (+) 0 (map (\ line -> 1 + (length line) `div` cols) (lines s))}
-- | Apply a predicate to a value and return FR.Success or FR.Failure as appropriate
ensure :: Show a

0 comments on commit a2faa7e

Please sign in to comment.