« Karabiner/Manipulator/Conditions/Variable_if.pm
↑ Summary
Karabiner/Manipulator/Conditions/Frontmost_application_if.pm »
| 1 | package JSON::Karabiner ; | ||||||||||||
| 2 | 15 | $JSON::Karabiner::VERSION = '0.008'; | |||||||||||
| 3 | 15 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | 15 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | 15 | ▶ | use JSON; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | 15 | ▶ | use Carp; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | 15 | ▶ | use JSON::Karabiner::Rule; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 8 | |||||||||||||
| 9 | sub new { | ||||||||||||
| 10 | 18 | ▶ | my $class = shift; | ||||||||||
|
Subroutine
new: called CC=9 SCAR=22.1
Pod
documented
| |||||||||||||
| 11 | 18 | my $title = shift; | |||||||||||
| 12 | 18 | my $file = shift; | |||||||||||
| 13 | 18 | my $opts = shift; | |||||||||||
| 14 | |||||||||||||
| 15 | 18 | ▶ | if ($opts) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 16 | 3 | ▶ | if (ref $opts ne 'HASH') { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 17 | 0 | croak 'Options must be passed as a hash reference.'; | |||||||||||
| 18 | } | ||||||||||||
| 19 | } | ||||||||||||
| 20 | 18 | ▶ | croak 'JSON::Karabiner constructor requires a title for the modification.' if !$title; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 21 | 16 | ▶ | croak 'JSON::Karabiner constructor requires a file name.' if !$file; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 22 | 15 | ▶ | croak 'File names are required to have a .json extenstion' if $file !~ /\.json$/; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 23 | my $self = { | ||||||||||||
| 24 | _file => $file, | ||||||||||||
| 25 | 14 | ▶ | _mod_file_dir => $opts->{mod_file_dir} || "$ENV{HOME}/.config/karabiner/assets/complex_modifications/", | ||||||||||
|
Condition100%2/2
$opts->{'mod_file_dir'} || "$ENV{'HOME'}/.config/karabiner/assets/complex_modifications/"
MC/DC100%1/1
$opts->{'mod_file_dir'} || "$ENV{'HOME'}/.config/karabiner/assets/complex_modifications/"
$opts->{'mod_file_dir'}
Truth table100%2/2
A
A= $opts->{'mod_file_dir'}
| |||||||||||||
| 26 | _karabiner => { title => $title, rules => [] }, | ||||||||||||
| 27 | _fake_write_flag => 0, | ||||||||||||
| 28 | _rule_obj => '', | ||||||||||||
| 29 | }; | ||||||||||||
| 30 | 14 | ▶ | if (!-d $self->{_mod_file_dir}) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 31 | 13 | ▶ | if ($opts->{_mod_file_dir}) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 32 | 0 | croak "The directory you attempted to set with the 'mod_file_dir' option does not exist.\n\n"; | |||||||||||
| 33 | } else { | ||||||||||||
| 34 | 13 | croak "The default directory for storing complex modifications does not exist. Do you have Karbiner-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"; | |||||||||||
| 35 | } | ||||||||||||
| 36 | } | ||||||||||||
| 37 | 1 | bless $self, $class; | |||||||||||
| 38 | 1 | return $self; | |||||||||||
| 39 | } | ||||||||||||
| 40 | |||||||||||||
| 41 | # used by test scripts | ||||||||||||
| 42 | sub _fake_write_file { | ||||||||||||
| 43 | 3 | ▶ | my $s = shift; | ||||||||||
|
Subroutine
_fake_write_file: called CC=1 SCAR=0.0
| |||||||||||||
| 44 | 3 | $s->{_fake_write_flag} = 1; | |||||||||||
| 45 | 3 | $s->write_file; | |||||||||||
| 46 | } | ||||||||||||
| 47 | |||||||||||||
| 48 | sub write_file { | ||||||||||||
| 49 | 3 | ▶ | my $s = shift; | ||||||||||
|
Subroutine
write_file: called CC=3 SCAR=16.8
Pod
documented
| |||||||||||||
| 50 | 3 | my $file = $s->{_file}; | |||||||||||
| 51 | 3 | my $dir = $s->{_mod_file_dir}; | |||||||||||
| 52 | 3 | my $destination = $dir . $file; | |||||||||||
| 53 | 3 | my $json = $s->_get_json(); | |||||||||||
| 54 | |||||||||||||
| 55 | #TODO ensure it works with utf8 | ||||||||||||
| 56 | 0 | ▶ | if (!$s->{_fake_write_flag}) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 57 | 0 | ▶ | open (FH, '>', $destination) or die 'Could not open file for writing.'; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 58 | 0 | print FH $json; | |||||||||||
| 59 | 0 | close FH; | |||||||||||
| 60 | } | ||||||||||||
| 61 | |||||||||||||
| 62 | 0 | 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" | |||||||||||
| 63 | } | ||||||||||||
| 64 | |||||||||||||
| 65 | sub _get_json { | ||||||||||||
| 66 | 3 | ▶ | my $s = shift; | ||||||||||
|
Subroutine
_get_json: called CC=1 SCAR=0.0
| |||||||||||||
| 67 | 3 | my $json = JSON->new(); | |||||||||||
| 68 | 3 | $json = $json->convert_blessed(); | |||||||||||
| 69 | 3 | return $json->pretty->encode($s->{_karabiner}); | |||||||||||
| 70 | } | ||||||||||||
| 71 | |||||||||||||
| 72 | sub add_rule { | ||||||||||||
| 73 | 1 | ▶ | my $s = shift; | ||||||||||
|
Subroutine
add_rule: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||
| 74 | 1 | my $desc = shift; | |||||||||||
| 75 | 1 | ▶ | croak "No description passed to rule." if !$desc; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 76 | 1 | my $rule = JSON::Karabiner::Rule->new($desc); | |||||||||||
| 77 | 1 | $s->_add_rule($rule); | |||||||||||
| 78 | 1 | $s->{_rule_object} = $rule; | |||||||||||
| 79 | 1 | return $rule; | |||||||||||
| 80 | } | ||||||||||||
| 81 | |||||||||||||
| 82 | sub _add_rule { | ||||||||||||
| 83 | 1 | ▶ | my $s = shift; | ||||||||||
|
Subroutine
_add_rule: called CC=1 SCAR=0.0
| |||||||||||||
| 84 | 1 | my $rule = shift; | |||||||||||
| 85 | 1 | push @{$s->{_karabiner}{rules}}, $rule; | |||||||||||
| 86 | } | ||||||||||||
| 87 | |||||||||||||
| 88 | ▶ | sub _check_if_file_exits { | |||||||||||
|
Subroutine
_check_if_file_exits: not called CC=1 SCAR=0.0
| |||||||||||||
| 89 | |||||||||||||
| 90 | } | ||||||||||||
| 91 | |||||||||||||
| 92 | sub _dump_json { | ||||||||||||
| 93 | 1 | ▶ | my $s = shift; | ||||||||||
|
Subroutine
_dump_json: called CC=1 SCAR=0.0
| |||||||||||||
| 94 | 1 | my $json = JSON->new(); | |||||||||||
| 95 | 1 | $json = $json->convert_blessed(); | |||||||||||
| 96 | |||||||||||||
| 97 | # suppress validity tests | ||||||||||||
| 98 | 1 | $s->{_rule_object}->_disable_validity_tests(); | |||||||||||
| 99 | |||||||||||||
| 100 | 15 | ▶ | use Data::Dumper qw(Dumper); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 101 | 1 | print Dumper $json->pretty->encode($s->{_karabiner}); | |||||||||||
| 102 | |||||||||||||
| 103 | # renable validity tests | ||||||||||||
| 104 | 1 | $s->{_rule_object}->_enable_validity_tests(); | |||||||||||
| 105 | } | ||||||||||||
| 106 | |||||||||||||
| 107 | |||||||||||||
| 108 | # ABSTRACT: easy JSON code generation for Karabiner-Elements | ||||||||||||
| 109 | |||||||||||||
| 110 | 15 | 1; | |||||||||||
| 111 | |||||||||||||
| 112 | __END__ | ||||||||||||