Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#51] Ability to print message with any rgb color #53

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions colourista.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ source-repository head
common common-options
build-depends: base >= 4.10.1.0 && < 4.16


ghc-options: -Wall
-Wcompat
-Widentities
Expand Down Expand Up @@ -73,6 +74,7 @@ library

build-depends: ansi-terminal >= 0.10 && < 0.12
, bytestring >= 0.10 && < 0.12
, colour >=2.1.0
, ghc-prim >= 0.5 && < 0.8
, text ^>= 1.2.3.0

Expand Down
10 changes: 10 additions & 0 deletions src/Colourista/IO.hs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ module Colourista.IO
, whiteMessage
, magentaMessage
, cyanMessage
, rgbMessage
-- ** Aliases with unicode indicators
, successMessage
, infoMessage
Expand All @@ -37,6 +38,10 @@ import Data.Semigroup (Semigroup (..))
#endif
import Data.Text (Text)

import Data.Word (Word8)
import Colourista.Mode (HasColourMode)


import qualified Data.Text.IO as TIO

import qualified Colourista.Pure as Colourista
Expand All @@ -45,6 +50,11 @@ import qualified Colourista.Pure as Colourista
-- Direct IO functions
----------------------------------------------------------------------------

-- | Print 'Text' coloured in specified RGB notaion
rgbMessage :: Word8 -> Word8 -> Word8 -> Text -> IO ()
rgbMessage red green blue = formattedMessage [ resColor ]
where resColor = Colourista.rgb red green blue

-- | Print 'Text' coloured in 'Colourista.red'.
redMessage :: Text -> IO ()
redMessage = formattedMessage [Colourista.red]
Expand Down
14 changes: 14 additions & 0 deletions src/Colourista/Pure.hs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ module Colourista.Pure
, white
, magenta
, cyan
, rgb

-- * Background
, redBg
Expand All @@ -41,14 +42,20 @@ module Colourista.Pure
) where

import Data.ByteString (ByteString)
import Data.Foldable (foldl')
import Data.Int (Int8)
import Data.List.NonEmpty (NonEmpty (..))
import Data.Semigroup (Semigroup (..))
import Data.String (IsString (..))
import Data.Text (Text)
import Data.Word (Word8)
import System.Console.ANSI (Color (..), ColorIntensity (Vivid), ConsoleIntensity (BoldIntensity),
ConsoleLayer (Background, Foreground), SGR (..), Underlining (..),
setSGRCode)

import Data.Colour.SRGB (sRGB24)
import Colourista.Mode (HasColourMode, withColourMode)


{- | General purpose function to format strings with multiple
options. If this function takes empty list as an argument, no
Expand Down Expand Up @@ -139,6 +146,13 @@ cyan = fromString $ setSGRCode [SetColor Foreground Vivid Cyan]
{-# SPECIALIZE cyan :: Text #-}
{-# SPECIALIZE cyan :: ByteString #-}

-- | Code to apply any arbitrary hex color for the terminal output.
dariodsa marked this conversation as resolved.
Show resolved Hide resolved
--
-- >>> rgb 0xff 0x00 0x00 "I am red"
-- >>> rgb 0xff 0xff 0xff "I am white"
rgb :: (HasColourMode, IsString str) => Word8 -> Word8 -> Word8 -> str
rgb r g b = withColourMode $ fromString $ setSGRCode [SetRGBColor Foreground (sRGB24 r g b)]

----------------------------------------------------------------------------
-- Background
----------------------------------------------------------------------------
Expand Down