1-- ----------------------------------------------------------------------------- 2-- 3-- Util.hs, part of Alex 4-- 5-- (c) Simon Marlow 2003 6-- 7-- General utilities used in various parts of Alex 8-- 9-- ----------------------------------------------------------------------------} 10 11module Util 12 ( str 13 , char 14 , nl 15 , paren 16 , brack 17 , interleave_shows 18 , space 19 , cjustify 20 , ljustify 21 , rjustify 22 , spaces 23 , hline 24 ) where 25 26-- Pretty-printing utilities 27 28str :: String -> String -> String 29str = showString 30 31char :: Char -> String -> String 32char c = (c :) 33 34nl :: String -> String 35nl = char '\n' 36 37paren :: (String -> String) -> String -> String 38paren s = char '(' . s . char ')' 39 40brack :: (String -> String) -> String -> String 41brack s = char '[' . s . char ']' 42 43interleave_shows :: (String -> String) -> [String -> String] -> String -> String 44interleave_shows _ [] = id 45interleave_shows s xs = foldr1 (\a b -> a . s . b) xs 46 47space :: String -> String 48space = char ' ' 49 50cjustify, ljustify, rjustify :: Int -> String -> String 51cjustify n s = spaces halfm ++ s ++ spaces (m - halfm) 52 where 53 m = n - length s 54 halfm = m `div` 2 55ljustify n s = s ++ spaces (max 0 (n - length s)) 56rjustify n s = spaces (n - length s) ++ s 57 58spaces :: Int -> String 59spaces n = replicate n ' ' 60 61hline :: String 62hline = replicate 77 '-' 63