Print-Colored

print, say, prompt with predefined colors


Keywords
colorization, perl
License
Artistic-2.0

Documentation

NAME

Print::Colored - print, say, prompt with predefined colors

SYNOPSIS

use Print::Colored;
use Print::Colored ':all';

# color
use Print::Colored ':color';

$colored_text = color_error $text;    # bright red
$colored_text = color_info $text;     # bright blue
$colored_text = color_input $text;    # bright cyan
$colored_text = color_ok $text;       # bright green
$colored_text = color_warn $text;     # bright magenta

# print
use Print::Colored ':print';

print_error $text;
print_info $text;
print_input $text;
print_ok $text;
print_warn $text;

# prompt
use Print::Colored ':prompt';

$input = prompt_error $text, @params;
$input = prompt_info $text, @params;
$input = prompt_input $text, @params;
$input = prompt_ok $text, @params;
$input = prompt_warn $text, @params;

$password = password_error $text, @params;
$password = password_info $text, @params;
$password = password_input $text, @params;
$password = password_ok $text, @params;
$password = password_warn $text, @params;

# say
use Print::Colored ':say';

say_error $text;
say_info $text;
say_input $text;
say_ok $text;
say_warn $text;

DESCRIPTION

Print::Colored provides functions to print, say, prompt with predefined colors.

  • error bright red
  • info bright blue
  • input bright cyan
  • ok bright green
  • warn bright magenta

We should use colors all the time we write sripts that run in the terminal. Read Use terminal colors to distinguish information by brian d foy to get some more ideas about it.

But experience shows that the more commands and constants we have to use the less colors our scripts have. This was the reason to build this rather simple module.

Limitations

Because the colors are predefined, there isn't much to configure. If you don't like them (and quite sure you don't) and until we come up with a better solution, you can use "coloralias" in Term::ANSIColor to modify them.

use Term::ANSIColor 'coloralias';

coloralias('error', 'yellow');          # default: bright_red
coloralias('info',  'white');           # default: bright_blue
coloralias('input', 'bright_white');    # default: bright_cyan
coloralias('ok',    'black');           # default: bright_green
coloralias('warn',  'red');             # default: bright_blue

All the commands except "color_" write directly to STDOUT.

print_ok $filehandle 'Everything okay.';    # ✗ no
say_ok $filehandle 'Everything okay.';      # ✗ no

You can't "print" and "say" to filehandles.

print $filehandle color_ok 'Everything okay.';    # ✓
say $filehandle color_ok 'Everything okay.';      # ✓

Instead you have to use one of the "color" functions.

color

use Print::Colored ':color';

Imports the functions "color_error", "color_info", "color_input", "color_ok", and "color_warn".

color_error

$colored_text = color_error 'There was an error';

Returns a text colored as error.

color_info

$colored_text = color_info 'This is an info';

Returns a text colored as info.

color_input

$colored_text = color_input 'Waiting for an input...';

Returns a text colored as input.

color_ok

$colored_text = color_ok 'Everything okay';

Returns a text colored as ok.

color_warn

$colored_text = color_warn 'Last warning';

Returns a text colored as warn.

print

use Print::Colored ':print';

Imports the functions "print_error", "print_info", "print_input", "print_ok", and "print_warn".

print_error

print_error 'There was an error';

Prints a text colored as error.

print_info

print_info 'This is an info';

Prints a text colored as info.

print_input

print_input 'Waiting for an input...';

Prints a text colored as input.

print_ok

print_ok 'Everything okay';

Prints a text colored as ok.

print_warn

print_warn 'Last warning';

Prints a text colored as warn.

prompt

use Print::Colored ':prompt';

Imports the functions "prompt_error", "prompt_info", "prompt_input", "prompt_ok", "prompt_warn", "password_error", "password_info", "password_input", "password_ok", and "password_warn". Internally they call "prompt" in IO::Prompter.

$password = prompt_input 'Enter your password: ', -echo => '*';
$password = password_input 'Enter your password: ';

password functions ask for a password and are identical to prompt with parameter <-echo = '*'>>.

prompt_error

$input = prompt_error 'Enter your data: ';

Prompts colored as error and returns the input.

prompt_info

$input = prompt_info 'Enter your data: ';

Prompts colored as info and returns the input.

prompt_input

$input = prompt_input 'Enter your data: ';

Prompts colored as input and returns the input.

prompt_ok

$input = prompt_ok 'Enter your data: ';

Prompts colored as ok and returns the input.

prompt_warn

$input = prompt_warn 'Enter your data: ';

Prompts colored as warn and returns the input.

password_error

$password = password_error 'Enter your password: ';

Prompts colored as error for a password and returns the input.

password_info

$password = password_info 'Enter your password: ';

Prompts colored as info for a password and returns the input.

password_input

$password = password_input 'Enter your password: ';

Prompts colored as input for a password and returns the input.

password_ok

$password = password_ok 'Enter your password: ';

Prompts colored as ok for a password and returns the input.

password_warn

$password = password_warn 'Enter your password: ';

Prompts colored as warn for a password and returns the input.

say

use Print::Colored ':say';

Imports the functions "say_error", "say_info", "say_input", "say_ok", and "say_warn".

say_error

say_error 'There was an error';

Prints a text with appended newline colored as error.

say_info

say_info 'This is an info';

Prints a text with appended newline colored as info.

say_input

say_input 'Waiting for an input...';

Prints a text with appended newline colored as input.

say_ok

say_ok 'Everything okay';

Prints a text with appended newline colored as ok.

say_warn

say_warn 'Last warning';

Prints a text with appended newline colored as warn.

AUTHOR & COPYRIGHT

© 2019-2022 by Tekki (Rolf Stöckli).

This program is free software, you can redistribute it and/or modify it under the terms of the Artistic License version 2.0.

SEE ALSO

IO::Prompter, Term::ANSIColor.