Karabiner.pm

filter: Statementstmt 95.5% 108 / 113 Branchbran 76.6% 23 / 30 Conditioncond 100% 2 / 2 MC/DCmcdc 100% 1 / 1 Subroutinesub 94.4% 17 / 18 Podpod 33.3% 1 / 3 totaltotal 91.0% 152 / 167 CC 20 SCAR 18.0
CC
20
Cov
92%
write_file
10.3
new
10.0
add_rule
2.0
CRAP 6.0
« Karabiner/Manipulator.pm ↑ Summary Karabiner/Manipulator/Conditions/Input_source_if.pm »
1 package JSON::Karabiner ;
2 19 $JSON::Karabiner::VERSION = '0.018';
3 19 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 19 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 19 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 19 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 19 ▶use File::HomeDir;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 19 ▶use JSON::Karabiner::Rule;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 37 ▶sub new {
Subroutine new: called CC=10 SCAR=23.1
Pod undocumented
11 37 my $class = shift;
12 37 my $title = shift;
13 37 my $file = shift;
14 37 my $opts = shift;
15  
16 37 ▶ if ($opts) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts) 4 33 37
17 4 ▶ if (ref $opts ne 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $opts ne "HASH") 0 4 4
18 0 croak 'Options must be passed as a hash reference.';
19 }
20 }
21 37 ▶ croak 'JSON::Karabiner constructor requires a title for the modification.' if !$title;
Branch: 1 branch
BranchTrueFalseTotal
unless $title 2 35 37
22 35 ▶ croak 'JSON::Karabiner constructor requires a file name.' if !$file;
Branch: 1 branch
BranchTrueFalseTotal
unless $file 1 34 35
23 34 ▶ croak 'File names are required to have a .json extenstion' if $file !~ /\.json$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $file =~ /\.json$/ 1 33 34
24 33 my $home = File::HomeDir->my_home;
25 my $self = {
26 _file => $file,
27 33 ▶ _mod_file_dir => $opts->{mod_file_dir} || "$home/.config/karabiner/assets/complex_modifications/",
Condition100%2/2
$opts->{'mod_file_dir'} || "$home/.config/karabiner/assets/complex_modifications/"
l!l
429
MC/DC100%1/1
$opts->{'mod_file_dir'} || "$home/.config/karabiner/assets/complex_modifications/"
$opts->{'mod_file_dir'}
Truth table100%2/2
A
A= $opts->{'mod_file_dir'}
Aresult
11
00
28 _karabiner => { title => $title, rules => [] },
29 _fake_write_flag => 0,
30 _rule_obj => '',
31 };
32 33 ▶ if (!-d $self->{_mod_file_dir}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $self->{"_mod_file_dir"}) 31 2 33
33 31 ▶ if ($opts->{mod_file_dir}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{'mod_file_dir'}) { } 2 29 31
34 2 croak "The directory you attempted to set with the 'mod_file_dir' option does not exist.\n\n";
35 } else {
36 29 ▶ croak "The default directory for storing complex modifications does not exist. Do you have Karabiner-Elements installed? Is it installed with a non-standard configuration? Try setting the location of the directory manually with the 'mod_file_dir' option. Consult this module's documentation for more information with using the 'perldoc JSON::Karabiner' command in the terminal.\n\n" unless $ENV{HARNESS_ACTIVE};
Branch: 1 branch
BranchTrueFalseTotal
unless $ENV{"HARNESS_ACTIVE"} 0 29 29
37 }
38 }
39 31 bless $self, $class;
40 19 ▶ { no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
41 31 $main::has_delayed_action = '';
42 31 $main::save_to_file_name = '';
43 }
44 31 return $self;
45 }
46  
47 # used by test scripts
48 3 ▶sub _fake_write_file {
Subroutine _fake_write_file: called CC=1 SCAR=0.2
49 3 my $s = shift;
50 3 $s->{_fake_write_flag} = 1;
51 3 $s->write_file;
52 0 $s->{_fake_write_flag} = 0;
53 }
54  
55 7 ▶sub write_file {
Subroutine write_file: called CC=10 SCAR=23.4
Pod documented
56  
57 7 my $s = shift;
58 7 my $using_dsl = ((caller(0))[0] eq 'JSON::Karabiner::Manipulator');
59 7 my $file = $s->{_file};
60 7 my $dir = $s->{_mod_file_dir};
61 7 my $destination = $dir . $file;
62  
63 7 ▶ if ($using_dsl) {
Branch: 1 branch
BranchTrueFalseTotal
if ($using_dsl) 4 3 7
64 4 my $rule = $s->{_karabiner}{rules};
65 4 ▶ if (!%main::saved_manips) {
Branch: 1 branch
BranchTrueFalseTotal
unless (%main::saved_manips) 4 0 4
66 4 %main::saved_manips = ();
67 19 ▶ { no warnings 'once'; $main::last_manip_set = ''; }
Subroutine BEGIN: called CC=1 SCAR=0.0
68 }
69 4 foreach my $r (@$rule) {
70 4 my $current_manip_set = $r->{description};
71 19 ▶ { no warnings 'once'; $main::manip_sets{$current_manip_set}{description} = $current_manip_set; }
Subroutine BEGIN: called CC=1 SCAR=0.0
72 4 ▶ if (! defined $main::manip_sets{$current_manip_set}{manipulators}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $main::manip_sets{$current_manip_set}{"manipulators"}) 2 2 4
73 2 $main::manip_sets{$current_manip_set}{manipulators} = [];
74 }
75 4 foreach my $manip ($r->{manipulators}) {
76 4 push @main::saved_manips, @{$manip};
77 4 push @{$main::manip_sets{$current_manip_set}{manipulators}}, @{$manip};
78 }
79 }
80  
81 4 my $count = 0;
82 4 foreach my $k (sort keys %main::manip_sets) {
83 4 my $manipulators = $main::manip_sets{$k}{manipulators};
84 4 my $description = $main::manip_sets{$k}{description};
85 4 my $new_hash = { manipulators => $manipulators, description => $description };
86 4 $s->{_karabiner}->{rules}[$count++] = $new_hash;
87  
88 }
89  
90 }
91 7 my $json = $s->_get_json();
92  
93 #TODO ensure it works with utf8
94 4 ▶ if (!$s->{_fake_write_flag}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($s->{"_fake_write_flag"}) 0 4 4
95 0 ▶ open (FH, '>', $destination) or die 'Could not open file for writing.';
Branch: 1 branch
BranchTrueFalseTotal
open FH, ">", $destination or die "Could not open file for writing." 0 0 0
96 0 print FH $json;
97 0 close FH;
98 }
99  
100 4 ▶ print "Your rules were successfully written to:\n\n $destination.\n\nOpen Karabiner-Elements to import the new rules you have generated.\n\nIf your rules do not appear, please report the issue to our issue queue:\n\nhttps://github.com/sdondley/JSON-Karabiner/issues \n\n" unless $ENV{HARNESS_ACTIVE};
Branch: 1 branch
BranchTrueFalseTotal
unless $ENV{"HARNESS_ACTIVE"} 0 4 4
101 }
102  
103 7 ▶sub _get_json {
Subroutine _get_json: called CC=1 SCAR=0.0
104 7 my $s = shift;
105 7 my $json = JSON->new();
106 7 $json = $json->convert_blessed();
107 7 return $json->canonical->pretty->encode($s->{_karabiner});
108 }
109  
110 30 ▶sub add_rule {
Subroutine add_rule: called CC=2 SCAR=6.9
Pod undocumented
111 30 my $s = shift;
112 30 my $desc = shift;
113 30 ▶ croak "No description passed to rule." if !$desc;
Branch: 1 branch
BranchTrueFalseTotal
unless $desc 2 28 30
114 28 my $rule = JSON::Karabiner::Rule->new($desc);
115 28 $s->_add_rule($rule);
116 28 $s->{_rule_object} = $rule;
117 28 return $rule;
118 }
119  
120 28 ▶sub _add_rule {
Subroutine _add_rule: called CC=1 SCAR=0.0
121 28 my $s = shift;
122 28 my $rule = shift;
123 28 push @{$s->{_karabiner}{rules}}, $rule;
124 }
125  
126 0 ▶sub _check_if_file_exits {
Subroutine _check_if_file_exits: not called CC=1 SCAR=0.0
127  
128 }
129  
130 21 ▶sub _dump_json {
Subroutine _dump_json: called CC=1 SCAR=0.0
131 21 my $s = shift;
132 21 my $json = JSON->new();
133 21 $json = $json->convert_blessed();
134  
135 # suppress validity tests
136 21 $s->{_rule_object}->_disable_validity_tests();
137  
138 19 ▶ use Data::Dumper qw(Dumper);
Subroutine BEGIN: called CC=1 SCAR=0.0
139 21 print Dumper $json->canonical->pretty->encode($s->{_karabiner});
140  
141 # renable validity tests
142 21 $s->{_rule_object}->_enable_validity_tests();
143 }
144  
145  
146 # ABSTRACT: easy JSON code generation for Karabiner-Elements
147  
148 19 1;
149  
150 __END__
« Karabiner/Manipulator.pm ↑ Summary Karabiner/Manipulator/Conditions/Input_source_if.pm »