diff --git a/README.md b/README.md index c7d4006..de09fea 100644 --- a/README.md +++ b/README.md @@ -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 | +
+

+

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 | +
+

+

-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"); + +
+

+

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"); + +
+

+

# RELATED TOOLS @@ -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. diff --git a/images/bad.png b/images/bad.png new file mode 100644 index 0000000..9e0723f Binary files /dev/null and b/images/bad.png differ diff --git a/images/good.png b/images/good.png new file mode 100644 index 0000000..0b19ddb Binary files /dev/null and b/images/good.png differ diff --git a/images/plain.png b/images/plain.png new file mode 100644 index 0000000..be345a5 Binary files /dev/null and b/images/plain.png differ diff --git a/images/shorten.png b/images/shorten.png new file mode 100644 index 0000000..8a002cf Binary files /dev/null and b/images/shorten.png differ diff --git a/lib/Text/ANSI/Printf.pm b/lib/Text/ANSI/Printf.pm index d1965cf..78d47ef 100644 --- a/lib/Text/ANSI/Printf.pm +++ b/lib/Text/ANSI/Printf.pm @@ -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 + +

+ +=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 + +

+ +=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 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 + +

+ +=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 + +

+ +=end html =head1 RELATED TOOLS @@ -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, and the companion command, C, 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 command alone.