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 50b7eb0
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 13 deletions.
25 changes: 19 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,27 +44,40 @@ this code produces the output like:
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");
"\e[31mRed\e[m", "\e[32;3mGreen\e[m", "\e[34;3;4mBlue\e[m");

this code produces undesirable result:

| 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.
<div>
<p><img width="750" src="https://raw.githubusercontent.com/tecolicom/Text-ANSI-Printf/master/images/bad.png">
</div>

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");
"\e[31mRed\e[m", "\e[32;3mGreen\e[m", "\e[34;3;4mBlue\e[m");

<div>
<p><img width="750" 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="750" src="https://raw.githubusercontent.com/tecolicom/Text-ANSI-Printf/master/images/shorten.png">
</div>

# RELATED TOOLS

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/shorten.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
36 changes: 29 additions & 7 deletions lib/Text/ANSI/Printf.pm
Original file line number Diff line number Diff line change
Expand Up @@ -87,27 +87,50 @@ this code produces the output like:
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");
"\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="750" 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");
"\e[31mRed\e[m", "\e[32;3mGreen\e[m", "\e[34;3;4mBlue\e[m");
=begin html
<p><img width="750" 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 html
<p><img width="750" src="https://raw.githubusercontent.com/tecolicom/Text-ANSI-Printf/master/images/shorten.png">
=end html
=head1 RELATED TOOLS
Expand All @@ -120,7 +143,6 @@ 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"));
Expand Down

0 comments on commit 50b7eb0

Please sign in to comment.