From 7aa06f6762b3db992d5968aca89b3a786733e61a Mon Sep 17 00:00:00 2001 From: Dponya Date: Fri, 14 Oct 2022 17:07:07 +0600 Subject: [PATCH 1/3] add putStdoutColoured & putStderrColoured --- src/Iris/Colour/Formatting.hs | 58 +++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/src/Iris/Colour/Formatting.hs b/src/Iris/Colour/Formatting.hs index 7a26fde..7d9364d 100644 --- a/src/Iris/Colour/Formatting.hs +++ b/src/Iris/Colour/Formatting.hs @@ -16,17 +16,21 @@ Helper functions to print with colouring. module Iris.Colour.Formatting ( putStdoutColouredLn , putStderrColouredLn + , putStdoutColoured + , putStderrColoured ) where import Control.Monad.IO.Class (MonadIO (..)) import Control.Monad.Reader (MonadReader) import Data.ByteString (ByteString) +import Data.Text (Text) import System.IO (stderr) import Iris.Colour.Mode (ColourMode (..)) import Iris.Env (CliEnv (..), asksCliEnv) import qualified Data.ByteString.Char8 as BS8 +import qualified Data.Text.IO as TIO {- | Print 'ByteString' to 'System.IO.stdout' by providing a custom @@ -80,3 +84,57 @@ putStderrColouredLn formatWithColour str = do liftIO $ BS8.hPutStrLn stderr $ case colourMode of DisableColour -> str EnableColour -> formatWithColour str + +{- | Print 'Text' to 'System.IO.stdout' by providing a custom +formatting function. Doesn't breaks output line that differs from +`putStdoutColouredLn` + +This works especially well with the @colourista@ package: + +@ +'putStdoutColoured' + (Colourista.formatWith [Colourista.bold, Colourista.green]) + "my message" +@ + +@since 0.0.0.0 +-} +putStdoutColoured + :: ( MonadReader (CliEnv cmd appEnv) m + , MonadIO m + ) + => (Text -> Text) + -> Text + -> m () +putStdoutColoured formatWithColour str = do + colourMode <- asksCliEnv cliEnvStdoutColourMode + liftIO $ TIO.putStr $ case colourMode of + DisableColour -> str + EnableColour -> formatWithColour str + +{- | Print 'Text' to 'System.IO.stderr' by providing a custom +formatting function. Doesn't breaks output line that differs from +`putStderrColouredLn` + +This works especially well with the @colourista@ package: + +@ +'putStderrColoured' + (Colourista.formatWith [Colourista.bold, Colourista.green]) + "my message" +@ + +@since 0.0.0.0 +-} +putStderrColoured + :: ( MonadReader (CliEnv cmd appEnv) m + , MonadIO m + ) + => (Text -> Text) + -> Text + -> m () +putStderrColoured formatWithColour str = do + colourMode <- asksCliEnv cliEnvStderrColourMode + liftIO $ TIO.hPutStr stderr $ case colourMode of + DisableColour -> str + EnableColour -> formatWithColour str From b4350cb783c9b5fc0f12c6245a9f6606aa2c8d36 Mon Sep 17 00:00:00 2001 From: Dponya Date: Sat, 15 Oct 2022 20:11:18 +0600 Subject: [PATCH 2/3] fix version in docs --- src/Iris/Colour/Formatting.hs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Iris/Colour/Formatting.hs b/src/Iris/Colour/Formatting.hs index 7d9364d..3d5e109 100644 --- a/src/Iris/Colour/Formatting.hs +++ b/src/Iris/Colour/Formatting.hs @@ -97,7 +97,7 @@ This works especially well with the @colourista@ package: "my message" @ -@since 0.0.0.0 +@since x.x.x.x -} putStdoutColoured :: ( MonadReader (CliEnv cmd appEnv) m @@ -124,7 +124,7 @@ This works especially well with the @colourista@ package: "my message" @ -@since 0.0.0.0 +@since x.x.x.x -} putStderrColoured :: ( MonadReader (CliEnv cmd appEnv) m From 1a65f251ca05c6948dfe606eb6eca02ceee031da Mon Sep 17 00:00:00 2001 From: Dponya Date: Sat, 15 Oct 2022 20:11:42 +0600 Subject: [PATCH 3/3] Add Changelog & update simple-grep --- CHANGELOG.md | 5 ++ examples/simple-grep/README.md | 101 ++++++++++++--------------------- 2 files changed, 42 insertions(+), 64 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8c1f237..15f631b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,11 @@ available [on GitHub][2]. ## [Unreleased] +- [#16](https://github.com/chshersh/iris/issues/16) + Update documentation, Add new example written in Literate Haskell & tutorial-style +- [#89](https://github.com/chshersh/iris/issues/89) + Add `putStderrColoured` and `putStdoutColoured` functions for putting string + without output line breaking ## [0.0.0.0] — 2022-08-09 🌇 diff --git a/examples/simple-grep/README.md b/examples/simple-grep/README.md index 0f22a4c..4bee4af 100644 --- a/examples/simple-grep/README.md +++ b/examples/simple-grep/README.md @@ -15,35 +15,21 @@ And will give the following result output: Starting grepping 🔥 - file name: iris.cabal - 2: -name: iris - 7: - See [README.md](https://github.com/chshersh/iris#iris) for more details. - 8: -homepage: https://github.com/chshersh/iris - 9: -bug-reports: https://github.com/chshersh/iris/issues - 26: - location: https://github.com/chshersh/iris.git - 79: - build-depends: , iris - 119: - autogen-modules: Paths_iris - 120: - other-modules: Paths_iris - 123: - , iris - 136: - autogen-modules: Paths_iris - 137: - other-modules: Paths_iris - 150: -test-suite iris-test - 160: - Paths_iris - 164: - , iris +file name: iris.cabal +2:name: iris +7: See [README.md](https://github.com/chshersh/iris#iris) for more details. +8:homepage: https://github.com/chshersh/iris +9:bug-reports: https://github.com/chshersh/iris/issues +26: location: https://github.com/chshersh/iris.git +79: build-depends: , iris +119: autogen-modules: Paths_iris +120: other-modules: Paths_iris +123: , iris +136: autogen-modules: Paths_iris +137: other-modules: Paths_iris +150:test-suite iris-test +160: Paths_iris +164: , iris ``` So, let's begin! @@ -67,6 +53,7 @@ import qualified Data.ByteString.Lazy as BSL import qualified Data.ByteString as BS import qualified Data.Text.Lazy as T import qualified Data.Text.Lazy.Encoding as TLE +import qualified Data.Text as TS import qualified Options.Applicative as Opt import qualified Colourista @@ -219,15 +206,15 @@ occurencesPrinter = mapM_ unpack printLine line printIdxWithColon :: Int -> App () - printIdxWithColon idx = Iris.putStderrColouredLn + printIdxWithColon idx = Iris.putStderrColoured (Colourista.formatWith [Colourista.yellow, Colourista.bold]) - $ BSL.toStrict $ (TLE.encodeUtf8 $ T.pack $ " " `mappend` show idx) + $ TS.pack $ " " `mappend` show idx `mappend` - (TLE.encodeUtf8 $ T.pack ":") + ":" printLine :: T.Text -> App () - printLine x = Iris.putStdoutColouredLn - (Colourista.formatWith [BS.empty]) - $ BSL.toStrict $ TLE.encodeUtf8 x + printLine x = Iris.putStdoutColoured + (Colourista.formatWith [TS.empty]) + $ T.toStrict $ x `mappend` "\n" ``` So, we'd wish to execute all that stuff. Let's do it! @@ -241,33 +228,19 @@ And output with occurences of "iris": ``` Starting grepping 🔥 - file name: iris.cabal - 2: -name: iris - 7: - See [README.md](https://github.com/chshersh/iris#iris) for more details. - 8: -homepage: https://github.com/chshersh/iris - 9: -bug-reports: https://github.com/chshersh/iris/issues - 26: - location: https://github.com/chshersh/iris.git - 79: - build-depends: , iris - 119: - autogen-modules: Paths_iris - 120: - other-modules: Paths_iris - 123: - , iris - 136: - autogen-modules: Paths_iris - 137: - other-modules: Paths_iris - 150: -test-suite iris-test - 160: - Paths_iris - 164: - , iris +file name: iris.cabal +2:name: iris +7: See [README.md](https://github.com/chshersh/iris#iris) for more details. +8:homepage: https://github.com/chshersh/iris +9:bug-reports: https://github.com/chshersh/iris/issues +26: location: https://github.com/chshersh/iris.git +79: build-depends: , iris +119: autogen-modules: Paths_iris +120: other-modules: Paths_iris +123: , iris +136: autogen-modules: Paths_iris +137: other-modules: Paths_iris +150:test-suite iris-test +160: Paths_iris +164: , iris ``` \ No newline at end of file