-
-
Notifications
You must be signed in to change notification settings - Fork 13.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The lib.ansi module provides a basic set of ANSI escape sequences to colorize and style text. The main purpose is to colorize trace messages (warnings, errors).
- Loading branch information
Showing
3 changed files
with
75 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
# This exports a basic set of ANSI escape sequences to colorize terminal output. | ||
|
||
{ ... }: | ||
|
||
let | ||
colorCodeMap = { | ||
black = 0; | ||
red = 1; | ||
green = 2; | ||
yellow = 3; | ||
blue = 4; | ||
magenta = 5; | ||
cyan = 6; | ||
white = 7; | ||
}; | ||
|
||
# Escape character. "\e" or similar encodings currently not supported by Nix. | ||
esc = ""; | ||
reset = "${esc}[0m"; | ||
style = { | ||
bold = "${esc}[1m"; | ||
italic = "${esc}[3m"; | ||
}; | ||
# Only foreground colors are supported at the moment. | ||
color = builtins.mapAttrs (name: value: "${esc}[3${toString value}m") colorCodeMap; | ||
|
||
/* Stylizes a string with ANSI escape sequences. | ||
Type: stylize :: [String] -> String -> String | ||
*/ | ||
stylize = | ||
# List of ANSI style attributes. | ||
styles: | ||
# Text to encapsulate by style. | ||
text: | ||
let | ||
ansiBefore = builtins.concatStringsSep "" styles; | ||
in | ||
if text == "" | ||
then "" | ||
else "${ansiBefore}${text}${reset}"; | ||
|
||
/* Stylizes a string as bold and red. | ||
Type: stylizeError :: String -> String | ||
*/ | ||
stylizeError = text: stylize [ style.bold color.red ] text; | ||
|
||
/* Stylizes a string as bold and yellow. | ||
Type: stylizeWarn :: String -> String | ||
*/ | ||
stylizeWarn = text: stylize [ style.bold color.yellow ] text; | ||
in | ||
{ | ||
inherit color style reset; | ||
inherit stylize stylizeError stylizeWarn; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters