Skip to content

Commit

Permalink
updated to a 2022 lager w/ the new LAGER_STRUCT
Browse files Browse the repository at this point in the history
  • Loading branch information
cyrilRomain committed Jul 11, 2022
1 parent 866166f commit bb395b8
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 65 deletions.
4 changes: 2 additions & 2 deletions nix/deps.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/ewig/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ std::pair<application, lager::effect<action>> quit(application app)
{
return {
put_message(app, "quitting... (waiting for operations to finish)"),
[] (auto&& ctx) { ctx.finish(); }
[] (auto&& ctx) { ctx.loop().finish(); }
};
}

Expand Down
19 changes: 9 additions & 10 deletions src/ewig/application.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
#include <ewig/buffer.hpp>

#include <lager/store.hpp>
#include <lager/debug/cereal/struct.hpp>
#include <lager/extra/cereal/struct.hpp>

#include <ctime>
#include <variant>
Expand All @@ -38,21 +38,17 @@ using arg_t = std::variant<std::monostate,
struct key_action { key_code key; };
struct resize_action { coord size; };
struct command_action { immer::box<std::string> 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<command_action,
key_action,
resize_action,
buffer_action>;
buffer_action,
resize_action>;

struct message
{
std::time_t time_stamp;
immer::box<std::string> content;
};
LAGER_CEREAL_STRUCT(message, (time_stamp)(content));

struct application
{
Expand All @@ -63,9 +59,6 @@ struct application
immer::vector<text> clipboard;
immer::vector<message> messages;
};
LAGER_CEREAL_STRUCT(
application,
(window_size)(keys)(input)(current)(clipboard)(messages));

using command = std::function<
std::pair<application, lager::effect<action>>(
Expand All @@ -87,3 +80,9 @@ application apply_edit(application state, coord size, buffer edit);
application apply_edit(application state, coord size, std::pair<buffer, text> 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);
12 changes: 5 additions & 7 deletions src/ewig/buffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,6 @@

namespace ewig {

immer::box<std::string> no_file::name = "*unnamed*";
text no_file::content = {};

bool load_in_progress(const buffer& buf)
{
return std::holds_alternative<loading_file>(buf.from);
Expand Down Expand Up @@ -98,7 +95,7 @@ auto load_file_effect(immer::box<std::string> 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);
Expand Down Expand Up @@ -137,14 +134,14 @@ auto load_file_effect(immer::box<std::string> file_name)
};
}

auto save_file_effect(immer::box<std::string> file_name,
lager::effect<buffer_action> save_file_effect(immer::box<std::string> 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);
Expand Down Expand Up @@ -179,7 +176,8 @@ std::pair<buffer, lager::effect<buffer_action>> save_buffer(buffer buf)
{
auto file = std::get<existing_file>(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<buffer, lager::effect<buffer_action>> load_buffer(buffer buf, const std::string& fname)
Expand Down
33 changes: 16 additions & 17 deletions src/ewig/buffer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
#include <ewig/coord.hpp>

#include <lager/store.hpp>
#include <lager/debug/cereal/struct.hpp>
#include <lager/extra/struct.hpp>

#include <immer/box.hpp>
#include <immer/flex_vector.hpp>
Expand All @@ -44,25 +44,22 @@ using text = immer::flex_vector<line>;

struct no_file
{
static immer::box<std::string> name;
static text content;
immer::box<std::string> name = "*unnamed*";
text content = {};
};
LAGER_CEREAL_STRUCT(no_file, (name)(content));

struct existing_file
{
immer::box<std::string> name;
text content;
};
LAGER_CEREAL_STRUCT(existing_file, (name)(content));

struct saving_file
{
immer::box<std::string> name;
text content;
std::size_t saved_lines;
};
LAGER_CEREAL_STRUCT(saving_file, (name)(content));

struct loading_file
{
Expand All @@ -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<no_file,
existing_file,
Expand All @@ -83,7 +79,6 @@ struct snapshot
text content;
coord cursor;
};
LAGER_CEREAL_STRUCT(snapshot, (content)(cursor));

struct buffer
{
Expand All @@ -95,22 +90,13 @@ struct buffer
immer::vector<snapshot> history;
std::optional<std::size_t> 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; };
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<load_progress_action,
load_done_action,
Expand Down Expand Up @@ -192,3 +178,16 @@ buffer undo(buffer);
std::pair<buffer, std::string> 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);
15 changes: 3 additions & 12 deletions src/ewig/coord.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

#pragma once

#include <lager/debug/cereal/struct.hpp>
#include <lager/extra/struct.hpp>

namespace ewig {

Expand All @@ -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);
44 changes: 28 additions & 16 deletions src/ewig/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,27 @@


#if EWIG_ENABLE_DEBUGGER
#include <lager/debug/enable.hpp>
#include <lager/debug/debugger.hpp>
#include <lager/debug/http_server.hpp>
#include <lager/debug/cereal/immer_vector.hpp>
#include <lager/debug/cereal/immer_box.hpp>
#include <lager/debug/cereal/tuple.hpp>
#include <lager/resources_path.hpp>
#include <lager/extra/cereal/immer_box.hpp>
#include <lager/extra/cereal/tuple.hpp>
#include <cereal/types/string.hpp>
#include <cereal/types/unordered_map.hpp>

namespace cereal {

LAGER_CEREAL_STRUCT(std::monostate);
// do not save serialize exceptions

template <typename Archive>
void save(Archive& ar, const std::exception_ptr& e)
{
}

template <typename Archive>
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
Expand Down Expand Up @@ -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<action>(
auto store = lager::make_store<action>(
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();
}

Expand Down

0 comments on commit bb395b8

Please sign in to comment.