| 1 | package Term::UI::History; | ||
| 2 | $Term::UI::History::VERSION = '0.48'; | ||
| 3 | |||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | |||
| 7 | use parent | ||
| 8 | qw! Exporter | ||
| 9 | Log::Message::Simple | ||
| 10 | !; | ||
| 11 | |||
| 12 | use Log::Message 'private' => 0; | ||
| 13 | |||
| 14 | our $HISTORY_FH = \ *STDOUT; | ||
| 15 | our @EXPORT = qw[ history ]; | ||
| 16 | |||
| 17 | my $log = Log::Message->new(); | ||
| 18 | |||
| 19 | =pod | ||
| 20 | |||
| 21 | =head1 NAME | ||
| 22 | |||
| 23 | Term::UI::History - history function | ||
| 24 | |||
| 25 | =head1 SYNOPSIS | ||
| 26 | |||
| 27 | use Term::UI::History qw[history]; | ||
| 28 | |||
| 29 | history("Some message"); | ||
| 30 | |||
| 31 | ### retrieve the history in printable form | ||
| 32 | $hist = Term::UI::History->history_as_string; | ||
| 33 | |||
| 34 | ### redirect output | ||
| 35 | local $Term::UI::History::HISTORY_FH = \*STDERR; | ||
| 36 | |||
| 37 | =head1 DESCRIPTION | ||
| 38 | |||
| 39 | This module provides the C<history> function for C<Term::UI>, | ||
| 40 | printing and saving all the C<UI> interaction. | ||
| 41 | |||
| 42 | Refer to the C<Term::UI> manpage for details on usage from | ||
| 43 | C<Term::UI>. | ||
| 44 | |||
| 45 | This module subclasses C<Log::Message::Simple>. Refer to its | ||
| 46 | manpage for additional functionality available via this package. | ||
| 47 | |||
| 48 | =head1 FUNCTIONS | ||
| 49 | |||
| 50 | =head2 history("message string" [,VERBOSE]) | ||
| 51 | |||
| 52 | Records a message on the stack, and prints it to C<STDOUT> | ||
| 53 | (or actually C<$HISTORY_FH>, see the C<GLOBAL VARIABLES> section | ||
| 54 | below), if the C<VERBOSE> option is true. | ||
| 55 | |||
| 56 | The C<VERBOSE> option defaults to true. | ||
| 57 | |||
| 58 | =cut | ||
| 59 | |||
| 60 | sub history { | ||
| 61 | my $msg = shift; | ||
| 62 | |||
| 63 | $log | ||
| 64 | ->store( 'message' => $msg, | ||
| 65 | 'tag' => 'HISTORY', | ||
| 66 | 'level' => 'history', | ||
| 67 | 'extra' => [ @_ ], | ||
| 68 | ); | ||
| 69 | } | ||
| 70 | |||
| 71 | sub history_as_string { | ||
| 72 | my $class = shift; | ||
| 73 | |||
| 74 | return join $/, map { $_->message } __PACKAGE__->stack; | ||
| 75 | } | ||
| 76 | |||
| 77 | |||
| 78 | { | ||
| 79 | package # hide this from PAUSE | ||
| 80 | Log::Message::Handlers; | ||
| 81 | |||
| 82 | sub history { | ||
| 83 | my $self = shift; | ||
| 84 | my $verbose = shift; | ||
| 85 | $verbose = 1 unless defined $verbose; # default to true | ||
| 86 | |||
| 87 | ### so you don't want us to print the msg? ### | ||
| 88 | return if defined $verbose && $verbose == 0; | ||
| 89 | |||
| 90 | local $| = 1; | ||
| 91 | my $old_fh = select $Term::UI::History::HISTORY_FH; | ||
| 92 | |||
| 93 | print $self->message . "\n"; | ||
| 94 | select $old_fh; | ||
| 95 | |||
| 96 | return; | ||
| 97 | } | ||
| 98 | } | ||
| 99 | |||
| 100 | |||
| 101 | =head1 GLOBAL VARIABLES | ||
| 102 | |||
| 103 | =over 4 | ||
| 104 | |||
| 105 | =item $HISTORY_FH | ||
| 106 | |||
| 107 | This is the filehandle all the messages sent to C<history()> are being | ||
| 108 | printed. This defaults to C<*STDOUT>. | ||
| 109 | |||
| 110 | =back | ||
| 111 | |||
| 112 | =head1 See Also | ||
| 113 | |||
| 114 | C<Log::Message::Simple>, C<Term::UI> | ||
| 115 | |||
| 116 | =head1 AUTHOR | ||
| 117 | |||
| 118 | This module by | ||
| 119 | Jos Boumans E<lt>kane@cpan.orgE<gt>. | ||
| 120 | |||
| 121 | =head1 COPYRIGHT | ||
| 122 | |||
| 123 | This module is | ||
| 124 | copyright (c) 2005 Jos Boumans E<lt>kane@cpan.orgE<gt>. | ||
| 125 | All rights reserved. | ||
| 126 | |||
| 127 | This library is free software; | ||
| 128 | you may redistribute and/or modify it under the same | ||
| 129 | terms as Perl itself. | ||
| 130 | |||
| 131 | =cut | ||
| 132 | |||
| 133 | 1; | ||
| 134 | |||
| 135 | # Local variables: | ||
| 136 | # c-indentation-style: bsd | ||
| 137 | # c-basic-offset: 4 | ||
| 138 | # indent-tabs-mode: nil | ||
| 139 | # End: | ||
| 140 | # vim: expandtab shiftwidth=4: |