| 1 | #!/usr/bin/env perl | ||
| 2 | |||
| 3 | package Neovim::RPC::App; | ||
| 4 | our $AUTHORITY = 'cpan:YANICK'; | ||
| 5 | $Neovim::RPC::App::VERSION = '1.0.1'; | ||
| 6 | use Log::Any::Adapter; | ||
| 7 | Log::Any::Adapter->set( | ||
| 8 | #{ category => 'MsgPack::Decoder' }, | ||
| 9 | 'Stderr', log_level => 'debug' ); | ||
| 10 | |||
| 11 | use 5.10.0; | ||
| 12 | |||
| 13 | use strict; | ||
| 14 | use warnings; | ||
| 15 | $| = 0; | ||
| 16 | |||
| 17 | use MooseX::App::Simple; | ||
| 18 | |||
| 19 | use Getopt::Long; | ||
| 20 | use Neovim::RPC; | ||
| 21 | |||
| 22 | use Log::Any '$Log'; | ||
| 23 | |||
| 24 | option std => ( | ||
| 25 | is => 'ro', | ||
| 26 | documentation => 'use stdin/stdout', | ||
| 27 | isa => 'Bool', | ||
| 28 | ); | ||
| 29 | |||
| 30 | option include => ( | ||
| 31 | cmd_aliases => [ 'I' ], | ||
| 32 | is => 'ro', | ||
| 33 | documentation => 'custom library path', | ||
| 34 | trigger => sub { | ||
| 35 | push @INC, split ',', $_[1]; | ||
| 36 | }, | ||
| 37 | ); | ||
| 38 | |||
| 39 | parameter io => ( | ||
| 40 | documentation => 'socket/address to use, defaults to NVIM_LISTEN_ADDRESS', | ||
| 41 | is => 'ro', | ||
| 42 | lazy => 1, | ||
| 43 | default => sub { | ||
| 44 | return [*STDIN, *STDOUT] if $_[0]->std; | ||
| 45 | |||
| 46 | $ENV{NVIM_LISTEN_ADDRESS} | ||
| 47 | or die "io not provided and NVIM_LISTEN_ADDRESS not set\n" | ||
| 48 | }, | ||
| 49 | ); | ||
| 50 | |||
| 51 | has rpc => ( | ||
| 52 | is => 'ro', | ||
| 53 | lazy => 1, | ||
| 54 | default => sub { | ||
| 55 | my $rpc = Neovim::RPC->new( | ||
| 56 | io => $_[0]->io, | ||
| 57 | ); | ||
| 58 | |||
| 59 | $rpc->subscribe( 'write', sub { $Log->debugf( 'write %s', $_[0]->message ) } ); | ||
| 60 | $rpc->subscribe( 'receive', sub { $Log->debugf( "receive %s", $_[0]->message ) } ); | ||
| 61 | $rpc->api->ready->done( sub { | ||
| 62 | $rpc->load_plugin( 'LoadPlugin' ); | ||
| 63 | }); | ||
| 64 | |||
| 65 | return $rpc; | ||
| 66 | }, | ||
| 67 | ); | ||
| 68 | |||
| 69 | |||
| 70 | sub run { | ||
| 71 | my $self = shift; | ||
| 72 | |||
| 73 | $self->rpc->loop->run; | ||
| 74 | } | ||
| 75 | |||
| 76 | 1; | ||
| 77 | |||
| 78 | |||
| 79 | Neovim::RPC::App->new_with_options->run; | ||
| 80 | |||
| 81 | __END__ |