Giter VIP home page Giter VIP logo

libnvc's Introduction

libnvc

I realize nvim now provides libnvim.a, this repo may not be needed anymore.

another c++-20 nvim msgpack-rpc client, nvim-0.5.0 tested.
nvim's rpc interface is convenient but not something you can finish in 10 minutes.
this repo creates libnvc.a and you can use it to read/write a process running neovim easily.

image

start nvim and use asio_socket to talk to it:

#include "libnvc.hpp"

int main()
{
    // start nvim:
    // $ nvim --listen "127.0.0.1:6666"
    libnvc::asio_socket socket;
    if(!socket.connect("localhost", 6666)){
        throw std::runtime_error("failed to connect to localhost:6666");
    }

    libnvc::api_client client(&socket);
    client.nvim_input("$i123<CR>123<ESC>");
    client.nvim_buf_set_name(1, "1234");

or use reproc_device to spawn a process running nvim in background:

#include "libnvc.hpp"

int main()
{
    // spawn nvim process with default parameters
    libnvc::reproc_device reproc_dev;
    reproc_dev.spawn();

    libnvc::api_client client(&reproc_dev);
    client.nvim_ui_attach(100, 80, {{"rgb", true}, {"ext_linegrid", true}});
    client.nvim_input("$i123<CR>123<ESC>");
    client.nvim_buf_set_name(1, "1234");

build

there is zero dependenct for user's building environment.
libnvc use asio, mpack and reproc internally but hiden by pimpl.

# build the libnvc library, nvim should be in your PATH
$ cd $HOME
$ git clone https://github.com/etorth/libnvc.git
$ mkdir b_libnvc && cd b_libnvc
$ cmake ../libnvc -DCMAKE_INSTALL_PREFIX=install -DCMAKE_C_COMPILER=gcc-11 -DCMAKE_CXX_COMPILER=g++-11
$ make && make install

# build the sample project, a simple nvim gui
# this requires SDL2, SDL2-image and SDL2-ttf installed
$ cd $HOME
$ mkdir b_nvim_sdl2 && cd b_nvim_sdl2
$ cmake ../libnvc/sample/nvim_sdl2 -DCMAKE_INSTALL_PREFIX=install -DLIBNVC_INCLUDE=$HOME/b_libnvc/install/include -DLIBNVC_LIB=$HOME/b_libnvc/install/lib
$ make && make install

# run the sample gui
$ cd $HOME/b_nvim_sdl2/install/nvim_sdl2 && ./nvim_sdl2

libnvc's People

Contributors

etorth avatar tricktux avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

libnvc's Issues

nvim_sdl2 crash

[testuser@legion nvim_sdl2]$ ./nvim_sdl2
INFO: : req_id = req::nvim_ui_attach, seq_id = 2
terminate called after throwing an instance of 'std::out_of_range'
  what():  vector::_M_range_check: __n (which is 0) >= this->size() (which is 0)
Aborted (core dumped)
[testuser@legion nvim_sdl2]$ gdb ./nvim_sdl2
Reading symbols from ./nvim_sdl2...
(gdb) r
Starting program: /home/testuser/b_nvim_sdl2/install/nvim_sdl2/nvim_sdl2
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/usr/lib/libthread_db.so.1".
[Detaching after fork from child process 2468]
[New Thread 0x7ffff7253700 (LWP 2469)]
[New Thread 0x7fffe9124700 (LWP 2470)]
[New Thread 0x7fffe3fff700 (LWP 2471)]
[New Thread 0x7fffe8923700 (LWP 2472)]
[New Thread 0x7fffe37fe700 (LWP 2473)]
[New Thread 0x7ffff41ab700 (LWP 2474)]
INFO: : req_id = req::nvim_ui_attach, seq_id = 2
DEBUG: Ignored notif: hl_group_set
<<some output removed>>
INFO:
DEBUG: Ignored notif: busy_stop
terminate called after throwing an instance of 'std::runtime_error'
  what():  In file: nvimclient.cpp:53, function: size_t peek_utf8_code(const char*, size_t, uint32_t*): Invalid argument: empty string

Thread 1 "nvim_sdl2" received signal SIGABRT, Aborted.
0x00007ffff78fcce5 in raise () from /usr/lib/libc.so.6
(gdb) info stack
#0  0x00007ffff78fcce5 in raise () from /usr/lib/libc.so.6
#1  0x00007ffff78e6857 in abort () from /usr/lib/libc.so.6
#2  0x00007ffff7c7f81d in __gnu_cxx::__verbose_terminate_handler ()
    at /build/gcc/src/gcc/libstdc++-v3/libsupc++/vterminate.cc:95
#3  0x00007ffff7c8c2ca in __cxxabiv1::__terminate (handler=<optimized out>)
    at /build/gcc/src/gcc/libstdc++-v3/libsupc++/eh_terminate.cc:48
#4  0x00007ffff7c8c337 in std::terminate () at /build/gcc/src/gcc/libstdc++-v3/libsupc++/eh_terminate.cc:58
#5  0x00007ffff7c8c59e in __cxxabiv1::__cxa_throw (obj=<optimized out>,
    tinfo=0x55555574b920 <typeinfo for std::runtime_error@@GLIBCXX_3.4>,
    dest=0x7ffff7ca27f0 <std::runtime_error::~runtime_error()>)
    at /build/gcc/src/gcc/libstdc++-v3/libsupc++/eh_throw.cc:95
#6  0x00005555556d723e in peek_utf8_code(char const*, unsigned long, unsigned int*) ()
#7  0x00005555556d82e4 in libnvc::nvim_client::set_cell(unsigned long, unsigned long, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, long, int) ()
#8  0x00005555556d8bc6 in libnvc::nvim_client::on_grid_line(long, long, long, std::vector<std::variant<bool, long, double, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::vector<libnvc::object_wrapper, std::allocator<libnvc::object_wrapper> >, std::map<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, libnvc::object_wrapper, std::less<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const, libnvc::object_wrapper> > > >, std::allocator<std::variant<bool, long, double, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::vector<libnvc::object_wrapper, std::allocator<libnvc::object_wrapper> >, std::map<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, libnvc::object_wrapper, std::less<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const, libnvc::object_wrapper> > > > > > const&) ()
#9  0x00005555556bbb59 in inn_dispatch_notif(char const*, mpack_node_t, libnvc::api_client*) ()
#10 0x00005555556c2b7b in libnvc::api_client::poll_one() ()
#11 0x0000555555694b2e in libnvc::api_client::poll (this=0x55555603a5a0)
    at /home/testuser/b_libnvc/install/include/libnvc.hpp:3653
#12 0x0000555555695740 in nvim_sdlwidget::update (this=0x7fffffffe290)
    at /home/testuser/libnvc/sample/nvim_sdl2/src/sdlwidget.hpp:70
#13 0x0000555555694345 in main () at /home/testuser/libnvc/sample/nvim_sdl2/src/main.cpp:154

Build errors on Manjaro linux

I tried to build library and examples follow readme.md, but have errors on make stage. What is the problem?

git clone https://github.com/etorth/libnvc.git && mkdir b_libnvc && cd b_libnvc && cmake ../libnvc -DCMAKE_INSTALL_PREFIX=install && make
Cloning into 'libnvc'...
remote: Enumerating objects: 156, done.
remote: Counting objects: 100% (156/156), done.
remote: Compressing objects: 100% (115/115), done.
remote: Total 778 (delta 95), reused 77 (delta 38), pack-reused 622
Receiving objects: 100% (778/778), 2.13 MiB | 212.00 KiB/s, done.
Resolving deltas: 100% (491/491), done.
-- The C compiler identification is GNU 9.3.0
-- The CXX compiler identification is GNU 9.3.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc - works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ - works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Performing Test COMPILER_SUPPORTS_CXX17
-- Performing Test COMPILER_SUPPORTS_CXX17 - Success
-- download mpack
-- download mpack done, in /home/testuser/b_libnvc/3rdparty/download/mpack
-- download strfunc
-- download strfunc done, in /home/testuser/b_libnvc/3rdparty/download/strfunc
-- Looking for pthread.h
-- Looking for pthread.h - found
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD - Failed
-- Check if compiler accepts -pthread
-- Check if compiler accepts -pthread - yes
-- Found Threads: TRUE
-- Performing Test COMPILER_HAS_HIDDEN_VISIBILITY
-- Performing Test COMPILER_HAS_HIDDEN_VISIBILITY - Success
-- Performing Test COMPILER_HAS_HIDDEN_INLINE_VISIBILITY
-- Performing Test COMPILER_HAS_HIDDEN_INLINE_VISIBILITY - Success
-- Performing Test COMPILER_HAS_DEPRECATED_ATTR
-- Performing Test COMPILER_HAS_DEPRECATED_ATTR - Success
-- Configuring done
-- Generating done
-- Build files have been written to: /home/testuser/b_libnvc
Scanning dependencies of target mirror_utf8cpp
[  2%] Creating directories for 'mirror_utf8cpp'
[  5%] Performing download step (git clone) for 'mirror_utf8cpp'
Cloning into 'mirror_utf8cpp'...
Already on 'master'
Your branch is up to date with 'origin/master'.
[  7%] No patch step for 'mirror_utf8cpp'
[ 10%] No update step for 'mirror_utf8cpp'
[ 12%] No configure step for 'mirror_utf8cpp'
[ 15%] No build step for 'mirror_utf8cpp'
[ 17%] No install step for 'mirror_utf8cpp'
[ 20%] Completed 'mirror_utf8cpp'
[ 20%] Built target mirror_utf8cpp
Scanning dependencies of target asio
[ 23%] Creating directories for 'asio'
[ 25%] Performing download step (download, verify and extract) for 'asio'
-- Downloading...
   dst='/home/testuser/b_libnvc/3rdparty/asio/src/asio-1-12-2.tar.gz'
   timeout='none'
-- Using src='https://github.com/chriskohlhoff/asio/archive/asio-1-12-2.tar.gz'
-- [download 100% complete]
-- Downloading... done
-- extracting...
     src='/home/testuser/b_libnvc/3rdparty/asio/src/asio-1-12-2.tar.gz'
     dst='/home/testuser/b_libnvc/3rdparty/asio/src/asio'
-- extracting... [tar xfz]
-- extracting... [analysis]
-- extracting... [rename]
-- extracting... [clean up]
-- extracting... done
[ 28%] No patch step for 'asio'
[ 30%] No update step for 'asio'
[ 33%] No configure step for 'asio'
[ 35%] No build step for 'asio'
[ 38%] No install step for 'asio'
[ 41%] Completed 'asio'
[ 41%] Built target asio
Scanning dependencies of target reproc
[ 43%] Building C object _deps/reproc-build/reproc/CMakeFiles/reproc.dir/src/reproc.c.o
[ 46%] Building C object _deps/reproc-build/reproc/CMakeFiles/reproc.dir/src/sink.c.o
[ 48%] Building C object _deps/reproc-build/reproc/CMakeFiles/reproc.dir/src/posix/error.c.o
[ 51%] Building C object _deps/reproc-build/reproc/CMakeFiles/reproc.dir/src/posix/handle.c.o
[ 53%] Building C object _deps/reproc-build/reproc/CMakeFiles/reproc.dir/src/posix/pipe.c.o
[ 56%] Building C object _deps/reproc-build/reproc/CMakeFiles/reproc.dir/src/posix/process.c.o
[ 58%] Building C object _deps/reproc-build/reproc/CMakeFiles/reproc.dir/src/posix/redirect.c.o
[ 58%] Built target reproc
Scanning dependencies of target reproc++
[ 61%] Building CXX object _deps/reproc-build/reproc++/CMakeFiles/reproc++.dir/src/detail/array.cpp.o
[ 64%] Building CXX object _deps/reproc-build/reproc++/CMakeFiles/reproc++.dir/src/error.cpp.o
[ 66%] Building CXX object _deps/reproc-build/reproc++/CMakeFiles/reproc++.dir/src/reproc.cpp.o
[ 69%] Building CXX object _deps/reproc-build/reproc++/CMakeFiles/reproc++.dir/src/sink.cpp.o
[ 69%] Built target reproc++
Scanning dependencies of target jinja2_proc
[ 71%] Generate nvim server msgpack-rpc api...
**** Nvim path           : nvim
**** Nvim api level      : 6
**** Api build signature : 3f91fd37-d572-447a-b75a-ded250a7338f
**** Jinja2 render input : /home/testuser/libnvc/template
**** Jinja2 render output: /home/testuser/b_libnvc/src

>>>>>>>> updated nvimwidget.cpp
>>>>>>>> updated nvimclient.cpp
>>>>>>>> updated apiclient.cpp
>>>>>>>> updated reprocdevice.cpp
>>>>>>>> updated mpinterf.cpp
>>>>>>>> updated libnvc.hpp
>>>>>>>> updated log.cpp
>>>>>>>> updated asiosocket.cpp
[ 71%] Built target jinja2_proc
Scanning dependencies of target nvc
[ 74%] Building CXX object CMakeFiles/nvc.dir/src/apiclient.cpp.o
/home/testuser/b_libnvc/src/apiclient.cpp: In constructor ‘libnvc::api_client::stream_decoder::stream_decoder(libnvc::io_device*)’:
/home/testuser/b_libnvc/src/apiclient.cpp:39:49: error: ‘str_fflprintf’ was not declared in this scope
   39 |                     throw std::invalid_argument(str_fflprintf(": Invalid io device: (nullptr)"));
      |                                                 ^~~~~~~~~~~~~
/home/testuser/b_libnvc/src/apiclient.cpp: In member function ‘std::optional<mpack_node_t> libnvc::api_client::stream_decoder::poll()’:
/home/testuser/b_libnvc/src/apiclient.cpp:72:54: error: ‘str_fflprintf’ was not declared in this scope
   72 |                             throw std::runtime_error(str_fflprintf(": Get mpack_tree_error(): %s", mpack_error_to_string(ec)));
      |                                                      ^~~~~~~~~~~~~
/home/testuser/b_libnvc/src/apiclient.cpp: In static member function ‘static size_t libnvc::api_client::stream_decoder::read_iodev(mpack_tree_t*, char*, size_t)’:
/home/testuser/b_libnvc/src/apiclient.cpp:83:42: error: ‘str_fflprintf’ was not declared in this scope
   83 |                 throw std::runtime_error(str_fflprintf(": Empty context in mpack_tree_t: %p", ptree));
      |                                          ^~~~~~~~~~~~~
/home/testuser/b_libnvc/src/apiclient.cpp: In function ‘void {anonymous}::check_node_type(mpack_node_t, mpack_type_t)’:
/home/testuser/b_libnvc/src/apiclient.cpp:104:38: error: ‘str_fflprintf’ was not declared in this scope
  104 |             throw std::runtime_error(str_fflprintf(": Node type doesn't match: %s, expecting: %s", mpack_type_to_string(type), mpack_type_to_string(expected_type)));
      |                                      ^~~~~~~~~~~~~

Failed to build sample apiclient

Steps:

  • Build project: mkdir b && cd b && cmake ..
  • Copied libnvc/sample/apiclient to a separate folder.
  • Created folders apiclient/{inc,lib}
  • Copied b/src/libnvc.hpp to apiclient/inc
  • Copied b/libnvc.a to apiclient/lib
  • Added SET(LIBNVC_INCLUDE "${CMAKE_CURRENT_SOURCE_DIR}/inc") to CMakeLists.txt
  • Added SET(LIBNVC_LIB "${CMAKE_CURRENT_SOURCE_DIR}/lib") to CMakeLists.txt
  • Build apiclient
    • Build project: mkdir b && cd b && cmake ..
  • Error linking:
build make                                ~/Documents/cpp/neovim_related/apiclient/build
Scanning dependencies of target apiclient
[ 50%] Building CXX object src/CMakeFiles/apiclient.dir/main.cpp.o
[100%] Linking CXX executable apiclient
/usr/bin/ld: ../../lib/libnvc.a(apiclient.cpp.o): in function `std::filesystem::__cxx11::p
ath::path<char [75], std::filesystem::__cxx11::path>(char const (&) [75], std::filesystem:
:__cxx11::path::format)':
/usr/include/c++/8.2.1/bits/fs_path.h:183: undefined reference to `std::filesystem::__cxx1
1::path::_M_split_cmpts()'
/usr/bin/ld: ../../lib/libnvc.a(asiosocket.cpp.o): in function `std::filesystem::__cxx11::
path::path<char [76], std::filesystem::__cxx11::path>(char const (&) [76], std::filesystem
::__cxx11::path::format)':
/usr/include/c++/8.2.1/bits/fs_path.h:183: undefined reference to `std::filesystem::__cxx1
1::path::_M_split_cmpts()'
/usr/bin/ld: ../../lib/libnvc.a(log.cpp.o): in function `std::filesystem::__cxx11::path::p
ath<char [69], std::filesystem::__cxx11::path>(char const (&) [69], std::filesystem::__cxx
11::path::format)':
/usr/include/c++/8.2.1/bits/fs_path.h:183: undefined reference to `std::filesystem::__cxx1
1::path::_M_split_cmpts()'
/usr/bin/ld: ../../lib/libnvc.a(mpinterf.cpp.o): in function `std::filesystem::__cxx11::pa
th::path<char [74], std::filesystem::__cxx11::path>(char const (&) [74], std::filesystem::
__cxx11::path::format)':
/usr/include/c++/8.2.1/bits/fs_path.h:183: undefined reference to `std::filesystem::__cxx1
1::path::_M_split_cmpts()'
collect2: error: ld returned 1 exit status
make[2]: *** [src/CMakeFiles/apiclient.dir/build.make:85: src/apiclient] Error 1
make[1]: *** [CMakeFiles/Makefile2:91: src/CMakeFiles/apiclient.dir/all] Error 2
make: *** [Makefile:130: all] Error 2

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.