lib: begin supporting colour

Add some basic helpers for working with ANSI colour codes,
and make strWidth and the various string layout functions aware of them.
This commit is contained in:
Simon Michael 2017-04-25 18:27:25 -07:00
parent 1af0f66e73
commit 9a86c9ee52
6 changed files with 44 additions and 5 deletions

View File

@ -24,6 +24,7 @@ module Hledger.Utils (---- provide these frequently used modules - or not, for c
module Hledger.Utils.String,
module Hledger.Utils.Text,
module Hledger.Utils.Test,
module Hledger.Utils.Color,
module Hledger.Utils.Tree,
-- Debug.Trace.trace,
-- module Data.PPrint,
@ -55,6 +56,7 @@ import Hledger.Utils.Regex
import Hledger.Utils.String
import Hledger.Utils.Text
import Hledger.Utils.Test
import Hledger.Utils.Color
import Hledger.Utils.Tree
-- import Prelude hiding (readFile,writeFile,appendFile,getContents,putStr,putStrLn)
-- import Hledger.Utils.UTF8IOCompat (readFile,writeFile,appendFile,getContents,putStr,putStrLn)

View File

@ -0,0 +1,23 @@
-- | Basic color helpers for prettifying console output.
{-# LANGUAGE OverloadedStrings #-}
module Hledger.Utils.Color
(
color,
bgColor,
Color(..),
ColorIntensity(..)
)
where
import System.Console.ANSI
-- | Wrap a string in ANSI codes to set and reset foreground colour.
color :: ColorIntensity -> Color -> String -> String
color int col s = setSGRCode [SetColor Foreground int col] ++ s ++ setSGRCode []
-- | Wrap a string in ANSI codes to set and reset background colour.
bgColor :: ColorIntensity -> Color -> String -> String
bgColor int col s = setSGRCode [SetColor Background int col] ++ s ++ setSGRCode []

View File

@ -15,6 +15,7 @@ module Hledger.Utils.String (
escapeQuotes,
words',
unwords',
stripAnsi,
-- * single-line layout
strip,
lstrip,
@ -319,12 +320,17 @@ takeWidth w (c:cs) | cw <= w = c:takeWidth (w-cw) cs
-- from Pandoc (copyright John MacFarlane, GPL)
-- see also http://unicode.org/reports/tr11/#Description
-- | Calculate the designated render width of a string, taking into
-- account wide characters and line breaks (the longest line within a
-- multi-line string determines the width ).
-- | Calculate the render width of a string, considering
-- wide characters (counted as double width), ANSI escape codes
-- (not counted), and line breaks (in a multi-line string, the longest
-- line determines the width).
strWidth :: String -> Int
strWidth "" = 0
strWidth s = maximum $ map (foldr (\a b -> charWidth a + b) 0) $ lines s
strWidth s = maximum $ map (foldr (\a b -> charWidth a + b) 0) $ lines s'
where s' = stripAnsi s
stripAnsi :: String -> String
stripAnsi = regexReplace "\ESC\\[([0-9]+;)*([0-9]+)?[ABCDHJKfmsu]" ""
-- | Get the designated render width of a character: 0 for a combining
-- character, 1 for a regular character, 2 for a wide character.

View File

@ -59,6 +59,7 @@ library
build-depends:
base >=4.8 && <5
, base-compat >=0.8.1
, ansi-terminal >= 0.6.2.3 && < 0.7
, array
, blaze-markup >=0.5.1
, bytestring
@ -131,6 +132,7 @@ library
Hledger.Reports.PostingsReport
Hledger.Reports.TransactionsReports
Hledger.Utils
Hledger.Utils.Color
Hledger.Utils.Debug
Hledger.Utils.Parse
Hledger.Utils.Regex
@ -153,6 +155,7 @@ test-suite doctests
build-depends:
base >=4.8 && <5
, base-compat >=0.8.1
, ansi-terminal >= 0.6.2.3 && < 0.7
, array
, blaze-markup >=0.5.1
, bytestring
@ -218,6 +221,7 @@ test-suite doctests
Hledger.Reports.ReportOptions
Hledger.Reports.TransactionsReports
Hledger.Utils
Hledger.Utils.Color
Hledger.Utils.Debug
Hledger.Utils.Parse
Hledger.Utils.Regex
@ -238,6 +242,7 @@ test-suite hunittests
build-depends:
base >=4.8 && <5
, base-compat >=0.8.1
, ansi-terminal >= 0.6.2.3 && < 0.7
, array
, blaze-markup >=0.5.1
, bytestring
@ -312,6 +317,7 @@ test-suite hunittests
Hledger.Reports.ReportOptions
Hledger.Reports.TransactionsReports
Hledger.Utils
Hledger.Utils.Color
Hledger.Utils.Debug
Hledger.Utils.Parse
Hledger.Utils.Regex

View File

@ -41,6 +41,7 @@ flags:
dependencies:
- base >=4.8 && <5
- base-compat >=0.8.1
- ansi-terminal >= 0.6.2.3 && < 0.7
- array
- blaze-markup >=0.5.1
- bytestring
@ -117,6 +118,7 @@ library:
- Hledger.Reports.PostingsReport
- Hledger.Reports.TransactionsReports
- Hledger.Utils
- Hledger.Utils.Color
- Hledger.Utils.Debug
- Hledger.Utils.Parse
- Hledger.Utils.Regex

View File

@ -17,7 +17,7 @@ import Graphics.Vty (Event(..),Key(..),Color,Attr,currentAttr)
import Lens.Micro.Platform
import System.Process
import Hledger
import Hledger hiding (Color)
import Hledger.UI.UITypes
import Hledger.UI.UIState