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