module Data.Boltzmann.Internal.Utils
( quote
, ensureLn
, closest
, bold
, italic
, underline
, boldColor
, csv
) where
import System.Console.Pretty
import Text.EditDistance
csv :: [String] -> String
csv [] = ""
csv xs = foldl1 (\x y -> x ++ ", " ++ y) xs
ensureLn :: String -> String
ensureLn s
| last s == '\n' = s
| otherwise = unlines [s]
quote :: String -> String
quote m = "'" ++ m ++ "'"
format :: (String -> String)
-> String -> IO String
format f s = do
inColor <- supportsPretty
return $ if inColor then f s
else s
bold :: String -> IO String
bold = format (style Bold)
italic :: String -> IO String
italic = format (style Italic)
underline :: String -> IO String
underline = format (style Underline)
boldColor :: Color -> String -> IO String
boldColor c = format (style Bold . color c)
closest :: [String] -> String -> String
closest dic s = closest' (tail dists) (head dists)
where dists = zip dic $ map (editDist s) dic
closest' :: Ord b => [(a, b)] -> (a, b) -> a
closest' [] (m, _) = m
closest' ((x,k):xs) (m, k')
| k < k' = closest' xs (x, k)
| otherwise = closest' xs (m, k')
editDist :: String -> String -> Int
editDist = levenshteinDistance defaultEditCosts