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

[#89] Add 'putStdoutColoured' and 'putStderrColoured' #92

Merged
Merged
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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 🌇

Expand Down
101 changes: 37 additions & 64 deletions examples/simple-grep/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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!
Expand All @@ -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
Expand Down Expand Up @@ -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"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There I used putStdoutColoured without Ln for stable purposes. When the API of putStdoutColouredLn will be changed from ByteString to Text, there could be a need to change simple-grep too. So, I just used \n

```

So, we'd wish to execute all that stuff. Let's do it!
Expand All @@ -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
```
58 changes: 58 additions & 0 deletions src/Iris/Colour/Formatting.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 x.x.x.x
-}
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 x.x.x.x
-}
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