module Talash.Brick.Internal (twoColumnText , columns , searchWidget , searchWidgetAux , headingAndBody , listWithHighlights , columnsListWithHighlights
                ,  theMain , module Export) where

import Brick as Export
import Brick.BChan as Export (BChan , newBChan , writeBChan)
import Brick.Markup as Export (Markup , (@?) , markup, GetAttr)
import Brick.Widgets.Border as Export (border, vBorder, hBorder, borderAttr)
import Brick.Widgets.Border.Style as Export
import Brick.Widgets.Center as Export (vCenter, center)
import Brick.Widgets.Edit  as Export (editor , editorText, renderEditor, Editor, handleEditorEvent, getEditContents, applyEdit )
import Brick.Widgets.List as Export (List, list ,handleListEvent, handleListEventVi, listAttr, listSelectedAttr, listSelectedElement , listSelectedL
                                    ,listReplace , renderListWithIndex, renderList , listElements, GenericList (listElements, listSelected))
import Data.Text.Markup as Export (toText)
import Graphics.Vty as Export (defAttr, cyan, white, blue, withStyle, bold, brightMagenta, black, magenta, brightBlue, Attr, defaultConfig, mkVty, green, standardIOConfig)
import Graphics.Vty.Config (Config(inputFd))
import Graphics.Vty.Input.Events as Export 
import Intro
import Lens.Micro as Export (ASetter' , over, set, (^.) , _1 , _2 , _3 , (.~) , (?~) , (%~))
import Lens.Micro.TH as Export ( makeLenses )
import System.Posix.IO
import System.Posix.Terminal

twoColumnText :: Int -> Text -> Text -> Widget n
twoColumnText :: Int -> Text -> Text -> Widget n
twoColumnText Int
n Text
t1 Text
t2 =  Widget n -> Widget n
forall n. Widget n -> Widget n
joinBorders (Widget n -> Widget n)
-> (Widget n -> Widget n) -> Widget n -> Widget n
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Int -> Widget n -> Widget n
forall n. Int -> Widget n -> Widget n
vLimit Int
1 (Widget n -> Widget n) -> Widget n -> Widget n
forall a b. (a -> b) -> a -> b
$ Int -> Text -> Widget n
forall n. Int -> Text -> Widget n
go Int
n Text
t1 Widget n -> Widget n -> Widget n
forall n. Widget n -> Widget n -> Widget n
<+> Int -> Text -> Widget n
forall n. Int -> Text -> Widget n
go Int
100 Text
t2
  where
    go :: Int -> Text -> Widget n
go Int
m Text
t = Int -> Widget n -> Widget n
forall n. Int -> Widget n -> Widget n
hLimitPercent Int
m (Widget n -> Widget n) -> Widget n -> Widget n
forall a b. (a -> b) -> a -> b
$ Int -> Widget n -> Widget n
forall n. Int -> Widget n -> Widget n
padLeftRight Int
2 (Text -> Widget n
forall n. Text -> Widget n
txt Text
t) Widget n -> Widget n -> Widget n
forall n. Widget n -> Widget n -> Widget n
<+> Char -> Widget n
forall n. Char -> Widget n
fill Char
' '

columns :: (a -> Widget n) -> [AttrName] -> [Int] -> [a] -> Widget n
columns :: (a -> Widget n) -> [AttrName] -> [Int] -> [a] -> Widget n
columns a -> Widget n
f [AttrName]
as [Int]
ls = Int -> Widget n -> Widget n
forall n. Int -> Widget n -> Widget n
vLimit Int
1 (Widget n -> Widget n) -> ([a] -> Widget n) -> [a] -> Widget n
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. [Widget n] -> Widget n
forall n. [Widget n] -> Widget n
hBox ([Widget n] -> Widget n) -> ([a] -> [Widget n]) -> [a] -> Widget n
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. (AttrName -> Int -> a -> Widget n)
-> [AttrName] -> [Int] -> [a] -> [Widget n]
forall a b c d. (a -> b -> c -> d) -> [a] -> [b] -> [c] -> [d]
zipWith3 (\AttrName
a Int
l a
t -> Int -> Widget n -> Widget n
forall n. Int -> Widget n -> Widget n
hLimitPercent Int
l (Widget n -> Widget n) -> Widget n -> Widget n
forall a b. (a -> b) -> a -> b
$ (Padding -> Widget n -> Widget n
forall n. Padding -> Widget n -> Widget n
padRight (Int -> Padding
Pad Int
2) (Widget n -> Widget n) -> (a -> Widget n) -> a -> Widget n
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. AttrName -> Widget n -> Widget n
forall n. AttrName -> Widget n -> Widget n
withAttr AttrName
a (Widget n -> Widget n) -> (a -> Widget n) -> a -> Widget n
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. a -> Widget n
f (a -> Widget n) -> a -> Widget n
forall a b. (a -> b) -> a -> b
$ a
t) Widget n -> Widget n -> Widget n
forall n. Widget n -> Widget n -> Widget n
<+> Char -> Widget n
forall n. Char -> Widget n
fill Char
' ') [AttrName]
as [Int]
ls

searchWidget :: (Ord n , Show n) => Bool -> Text -> Editor Text n -> Widget n
searchWidget :: Bool -> Text -> Editor Text n -> Widget n
searchWidget Bool
b Text
p Editor Text n
e = AttrName -> Widget n -> Widget n
forall n. AttrName -> Widget n -> Widget n
withAttr AttrName
"Prompt" (Int -> Widget n -> Widget n
forall n. Int -> Widget n -> Widget n
padLeftRight Int
2 (Widget n -> Widget n) -> (Text -> Widget n) -> Text -> Widget n
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Text -> Widget n
forall n. Text -> Widget n
txt (Text -> Widget n) -> Text -> Widget n
forall a b. (a -> b) -> a -> b
$ Text
p) Widget n -> Widget n -> Widget n
forall n. Widget n -> Widget n -> Widget n
<+> Int -> Widget n -> Widget n
forall n. Int -> Widget n -> Widget n
padLeftRight Int
2 (([Text] -> Widget n) -> Bool -> Editor Text n -> Widget n
forall n t.
(Ord n, Show n, Monoid t, TextWidth t, GenericTextZipper t) =>
([t] -> Widget n) -> Bool -> Editor t n -> Widget n
renderEditor ([Widget n] -> Widget n
forall n. [Widget n] -> Widget n
hBox ([Widget n] -> Widget n)
-> ([Text] -> [Widget n]) -> [Text] -> Widget n
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. (Text -> Widget n) -> [Text] -> [Widget n]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
map Text -> Widget n
forall n. Text -> Widget n
txt) Bool
b Editor Text n
e)

searchWidgetAux :: (Ord n , Show n) => Bool -> Text -> Editor Text n -> Widget n -> Widget n
searchWidgetAux :: Bool -> Text -> Editor Text n -> Widget n -> Widget n
searchWidgetAux Bool
b Text
p Editor Text n
e Widget n
w = AttrName -> Widget n -> Widget n
forall n. AttrName -> Widget n -> Widget n
withAttr AttrName
"Prompt" (Int -> Widget n -> Widget n
forall n. Int -> Widget n -> Widget n
padLeftRight Int
2 (Widget n -> Widget n) -> (Text -> Widget n) -> Text -> Widget n
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Text -> Widget n
forall n. Text -> Widget n
txt (Text -> Widget n) -> Text -> Widget n
forall a b. (a -> b) -> a -> b
$ Text
p) Widget n -> Widget n -> Widget n
forall n. Widget n -> Widget n -> Widget n
<+> Int -> Widget n -> Widget n
forall n. Int -> Widget n -> Widget n
padLeftRight Int
2 (([Text] -> Widget n) -> Bool -> Editor Text n -> Widget n
forall n t.
(Ord n, Show n, Monoid t, TextWidth t, GenericTextZipper t) =>
([t] -> Widget n) -> Bool -> Editor t n -> Widget n
renderEditor ([Widget n] -> Widget n
forall n. [Widget n] -> Widget n
hBox ([Widget n] -> Widget n)
-> ([Text] -> [Widget n]) -> [Text] -> Widget n
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. (Text -> Widget n) -> [Text] -> [Widget n]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
map Text -> Widget n
forall n. Text -> Widget n
txt) Bool
b Editor Text n
e) Widget n -> Widget n -> Widget n
forall n. Widget n -> Widget n -> Widget n
<+> Widget n
w

highlightAlternate :: Foldable f => (a -> Widget n) -> f a -> Widget n
highlightAlternate :: (a -> Widget n) -> f a -> Widget n
highlightAlternate a -> Widget n
f = (Widget n, Bool) -> Widget n
forall a b. (a, b) -> a
fst ((Widget n, Bool) -> Widget n)
-> (f a -> (Widget n, Bool)) -> f a -> Widget n
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. ((Widget n, Bool) -> a -> (Widget n, Bool))
-> (Widget n, Bool) -> f a -> (Widget n, Bool)
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' (\(!Widget n
w , !Bool
b) !a
n  -> (Widget n
w Widget n -> Widget n -> Widget n
forall n. Widget n -> Widget n -> Widget n
<+> if Bool
b then AttrName -> Widget n -> Widget n
forall n. AttrName -> Widget n -> Widget n
withAttr AttrName
"Highlight" (a -> Widget n
f a
n) else a -> Widget n
f a
n , Bool -> Bool
not Bool
b)) (Widget n
forall n. Widget n
emptyWidget , Bool
False)

headingAndBody :: Text -> Text -> Widget n
headingAndBody :: Text -> Text -> Widget n
headingAndBody Text
h Text
b = AttrName -> Widget n -> Widget n
forall n. AttrName -> Widget n -> Widget n
withAttr AttrName
"Heading" (Text -> Widget n
forall n. Text -> Widget n
txt Text
h) Widget n -> Widget n -> Widget n
forall n. Widget n -> Widget n -> Widget n
<=> Text -> Widget n
forall n. Text -> Widget n
txtWrap Text
b

listWithHighlights :: (Foldable f , Ord n , Show n) => Text -> (a -> f Text) -> Bool -> List n a -> Widget n
listWithHighlights :: Text -> (a -> f Text) -> Bool -> List n a -> Widget n
listWithHighlights Text
c a -> f Text
f = (Bool -> a -> Widget n) -> Bool -> List n a -> Widget n
forall (t :: * -> *) n e.
(Traversable t, Splittable t, Ord n, Show n) =>
(Bool -> e -> Widget n) -> Bool -> GenericList n t e -> Widget n
renderList (\Bool
s a
e -> Int -> Widget n -> Widget n
forall n. Int -> Widget n -> Widget n
vLimit Int
1 (Widget n -> Widget n) -> (a -> Widget n) -> a -> Widget n
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. (Text -> Widget n
forall n. Text -> Widget n
txt (if Bool
s then Text
c else Text
"  ") Widget n -> Widget n -> Widget n
forall n. Widget n -> Widget n -> Widget n
<+>) (Widget n -> Widget n) -> (a -> Widget n) -> a -> Widget n
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. (Widget n -> Widget n -> Widget n
forall n. Widget n -> Widget n -> Widget n
<+> Char -> Widget n
forall n. Char -> Widget n
fill Char
' ') (Widget n -> Widget n) -> (a -> Widget n) -> a -> Widget n
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. (Text -> Widget n) -> f Text -> Widget n
forall (f :: * -> *) a n.
Foldable f =>
(a -> Widget n) -> f a -> Widget n
highlightAlternate Text -> Widget n
forall n. Text -> Widget n
txt (f Text -> Widget n) -> (a -> f Text) -> a -> Widget n
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. a -> f Text
f (a -> Widget n) -> a -> Widget n
forall a b. (a -> b) -> a -> b
$! a
e)

columnsListWithHighlights :: (Foldable f , Ord n , Show n) => Text -> (a -> [f Text]) -> [AttrName] -> [Int] -> Bool -> List n a -> Widget n
columnsListWithHighlights :: Text
-> (a -> [f Text])
-> [AttrName]
-> [Int]
-> Bool
-> List n a
-> Widget n
columnsListWithHighlights Text
c a -> [f Text]
f [AttrName]
as [Int]
ls = (Bool -> a -> Widget n) -> Bool -> List n a -> Widget n
forall (t :: * -> *) n e.
(Traversable t, Splittable t, Ord n, Show n) =>
(Bool -> e -> Widget n) -> Bool -> GenericList n t e -> Widget n
renderList (\Bool
s a
e -> (Text -> Widget n
forall n. Text -> Widget n
txt (if Bool
s then Text
c else Text
"  ") Widget n -> Widget n -> Widget n
forall n. Widget n -> Widget n -> Widget n
<+>) (Widget n -> Widget n) -> (a -> Widget n) -> a -> Widget n
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. (f Text -> Widget n) -> [AttrName] -> [Int] -> [f Text] -> Widget n
forall a n.
(a -> Widget n) -> [AttrName] -> [Int] -> [a] -> Widget n
columns ((Text -> Widget n) -> f Text -> Widget n
forall (f :: * -> *) a n.
Foldable f =>
(a -> Widget n) -> f a -> Widget n
highlightAlternate Text -> Widget n
forall n. Text -> Widget n
txt) [AttrName]
as [Int]
ls ([f Text] -> Widget n) -> (a -> [f Text]) -> a -> Widget n
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. a -> [f Text]
f (a -> Widget n) -> a -> Widget n
forall a b. (a -> b) -> a -> b
$! a
e)

theMain :: Ord n => App b e n -> BChan e -> b -> IO b
theMain :: App b e n -> BChan e -> b -> IO b
theMain App b e n
a BChan e
b b
s = (\Vty
v -> Vty -> IO Vty -> Maybe (BChan e) -> App b e n -> b -> IO b
forall n e s.
Ord n =>
Vty -> IO Vty -> Maybe (BChan e) -> App s e n -> s -> IO s
customMain Vty
v (Vty -> IO Vty
forall (f :: * -> *) a. Applicative f => a -> f a
pure Vty
v) (BChan e -> Maybe (BChan e)
forall a. a -> Maybe a
Just BChan e
b) App b e n
a b
s) (Vty -> IO b) -> IO Vty -> IO b
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Config -> IO Vty
mkVty (Config -> IO Vty) -> IO Config -> IO Vty
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<<(\Config
c -> (\Fd
fd -> Config
c {inputFd :: Maybe Fd
inputFd = Fd -> Maybe Fd
forall a. a -> Maybe a
Just Fd
fd}) (Fd -> Config) -> IO Fd -> IO Config
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> IO Fd
termFd) (Config -> IO Config) -> IO Config -> IO Config
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< IO Config
standardIOConfig
  where
    termFd :: IO Fd
termFd = (\FilePath
f -> FilePath -> OpenMode -> Maybe FileMode -> OpenFileFlags -> IO Fd
openFd FilePath
f OpenMode
ReadOnly Maybe FileMode
forall a. Maybe a
Nothing (Bool -> Bool -> Bool -> Bool -> Bool -> OpenFileFlags
OpenFileFlags Bool
False Bool
False Bool
False Bool
False Bool
False)) (FilePath -> IO Fd) -> IO FilePath -> IO Fd
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< IO FilePath
getControllingTerminalName