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:
parent
1af0f66e73
commit
9a86c9ee52
@ -24,6 +24,7 @@ module Hledger.Utils (---- provide these frequently used modules - or not, for c
|
|||||||
module Hledger.Utils.String,
|
module Hledger.Utils.String,
|
||||||
module Hledger.Utils.Text,
|
module Hledger.Utils.Text,
|
||||||
module Hledger.Utils.Test,
|
module Hledger.Utils.Test,
|
||||||
|
module Hledger.Utils.Color,
|
||||||
module Hledger.Utils.Tree,
|
module Hledger.Utils.Tree,
|
||||||
-- Debug.Trace.trace,
|
-- Debug.Trace.trace,
|
||||||
-- module Data.PPrint,
|
-- module Data.PPrint,
|
||||||
@ -55,6 +56,7 @@ import Hledger.Utils.Regex
|
|||||||
import Hledger.Utils.String
|
import Hledger.Utils.String
|
||||||
import Hledger.Utils.Text
|
import Hledger.Utils.Text
|
||||||
import Hledger.Utils.Test
|
import Hledger.Utils.Test
|
||||||
|
import Hledger.Utils.Color
|
||||||
import Hledger.Utils.Tree
|
import Hledger.Utils.Tree
|
||||||
-- import Prelude hiding (readFile,writeFile,appendFile,getContents,putStr,putStrLn)
|
-- import Prelude hiding (readFile,writeFile,appendFile,getContents,putStr,putStrLn)
|
||||||
-- import Hledger.Utils.UTF8IOCompat (readFile,writeFile,appendFile,getContents,putStr,putStrLn)
|
-- import Hledger.Utils.UTF8IOCompat (readFile,writeFile,appendFile,getContents,putStr,putStrLn)
|
||||||
|
|||||||
23
hledger-lib/Hledger/Utils/Color.hs
Normal file
23
hledger-lib/Hledger/Utils/Color.hs
Normal 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 []
|
||||||
@ -15,6 +15,7 @@ module Hledger.Utils.String (
|
|||||||
escapeQuotes,
|
escapeQuotes,
|
||||||
words',
|
words',
|
||||||
unwords',
|
unwords',
|
||||||
|
stripAnsi,
|
||||||
-- * single-line layout
|
-- * single-line layout
|
||||||
strip,
|
strip,
|
||||||
lstrip,
|
lstrip,
|
||||||
@ -319,12 +320,17 @@ takeWidth w (c:cs) | cw <= w = c:takeWidth (w-cw) cs
|
|||||||
-- from Pandoc (copyright John MacFarlane, GPL)
|
-- from Pandoc (copyright John MacFarlane, GPL)
|
||||||
-- see also http://unicode.org/reports/tr11/#Description
|
-- see also http://unicode.org/reports/tr11/#Description
|
||||||
|
|
||||||
-- | Calculate the designated render width of a string, taking into
|
-- | Calculate the render width of a string, considering
|
||||||
-- account wide characters and line breaks (the longest line within a
|
-- wide characters (counted as double width), ANSI escape codes
|
||||||
-- multi-line string determines the width ).
|
-- (not counted), and line breaks (in a multi-line string, the longest
|
||||||
|
-- line determines the width).
|
||||||
strWidth :: String -> Int
|
strWidth :: String -> Int
|
||||||
strWidth "" = 0
|
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
|
-- | Get the designated render width of a character: 0 for a combining
|
||||||
-- character, 1 for a regular character, 2 for a wide character.
|
-- character, 1 for a regular character, 2 for a wide character.
|
||||||
|
|||||||
@ -59,6 +59,7 @@ library
|
|||||||
build-depends:
|
build-depends:
|
||||||
base >=4.8 && <5
|
base >=4.8 && <5
|
||||||
, base-compat >=0.8.1
|
, base-compat >=0.8.1
|
||||||
|
, ansi-terminal >= 0.6.2.3 && < 0.7
|
||||||
, array
|
, array
|
||||||
, blaze-markup >=0.5.1
|
, blaze-markup >=0.5.1
|
||||||
, bytestring
|
, bytestring
|
||||||
@ -131,6 +132,7 @@ library
|
|||||||
Hledger.Reports.PostingsReport
|
Hledger.Reports.PostingsReport
|
||||||
Hledger.Reports.TransactionsReports
|
Hledger.Reports.TransactionsReports
|
||||||
Hledger.Utils
|
Hledger.Utils
|
||||||
|
Hledger.Utils.Color
|
||||||
Hledger.Utils.Debug
|
Hledger.Utils.Debug
|
||||||
Hledger.Utils.Parse
|
Hledger.Utils.Parse
|
||||||
Hledger.Utils.Regex
|
Hledger.Utils.Regex
|
||||||
@ -153,6 +155,7 @@ test-suite doctests
|
|||||||
build-depends:
|
build-depends:
|
||||||
base >=4.8 && <5
|
base >=4.8 && <5
|
||||||
, base-compat >=0.8.1
|
, base-compat >=0.8.1
|
||||||
|
, ansi-terminal >= 0.6.2.3 && < 0.7
|
||||||
, array
|
, array
|
||||||
, blaze-markup >=0.5.1
|
, blaze-markup >=0.5.1
|
||||||
, bytestring
|
, bytestring
|
||||||
@ -218,6 +221,7 @@ test-suite doctests
|
|||||||
Hledger.Reports.ReportOptions
|
Hledger.Reports.ReportOptions
|
||||||
Hledger.Reports.TransactionsReports
|
Hledger.Reports.TransactionsReports
|
||||||
Hledger.Utils
|
Hledger.Utils
|
||||||
|
Hledger.Utils.Color
|
||||||
Hledger.Utils.Debug
|
Hledger.Utils.Debug
|
||||||
Hledger.Utils.Parse
|
Hledger.Utils.Parse
|
||||||
Hledger.Utils.Regex
|
Hledger.Utils.Regex
|
||||||
@ -238,6 +242,7 @@ test-suite hunittests
|
|||||||
build-depends:
|
build-depends:
|
||||||
base >=4.8 && <5
|
base >=4.8 && <5
|
||||||
, base-compat >=0.8.1
|
, base-compat >=0.8.1
|
||||||
|
, ansi-terminal >= 0.6.2.3 && < 0.7
|
||||||
, array
|
, array
|
||||||
, blaze-markup >=0.5.1
|
, blaze-markup >=0.5.1
|
||||||
, bytestring
|
, bytestring
|
||||||
@ -312,6 +317,7 @@ test-suite hunittests
|
|||||||
Hledger.Reports.ReportOptions
|
Hledger.Reports.ReportOptions
|
||||||
Hledger.Reports.TransactionsReports
|
Hledger.Reports.TransactionsReports
|
||||||
Hledger.Utils
|
Hledger.Utils
|
||||||
|
Hledger.Utils.Color
|
||||||
Hledger.Utils.Debug
|
Hledger.Utils.Debug
|
||||||
Hledger.Utils.Parse
|
Hledger.Utils.Parse
|
||||||
Hledger.Utils.Regex
|
Hledger.Utils.Regex
|
||||||
|
|||||||
@ -41,6 +41,7 @@ flags:
|
|||||||
dependencies:
|
dependencies:
|
||||||
- base >=4.8 && <5
|
- base >=4.8 && <5
|
||||||
- base-compat >=0.8.1
|
- base-compat >=0.8.1
|
||||||
|
- ansi-terminal >= 0.6.2.3 && < 0.7
|
||||||
- array
|
- array
|
||||||
- blaze-markup >=0.5.1
|
- blaze-markup >=0.5.1
|
||||||
- bytestring
|
- bytestring
|
||||||
@ -117,6 +118,7 @@ library:
|
|||||||
- Hledger.Reports.PostingsReport
|
- Hledger.Reports.PostingsReport
|
||||||
- Hledger.Reports.TransactionsReports
|
- Hledger.Reports.TransactionsReports
|
||||||
- Hledger.Utils
|
- Hledger.Utils
|
||||||
|
- Hledger.Utils.Color
|
||||||
- Hledger.Utils.Debug
|
- Hledger.Utils.Debug
|
||||||
- Hledger.Utils.Parse
|
- Hledger.Utils.Parse
|
||||||
- Hledger.Utils.Regex
|
- Hledger.Utils.Regex
|
||||||
|
|||||||
@ -17,7 +17,7 @@ import Graphics.Vty (Event(..),Key(..),Color,Attr,currentAttr)
|
|||||||
import Lens.Micro.Platform
|
import Lens.Micro.Platform
|
||||||
import System.Process
|
import System.Process
|
||||||
|
|
||||||
import Hledger
|
import Hledger hiding (Color)
|
||||||
import Hledger.UI.UITypes
|
import Hledger.UI.UITypes
|
||||||
import Hledger.UI.UIState
|
import Hledger.UI.UIState
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user