module Options.Applicative.Help.Chunk
( Chunk(..)
, chunked
, listToChunk
, (<<+>>)
, (<</>>)
, vcatChunks
, vsepChunks
, isEmpty
, stringChunk
, paragraph
, extractChunk
, tabulate
) where
import Control.Applicative
import Control.Monad
import Data.List.NonEmpty (NonEmpty(..))
import Data.Maybe
import Data.Semigroup
import Prelude
import Options.Applicative.Help.Pretty
newtype Chunk a = Chunk
{ forall a. Chunk a -> Maybe a
unChunk :: Maybe a }
deriving (Chunk a -> Chunk a -> Bool
forall a. Eq a => Chunk a -> Chunk a -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Chunk a -> Chunk a -> Bool
$c/= :: forall a. Eq a => Chunk a -> Chunk a -> Bool
== :: Chunk a -> Chunk a -> Bool
$c== :: forall a. Eq a => Chunk a -> Chunk a -> Bool
Eq, Int -> Chunk a -> ShowS
forall a. Show a => Int -> Chunk a -> ShowS
forall a. Show a => [Chunk a] -> ShowS
forall a. Show a => Chunk a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Chunk a] -> ShowS
$cshowList :: forall a. Show a => [Chunk a] -> ShowS
show :: Chunk a -> String
$cshow :: forall a. Show a => Chunk a -> String
showsPrec :: Int -> Chunk a -> ShowS
$cshowsPrec :: forall a. Show a => Int -> Chunk a -> ShowS
Show)
instance Functor Chunk where
fmap :: forall a b. (a -> b) -> Chunk a -> Chunk b
fmap a -> b
f = forall a. Maybe a -> Chunk a
Chunk forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
f forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Chunk a -> Maybe a
unChunk
instance Applicative Chunk where
pure :: forall a. a -> Chunk a
pure = forall a. Maybe a -> Chunk a
Chunk forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *) a. Applicative f => a -> f a
pure
Chunk Maybe (a -> b)
f <*> :: forall a b. Chunk (a -> b) -> Chunk a -> Chunk b
<*> Chunk Maybe a
x = forall a. Maybe a -> Chunk a
Chunk (Maybe (a -> b)
f forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Maybe a
x)
instance Alternative Chunk where
empty :: forall a. Chunk a
empty = forall a. Maybe a -> Chunk a
Chunk forall (f :: * -> *) a. Alternative f => f a
Control.Applicative.empty
Chunk a
a <|> :: forall a. Chunk a -> Chunk a -> Chunk a
<|> Chunk a
b = forall a. Maybe a -> Chunk a
Chunk forall a b. (a -> b) -> a -> b
$ forall a. Chunk a -> Maybe a
unChunk Chunk a
a forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> forall a. Chunk a -> Maybe a
unChunk Chunk a
b
instance Monad Chunk where
return :: forall a. a -> Chunk a
return = forall (f :: * -> *) a. Applicative f => a -> f a
pure
Chunk a
m >>= :: forall a b. Chunk a -> (a -> Chunk b) -> Chunk b
>>= a -> Chunk b
f = forall a. Maybe a -> Chunk a
Chunk forall a b. (a -> b) -> a -> b
$ forall a. Chunk a -> Maybe a
unChunk Chunk a
m forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall a. Chunk a -> Maybe a
unChunk forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Chunk b
f
instance Semigroup a => Semigroup (Chunk a) where
<> :: Chunk a -> Chunk a -> Chunk a
(<>) = forall a. (a -> a -> a) -> Chunk a -> Chunk a -> Chunk a
chunked forall a. Semigroup a => a -> a -> a
(<>)
instance Semigroup a => Monoid (Chunk a) where
mempty :: Chunk a
mempty = forall a. Maybe a -> Chunk a
Chunk forall a. Maybe a
Nothing
mappend :: Chunk a -> Chunk a -> Chunk a
mappend = forall a. Semigroup a => a -> a -> a
(<>)
instance MonadPlus Chunk where
mzero :: forall a. Chunk a
mzero = forall a. Maybe a -> Chunk a
Chunk forall (m :: * -> *) a. MonadPlus m => m a
mzero
mplus :: forall a. Chunk a -> Chunk a -> Chunk a
mplus Chunk a
m1 Chunk a
m2 = forall a. Maybe a -> Chunk a
Chunk forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. MonadPlus m => m a -> m a -> m a
mplus (forall a. Chunk a -> Maybe a
unChunk Chunk a
m1) (forall a. Chunk a -> Maybe a
unChunk Chunk a
m2)
chunked :: (a -> a -> a)
-> Chunk a -> Chunk a -> Chunk a
chunked :: forall a. (a -> a -> a) -> Chunk a -> Chunk a -> Chunk a
chunked a -> a -> a
_ (Chunk Maybe a
Nothing) Chunk a
y = Chunk a
y
chunked a -> a -> a
_ Chunk a
x (Chunk Maybe a
Nothing) = Chunk a
x
chunked a -> a -> a
f (Chunk (Just a
x)) (Chunk (Just a
y)) = forall a. Maybe a -> Chunk a
Chunk (forall a. a -> Maybe a
Just (a -> a -> a
f a
x a
y))
listToChunk :: Semigroup a => [a] -> Chunk a
listToChunk :: forall a. Semigroup a => [a] -> Chunk a
listToChunk [] = forall a. Monoid a => a
mempty
listToChunk (a
x:[a]
xs) = forall (f :: * -> *) a. Applicative f => a -> f a
pure (forall a. Semigroup a => NonEmpty a -> a
sconcat (a
x forall a. a -> [a] -> NonEmpty a
:| [a]
xs))
extractChunk :: Monoid a => Chunk a -> a
= forall a. a -> Maybe a -> a
fromMaybe forall a. Monoid a => a
mempty forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Chunk a -> Maybe a
unChunk
(<<+>>) :: Chunk Doc -> Chunk Doc -> Chunk Doc
<<+>> :: Chunk Doc -> Chunk Doc -> Chunk Doc
(<<+>>) = forall a. (a -> a -> a) -> Chunk a -> Chunk a -> Chunk a
chunked Doc -> Doc -> Doc
(<+>)
(<</>>) :: Chunk Doc -> Chunk Doc -> Chunk Doc
<</>> :: Chunk Doc -> Chunk Doc -> Chunk Doc
(<</>>) = forall a. (a -> a -> a) -> Chunk a -> Chunk a -> Chunk a
chunked Doc -> Doc -> Doc
(</>)
vcatChunks :: [Chunk Doc] -> Chunk Doc
vcatChunks :: [Chunk Doc] -> Chunk Doc
vcatChunks = forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr (forall a. (a -> a -> a) -> Chunk a -> Chunk a -> Chunk a
chunked Doc -> Doc -> Doc
(.$.)) forall a. Monoid a => a
mempty
vsepChunks :: [Chunk Doc] -> Chunk Doc
vsepChunks :: [Chunk Doc] -> Chunk Doc
vsepChunks = forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr (forall a. (a -> a -> a) -> Chunk a -> Chunk a -> Chunk a
chunked (\Doc
x Doc
y -> Doc
x Doc -> Doc -> Doc
.$. forall a. Monoid a => a
mempty Doc -> Doc -> Doc
.$. Doc
y)) forall a. Monoid a => a
mempty
isEmpty :: Chunk a -> Bool
isEmpty :: forall a. Chunk a -> Bool
isEmpty = forall a. Maybe a -> Bool
isNothing forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Chunk a -> Maybe a
unChunk
stringChunk :: String -> Chunk Doc
stringChunk :: String -> Chunk Doc
stringChunk String
"" = forall a. Monoid a => a
mempty
stringChunk String
s = forall (f :: * -> *) a. Applicative f => a -> f a
pure (String -> Doc
string String
s)
paragraph :: String -> Chunk Doc
paragraph :: String -> Chunk Doc
paragraph = forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr (forall a. (a -> a -> a) -> Chunk a -> Chunk a -> Chunk a
chunked Doc -> Doc -> Doc
(</>) forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Chunk Doc
stringChunk) forall a. Monoid a => a
mempty
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> [String]
words
tabulate :: Int -> [(Doc, Doc)] -> Chunk Doc
tabulate :: Int -> [(Doc, Doc)] -> Chunk Doc
tabulate Int
_ [] = forall a. Monoid a => a
mempty
tabulate Int
size [(Doc, Doc)]
table = forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ [Doc] -> Doc
vcat
[ Int -> Doc -> Doc
indent Int
2 (Int -> Doc -> Doc
fillBreak Int
size Doc
key Doc -> Doc -> Doc
<+> Doc
value)
| (Doc
key, Doc
value) <- [(Doc, Doc)]
table ]