-- | This module handles the complexities of writing information to the
-- terminal, including modifying text in place.

module Test.HUnit.Terminal (
        terminalAppearance
    ) where

import Data.Char (isPrint)


-- | Simplifies the input string by interpreting @\\r@ and @\\b@ characters
-- specially so that the result string has the same final (or /terminal/,
-- pun intended) appearance as would the input string when written to a
-- terminal that overwrites character positions following carriage
-- returns and backspaces.

terminalAppearance :: String -> String
terminalAppearance :: String -> String
terminalAppearance String
str = forall t. (String -> t) -> String -> String -> String -> t
ta forall a. a -> a
id String
"" String
"" String
str

-- | The helper function @ta@ takes an accumulating @ShowS@-style function
-- that holds /committed/ lines of text, a (reversed) list of characters
-- on the current line /before/ the cursor, a (normal) list of characters
-- on the current line /after/ the cursor, and the remaining input.

ta
    :: ([Char] -> t) -- ^ An accumulating @ShowS@-style function
                     -- that holds /committed/ lines of text
    -> [Char] -- ^ A (reversed) list of characters
              -- on the current line /before/ the cursor
    -> [Char] -- ^ A (normal) list of characters
              -- on the current line /after/ the cursor
    -> [Char] -- ^ The remaining input
    -> t
ta :: forall t. (String -> t) -> String -> String -> String -> t
ta String -> t
f    String
bs  String
as (Char
'\n':String
cs) = forall t. (String -> t) -> String -> String -> String -> t
ta (\String
t -> String -> t
f (forall a. [a] -> [a]
reverse String
bs forall a. [a] -> [a] -> [a]
++ String
as forall a. [a] -> [a] -> [a]
++ Char
'\n' forall a. a -> [a] -> [a]
: String
t)) String
"" String
"" String
cs
ta String -> t
f    String
bs  String
as (Char
'\r':String
cs) = forall t. (String -> t) -> String -> String -> String -> t
ta String -> t
f String
"" (forall a. [a] -> [a]
reverse String
bs forall a. [a] -> [a] -> [a]
++ String
as) String
cs
ta String -> t
f (Char
b:String
bs) String
as (Char
'\b':String
cs) = forall t. (String -> t) -> String -> String -> String -> t
ta String -> t
f String
bs (Char
bforall a. a -> [a] -> [a]
:String
as) String
cs
ta String -> t
_    String
""   String
_ (Char
'\b': String
_) = forall a. HasCallStack => String -> a
error String
"'\\b' at beginning of line"
ta String -> t
f    String
bs  String
as (Char
c:String
cs)
    | Bool -> Bool
not (Char -> Bool
isPrint Char
c)    = forall a. HasCallStack => String -> a
error String
"invalid nonprinting character"
    | forall (t :: * -> *) a. Foldable t => t a -> Bool
null String
as            = forall t. (String -> t) -> String -> String -> String -> t
ta String -> t
f (Char
cforall a. a -> [a] -> [a]
:String
bs) String
""        String
cs
    | Bool
otherwise          = forall t. (String -> t) -> String -> String -> String -> t
ta String -> t
f (Char
cforall a. a -> [a] -> [a]
:String
bs) (forall a. [a] -> [a]
tail String
as) String
cs
ta String -> t
f    String
bs  String
as       String
""  = String -> t
f (forall a. [a] -> [a]
reverse String
bs forall a. [a] -> [a] -> [a]
++ String
as)