module Hydra.TUI.Drawing.Utils where

import Brick (Widget, emptyWidget, txt)
import Data.Text qualified as Text
import Hydra.Cardano.Api (SerialiseAsRawBytes, serialiseToRawBytesHexText)
import Hydra.Prelude

drawHex :: SerialiseAsRawBytes a => a -> Widget n
drawHex :: forall a n. SerialiseAsRawBytes a => a -> Widget n
drawHex = Text -> Widget n
forall n. Text -> Widget n
txt (Text -> Widget n) -> (a -> Text) -> a -> Widget n
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Text
" - " <>) (Text -> Text) -> (a -> Text) -> a -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Text
forall a. SerialiseAsRawBytes a => a -> Text
serialiseToRawBytesHexText

drawShow :: forall a n. Show a => a -> Widget n
drawShow :: forall a n. Show a => a -> Widget n
drawShow = Text -> Widget n
forall n. Text -> Widget n
txt (Text -> Widget n) -> (a -> Text) -> a -> Widget n
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Text
" - " <>) (Text -> Text) -> (a -> Text) -> a -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Text
forall b a. (Show a, IsString b) => a -> b
show

maybeWidget :: (a -> Widget n) -> Maybe a -> Widget n
maybeWidget :: forall a n. (a -> Widget n) -> Maybe a -> Widget n
maybeWidget = Widget n -> (a -> Widget n) -> Maybe a -> Widget n
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Widget n
forall n. Widget n
emptyWidget

ellipsize :: Int -> Text -> Text
ellipsize :: Int -> Text -> Text
ellipsize Int
n Text
t = Int -> Text -> Text
Text.take (Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
2) Text
t Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
".."