File Coverage

lib/Term/RouterCLI/Log/History.pm
Criterion Covered Total %
statement 18 59 30.5
branch 0 10 0.0
condition 0 9 0.0
subroutine 6 11 54.5
pod 0 5 0.0
total 24 94 25.5


line stmt bran cond sub pod time code
1             #####################################################################
2             # This program is not guaranteed to work at all, and by using this #
3             # program you release the author of any and all liability. #
4             # #
5             # You may use this code as long as you are in compliance with the #
6             # license (see the LICENSE file) and this notice, disclaimer and #
7             # comment box remain intact and unchanged. #
8             # #
9             # Package: Term::RouterCLI::Log #
10             # Class: History #
11             # Description: Methods for building a Router (Stanford) style CLI #
12             # #
13             # Written by: Bret Jordan (jordan at open1x littledot org) #
14             # Created: 2011-02-21 #
15             #####################################################################
16             #
17             #
18             #
19             #
20             package Term::RouterCLI::Log::History;
21              
22 4     4   51 use 5.8.8;
  4         13  
  4         186  
23 4     4   22 use strict;
  4         7  
  4         138  
24 4     4   22 use warnings;
  4         8  
  4         164  
25 4     4   22 use parent qw(Term::RouterCLI::Log);
  4         7  
  4         36  
26 4     4   256 use Term::RouterCLI::Debugger;
  4         9  
  4         76  
27 4     4   20 use Log::Log4perl;
  4         6  
  4         26  
28              
29             our $VERSION = '1.00';
30             $VERSION = eval $VERSION;
31              
32              
33             my $oDebugger = new Term::RouterCLI::Debugger();
34              
35              
36             sub ClearHistory
37             {
38             # This method will clear the history out of the terminal.
39 0     0 0   my $self = shift;
40 0           $self->{'_oParent'}->{_oTerm}->clear_history();
41 0           $self->ClearExistingLogData();
42             }
43              
44             sub GetHistory
45             {
46             # This method will get the current history from the terminal
47             # Return:
48             # array_ref(command history)
49 0     0 0   my $self = shift;
50 0           my @aHistoryCommandBuffer = $self->{'_oParent'}->{_oTerm}->GetHistory();
51 0           return \@aHistoryCommandBuffer;
52             }
53              
54             sub LoadCommandHistoryFromFile
55             {
56             # This method will read in the content of the local history file if it exists
57 0     0 0   my $self = shift;
58 0           my $logger = $oDebugger->GetLogger($self);
59            
60 0           $logger->debug("$self->{'_sName'} - ", '### Entering Method ###');
61            
62 0           $self->ReadLogFile();
63              
64 0           foreach (@{$self->{'_aCurrentLogData'}})
  0            
65             {
66 0           $logger->debug("$self->{'_sName'} - _aCurrentLogData: $_");
67 0           chomp();
68 0 0         next unless /\S/;
69 0           $self->{'_oParent'}->{_oTerm}->addhistory($_);
70             }
71 0           $logger->debug("$self->{'_sName'} - ", '### Leaving Method ###');
72             }
73              
74             sub SaveCommandHistoryToFile
75             {
76             # This method will save the current command history to a text file or other sources as defined
77 0     0 0   my $self = shift;
78 0           my $logger = $oDebugger->GetLogger($self);
79            
80 0           $logger->debug("$self->{'_sName'} - ", '### Entering Method ###');
81              
82 0 0 0       return unless ((defined $self->{'_sFilename'}) && ($self->{'_iFileLength'} > 0));
83 0 0         return unless $self->{'_oParent'}->{_oTerm}->can('GetHistory');
84            
85 0           $self->{'_aCurrentLogData'} = $self->GetHistory();
86            
87             # Let store the current size of the log data
88 0           $self->SetCurrentLogSize();
89            
90            
91 0           $logger->debug("$self->{'_sName'} - _aCurrentLogData:\n", ${$oDebugger->DumpArray($self->{'_aCurrentLogData'})});
  0            
92            
93 0           $self->WriteExistingLogData();
94 0           $logger->debug("$self->{'_sName'} - ", '### Leaving Method ###');
95             }
96              
97             sub PrintHistory
98             {
99             # This method will print out the command history from the terminal. You can pass in an
100             # integer to tell it how many lines of history to print out.
101 0     0 0   my $self = shift;
102 0           my $logger = $oDebugger->GetLogger($self);
103              
104 0           my $iNumberOfLinesToPrint = -1;
105            
106             # Lets grab the number of lines to print from the passed in values, but only if it is an integer
107 0           my $parameter = $self->{'_oParent'}->{_aCommandArguments}->[0];
108 0 0 0       if((defined $parameter) && ($parameter =~ /^(\d+)$/))
109             {
110 0           $iNumberOfLinesToPrint = $1;
111             }
112              
113 0           my $aHistoryCommandBuffer = $self->GetHistory();
114            
115             # What is the current number of lines in the history buffer
116 0           my $iHistoryBufferSize = @$aHistoryCommandBuffer;
117            
118             # Lets only print out the number of records requested up to the size of the history buffer
119 0 0 0       if ($iNumberOfLinesToPrint == -1 || $iNumberOfLinesToPrint > $iHistoryBufferSize)
120             {
121 0           $iNumberOfLinesToPrint = $iHistoryBufferSize;
122             }
123            
124             # Set a starting point for the array so that we will only print the last X number of history items if requested
125 0           my $iArrayOffsetNumber = $iHistoryBufferSize - $iNumberOfLinesToPrint;
126            
127             # Print out the lines of the history buffer
128 0           foreach ($iArrayOffsetNumber..$iHistoryBufferSize-1)
129             {
130 0           print "\($_\) $aHistoryCommandBuffer->[$_]\n";
131             }
132             }
133              
134             return 1;