diff --git a/hledger-lib/Hledger/Utils.hs b/hledger-lib/Hledger/Utils.hs index 21c651c7c..c826e985d 100644 --- a/hledger-lib/Hledger/Utils.hs +++ b/hledger-lib/Hledger/Utils.hs @@ -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) diff --git a/hledger-lib/Hledger/Utils/Color.hs b/hledger-lib/Hledger/Utils/Color.hs new file mode 100644 index 000000000..a3950cb9c --- /dev/null +++ b/hledger-lib/Hledger/Utils/Color.hs @@ -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 [] diff --git a/hledger-lib/Hledger/Utils/String.hs b/hledger-lib/Hledger/Utils/String.hs index d6a23b007..f86e31379 100644 --- a/hledger-lib/Hledger/Utils/String.hs +++ b/hledger-lib/Hledger/Utils/String.hs @@ -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. diff --git a/hledger-lib/hledger-lib.cabal b/hledger-lib/hledger-lib.cabal index e5ed38f77..e5e594c80 100644 --- a/hledger-lib/hledger-lib.cabal +++ b/hledger-lib/hledger-lib.cabal @@ -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 diff --git a/hledger-lib/package.yaml b/hledger-lib/package.yaml index f3864dc5a..7696f87f6 100644 --- a/hledger-lib/package.yaml +++ b/hledger-lib/package.yaml @@ -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 diff --git a/hledger-ui/Hledger/UI/UIUtils.hs b/hledger-ui/Hledger/UI/UIUtils.hs index 24c64f23f..ac5c0cf65 100644 --- a/hledger-ui/Hledger/UI/UIUtils.hs +++ b/hledger-ui/Hledger/UI/UIUtils.hs @@ -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