From bb395b8d1c249e2c585203368ba535fb7b933a77 Mon Sep 17 00:00:00 2001 From: Cyril Romain Date: Mon, 11 Jul 2022 14:01:19 +0200 Subject: [PATCH] updated to a 2022 lager w/ the new LAGER_STRUCT --- nix/deps.nix | 4 ++-- src/ewig/application.cpp | 2 +- src/ewig/application.hpp | 19 ++++++++--------- src/ewig/buffer.cpp | 12 +++++------ src/ewig/buffer.hpp | 33 +++++++++++++++--------------- src/ewig/coord.hpp | 15 +++----------- src/ewig/main.cpp | 44 +++++++++++++++++++++++++--------------- 7 files changed, 64 insertions(+), 65 deletions(-) diff --git a/nix/deps.nix b/nix/deps.nix index d75f139..97b609a 100644 --- a/nix/deps.nix +++ b/nix/deps.nix @@ -107,12 +107,12 @@ rec { lager = stdenv.mkDerivation rec { name = "lager"; version = "git-${commit}"; - commit = "bee1c04c058b872ea998421d43587de9e90f079e"; + commit = "56125daacdd2301ab2a8298801d247a593bd4d25"; src = fetchFromGitHub { owner = "arximboldi"; repo = "lager"; rev = commit; - sha256 = "1222nydan0vflgfyijvkb1rwgspnbkimp05mm9zwzx6i3hhax4yk"; + sha256 = "093kw3xahl9lgscbkkx5n6f0mmd0gwa4ra1l34gan1ywhf24kn9v"; }; buildInputs = [ ncurses diff --git a/src/ewig/application.cpp b/src/ewig/application.cpp index 4f36fb9..98663f1 100644 --- a/src/ewig/application.cpp +++ b/src/ewig/application.cpp @@ -139,7 +139,7 @@ std::pair> quit(application app) { return { put_message(app, "quitting... (waiting for operations to finish)"), - [] (auto&& ctx) { ctx.finish(); } + [] (auto&& ctx) { ctx.loop().finish(); } }; } diff --git a/src/ewig/application.hpp b/src/ewig/application.hpp index 389a69a..221646a 100644 --- a/src/ewig/application.hpp +++ b/src/ewig/application.hpp @@ -24,7 +24,7 @@ #include #include -#include +#include #include #include @@ -38,21 +38,17 @@ using arg_t = std::variant name; arg_t arg; }; -LAGER_CEREAL_STRUCT(key_action, (key)); -LAGER_CEREAL_STRUCT(resize_action, (size)); -LAGER_CEREAL_STRUCT(command_action, (name)(arg)); using action = std::variant; + buffer_action, + resize_action>; struct message { std::time_t time_stamp; immer::box content; }; -LAGER_CEREAL_STRUCT(message, (time_stamp)(content)); struct application { @@ -63,9 +59,6 @@ struct application immer::vector clipboard; immer::vector messages; }; -LAGER_CEREAL_STRUCT( - application, - (window_size)(keys)(input)(current)(clipboard)(messages)); using command = std::function< std::pair>( @@ -87,3 +80,9 @@ application apply_edit(application state, coord size, buffer edit); application apply_edit(application state, coord size, std::pair edit); } // namespace ewig + +LAGER_STRUCT(ewig, key_action, key); +LAGER_STRUCT(ewig, resize_action, size); +LAGER_STRUCT(ewig, command_action, name, arg); +LAGER_STRUCT(ewig, message, time_stamp, content); +LAGER_STRUCT(ewig, application, window_size, keys, input, current, clipboard, messages); diff --git a/src/ewig/buffer.cpp b/src/ewig/buffer.cpp index 53302bb..f3172d9 100644 --- a/src/ewig/buffer.cpp +++ b/src/ewig/buffer.cpp @@ -33,9 +33,6 @@ namespace ewig { -immer::box no_file::name = "*unnamed*"; -text no_file::content = {}; - bool load_in_progress(const buffer& buf) { return std::holds_alternative(buf.from); @@ -98,7 +95,7 @@ auto load_file_effect(immer::box file_name) constexpr auto progress_report_rate_bytes = 1 << 20; return [=] (auto& ctx) { - ctx.async([=] { + ctx.loop().async([=] { auto content = text{}.transient(); auto file = std::ifstream{}; file.exceptions(std::fstream::badbit | std::fstream::failbit); @@ -137,14 +134,14 @@ auto load_file_effect(immer::box file_name) }; } -auto save_file_effect(immer::box file_name, +lager::effect save_file_effect(immer::box file_name, text old_content, text new_content) { constexpr auto progress_report_rate_lines = std::size_t{(1 << 20) / 40}; return [=] (auto& ctx) { - ctx.async([=] { + ctx.loop().async([=] { auto progress = saving_file{ file_name, new_content, 0 }; auto file = std::ofstream{}; file.exceptions(std::fstream::badbit | std::fstream::failbit); @@ -179,7 +176,8 @@ std::pair> save_buffer(buffer buf) { auto file = std::get(buf.from); buf.from = saving_file{file.name, buf.content, {}}; - return { buf, save_file_effect(file.name, file.content, buf.content) }; + auto effect = save_file_effect(file.name, file.content, buf.content); + return { buf, effect }; } std::pair> load_buffer(buffer buf, const std::string& fname) diff --git a/src/ewig/buffer.hpp b/src/ewig/buffer.hpp index 712a1fe..1faf3a8 100644 --- a/src/ewig/buffer.hpp +++ b/src/ewig/buffer.hpp @@ -23,7 +23,7 @@ #include #include -#include +#include #include #include @@ -44,17 +44,15 @@ using text = immer::flex_vector; struct no_file { - static immer::box name; - static text content; + immer::box name = "*unnamed*"; + text content = {}; }; -LAGER_CEREAL_STRUCT(no_file, (name)(content)); struct existing_file { immer::box name; text content; }; -LAGER_CEREAL_STRUCT(existing_file, (name)(content)); struct saving_file { @@ -62,7 +60,6 @@ struct saving_file text content; std::size_t saved_lines; }; -LAGER_CEREAL_STRUCT(saving_file, (name)(content)); struct loading_file { @@ -71,7 +68,6 @@ struct loading_file std::streamoff loaded_bytes; std::streamoff total_bytes; }; -LAGER_CEREAL_STRUCT(loading_file, (name)(content)(loaded_bytes)(total_bytes)); using file = std::variant history; std::optional history_pos; }; -LAGER_CEREAL_STRUCT( - buffer, - (from)(content)(cursor)(scroll)(selection_start)(history)(history_pos)); struct load_progress_action { loading_file file; }; struct load_done_action { existing_file file; }; @@ -105,12 +97,6 @@ struct load_error_action { existing_file file; std::exception_ptr err; }; struct save_progress_action { saving_file file; }; struct save_done_action { existing_file file; }; struct save_error_action { existing_file file; std::exception_ptr err; }; -LAGER_CEREAL_STRUCT(load_progress_action, (file)); -LAGER_CEREAL_STRUCT(load_done_action, (file)); -LAGER_CEREAL_STRUCT(load_error_action, (file)); -LAGER_CEREAL_STRUCT(save_progress_action, (file)); -LAGER_CEREAL_STRUCT(save_done_action, (file)); -LAGER_CEREAL_STRUCT(save_error_action, (file)); using buffer_action = std::variant record(buffer before, buffer after); } // namespace ewig + +LAGER_STRUCT(ewig, no_file, name, content); +LAGER_STRUCT(ewig, existing_file, name, content); +LAGER_STRUCT(ewig, saving_file, name, content, saved_lines); +LAGER_STRUCT(ewig, loading_file, name, content, loaded_bytes, total_bytes); +LAGER_STRUCT(ewig, snapshot, content, cursor); +LAGER_STRUCT(ewig, buffer, from, content, cursor, scroll, selection_start, history, history_pos); +LAGER_STRUCT(ewig, load_progress_action, file); +LAGER_STRUCT(ewig, load_done_action, file); +LAGER_STRUCT(ewig, load_error_action, file, err); +LAGER_STRUCT(ewig, save_progress_action, file); +LAGER_STRUCT(ewig, save_done_action, file); +LAGER_STRUCT(ewig, save_error_action, file, err); diff --git a/src/ewig/coord.hpp b/src/ewig/coord.hpp index 514fce8..5487500 100644 --- a/src/ewig/coord.hpp +++ b/src/ewig/coord.hpp @@ -20,7 +20,7 @@ #pragma once -#include +#include namespace ewig { @@ -31,21 +31,12 @@ struct coord index row = {}; index col = {}; }; -LAGER_CEREAL_STRUCT(coord, (row)(col)); inline bool operator<(const coord& a, const coord& b) { return a.row < b.row || (a.row == b.row && a.col < b.col); } -inline bool operator==(const coord& a, const coord& b) -{ - return a.row == b.row && a.col == b.col; -} - -inline bool operator!=(const coord& a, const coord& b) -{ - return !(a == b); -} - } // namespace ewig + +LAGER_STRUCT(ewig, coord, row, col); diff --git a/src/ewig/main.cpp b/src/ewig/main.cpp index a8fccd6..0440163 100644 --- a/src/ewig/main.cpp +++ b/src/ewig/main.cpp @@ -28,16 +28,27 @@ #if EWIG_ENABLE_DEBUGGER -#include +#include #include -#include -#include -#include +#include +#include +#include +#include #include namespace cereal { -LAGER_CEREAL_STRUCT(std::monostate); +// do not save serialize exceptions + +template +void save(Archive& ar, const std::exception_ptr& e) +{ +} + +template +void load(Archive& ar, std::exception_ptr& e) +{ +} // custom serialization of text to make text look prettier by looking // like a list of strings, as opposed to just a list of numbers @@ -110,21 +121,22 @@ const auto key_map_emacs = make_key_map( void run(int argc, const char** argv, const std::string& fname) { #if EWIG_ENABLE_DEBUGGER - auto debugger = lager::http_debug_server{argc, argv, 8080}; - auto enhancer = lager::enable_debug(debugger); -#else - auto enhancer = lager::identity; + auto debugger = + lager::http_debug_server{argc, argv, 8080, lager::resources_path()}; #endif auto serv = boost::asio::io_service{}; auto term = terminal{serv}; - auto st = lager::make_store( + auto store = lager::make_store( application{term.size(), key_map_emacs}, - update, - draw, - lager::with_boost_asio_event_loop{serv, [&] { term.stop(); }}, - enhancer); - term.start([&] (auto ev) { st.dispatch (ev); }); - st.dispatch(command_action{"load", fname}); + lager::with_boost_asio_event_loop{serv.get_executor(), [&] { term.stop(); }}, + zug::comp( +#ifdef EWIG_ENABLE_DEBUGGER + lager::with_debugger(debugger), +#endif + lager::identity)); + watch(store, draw); + term.start([&] (auto ev) { store.dispatch(ev); }); + store.dispatch(command_action{"load", fname}); serv.run(); }