module Shell.Utility.Quote (always, minimal, lazy) where
mustEscape, mustQuote :: [Char]
mustEscape = "\"$`\\!"
mustQuote = "' \t\n|&;()<>{}[]*?^#"
escapeChar :: Char -> String
escapeChar c = (if elem c mustEscape then ('\\':) else id) [c]
enclose :: String -> String
enclose txt = '"' : txt ++ '"' : []
always :: String -> String
always = enclose . concatMap escapeChar
minimal :: String -> String
minimal txt =
if null txt || any (flip elem mustQuote) txt
then always txt
else concatMap escapeChar txt
lazy :: String -> String
lazy "" = "\"\""
lazy txt =
let go "" = ""
go str@(c:cs) =
if elem c mustQuote then always str else escapeChar c ++ go cs
in go txt