Skip to content

Commit

Permalink
Re #149 Add underlining colours and styles
Browse files Browse the repository at this point in the history
  • Loading branch information
mpilgrem committed Feb 7, 2024
1 parent f3fb942 commit c70e339
Show file tree
Hide file tree
Showing 17 changed files with 97 additions and 42 deletions.
12 changes: 6 additions & 6 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,20 @@ jobs:
- "9.2.8"
- "9.4.8"
include:
- ghc: "9.6.3"
- ghc: "9.6.4"
cabal: "3.10"
- ghc: "9.8.1"
cabal: "3.10"
steps:
- uses: actions/checkout@v4
- uses: haskell/actions/setup@v2
- uses: haskell-actions/setup@v2
id: setup-haskell-cabal
with:
ghc-version: ${{ matrix.ghc }}
cabal-version: ${{ matrix.cabal }}
- run: cabal v2-update
- run: cabal v2-freeze
- uses: actions/cache@v2
- uses: actions/cache@v4
with:
path: |
${{ steps.setup-haskell-cabal.outputs.cabal-store }}
Expand Down Expand Up @@ -74,22 +74,22 @@ jobs:
- ghc: "9.4.8"
cabal: "3.8"
winio: true
- ghc: "9.6.3"
- ghc: "9.6.4"
cabal: "3.10"
winio: true
- ghc: "9.8.1"
cabal: "3.10"
winio: true
steps:
- uses: actions/checkout@v4
- uses: haskell/actions/setup@v2
- uses: haskell-actions/setup@v2
id: setup-haskell-cabal
with:
ghc-version: ${{ matrix.ghc }}
cabal-version: ${{ matrix.cabal }}
- run: cabal v2-update
- run: cabal v2-freeze
- uses: actions/cache@v2
- uses: actions/cache@v4
with:
path: |
${{ steps.setup-haskell-cabal.outputs.cabal-store }}
Expand Down
2 changes: 1 addition & 1 deletion Contributing.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Many other packages depend on `ansi-terminal`. As at January 2024, over 4,700
Many other packages depend on `ansi-terminal`. As at February 2024, over 4,700
packages on [Hackage](https:https://hackage.haskell.org/) depend on it, directly
or indirectly. Consider raising an
[issue](https://github.com/UnkindPartition/ansi-terminal/issues) to
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ Description

['ANSI' terminal escape code](http://en.wikipedia.org/wiki/ANSI_escape_sequences)
support for Haskell, which allows:
- Colored text output, with control over both foreground and background
colors
- Colored text output, with control over foreground, background and (where
supported) underlining colors
- Clearing parts of a line or the screen
- Hiding or showing the cursor
- Moving the cursor around
Expand Down
7 changes: 7 additions & 0 deletions ansi-terminal-types/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
Changes
=======

Version 1.1
--------------

* Add `Underlining` constructor to type `ConsoleLayer`.
* Add `CurlyUnderline`, `DottedUnderline`, and `DashedUnderline` constructors to
type `Underlining`.

Version 0.11.5
--------------

Expand Down
2 changes: 1 addition & 1 deletion ansi-terminal-types/ansi-terminal-types.cabal
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Cabal-Version: 1.22
Name: ansi-terminal-types
Version: 0.11.5
Version: 1.1
Category: User Interfaces
Synopsis: Types and functions used to represent SGR aspects
Description: The \'ANSI\' standards refer to the visual style of
Expand Down
10 changes: 9 additions & 1 deletion ansi-terminal-types/src/System/Console/ANSI/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ data ColorIntensity
data ConsoleLayer
= Foreground
| Background
| Underlining
-- ^ Not widely supported.
deriving (Bounded, Eq, Enum, Ix, Ord, Read, Show)

-- | ANSI blink speeds: values other than 'NoBlink' are not widely supported
Expand All @@ -70,8 +72,14 @@ data BlinkSpeed
-- | ANSI text underlining
data Underlining
= SingleUnderline
-- | Not widely supported. Not supported natively on Windows 10
| DoubleUnderline
-- ^ Not widely supported.
| CurlyUnderline
-- ^ Not widely supported.
| DottedUnderline
-- ^ Not widely supported.
| DashedUnderline
-- ^ Not widely supported.
| NoUnderline
deriving (Bounded, Eq, Enum, Ix, Ord, Read, Show)

Expand Down
2 changes: 1 addition & 1 deletion ansi-terminal-types/stack.yaml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
resolver: lts-22.3 # GHC 9.6.3
resolver: lts-22.9 # GHC 9.6.4
7 changes: 4 additions & 3 deletions ansi-terminal/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
Changes
=======

Version 1.0.3
-------------
Version 1.1
-----------

* Add support for console layer `Underlining` and underlining styles
`CurlyUnderline`, `DottedUnderline` and `DashedUnderline`.
* Add type synonyms `Parameter`, `SubParam`, and `ParamWithSubs` to represent
SGR parameter values with and without following parameter substrings comprised
of one or more parameter elements (including empty elements).
Expand Down Expand Up @@ -31,7 +33,6 @@ Version 1.0.1
* Deprecated `hSupportsANSIWithoutEmulation` is now consistent with
`hNowSupportsANSI`.
* Improvements to Haddock documentation.

Version 1.0
-----------

Expand Down
37 changes: 20 additions & 17 deletions ansi-terminal/LICENSE
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
Copyright (c) 2008, Maximilian Bolingbroke
All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted
provided that the following conditions are met:
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this list of
conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this list of
conditions and the following disclaimer in the documentation and/or other materials
provided with the distribution.
* Neither the name of Maximilian Bolingbroke nor the names of other contributors may be used to
endorse or promote products derived from this software without specific prior written permission.
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this
list of conditions and the following disclaimer in the documentation and/or
other materials provided with the distribution.
* Neither the name of Maximilian Bolingbroke nor the names of other contributors
may be used to endorse or promote products derived from this software without
specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
4 changes: 2 additions & 2 deletions ansi-terminal/ansi-terminal.cabal
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Cabal-Version: 1.22
Name: ansi-terminal
Version: 1.0.3
Version: 1.1
Category: User Interfaces
Synopsis: Simple ANSI terminal support
Description: ANSI terminal support for Haskell: allows cursor movement,
Expand Down Expand Up @@ -39,7 +39,7 @@ Library
Other-Modules: System.Console.ANSI.Internal

Build-Depends: base >= 4.8.0.0 && < 5
, ansi-terminal-types == 0.11.5
, ansi-terminal-types == 1.1
, colour >= 2.1.0
if os(windows)
Hs-Source-Dirs: win
Expand Down
27 changes: 25 additions & 2 deletions ansi-terminal/app/Example.hs
Original file line number Diff line number Diff line change
Expand Up @@ -243,15 +243,16 @@ screenBuffersExample = do
sgrColorExample :: IO ()
sgrColorExample = do
let colors = enumFromTo minBound maxBound :: [Color]
forM_ [Foreground, Background] $ \layer -> do
forM_ [Foreground, Background, Underlining] $ \layer -> do
forM_ [Dull, Vivid] $ \intensity -> do
resetScreen
forM_ colors $ \color -> do
setSGR [Reset]
setSGR [SetColor layer intensity color]
setSGR [SetUnderlining SingleUnderline]
print color
pause
-- The ANSI eight standard colors, 4 times in sequence (two layers and two
-- The ANSI eight standard colors, 6 times in sequence (three layers and two
-- intensities)

resetScreen
Expand Down Expand Up @@ -333,6 +334,25 @@ sgrColorExample = do
putStr " "
replicateM_ 5 pause

resetScreen
setSGR [Reset]
setSGR [SetUnderlining SingleUnderline]
forM_
(zip "Underlining: True color (24 bit color depth)" (cycle [0, 10 .. 255])) $
\(c, i) -> do
setSGR [SetRGBColor Underlining $ sRGB24 i 0 0]
putChar c
putChar '\n'
putChar '\n'
setSGR [Reset]
setSGR [SetUnderlining SingleUnderline]
forM_ (zip "Underlining: A 256-color palette" (cycle [0 .. 5])) $
\(c, i) -> do
setSGR [SetPaletteColor Underlining $ xterm6LevelRGB i 0 0]
putChar c
putChar '\n'
replicateM_ 5 pause

sgrOtherExample :: IO ()
sgrOtherExample = do
let named_styles = [ (SetConsoleIntensity BoldIntensity, "Bold")
Expand All @@ -342,6 +362,9 @@ sgrOtherExample = do
, (SetItalicized False, "No Italics")
, (SetUnderlining SingleUnderline, "Single Underline")
, (SetUnderlining DoubleUnderline, "Double Underline")
, (SetUnderlining CurlyUnderline, "Curly Underline")
, (SetUnderlining DottedUnderline, "Dotted Underline")
, (SetUnderlining DashedUnderline, "Dashed Underline")
, (SetUnderlining NoUnderline, "No Underline")
, (SetBlinkSpeed SlowBlink, "Slow Blink")
, (SetBlinkSpeed RapidBlink, "Rapid Blink")
Expand Down
1 change: 1 addition & 0 deletions ansi-terminal/src/System/Console/ANSI.hs
Original file line number Diff line number Diff line change
Expand Up @@ -897,6 +897,7 @@ layerColor layer = do
void $ string $ case layer of
Foreground -> "10"
Background -> "11"
Underlining -> fail "reportLayerColor does not support underlining"
void $ string ";rgb:"
redHex <- hexadecimal -- A non-negative whole hexadecimal number
void $ char '/'
Expand Down
16 changes: 14 additions & 2 deletions ansi-terminal/src/System/Console/ANSI/Codes.hs
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,9 @@ sgrToCode' sgr = case sgr of
SetUnderlining underlining -> case underlining of
SingleUnderline -> Right [4]
DoubleUnderline -> Right [21]
CurlyUnderline -> Left (4, [Just 3])
DottedUnderline -> Left (4, [Just 4])
DashedUnderline -> Left (4, [Just 5])
NoUnderline -> Right [24]
SetBlinkSpeed blink_speed -> case blink_speed of
SlowBlink -> Right [5]
Expand All @@ -202,15 +205,21 @@ sgrToCode' sgr = case sgr of
SetColor Foreground Vivid color -> Right [90 + colorToCode color]
SetColor Background Dull color -> Right [40 + colorToCode color]
SetColor Background Vivid color -> Right [100 + colorToCode color]
SetColor Underlining Dull color -> Left (58, [Just 5, Just $ colorToCode color])
SetColor Underlining Vivid color -> Left (58, [Just 5, Just $ 8 + colorToCode color])
SetPaletteColor Foreground index -> Right [38, 5, fromIntegral index]
SetPaletteColor Background index -> Right [48, 5, fromIntegral index]
SetPaletteColor Underlining index -> Left (58, [Just 5, Just $ fromIntegral index])
SetRGBColor Foreground color -> Right $ [38, 2] ++ toRGB color
SetRGBColor Background color -> Right $ [48, 2] ++ toRGB color
SetRGBColor Underlining color -> Left (58, [Just 2, Nothing] ++ toRGB' color)
SetDefaultColor Foreground -> Right [39]
SetDefaultColor Background -> Right [49]
SetDefaultColor Underlining -> Right [59]
where
toRGB color = let RGB r g b = toSRGB24 color
in map fromIntegral [r, g, b]
toRGB' = map Just . toRGB

cursorUpCode, cursorDownCode, cursorForwardCode, cursorBackwardCode ::
Int -- ^ Number of lines or characters to move
Expand Down Expand Up @@ -262,9 +271,11 @@ restoreCursorCode = "\ESC8"
reportCursorPositionCode :: String
reportCursorPositionCode = csi [] "6n"

-- | Code to emit the layer color into the console input stream, immediately
-- after being recognised on the output stream, as:
-- | Code to emit the foreground or backgrond layer color into the console input
-- stream, immediately after being recognised on the output stream, as:
--
-- @ESC ] \<Ps> ; rgb: \<red> ; \<green> ; \<blue> \<ST>@
--
-- where @\<Ps>@ is @10@ for 'Foreground' and @11@ for 'Background'; @\<red>@,
-- @\<green>@ and @\<blue>@ are the color channel values in hexadecimal (4, 8,
-- 12 and 16 bit values are possible, although 16 bit values are most common);
Expand All @@ -280,6 +291,7 @@ reportCursorPositionCode = csi [] "6n"
reportLayerColorCode :: ConsoleLayer -> String
reportLayerColorCode Foreground = osc "10" "?"
reportLayerColorCode Background = osc "11" "?"
reportLayerColorCode Underlining = [] -- Not supported.

clearFromCursorToScreenEndCode, clearFromCursorToScreenBeginningCode,
clearScreenCode :: String
Expand Down
2 changes: 1 addition & 1 deletion ansi-terminal/stack.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
resolver: lts-22.3 # GHC 9.6.3
snapshot: lts-22.9 # GHC 9.6.4
flags:
ansi-terminal:
example: false
Expand Down
2 changes: 1 addition & 1 deletion stack-ghc-9.4.8.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
resolver: lts-21.25 # GHC 9.4.8
snapshot: lts-21.25 # GHC 9.4.8
flags:
ansi-terminal:
example: false
Expand Down
2 changes: 1 addition & 1 deletion stack-winio.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
resolver: lts-22.3 # GHC 9.6.3
snapshot: lts-22.9 # GHC 9.6.4
flags:
ansi-terminal:
example: false
Expand Down
2 changes: 1 addition & 1 deletion stack.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
resolver: lts-22.3 # GHC 9.6.3
snapshot: lts-22.9 # GHC 9.6.4
flags:
ansi-terminal:
example: false
Expand Down

0 comments on commit c70e339

Please sign in to comment.