module Darcs.Util.English where
import Darcs.Prelude
import Data.Char (toUpper)
import Data.List (isSuffixOf)
import Darcs.Util.Printer ( Doc, vcat, text )
englishNum :: Countable n => Int -> n -> ShowS
englishNum :: Int -> n -> ShowS
englishNum Int
x = if Int
x Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
1 then n -> ShowS
forall a. Countable a => a -> ShowS
singular else n -> ShowS
forall a. Countable a => a -> ShowS
plural
class Countable a where
plural :: a -> ShowS
singular :: a -> ShowS
newtype Noun = Noun String
data Pronoun = It
instance Countable Noun where
plural :: Noun -> ShowS
plural (Noun String
s) | String
"ch" String -> String -> Bool
forall a. Eq a => [a] -> [a] -> Bool
`isSuffixOf` String
s = String -> ShowS
showString String
s ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> ShowS
showString String
"es"
plural (Noun String
s) | String
"y" String -> String -> Bool
forall a. Eq a => [a] -> [a] -> Bool
`isSuffixOf` String
s
Bool -> Bool -> Bool
&& String -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length String
s Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
1
Bool -> Bool -> Bool
&& String -> Char
forall a. [a] -> a
last (ShowS
forall a. [a] -> [a]
init String
s) Char -> String -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`notElem` String
"aeiou" =
String -> ShowS
showString (ShowS
forall a. [a] -> [a]
init String
s) ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> ShowS
showString String
"ies"
plural (Noun String
s) = String -> ShowS
showString String
s ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Char -> ShowS
showChar Char
's'
singular :: Noun -> ShowS
singular (Noun String
s) = String -> ShowS
showString String
s
instance Countable Pronoun where
plural :: Pronoun -> ShowS
plural Pronoun
It = String -> ShowS
showString String
"them"
singular :: Pronoun -> ShowS
singular Pronoun
It = String -> ShowS
showString String
"it"
data This = This Noun
instance Countable This where
plural :: This -> ShowS
plural (This Noun
s) = String -> ShowS
showString String
"these " ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Noun -> ShowS
forall a. Countable a => a -> ShowS
plural Noun
s
singular :: This -> ShowS
singular (This Noun
s) = String -> ShowS
showString String
"this " ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Noun -> ShowS
forall a. Countable a => a -> ShowS
singular Noun
s
andClauses, orClauses :: [String] -> String
andClauses :: [String] -> String
andClauses = String -> [String] -> String
itemize String
"and"
orClauses :: [String] -> String
orClauses = String -> [String] -> String
itemize String
"or"
anyOfClause :: [String] -> Doc
anyOfClause :: [String] -> Doc
anyOfClause [String]
names = if [String] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [String]
names Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
1 then String -> Doc
text String
"any of" else Doc
forall a. Monoid a => a
mempty
itemizeVertical :: Int -> [String] -> Doc
itemizeVertical :: Int -> [String] -> Doc
itemizeVertical Int
indent = [Doc] -> Doc
vcat ([Doc] -> Doc) -> ([String] -> [Doc]) -> [String] -> Doc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (String -> Doc) -> [String] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map (String -> Doc
text (String -> Doc) -> ShowS -> String -> Doc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((Int -> Char -> String
forall a. Int -> a -> [a]
replicate Int
indent Char
' ' String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
"- ") String -> ShowS
forall a. [a] -> [a] -> [a]
++))
itemize :: String -> [String] -> String
itemize :: String -> [String] -> String
itemize String
_ [] = String
""
itemize String
_ [String
x] = String
x
itemize String
sep [String
x,String
x'] = [String] -> String
unwords [String
x, String
sep, String
x']
itemize String
sep (String
x:String
x':[String]
xs) = String -> String -> [String] -> String
itemize' String
x String
x' [String]
xs where
itemize' :: String -> String -> [String] -> String
itemize' String
y String
y' [] = [String] -> String
unwords [String
y String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
",", String
sep, String
y']
itemize' String
y String
y' (String
y'':[String]
ys) = [String] -> String
unwords [String
y String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
",", String -> String -> [String] -> String
itemize' String
y' String
y'' [String]
ys]
presentParticiple :: String -> String
presentParticiple :: ShowS
presentParticiple String
v | String -> Char
forall a. [a] -> a
last String
v Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
'e' = ShowS
forall a. [a] -> [a]
init String
v String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
"ing"
| Bool
otherwise = String
v String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
"ing"
capitalize :: String -> String
capitalize :: ShowS
capitalize [] = []
capitalize (Char
x:String
xs) = Char -> Char
toUpper Char
x Char -> ShowS
forall a. a -> [a] -> [a]
: String
xs