1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
|
package Demeter::MRU;
=for Copyright
.
Copyright (c) 2006-2019 Bruce Ravel (http://bruceravel.github.io/home).
All rights reserved.
.
This file is free software; you can redistribute it and/or
modify it under the same terms as Perl itself. See The Perl
Artistic License.
.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
=cut
use Moose::Role;
use Demeter::IniReader;
use Demeter::IniWriter;
#use Config::INI::Writer;
#use Config::IniFiles;
use File::Spec;
use List::MoreUtils qw(uniq);
use Encode qw(decode);
my $max_mru = 15;
sub push_mru {
my ($self, $group, $file, $record) = @_;
return $self if ($self->mo->ui eq 'web');
if ($record) {
$file = $file . " <$record>";
};
my $stash = $self->stash_folder;
$stash =~ s{\\}{\\\\}g if $self->is_windows; # it seems like there should be something more elegant...
return $self if ($file =~ m{$stash});
my $mrufile = File::Spec->catfile($self->dot_folder, "demeter.mru");
my $slurp = Demeter->slurp($mrufile);
my $rmru = Demeter::IniReader->read_file($mrufile); # no clue what causes it, but this averts a crash in Config::INI:Reader
my %mru = %$rmru;
#tie %mru, 'Config::IniFiles', ( -file => $mrufile );
my @list_of_files;
if (exists $mru{$group}) {
my %hash = %{ $mru{$group} };
@list_of_files = map { $hash{$_} } sort {$a <=> $b} keys %hash;
} else {
$mru{$group} = {};
};
unshift @list_of_files, $file;
@list_of_files = uniq @list_of_files;
($#list_of_files = $max_mru) if ($#list_of_files > $max_mru);
my $i = 0;
foreach my $f (@list_of_files) {
$mru{$group}{$i} = $f;
++$i;
};
#tied(%mru)->WriteConfig($mrufile);
Demeter::IniWriter->write_file(\%mru, $mrufile);
#Config::INI::Writer->write_file(\%mru, $mrufile);
#undef %mru;
return $self;
};
sub get_mru_list {
my ($self, @groups) = @_;
#my $rmru = Demeter::IniReader->read_file(File::Spec->catfile($self->dot_folder, "demeter.mru"));
my $rmru;
eval {local $SIG{__DIE__}=sub {}; $rmru = Demeter::IniReader->read_file(File::Spec->catfile($self->dot_folder, "demeter.mru"))};
return () if ($@);
my %mru = %$rmru;
#tie %mru, 'Config::IniFiles', ( -file => File::Spec->catfile($self->dot_folder, "demeter.mru") );
my @list_of_files = ();
foreach my $g (@groups) {
next if not $mru{$g};
my %hash = %{ $mru{$g} };
#foreach my $k (keys %hash) {
# $hash{$k} = decode('UTF-8', $hash{$k});
# (-e $hash{$k}) ? print "yup ".$hash{$k}.$/ :print "nope ".$hash{$k}.$/ ;
#};
push @list_of_files, map { [$hash{$_}, $g] } grep {$self->minus_e($hash{$_})} sort {$a <=> $b} keys %hash;
};
undef %mru;
return @list_of_files;
};
sub minus_e {
my ($self, $string) = @_;
return 1 if -e $string;
if ($string =~ m{(\s+<\d?>)\z}) {
$string =~ s{$1}{};
};
return 1 if -e $string;
return 0;
};
1;
=head1 NAME
Demeter::MRU - Handle lists of recently used file
=head1 VERSION
This documentation refers to Demeter version 0.9.26.
=head1 DESCRIPTION
This module contains methods for reading from and writing to lists of
recently used files.
=head1 METHODS
=over 4
=item C<push_mru>
Push a file onto the top of the list of recently used files for a file
group.
$atoms_object -> push_mru( "atoms", $input_file);
This pushes C<$input_file> to the head of the list of recent files in
the C<atoms> group.
You can maintain any number of groups. For instance, you might have
separate groups for data, project files, crystal data files, and so
on.
=item C<get_mru_list>
Return the list of recently used files from a file group:
my @list_of_files = $atoms_object->get_mru_list("atoms");
or
my @list_of_files = $atoms_object->get_mru_list(@list_of_goups);
The argument is one or more group names.
This list is actually a list of lists, like so:
[ [file1, group1],
[file2, group2],
...
[fileN, groupN],
]
where the groups are things like "atoms", "feff" -- that is categories
of files used to organize the ini file containing the recent files.
=back
=head1 DIAGNOSTICS
None.
=head1 CONFIGURATION AND ENVIRONMENT
The recently used file lists are kept in a file called F<demeter.mru>
in the dotfile directory, C<$HOME/.horae> on unix and C<something> on
WIndows.
See L<Demeter::Config> for a description of the configuration system.
=head1 DEPENDENCIES
Demeter's dependencies are in the F<Build.PL> file.
=head1 BUGS AND LIMITATIONS
=over 4
=item *
List length is 16 items. This should be configurable.
=back
Please report problems to the Ifeffit Mailing List
(L<http://cars9.uchicago.edu/mailman/listinfo/ifeffit/>)
Patches are welcome.
=head1 AUTHOR
Bruce Ravel, L<http://bruceravel.github.io/home>
L<http://bruceravel.github.io/demeter/>
=head1 LICENCE AND COPYRIGHT
Copyright (c) 2006-2019 Bruce Ravel (L<http://bruceravel.github.io/home>). All rights reserved.
This module is free software; you can redistribute it and/or
modify it under the same terms as Perl itself. See L<perlgpl>.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
=cut
|