| 1 | # You may distribute under the terms of either the GNU General Public License | ||||||||||||||||||||||||||||
| 2 | # or the Artistic License (the same terms as Perl itself) | ||||||||||||||||||||||||||||
| 3 | # | ||||||||||||||||||||||||||||
| 4 | # (C) Paul Evans, 2022-2024 -- leonerd@leonerd.org.uk | ||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | package Commandable::Output 0.14; | ||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 11 | ▶ | use v5.26; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 11 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 11 | ▶ | use experimental qw( signatures ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 11 | ▶ | use constant HAVE_STRING_TAGGED => defined eval { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.1
| |||||||||||||||||||||||||||||
| 13 | 11 | require String::Tagged; | |||||||||||||||||||||||||||
| 14 | 0 | require Convert::Color; | |||||||||||||||||||||||||||
| 15 | 11 | }; | |||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 11 | ▶ | use constant HAVE_STRING_TAGGED_TERMINAL => defined eval { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | 11 | require String::Tagged::Terminal; | |||||||||||||||||||||||||||
| 19 | 11 | }; | |||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | =head1 NAME | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | C<Commandable::Output> - abstractions for printing output from commands | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | This package contains default implementations of methods for providing printed | ||||||||||||||||||||||||||||
| 28 | output from commands implemented using L<Commandable>. These methods are | ||||||||||||||||||||||||||||
| 29 | provided for the convenience of user code, and are also used by built-in | ||||||||||||||||||||||||||||
| 30 | commands provided by the C<Commandable> system itself. | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | Implementations are permitted (encouraged, even) to replace any of these | ||||||||||||||||||||||||||||
| 33 | methods in order to customise their behaviour. | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | =head2 WITH C<String::Tagged> | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | If L<String::Tagged> and L<Convert::Color> are available, this module applies | ||||||||||||||||||||||||||||
| 38 | formatting to strings by using the L<String::Tagged::Formatting> conventions. | ||||||||||||||||||||||||||||
| 39 | The C<format_heading> and C<format_note> methods will return results as | ||||||||||||||||||||||||||||
| 40 | instances of C<String::Tagged>, suitable to pass into the main C<printf> | ||||||||||||||||||||||||||||
| 41 | method. | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | =cut | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | =head1 METHODS | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | =cut | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 7 | ▶ | sub _format_string ( $self, $text, $tagmethod ) | ||||||||||||||||||||||||||
|
Subroutine
_format_string: called CC=4 SCAR=28.1
| |||||||||||||||||||||||||||||
| 50 | { | ||||||||||||||||||||||||||||
| 51 | 7 | return $text unless HAVE_STRING_TAGGED; | |||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 0 | my %tags; | |||||||||||||||||||||||||||
| 54 | 0 | ▶ | %tags = $self->$tagmethod if $self->can( $tagmethod ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 0 | ▶ | if( $tags{fg} and !ref $tags{fg} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$tags{"fg"} and not ref $tags{"fg"}
MC/DC0%0/2
$tags{"fg"} and not ref $tags{"fg"}
$tags{"fg"}
not ref $tags{"fg"}
Truth table0%0/3
A and not(B)
A= $tags{"fg"}B= not ref $tags{"fg"}
| |||||||||||||||||||||||||||||
| 57 | 0 | $tags{fg} = Convert::Color->new( $tags{fg} ); | |||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 0 | return String::Tagged->new_tagged( $text, %tags ); | |||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | =head2 printf | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | Commandable::Output->printf( $format, @args ); | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | The main output method, used to send messages for display to the user. The | ||||||||||||||||||||||||||||
| 68 | arguments are formatted into a single string by Perl's C<printf> function. | ||||||||||||||||||||||||||||
| 69 | This method does not append a linefeed. To output a complete line of text, | ||||||||||||||||||||||||||||
| 70 | remember to include the C<"\n"> at the end of the format string. | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | The default implementation writes output on the terminal via STDOUT. | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | In cases where the output should be sent to some other place (perhaps a GUI | ||||||||||||||||||||||||||||
| 75 | display widget of some kind), the application should replace this method with | ||||||||||||||||||||||||||||
| 76 | something that writes the display to somewhere more appropriate. Don't forget | ||||||||||||||||||||||||||||
| 77 | to use C<sprintf> to format the arguments into a string. | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | no warnings 'redefine'; | ||||||||||||||||||||||||||||
| 80 | sub Commandable::Output::printf | ||||||||||||||||||||||||||||
| 81 | { | ||||||||||||||||||||||||||||
| 82 | shift; # the package name | ||||||||||||||||||||||||||||
| 83 | my ( $format, @args ) = @_; | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | my $str = sprintf $format, @args; | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | $gui_display_widget->append_text( $str ); | ||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | If L<String::Tagged::Terminal> is available, the output will be printed using | ||||||||||||||||||||||||||||
| 91 | this module, by first converting the format string and arguments using | ||||||||||||||||||||||||||||
| 92 | L<String::Tagged/from_sprintf> and then constructing a terminal string using | ||||||||||||||||||||||||||||
| 93 | L<String::Tagged::Terminal/new_from_formatting>. This means the default | ||||||||||||||||||||||||||||
| 94 | implementation will be able to output formatted strings using the | ||||||||||||||||||||||||||||
| 95 | L<String::Tagged::Formatting> conventions. | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | =cut | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 0 | ▶ | sub printf ( $self, $format, @args ) | ||||||||||||||||||||||||||
|
Subroutine
printf: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 100 | { | ||||||||||||||||||||||||||||
| 101 | 0 | if( HAVE_STRING_TAGGED_TERMINAL ) { | |||||||||||||||||||||||||||
| 102 | String::Tagged::Terminal->new_from_formatting( | ||||||||||||||||||||||||||||
| 103 | String::Tagged->from_sprintf( $format, @args ) | ||||||||||||||||||||||||||||
| 104 | )->print_to_terminal; | ||||||||||||||||||||||||||||
| 105 | return; | ||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 0 | printf $format, @args; | |||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | =head2 print_heading | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | Commandable::Output->print_heading( $text, $level ); | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | Used to send output that should be considered like a section heading. | ||||||||||||||||||||||||||||
| 116 | I<$level> may be an integer used to express sub-levels; increasing values from | ||||||||||||||||||||||||||||
| 117 | 1 upwards indicate increasing sub-levels. | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | The default implementation formats the text string using L</format_heading> | ||||||||||||||||||||||||||||
| 120 | then prints it using L</printf> with a trailing linefeed. | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | =cut | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 5 | ▶ | sub print_heading ( $self, $text, $level = 1 ) | ||||||||||||||||||||||||||
|
Subroutine
print_heading: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 125 | { | ||||||||||||||||||||||||||||
| 126 | 5 | $self->printf( "%s\n", $self->format_heading( $text, $level ) ); | |||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | =head2 format_heading | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | $str = Commandable::Output->format_heading( $text, $level ); | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | Returns a value for printing, to represent a section heading for the given | ||||||||||||||||||||||||||||
| 134 | text and level. | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | The default implementation applies the following formatting if | ||||||||||||||||||||||||||||
| 137 | C<String::Tagged> is available: | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | =over 4 | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | =item Level 1 | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | Underlined | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | =item Level 2 | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | Underlined, cyan colour | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | =item Level 3 | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | Bold | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | =back | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | =cut | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | 11 | ▶ | use constant TAGS_FOR_HEADING_1 => ( under => 1 ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 158 | 11 | ▶ | use constant TAGS_FOR_HEADING_2 => ( under => 1, fg => "vga:cyan", ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 159 | 11 | ▶ | use constant TAGS_FOR_HEADING_3 => ( bold => 1 ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | 2 | ▶ | sub format_heading ( $self, $text, $level = 1 ) | ||||||||||||||||||||||||||
|
Subroutine
format_heading: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 162 | { | ||||||||||||||||||||||||||||
| 163 | 2 | return $self->_format_string( $text, "TAGS_FOR_HEADING_$level" ); | |||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | =head2 format_note | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | $str = Commandable::Output->format_note( $text, $level ); | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | Returns a value for printing, to somehow highlight the given text (which | ||||||||||||||||||||||||||||
| 171 | should be a short word or string) at the given level. | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | The default implementation applies the following formatting if | ||||||||||||||||||||||||||||
| 174 | C<String::Tagged> is available: | ||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | =over 4 | ||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | =item Level 0 | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | Bold, yellow colour | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | =item Level 1 | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | Bold, cyan colour | ||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | =item Level 2 | ||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | Bold, magenta colour | ||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | =back | ||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | =cut | ||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | 11 | ▶ | use constant TAGS_FOR_NOTE_0 => ( bold => 1, fg => "vga:yellow" ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 195 | 11 | ▶ | use constant TAGS_FOR_NOTE_1 => ( bold => 1, fg => "vga:cyan" ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 196 | 11 | ▶ | use constant TAGS_FOR_NOTE_2 => ( bold => 1, fg => "vga:magenta" ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | 5 | ▶ | sub format_note ( $self, $text, $level = 0 ) | ||||||||||||||||||||||||||
|
Subroutine
format_note: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 199 | { | ||||||||||||||||||||||||||||
| 200 | 5 | return $self->_format_string( $text, "TAGS_FOR_NOTE_$level" ); | |||||||||||||||||||||||||||
| 201 | } | ||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | Paul Evans <leonerd@leonerd.org.uk> | ||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | =cut | ||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||
| 209 | 11 | 0x55AA; | |||||||||||||||||||||||||||