Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
EarthCitizen committed Jan 26, 2020
2 parents d322ce8 + 8d767a6 commit db35150
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 16 deletions.
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ Name | Description
Symbol | Purpose
------ | -------
`^$` | Same as `$`, but one level of precedence higher than `<>` for avoiding the use of parentheses when needing to use `$` in the same expression as `<>`. See examples below.
`/<>/` | The same as `<>`, except that any argument that is not of type `Escapable` will be wrapped in `Inherit` before being combined with the other argument via `<>`.

## Examples

Expand Down Expand Up @@ -242,7 +243,7 @@ putEscLn underlines

The same type of functionality applies as well to `BlinkOff`, `BrightOff` and `InverseOff`.

### Operator ^$
### Operator `^$`

This operator allows you to avoid parentheses in cases where you need to use `$` and `<>` in he same expression.

Expand All @@ -263,6 +264,19 @@ Without `^$`, this would have to be written as:
Underline $ (Bright $ FgGreen "GREEN") <> Default " " <> FgYellow "YELLOW"
```

### Operator `/<>/`

This operator allows `Inherit` to be omitted.

```haskell
BgRed $ Inherit 4 <> BgCyan " " <> Inherit 5 <> BgGreen " " <> Inherit 9
```
can simply be written as:

```haskell
BgRed $ 4 /<>/ BgCyan " " /<>/ 5 /<>/ BgGreen " " /<>/ 9
```

## Advanced Examples

### Fun with Colors
Expand Down
7 changes: 2 additions & 5 deletions escape-artist.cabal
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: escape-artist
version: 1.2.0
version: 1.2.0.0
synopsis: ANSI Escape Sequence Text Decoration Made Easy
description:
A library for text decoration with ANSI escape sequences made easy. Decorate your terminal text expressively.
Expand Down Expand Up @@ -36,9 +36,6 @@ description:
&#x20; toEscapable a = FgRed $ show a
@
.
NOTE: For GHC < 7.10 you will also need to explicitly derive 'Typeable' for custom data types
implementing 'ToEscapable'. See the section Explicitly Derived Typeable in the documentation.
.
Comprehensive Documentation
.
See comprehensive documentation with many examples here:
Expand Down Expand Up @@ -66,7 +63,7 @@ library
, text >= 1.2.0.4 && < 1.3
if impl(ghc < 7.10.0)
build-depends: unsupported-ghc-version > 1 && < 1
ghc-options: -Wall
ghc-options: -Wall -Wno-type-defaults
default-language: Haskell2010

test-suite escape-artist-spec-test
Expand Down
6 changes: 1 addition & 5 deletions src/Text/EscapeArtist.hs
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,11 @@ instance (ToEscapable a) => ToEscapable (Maybe a) where
See the documentation on 'ToEscapable' below for a more advanced example.
For GHC < 7.10 you will also need to explicitly derive 'Data.Typeable.Typeable' for custom data types
implementing 'ToEscapable'. See the section __/Explicitly Derived Typeable/__ in the
<https://github.com/EarthCitizen/escape-artist#explicitly-derived-typeable documentation>.
Comprehensive documentation with many examples here:
<https://github.com/EarthCitizen/escape-artist#readme>
-}

module Text.EscapeArtist (Escapable(..), ToEscapable(..), putEscLn, putEsc, escToString, (^$)) where
module Text.EscapeArtist (Escapable(..), ToEscapable(..), putEscLn, putEsc, escToString, (^$), (/<>/)) where

import Text.EscapeArtist.Internal hiding (Atom, Sum)
31 changes: 26 additions & 5 deletions src/Text/EscapeArtist/Internal.hs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{-# OPTIONS_HADDOCK hide #-}

module Text.EscapeArtist.Internal (Escapable(..), ToEscapable(..), putEscLn, putEsc, escToString, (^$)) where
module Text.EscapeArtist.Internal (Escapable(..), ToEscapable(..), putEscLn, putEsc, escToString, (^$), (/<>/)) where

import qualified Data.ByteString as BS
import qualified Data.ByteString.Char8 as BSC
Expand Down Expand Up @@ -201,10 +201,6 @@ putEscLn $ mkSyntaxError "some/File.hs" 1 23
putEscLn mkStatusOK
@
/Note:/ For GHC < 7.10 you will also need to explicitly derive 'Data.Typeable.Typeable' for custom data types
implementing 'ToEscapable'. See the section __/Explicitly Derived Typeable/__ in the
<https://github.com/EarthCitizen/escape-artist#explicitly-derived-typeable documentation>.
<<https://raw.githubusercontent.com/EarthCitizen/escape-artist/master/images/either_error.png>>
-}
class (Show a, Typeable a, Eq a) => ToEscapable a where
Expand Down Expand Up @@ -335,6 +331,31 @@ instance Monoid Escapable where
mappend a b = Sum [a, b]
#endif

defInherit :: forall a. (ToEscapable a, Typeable a) => a -> Escapable
defInherit a = case cast a of
Nothing -> Inherit a
Just a' -> a'

infixr 6 /<>/

-- | The same as '<>', except that any argument that is not of type 'Escapable' will be wrapped in 'Inherit'
-- before being combined with the other argument via '<>'. For example:
--
-- @
-- BgRed $ Inherit 4 <> BgCyan " " <> Inherit 5 <> BgGreen " " <> Inherit 9
-- @
--
-- can simply be written as:
--
-- @
-- BgRed $ 4 \/<>\/ BgCyan " " \/<>\/ 5 \/<>\/ BgGreen " " \/<>\/ 9
-- @
--
-- In this example, 'Inherit' can be omitted.
--
(/<>/) :: forall a b. (ToEscapable a, ToEscapable b) => a -> b -> Escapable
(/<>/) a b = defInherit a <> defInherit b

-- | Convert any instance of 'ToEscapable' to a 'String' and output it to the terminal followed by a newline
putEscLn :: (ToEscapable a) => a -> IO ()
putEscLn = putStrLn . escToString
Expand Down
6 changes: 6 additions & 0 deletions test/Text/EscapeArtistSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ spec = do
it "gets processed before <>" $ do
(FgRed ^$ Underline 5 <> FgBlue 3 <> FgYellow 9) `shouldBe` (Sum [FgRed (Underline 5), FgBlue 3, FgYellow 9])

describe "/<>/" $ do
it "produces the same result as <>" $ do
(FgRed "A" /<>/ FgBlue 10) `shouldBe` (FgRed "A" <> FgBlue 10)
it "wraps non-Escapable values in Inherit" $ do
(5 /<>/ FgRed "A" /<>/ 'C') `shouldBe` (Sum [Inherit 5, FgRed "A", Inherit 'C'])

describe "Eq Escapable" $ do
it "considers escapables equal when constructors and contained values are same" $ forM_ eqTestCases $ do
\(a, b) -> a `shouldBe` b
Expand Down

0 comments on commit db35150

Please sign in to comment.