| 1 | package JSON::Karabiner ; | ||||||||||||
| 2 | 17 | $JSON::Karabiner::VERSION = '0.011'; | |||||||||||
| 3 | 17 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | 17 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | 17 | ▶ | use JSON; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | 17 | ▶ | use Carp; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | 17 | ▶ | use File::HomeDir; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 8 | 17 | ▶ | use JSON::Karabiner::Rule; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 9 | |||||||||||||
| 10 | sub new { | ||||||||||||
| 11 | 34 | ▶ | my $class = shift; | ||||||||||
|
Subroutine
new: called CC=10 SCAR=23.1
Pod
documented
| |||||||||||||
| 12 | 34 | my $title = shift; | |||||||||||
| 13 | 34 | my $file = shift; | |||||||||||
| 14 | 34 | my $opts = shift; | |||||||||||
| 15 | |||||||||||||
| 16 | 34 | ▶ | if ($opts) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 17 | 4 | ▶ | if (ref $opts ne 'HASH') { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 18 | 0 | croak 'Options must be passed as a hash reference.'; | |||||||||||
| 19 | } | ||||||||||||
| 20 | } | ||||||||||||
| 21 | 34 | ▶ | croak 'JSON::Karabiner constructor requires a title for the modification.' if !$title; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 22 | 32 | ▶ | croak 'JSON::Karabiner constructor requires a file name.' if !$file; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 23 | 31 | ▶ | croak 'File names are required to have a .json extenstion' if $file !~ /\.json$/; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 24 | 30 | my $home = File::HomeDir->my_home; | |||||||||||
| 25 | my $self = { | ||||||||||||
| 26 | _file => $file, | ||||||||||||
| 27 | 30 | ▶ | _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/"
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'}
| |||||||||||||
| 28 | _karabiner => { title => $title, rules => [] }, | ||||||||||||
| 29 | _fake_write_flag => 0, | ||||||||||||
| 30 | _rule_obj => '', | ||||||||||||
| 31 | }; | ||||||||||||
| 32 | 30 | ▶ | if (!-d $self->{_mod_file_dir}) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 33 | 28 | ▶ | if ($opts->{mod_file_dir}) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 34 | 2 | croak "The directory you attempted to set with the 'mod_file_dir' option does not exist.\n\n"; | |||||||||||
| 35 | } else { | ||||||||||||
| 36 | 26 | ▶ | 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
| |||||||||||||
| 37 | } | ||||||||||||
| 38 | } | ||||||||||||
| 39 | 28 | bless $self, $class; | |||||||||||
| 40 | 28 | return $self; | |||||||||||
| 41 | } | ||||||||||||
| 42 | |||||||||||||
| 43 | # used by test scripts | ||||||||||||
| 44 | sub _fake_write_file { | ||||||||||||
| 45 | 3 | ▶ | my $s = shift; | ||||||||||
|
Subroutine
_fake_write_file: called CC=1 SCAR=0.2
| |||||||||||||
| 46 | 3 | $s->{_fake_write_flag} = 1; | |||||||||||
| 47 | 3 | $s->write_file; | |||||||||||
| 48 | 0 | $s->{_fake_write_flag} = 0; | |||||||||||
| 49 | } | ||||||||||||
| 50 | |||||||||||||
| 51 | sub write_file { | ||||||||||||
| 52 | 5 | ▶ | my $s = shift; | ||||||||||
|
Subroutine
write_file: called CC=7 SCAR=19.6
Pod
documented
| |||||||||||||
| 53 | 5 | my $using_dsl = ((caller(0))[0] eq 'JSON::Karabiner::Manipulator'); | |||||||||||
| 54 | 5 | my $file = $s->{_file}; | |||||||||||
| 55 | 5 | my $dir = $s->{_mod_file_dir}; | |||||||||||
| 56 | 5 | my $destination = $dir . $file; | |||||||||||
| 57 | |||||||||||||
| 58 | 5 | ▶ | if ($using_dsl) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 59 | 2 | my $rule = $s->{_karabiner}{rules}; | |||||||||||
| 60 | 2 | ▶ | if (!@main::saved_manips) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 61 | 2 | @main::saved_manips = (); | |||||||||||
| 62 | } | ||||||||||||
| 63 | 2 | foreach my $r (@$rule) { | |||||||||||
| 64 | 2 | foreach my $manip ($r->{manipulators}) { | |||||||||||
| 65 | 2 | push @main::saved_manips, @{$manip}; | |||||||||||
| 66 | } | ||||||||||||
| 67 | } | ||||||||||||
| 68 | |||||||||||||
| 69 | 2 | @{$s->{_karabiner}->{rules}} = @main::saved_manips; | |||||||||||
| 70 | } | ||||||||||||
| 71 | |||||||||||||
| 72 | 5 | my $json = $s->_get_json(); | |||||||||||
| 73 | |||||||||||||
| 74 | #TODO ensure it works with utf8 | ||||||||||||
| 75 | 2 | ▶ | if (!$s->{_fake_write_flag}) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 76 | 1 | ▶ | open (FH, '>', $destination) or die 'Could not open file for writing.'; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 77 | 0 | print FH $json; | |||||||||||
| 78 | 0 | close FH; | |||||||||||
| 79 | } | ||||||||||||
| 80 | |||||||||||||
| 81 | 1 | 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" | |||||||||||
| 82 | } | ||||||||||||
| 83 | |||||||||||||
| 84 | sub _get_json { | ||||||||||||
| 85 | 5 | ▶ | my $s = shift; | ||||||||||
|
Subroutine
_get_json: called CC=1 SCAR=0.0
| |||||||||||||
| 86 | 5 | my $json = JSON->new(); | |||||||||||
| 87 | 5 | $json = $json->convert_blessed(); | |||||||||||
| 88 | 5 | return $json->pretty->encode($s->{_karabiner}); | |||||||||||
| 89 | } | ||||||||||||
| 90 | |||||||||||||
| 91 | sub add_rule { | ||||||||||||
| 92 | 27 | ▶ | my $s = shift; | ||||||||||
|
Subroutine
add_rule: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||
| 93 | 27 | my $desc = shift; | |||||||||||
| 94 | 27 | ▶ | croak "No description passed to rule." if !$desc; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 95 | 25 | my $rule = JSON::Karabiner::Rule->new($desc); | |||||||||||
| 96 | 25 | $s->_add_rule($rule); | |||||||||||
| 97 | 25 | $s->{_rule_object} = $rule; | |||||||||||
| 98 | 25 | return $rule; | |||||||||||
| 99 | } | ||||||||||||
| 100 | |||||||||||||
| 101 | sub _add_rule { | ||||||||||||
| 102 | 25 | ▶ | my $s = shift; | ||||||||||
|
Subroutine
_add_rule: called CC=1 SCAR=0.0
| |||||||||||||
| 103 | 25 | my $rule = shift; | |||||||||||
| 104 | 25 | push @{$s->{_karabiner}{rules}}, $rule; | |||||||||||
| 105 | } | ||||||||||||
| 106 | |||||||||||||
| 107 | ▶ | sub _check_if_file_exits { | |||||||||||
|
Subroutine
_check_if_file_exits: not called CC=1 SCAR=0.0
| |||||||||||||
| 108 | |||||||||||||
| 109 | } | ||||||||||||
| 110 | |||||||||||||
| 111 | sub _dump_json { | ||||||||||||
| 112 | 20 | ▶ | my $s = shift; | ||||||||||
|
Subroutine
_dump_json: called CC=1 SCAR=0.0
| |||||||||||||
| 113 | 20 | my $json = JSON->new(); | |||||||||||
| 114 | 20 | $json = $json->convert_blessed(); | |||||||||||
| 115 | |||||||||||||
| 116 | # suppress validity tests | ||||||||||||
| 117 | 20 | $s->{_rule_object}->_disable_validity_tests(); | |||||||||||
| 118 | |||||||||||||
| 119 | 17 | ▶ | use Data::Dumper qw(Dumper); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 120 | 20 | print Dumper $json->pretty->encode($s->{_karabiner}); | |||||||||||
| 121 | |||||||||||||
| 122 | # renable validity tests | ||||||||||||
| 123 | 20 | $s->{_rule_object}->_enable_validity_tests(); | |||||||||||
| 124 | } | ||||||||||||
| 125 | |||||||||||||
| 126 | |||||||||||||
| 127 | # ABSTRACT: easy JSON code generation for Karabiner-Elements | ||||||||||||
| 128 | |||||||||||||
| 129 | 17 | 1; | |||||||||||
| 130 | |||||||||||||
| 131 | __END__ | ||||||||||||