{-# LANGUAGE OverloadedStrings #-}
module Cursor.Brick.Text where
import qualified Data.Text as T
import Data.Text (Text)
import Cursor.Text
import Brick.Types as Brick
import Brick.Widgets.Core as Brick
selectedTextCursorWidget :: n -> TextCursor -> Widget n
selectedTextCursorWidget n tc =
Brick.showCursor n (Brick.Location (textCursorIndex tc, 0)) $
textCursorWidget tc
textCursorWidget :: TextCursor -> Widget n
textCursorWidget tc =
txt $
let t = sanitiseText $ rebuildTextCursor tc
in if T.null t
then " "
else t
textWidget :: Text -> Widget n
textWidget = txt . nonNullLinesText . sanitiseText
textWidgetWrap :: Text -> Widget n
textWidgetWrap = txtWrap . nonNullLinesText . sanitiseText
textLineWidget :: Text -> Widget n
textLineWidget = txt . nonNullText . sanitiseText
textLineWidgetWrap :: Text -> Widget n
textLineWidgetWrap = txtWrap . nonNullText . sanitiseText
nonNullLinesText :: Text -> Text
nonNullLinesText = T.intercalate "\n" . map nonNullText . T.splitOn "\n"
nonNullText :: Text -> Text
nonNullText "" = " "
nonNullText t = t
sanitiseText :: Text -> Text
sanitiseText =
T.map $ \c ->
case c of
'\t' -> ' '
_ -> c