System.Terminal.Utility
Description
This module provides various utilities and short-cuts.
- set_colour :: Colour -> IO ()
- set_colours_default :: IO ()
- putStrLnC :: Colour -> String -> IO ()
- putPairLn :: (Colour, String) -> (Colour, String) -> IO ()
- highlight :: ((Colour, Colour), (Colour, Colour), (Colour, Colour)) -> Int -> String -> IO ()
- highlightLN :: ((Colour, Colour), (Colour, Colour), (Colour, Colour)) -> Int -> String -> IO ()
- default_exception_handler :: Exception e => e -> IO x
- with_default_exception_handler :: IO x -> IO x
Setting colours
set_colour :: Colour -> IO ()Source
Set terminal foreground colour (background is set to black).
set_colours_default :: IO ()Source
Set default terminal colours (DWhite on DBlack).
Printing output
putStrLnC :: Colour -> String -> IO ()Source
Set terminal [foreground] colour and then putStrLn
a string.
putPairLn :: (Colour, String) -> (Colour, String) -> IO ()Source
Write a pair of text strings on a single line, with different [foreground] text colours.
highlight :: ((Colour, Colour), (Colour, Colour), (Colour, Colour)) -> Int -> String -> IO ()Source
Print a single line of text, with a given character highlighted in colour. Useful for, say, highlighting the location of a syntax error in an expression.
The tuple consists of three colour pairs. Each pair is a foreground/background pair. The first pair applies to the next before the nominated position, the second pair applies to the nominated position itself, and the third pair applies to any text after the nominated position.
The nominated position is given by the Int
argument,
with 0 being the very first character of the string.
Note that if the position is off the end of the
string, a blank space will be added to the end of the
string and that will be highlighted.
Note that no newline is written. If you want one, you must output it yourself.
highlightLN :: ((Colour, Colour), (Colour, Colour), (Colour, Colour)) -> Int -> String -> IO ()Source
A version of highlight
that outputs a newline after
the final character of text.
Handling exceptions and errors
default_exception_handler :: Exception e => e -> IO xSource
A default top-level exception handler, for exceptions that fail to be caught before reaching the top level.
In a properly designed application, exceptions should be anticipated, caught and handled in the correct place. (E.g., if you try to open a file, you should anticipate the possibility of an I/O exception and catch/process this appropriately.) Thus an exception reaching the top-level of the program would indicate a programming bug, and the generated error message reflects this. On a crash, the text
An internal program malfunction has occurred. Please report this as a bug to the program developers.
will be emitted on stderr
, coloured bright yellow on a
bright red background. The exception is then re-thrown
(presumably halting the program).
with_default_exception_handler :: IO x -> IO xSource
Take an IO
action, and run it with the
default_exception_handler
installed. Typically you would
do something like
main = with_default_exception_handler main2 main2 = do ...
Now all unhandled exceptions in your program will cause a
suitable message to be written to stderr
.