{-# LANGUAGE OverloadedStrings #-}

module Data.Text.Extras(
      abbreviate
    , tshow
    ) where

import Data.Text qualified as T

tshow :: Show a => a -> T.Text
tshow :: a -> Text
tshow = String -> Text
T.pack (String -> Text) -> (a -> String) -> a -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> String
forall a. Show a => a -> String
show

abbreviate :: Int -> T.Text -> T.Text
abbreviate :: Int -> Text -> Text
abbreviate Int
n Text
txt
    | Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
0 = Text
""
    | Text -> Int
T.length Text
txt Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
n = Int -> Text -> Text
T.take (Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) Text
txt Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"…"
    | Bool
otherwise = Text
txt