Skip to content

Commit

Permalink
update document with images
Browse files Browse the repository at this point in the history
  • Loading branch information
kaz-utashiro committed Aug 26, 2024
1 parent f6de466 commit fa23331
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 26 deletions.
39 changes: 25 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,36 +35,49 @@ based on its visible appearance.

For example,

printf "| %-5s | %-5s | %-5s |\n", "Red", "Green", "Blue";
printf("| %-8s | %-8s | %-8s |\n", "Red", "Green", "Blue");

this code produces the output like:

| Red | Green | Blue |
<div>
<p><img width="300" src="https://raw.githubusercontent.com/tecolicom/Text-ANSI-Printf/master/images/plain.png">
</div>

However, if the arguments are colored by ANSI sequence,

printf("| %-5s | %-5s | %-5s |\n",
"\e[31mRed\e[m", "\e[32mGreen\e[m", "\e[34mBlue\e[m");
printf("| %-8s | %-8s | %-8s |\n",
"\e[31mRed\e[m", "\e[32;3mGreen\e[m", "\e[34;3;4mBlue\e[m");

this code produces undesirable result:

| Red | Green | Blue |
<div>
<p><img width="300" src="https://raw.githubusercontent.com/tecolicom/Text-ANSI-Printf/master/images/bad.png">
</div>

This is still better because it is readable, but if the result is
shorter than the original string, for example, "%.3s", the result will
be disastrous.
This is still better because the output is readable, but if the result
is shorter than the original string, for example, "%3.3s", the result
will be disastrous.

`ansi_printf` can be used to properly format colored text.

use Text::ANSI::Printf 'ansi_printf';
ansi_printf("| %-5s | %-5s | %-5s |\n",
"\e[31mRed\e[m", "\e[32;3mGreen\e[m", "\e[34;3;4mBlue\e[m");
ansi_printf("| %-8s | %-8s | %-8s |\n",
"\e[31mRed\e[m", "\e[32;3mGreen\e[m", "\e[34;3;4mBlue\e[m");

<div>
<p><img width="300" src="https://raw.githubusercontent.com/tecolicom/Text-ANSI-Printf/master/images/good.png">
</div>

It does not matter if the result is shorter than the original text.
Next code produces `[R] [G] [B]` in proper color.

use Text::ANSI::Printf 'ansi_printf';
ansi_printf("[%.1s] [%.1s] [%.1s]\n",
"\e[31mRed\e[m", "\e[32;3mGreen\e[m", "\e[34;3;4mBlue\e[m");
"\e[31mRed\e[m", "\e[32;3mGreen\e[m", "\e[34;3;4mBlue\e[m");

<div>
<p><img width="300" src="https://raw.githubusercontent.com/tecolicom/Text-ANSI-Printf/master/images/shorten.png">
</div>

# RELATED TOOLS

Expand All @@ -77,15 +90,13 @@ follows.

use Text::ANSI::Printf 'ansi_printf';
use Term::ANSIColor::Concise 'ansi_color';

ansi_printf("| %-5s | %-5s | %-5s |\n",
ansi_color("R", "Red", "GI", "Green", "BIU", "Blue"));

Using the command line interface, `ansiprintf`, and the companion
command, `ansiecho`, the shell command can be executed as follows.

ansiprintf "| %-5s | %-5s | %-5s |\n" \
$(ansiecho -cR Red -cGI Green -cBIU Blue)
ansiprintf "| %-5s | %-5s | %-5s |\n" $(ansiecho -cR Red -cGI Green -cBIU Blue)

In fact, this can be done with the `ansiecho` command alone.

Expand Down
Binary file added images/bad.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/good.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/plain.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/shorten.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
61 changes: 49 additions & 12 deletions lib/Text/ANSI/Printf.pm
Original file line number Diff line number Diff line change
Expand Up @@ -78,36 +78,75 @@ based on its visible appearance.
For example,
printf "| %-5s | %-5s | %-5s |\n", "Red", "Green", "Blue";
printf("| %-8s | %-8s | %-8s |\n", "Red", "Green", "Blue");
this code produces the output like:
=begin :text
| Red | Green | Blue |
=end :text
=begin html
<p><img width="300" src="https://raw.githubusercontent.com/tecolicom/Text-ANSI-Printf/master/images/plain.png">
=end html
However, if the arguments are colored by ANSI sequence,
printf("| %-5s | %-5s | %-5s |\n",
"\e[31mRed\e[m", "\e[32mGreen\e[m", "\e[34mBlue\e[m");
printf("| %-8s | %-8s | %-8s |\n",
"\e[31mRed\e[m", "\e[32;3mGreen\e[m", "\e[34;3;4mBlue\e[m");
this code produces undesirable result:
=begin :text
| Red | Green | Blue |
This is still better because it is readable, but if the result is
shorter than the original string, for example, "%.3s", the result will
be disastrous.
=end :text
=begin html
<p><img width="300" src="https://raw.githubusercontent.com/tecolicom/Text-ANSI-Printf/master/images/bad.png">
=end html
This is still better because the output is readable, but if the result
is shorter than the original string, for example, "%3.3s", the result
will be disastrous.
C<ansi_printf> can be used to properly format colored text.
use Text::ANSI::Printf 'ansi_printf';
ansi_printf("| %-5s | %-5s | %-5s |\n",
"\e[31mRed\e[m", "\e[32;3mGreen\e[m", "\e[34;3;4mBlue\e[m");
ansi_printf("| %-8s | %-8s | %-8s |\n",
"\e[31mRed\e[m", "\e[32;3mGreen\e[m", "\e[34;3;4mBlue\e[m");
=begin html
<p><img width="300" src="https://raw.githubusercontent.com/tecolicom/Text-ANSI-Printf/master/images/good.png">
=end html
It does not matter if the result is shorter than the original text.
Next code produces C<[R] [G] [B]> in proper color.
use Text::ANSI::Printf 'ansi_printf';
ansi_printf("[%.1s] [%.1s] [%.1s]\n",
"\e[31mRed\e[m", "\e[32;3mGreen\e[m", "\e[34;3;4mBlue\e[m");
"\e[31mRed\e[m", "\e[32;3mGreen\e[m", "\e[34;3;4mBlue\e[m");
=begin :text
[R] [G] [B]
=end :text
=begin html
<p><img width="300" src="https://raw.githubusercontent.com/tecolicom/Text-ANSI-Printf/master/images/shorten.png">
=end html
=head1 RELATED TOOLS
Expand All @@ -120,15 +159,13 @@ follows.
use Text::ANSI::Printf 'ansi_printf';
use Term::ANSIColor::Concise 'ansi_color';
ansi_printf("| %-5s | %-5s | %-5s |\n",
ansi_color("R", "Red", "GI", "Green", "BIU", "Blue"));
Using the command line interface, C<ansiprintf>, and the companion
command, C<ansiecho>, the shell command can be executed as follows.
ansiprintf "| %-5s | %-5s | %-5s |\n" \
$(ansiecho -cR Red -cGI Green -cBIU Blue)
ansiprintf "| %-5s | %-5s | %-5s |\n" $(ansiecho -cR Red -cGI Green -cBIU Blue)
In fact, this can be done with the C<ansiecho> command alone.
Expand Down

0 comments on commit fa23331

Please sign in to comment.