nixpkgs mirror (for testing)
github.com/NixOS/nixpkgs
nix
1{ lib }:
2
3let
4 echo_colored_body =
5 start_escape:
6 # Body of a function that behaves like "echo" but
7 # has the output colored by the given start_escape
8 # sequence. E.g.
9 #
10 # * echo_x "Building ..."
11 # * echo_x -n "Running "
12 #
13 # This is more complicated than apparent at first sight
14 # because:
15 # * The color markers and the text must be print
16 # in the same echo statement. Otherwise, other
17 # intermingled text from concurrent builds will
18 # be colored as well.
19 # * We need to preserve the trailing newline of the
20 # echo if and only if it is present. Bash likes
21 # to strip those if we capture the output of echo
22 # in a variable.
23 # * Leading "-" will be interpreted by test as an
24 # option for itself. Therefore, we prefix it with
25 # an x in `[[ "x$1" =~ ^x- ]]`.
26 ''
27 local echo_args="";
28 while [[ "x$1" =~ ^x- ]]; do
29 echo_args+=" $1"
30 shift
31 done
32
33 local start_escape="$(printf '${start_escape}')"
34 local reset="$(printf '\033[0m')"
35 echo $echo_args $start_escape"$@"$reset
36 '';
37 echo_conditional_colored_body =
38 colors: start_escape:
39 if colors == "always" then (echo_colored_body start_escape) else ''echo "$@"'';
40in
41{
42 echo_colored = colors: ''
43 echo_colored() {
44 ${echo_conditional_colored_body colors ''\033[0;1;32m''}
45 }
46
47 echo_error() {
48 ${echo_conditional_colored_body colors ''\033[0;1;31m''}
49 }
50 '';
51
52 noisily = colors: verbose: ''
53 noisily() {
54 ${lib.optionalString verbose ''
55 echo_colored -n "Running "
56 echo $@
57 ''}
58 $@
59 }
60 '';
61}