source: trunk/LATMOS-Accounts/lib/LATMOS/Accounts/Cli/Context.pm @ 2424

Last change on this file since 2424 was 2424, checked in by nanardon, 4 years ago

Add setting command to la-cli

By the way this patch add a limit to history.

File size: 4.2 KB
Line 
1package LATMOS::Accounts::Cli::Context;
2
3# $Id: Cli.pm 2145 2018-08-29 18:15:46Z nanardon $
4
5use strict;
6use warnings;
7use Moose;
8use LATMOS::Accounts::Log;
9use LATMOS::Accounts::Utils;
10use Term::ReadLine;
11use Text::ParseWords;
12use Getopt::Long;
13use YAML;
14use utf8;
15use open qw( :std :utf8 );
16
17=head1 NAME
18
19LATMOS::Accounts::Cli - Command line interface functions
20
21=head1 DESCRIPTION
22
23This module handle envirronment and functons for L<la-cli> tools.
24
25=cut
26
27{
28    open (my $fh, "/dev/tty" )
29        or eval 'sub Term::ReadLine::findConsole { ("&STDIN", "&STDERR") }';
30    die $@ if $@;
31    close ($fh);
32}
33
34has Term => ( is => 'ro', isa => 'Term::ReadLine' );
35has TransMode => ( is => 'rw', isa => 'Bool', default => 0 );
36has TransStarted => ( is => 'rw', isa => 'Bool', default => 0 );
37has base => ( is => 'ro' );
38has La => ( is => 'ro' );
39has Out => ( is => 'ro' );
40has TempOut => ( is => 'rw' );
41has Interractive => ( is => 'rw', isa => 'Bool', default => 1 );
42has Preferences => ( is => 'rw', default => sub { {} } );
43
44=head1 FUNCTIONS
45
46=head2 new ($env, $labase)
47
48Create an envirronment object.
49
50C<$env> is functions descriptions.
51
52=cut
53
54around BUILDARGS => sub {
55    my $orig = shift;
56    my $class = shift;
57
58    my $term = Term::ReadLine->new('LA CLI', \*STDIN, \*STDOUT );
59    binmode($term->IN, ':utf8');
60    $term->MinLine(99999);
61    my $OUT = \*STDOUT;
62
63    return $class->$orig( Out => $OUT, Term => $term, @_ );
64
65};
66
67sub _historyFile { "$ENV{HOME}/.lacli_history" }
68
69sub ReadHistory {
70    my ( $self ) = @_;
71    $self->Term->ReadHistory( _historyFile() );
72}
73
74sub WriteHistory {
75    my ( $self ) = @_;
76    $self->Term->WriteHistory( _historyFile() );
77}
78
79sub _preferenceFile { "$ENV{HOME}/.lacli_preference" }
80
81sub ReadPreferences {
82    my ( $self ) = @_;
83
84    if (-f _preferenceFile() ) {
85        $self->Preferences( YAML::LoadFile( _preferenceFile() ) );
86    }
87   
88    $self->ApplySetting();
89}
90
91sub WritePreferences {
92    my ( $self ) = @_;
93
94    YAML::DumpFile( _preferenceFile(), $self->Preferences() );
95}
96
97sub DefaultSetting {
98    my ( $self, $setting ) = @_;
99
100    my $defaults = {
101        'historysize' => 1000,
102    };
103    if ($setting) {
104        return $defaults->{ $setting };
105    } else {
106        return sort keys %{ $defaults };
107    }
108}
109
110sub ApplySetting {
111    my ( $self ) = @_;
112
113    $self->Term->StifleHistory( $self->Setting('historysize') );
114}
115
116sub Setting {
117    my ( $self, $setting, $value ) = @_;
118
119    my $oldvalue = defined($self->Preferences->{settings}{ $setting })
120        ? $self->Preferences->{settings}{ $setting } : $self->DefaultSetting( $setting );
121
122    if (defined($value)) {
123        if ($value eq 'default') {
124            delete($self->Preferences->{settings}{ $setting });
125        } else {
126            $self->Preferences->{settings}{ $setting } = $value;
127        }
128
129        $self->ApplySetting();
130    }
131
132    return ($oldvalue);
133}
134
135=head2 print
136
137=cut
138
139sub print {
140    my ( $self, @args ) = @_;
141    my $out = $self->TempOut || $self->Out;
142
143    print $out @args;
144}
145
146=head2 printf
147
148=cut
149
150sub printf {
151    my ( $self, $str, @args ) = @_;
152    my $out = $self->TempOut || $self->Out;
153
154    printf $out $str, @args;
155}
156
157=head2 commit
158
159Call commit to base unelss in transaction mode
160
161=cut
162
163sub commit {
164    my ($self) = @_;
165    if ($self->TransMode || $self->TransStarted) {
166        $self->TransStarted(1);
167    } else {
168        $self->_commit;
169    }
170}
171
172sub _commit {
173    my ($self) = @_;
174    $self->base->commit;
175    $self->TransStarted(0);
176}
177
178=head2 rollback
179
180Perform rollback unless in transaction mode
181
182=cut
183
184sub rollback {
185    my ($self) = @_;
186    if ($self->TransMode) {
187        $self->print("All pending changes get rollback\n");
188    }
189    if (!$self->TransStarted) {
190        $self->_rollback;
191    }
192}
193
194sub _rollback {
195    my ($self) = @_;
196    $self->base->rollback;
197    $self->TransStarted(0);
198}
199
2001;
201
202__END__
203
204=head1 SEE ALSO
205
206L<LATMOS::Accounts>
207
208=head1 AUTHOR
209
210Olivier Thauvin, E<lt>olivier.thauvin@latmos.ipsl.frE<gt>
211
212=head1 COPYRIGHT AND LICENSE
213
214Copyright (C) 2008, 2009, 2010, 2011, 2012 CNRS SA/CETP/LATMOS
215
216This library is free software; you can redistribute it and/or modify
217it under the same terms as Perl itself, either Perl version 5.10.0 or,
218at your option, any later version of Perl 5 you may have available.
219
220=cut
Note: See TracBrowser for help on using the repository browser.