summaryrefslogtreecommitdiffstats
path: root/src/Blessings.hs
diff options
context:
space:
mode:
authorKierán Meinhardt <kieran.meinhardt@gmail.com>2019-01-29 23:03:22 +0100
committerKierán Meinhardt <kieran.meinhardt@gmail.com>2019-02-05 20:52:24 +0100
commitfa87fc922cd4c34b46baa32be762dda495ef6a2b (patch)
treebcd0600a8ba714278a74d210232f2363475d2d06 /src/Blessings.hs
parente75d0cf94582a5aa6dde781b8428ffff45cf7e76 (diff)
Blessings: add test
~ change SGR type to Word8 + add hspec with QuickCheck
Diffstat (limited to 'src/Blessings.hs')
-rw-r--r--src/Blessings.hs26
1 files changed, 14 insertions, 12 deletions
diff --git a/src/Blessings.hs b/src/Blessings.hs
index 302b8bc..26a0666 100644
--- a/src/Blessings.hs
+++ b/src/Blessings.hs
@@ -7,15 +7,17 @@ module Blessings
, module Blessings
) where
-import qualified Prelude
import Blessings.Internal as Export (Blessable)
import qualified Blessings.Internal as Bless
-import Prelude hiding (length,drop,take)
import Control.Applicative
-import Data.String
import Data.Ix (inRange)
+import Data.List (genericDrop)
+import Data.String
+import Data.Word (Word8)
+import qualified Prelude
+import Prelude hiding (drop, length, take)
-type Ps = Int
+type Ps = Word8
type Pm = [Ps]
data Blessings a
@@ -114,7 +116,7 @@ instance IsPm Blink where
toPm NoBlink = [25]
fromPm = rec . filterPm sgrColor
where
- rec xs = case filter (`elem` ([5,25] :: [Int])) xs of
+ rec xs = case filter (`elem` ([5,25] :: [Word8])) xs of
[] -> Nothing
xs' -> case last xs' of
5 -> Just Blink
@@ -130,7 +132,7 @@ instance IsPm Bold where
toPm NoBold = [22]
fromPm = rec . filterPm sgrColor
where
- rec xs = case filter (`elem` ([1,22] :: [Int])) xs of
+ rec xs = case filter (`elem` ([1,22] :: [Word8])) xs of
[] -> Nothing
xs' -> case last xs' of
1 -> Just Bold
@@ -146,7 +148,7 @@ instance IsPm Underline where
toPm NoUnderline = [24]
fromPm = rec . filterPm sgrColor
where
- rec xs = case filter (`elem` ([4,24] :: [Int])) xs of
+ rec xs = case filter (`elem` ([4,24] :: [Word8])) xs of
[] -> Nothing
xs' -> case last xs' of
1 -> Just Underline
@@ -184,15 +186,15 @@ fromSGRPm SGRPm{..} = rec Nothing
-- that look like the (shorter) sequences we're searching.
-- E.g. we could find [1] (bold) in any extended color sequence.
-- TODO Can we combine this whole from*Pm with Scanner?
-filterPm :: (Pm -> Maybe Int) -> Pm -> Pm
+filterPm :: (Pm -> Maybe Word8) -> Pm -> Pm
filterPm f = rec []
where
rec ys xs@(xhead:xtail) = maybe (rec (ys ++ [xhead]) xtail)
- (rec ys . flip Prelude.drop xs)
+ (rec ys . flip genericDrop xs)
(f xs)
rec ys _ = ys
-sgrColor, sgrFColor, sgrBColor :: Pm -> Maybe Int
+sgrColor, sgrFColor, sgrBColor :: Pm -> Maybe Word8
sgrColor xs = sgrFColor xs <|> sgrBColor xs
@@ -248,7 +250,7 @@ render _ Empty y = y
renderSGR :: (Blessable a) => Pm -> a
renderSGR [] = mempty
renderSGR pm =
- ("\ESC["<>) . (<>"m") . Bless.intercalate ";" . map Bless.fromInt $ pm
+ ("\ESC["<>) . (<>"m") . Bless.intercalate ";" . map Bless.fromWord8 $ pm
stripSGR :: Blessings a -> Blessings a
@@ -300,4 +302,4 @@ instance Blessable a => Blessable (Blessings a) where
[t] -> t
(t:ts) -> t <> i <> Bless.intercalate i ts
- fromInt = Plain . Bless.fromInt
+ fromWord8 = Plain . Bless.fromWord8