| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package App::Kritika::Settings; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 1 |  |  | 1 |  | 653 | use strict; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 21 |  | 
| 4 | 1 |  |  | 1 |  | 5 | use warnings; | 
|  | 1 |  |  |  |  | 1 |  | 
|  | 1 |  |  |  |  | 20 |  | 
| 5 |  |  |  |  |  |  |  | 
| 6 | 1 |  |  | 1 |  | 4 | use Cwd qw(getcwd); | 
|  | 1 |  |  |  |  | 1 |  | 
|  | 1 |  |  |  |  | 50 |  | 
| 7 | 1 |  |  | 1 |  | 5 | use File::Basename qw(dirname); | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 41 |  | 
| 8 | 1 |  |  | 1 |  | 4 | use File::Spec; | 
|  | 1 |  |  |  |  | 1 |  | 
|  | 1 |  |  |  |  | 25 |  | 
| 9 | 1 |  |  | 1 |  | 380 | use File::HomeDir (); | 
|  | 1 |  |  |  |  | 4163 |  | 
|  | 1 |  |  |  |  | 445 |  | 
| 10 |  |  |  |  |  |  |  | 
| 11 |  |  |  |  |  |  | sub new { | 
| 12 | 5 |  |  | 5 | 0 | 10312 | my $class = shift; | 
| 13 | 5 |  |  |  |  | 11 | my (%params) = @_; | 
| 14 |  |  |  |  |  |  |  | 
| 15 | 5 |  |  |  |  | 9 | my $self = {}; | 
| 16 | 5 |  |  |  |  | 6 | bless $self, $class; | 
| 17 |  |  |  |  |  |  |  | 
| 18 | 5 |  |  |  |  | 13 | $self->{file} = $params{file}; | 
| 19 |  |  |  |  |  |  |  | 
| 20 | 5 |  |  |  |  | 13 | return $self; | 
| 21 |  |  |  |  |  |  | } | 
| 22 |  |  |  |  |  |  |  | 
| 23 |  |  |  |  |  |  | sub settings { | 
| 24 | 5 |  |  | 5 | 0 | 15 | my $self = shift; | 
| 25 |  |  |  |  |  |  |  | 
| 26 | 5 |  |  |  |  | 8 | my $rc_file = $self->_detect; | 
| 27 | 5 | 50 |  |  |  | 14 | die "Error: Can't find rc file\n" unless defined $rc_file; | 
| 28 |  |  |  |  |  |  |  | 
| 29 | 5 | 50 |  |  |  | 5 | my $config = do { local $/; open my $fh, '<', $rc_file or die $!; <$fh> }; | 
|  | 5 |  |  |  |  | 12 |  | 
|  | 5 |  |  |  |  | 144 |  | 
|  | 5 |  |  |  |  | 129 |  | 
| 30 |  |  |  |  |  |  |  | 
| 31 | 5 |  |  |  |  | 15 | my $settings = $self->_parse($config); | 
| 32 |  |  |  |  |  |  |  | 
| 33 | 5 | 100 |  |  |  | 9 | if (!$settings->{root}) { | 
| 34 | 4 |  |  |  |  | 108 | $settings->{root} = dirname $rc_file; | 
| 35 |  |  |  |  |  |  | } | 
| 36 |  |  |  |  |  |  |  | 
| 37 | 5 |  |  |  |  | 27 | return $settings; | 
| 38 |  |  |  |  |  |  | } | 
| 39 |  |  |  |  |  |  |  | 
| 40 |  |  |  |  |  |  | sub _detect { | 
| 41 | 5 |  |  | 5 |  | 6 | my $self = shift; | 
| 42 |  |  |  |  |  |  |  | 
| 43 | 5 |  |  |  |  | 175 | my $dirname = dirname($self->{file}); | 
| 44 |  |  |  |  |  |  |  | 
| 45 | 5 | 100 |  |  |  | 37 | if (!File::Spec->file_name_is_absolute($dirname)) { | 
| 46 | 1 |  |  |  |  | 15 | $dirname = File::Spec->catdir(getcwd(), $dirname); | 
| 47 |  |  |  |  |  |  | } | 
| 48 |  |  |  |  |  |  |  | 
| 49 | 5 |  |  |  |  | 49 | my ($volume, $dirs, $file) = File::Spec->splitpath($dirname); | 
| 50 | 5 | 50 |  |  |  | 33 | $dirs = File::Spec->catdir($dirs, $file) if $file ne ''; | 
| 51 |  |  |  |  |  |  |  | 
| 52 | 5 |  |  |  |  | 25 | my @dir = File::Spec->splitdir($dirs); | 
| 53 | 5 | 50 |  |  |  | 11 | unshift @dir, $volume if $volume; | 
| 54 |  |  |  |  |  |  |  | 
| 55 | 5 |  |  |  |  | 7 | while (@dir) { | 
| 56 | 9 |  |  |  |  | 18 | for ($self->_rc_names) { | 
| 57 | 14 |  |  |  |  | 108 | my $location = File::Spec->catfile(@dir, $_); | 
| 58 | 14 | 100 |  |  |  | 207 | return $location if -f $location; | 
| 59 |  |  |  |  |  |  | } | 
| 60 |  |  |  |  |  |  |  | 
| 61 | 4 |  |  |  |  | 10 | pop @dir; | 
| 62 |  |  |  |  |  |  | } | 
| 63 |  |  |  |  |  |  |  | 
| 64 | 0 |  |  |  |  | 0 | for ($self->_rc_names) { | 
| 65 | 0 |  |  |  |  | 0 | my $location = File::Spec->catfile(File::HomeDir->my_home, $_); | 
| 66 | 0 | 0 |  |  |  | 0 | return $location if -f $location; | 
| 67 |  |  |  |  |  |  | } | 
| 68 |  |  |  |  |  |  |  | 
| 69 | 0 |  |  |  |  | 0 | return; | 
| 70 |  |  |  |  |  |  | } | 
| 71 |  |  |  |  |  |  |  | 
| 72 |  |  |  |  |  |  | sub _parse { | 
| 73 | 5 |  |  | 5 |  | 6 | my $self = shift; | 
| 74 | 5 |  |  |  |  | 8 | my ($input) = @_; | 
| 75 |  |  |  |  |  |  |  | 
| 76 | 5 |  |  |  |  | 14 | my @lines = split /\r?\n/, $input; | 
| 77 |  |  |  |  |  |  |  | 
| 78 | 5 |  |  |  |  | 8 | my $options = {}; | 
| 79 | 5 |  |  |  |  | 9 | foreach my $line (@lines) { | 
| 80 | 5 | 50 |  |  |  | 11 | next if $line =~ m/^\s*#/; | 
| 81 | 5 | 50 |  |  |  | 9 | next if $line eq ''; | 
| 82 |  |  |  |  |  |  |  | 
| 83 | 5 |  |  |  |  | 10 | my ($key, $value) = split /=/, $line, 2; | 
| 84 | 5 |  |  |  |  | 12 | $key =~ s{^\s+}{}; | 
| 85 | 5 |  |  |  |  | 10 | $key =~ s{\s+$}{}; | 
| 86 | 5 |  |  |  |  | 6 | $value =~ s{^\s+}{}; | 
| 87 | 5 |  |  |  |  | 5 | $value =~ s{\s+$}{}; | 
| 88 |  |  |  |  |  |  |  | 
| 89 | 5 |  |  |  |  | 14 | $options->{$key} = $value; | 
| 90 |  |  |  |  |  |  | } | 
| 91 |  |  |  |  |  |  |  | 
| 92 | 5 |  |  |  |  | 8 | return $options; | 
| 93 |  |  |  |  |  |  | } | 
| 94 |  |  |  |  |  |  |  | 
| 95 |  |  |  |  |  |  | sub _rc_names { | 
| 96 | 9 |  |  | 9 |  | 21 | return ('.kritikarc', '_kritikarc'); | 
| 97 |  |  |  |  |  |  | } | 
| 98 |  |  |  |  |  |  |  | 
| 99 |  |  |  |  |  |  | 1; |