<commit_before>{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TemplateHaskell #-}

module Ui where

import Prelude
import qualified Data.Text as T
import Data.Maybe(fromMaybe)
import Control.Monad.IO.Class(liftIO)
import Data.List(sort)
import Lens.Micro((&), (^.), (.~))
import Lens.Micro.TH (makeLenses)
import Data.Time.Clock(getCurrentTime, UTCTime)
import Data.Time.Format(defaultTimeLocale, formatTime)
import qualified Graphics.Vty as V
import qualified Brick.Types as BT
import Brick.Markup(markup, (@?))
import Brick.Util (on)
import Brick.Widgets.Core(
      txt
    , hBox
    , vBox
    , padTopBottom
    , withAttr
    )
import Brick.AttrMap (attrMap, AttrMap, AttrName)
import Brick.Main (
      App(..)
    , showFirstCursor
    , halt
    , continue
    )
import qualified Brick.Widgets.Center as BWC 
import Types( Repo
            ,  slug
            , lastBuildState
            , active
            , BuildState(..)
            , Conf
            , travisUser)

data AppState = 
    AppState { _conf :: Conf
             , _stLastVtyEvent :: Maybe V.Event 
             , _repos :: [Repo]
             , _timestamp :: UTCTime
             , _errMsg :: Maybe T.Text
}
    deriving (Eq, Show)

makeLenses ''AppState

getDateStr :: IO String
getDateStr = fmap show getCurrentTime

padStr :: String -> Int -> String
padStr s n = p ++ s ++ p
  where p = take n . repeat $ ' '


ui :: AppState -> [BT.Widget ()]
ui st =
    case (st ^. errMsg) of
        Nothing -> [vBox [header, repoUI activeRepos, statusBar']]
        (Just msg) -> [vBox [padTopBottom 1 (txt msg), statusBar']]
    where
        statusBar' = statusBar (st ^. timestamp)
        activeRepos = filter (\repo -> fromMaybe False (repo ^. active)) $ st ^. repos
        header = BWC.hCenter $ markup $ headerTxt @? "status.normal"
            where headerTxt = T.concat [ "Travis projects for "
                               , st ^. conf . travisUser]

repoUI :: [Repo]-> BT.Widget ()
repoUI repos' 
    | not (null repos')  = vBox . fmap renderRepo . sort $ repos'
    | otherwise = txt "no repos" 
    where
        renderRepo repo =  markup (repoTxt repo @? colorRepo repo)

        repoTxt :: Repo -> T.Text
        repoTxt repo = T.concat 
            [ fromMaybe "-" (repo ^. slug)
            , " . " 
            , T.pack . show . fromMaybe Unknown $ repo ^. lastBuildState]

timestampTxt :: UTCTime -> T.Text
timestampTxt ts = T.pack $  formatTime defaultTimeLocale "%H:%m:%S" ts 
<commit_msg>removed obsolete function<commit_after>{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TemplateHaskell #-}

module Ui where

import Prelude
import qualified Data.Text as T
import Data.Maybe(fromMaybe)
import Control.Monad.IO.Class(liftIO)
import Data.List(sort)
import Lens.Micro((&), (^.), (.~))
import Lens.Micro.TH (makeLenses)
import Data.Time.Clock(getCurrentTime, UTCTime)
import Data.Time.Format(defaultTimeLocale, formatTime)
import qualified Graphics.Vty as V
import qualified Brick.Types as BT
import Brick.Markup(markup, (@?))
import Brick.Util (on)
import Brick.Widgets.Core(
      txt
    , hBox
    , vBox
    , padTopBottom
    , withAttr
    )
import Brick.AttrMap (attrMap, AttrMap, AttrName)
import Brick.Main (
      App(..)
    , showFirstCursor
    , halt
    , continue
    )
import qualified Brick.Widgets.Center as BWC 
import Types( Repo
            ,  slug
            , lastBuildState
            , active
            , BuildState(..)
            , Conf
            , travisUser)

data AppState = 
    AppState { _conf :: Conf
             , _stLastVtyEvent :: Maybe V.Event 
             , _repos :: [Repo]
             , _timestamp :: UTCTime
             , _errMsg :: Maybe T.Text
}
    deriving (Eq, Show)

makeLenses ''AppState

getDateStr :: IO String
getDateStr = fmap show getCurrentTime

ui :: AppState -> [BT.Widget ()]
ui st =
    case (st ^. errMsg) of
        Nothing -> [vBox [header, repoUI activeRepos, statusBar']]
        (Just msg) -> [vBox [padTopBottom 1 (txt msg), statusBar']]
    where
        statusBar' = statusBar (st ^. timestamp)
        activeRepos = filter (\repo -> fromMaybe False (repo ^. active)) $ st ^. repos
        header = BWC.hCenter $ markup $ headerTxt @? "status.normal"
            where headerTxt = T.concat [ "Travis projects for "
                               , st ^. conf . travisUser]

repoUI :: [Repo]-> BT.Widget ()
repoUI repos' 
    | not (null repos')  = vBox . fmap renderRepo . sort $ repos'
    | otherwise = txt "no repos" 
    where
        renderRepo repo =  markup (repoTxt repo @? colorRepo repo)

        repoTxt :: Repo -> T.Text
        repoTxt repo = T.concat 
            [ fromMaybe "-" (repo ^. slug)
            , " . " 
            , T.pack . show . fromMaybe Unknown $ repo ^. lastBuildState]

timestampTxt :: UTCTime -> T.Text
timestampTxt ts = T.pack $  formatTime defaultTimeLocale "%H:%m:%S" ts 

{'fraction_non_alphanumeric': 0.11745447104563127, 'fraction_numerical': 0.0004092490280335584, 'mean_word_length': 2.2761394101876675, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '172097', 'n_tokens_mistral': 1600, 'n_tokens_neox': 1561, 'n_words': 431}