From 50fc355a0f36b7b033690558e4a273a280eab9b7 Mon Sep 17 00:00:00 2001 From: hippoz Date: Mon, 3 May 2021 14:51:05 +0300 Subject: [PATCH] allow lua to clear the textbuffer --- corescripts/_global.lua | 27 ++++++++++++--------------- lua.c | 6 ++++++ main.c | 3 ++- pages/test.lua | 11 +++-------- ui.c | 4 ++++ ui.h | 1 + 6 files changed, 28 insertions(+), 24 deletions(-) diff --git a/corescripts/_global.lua b/corescripts/_global.lua index ba53a90..7165ee2 100644 --- a/corescripts/_global.lua +++ b/corescripts/_global.lua @@ -1,25 +1,22 @@ --[[ utility functions ]]-- -function _el(name) - return function(args) - return { name, args } - end -end - -t = _el("t") +-- --[[ page module ]]-- page = {} -function page.Commit(content) - --__ext_pageview_clear() +function page.Set(text) + __ext_pageview_set_content(text) +end + +function page.Append(content) for i, v in pairs(content) do - -- tag_name = v[1] - -- tag_props = v[2] - - print(v[1], v[2]) - - __ext_pageview_append_tag(v[1], v[2]) + __ext_pageview_append_tag("t", v[1] or v) end +end + +function page.Commit(content) + page.Set("") + page.Append(content) end \ No newline at end of file diff --git a/lua.c b/lua.c index c1d9caf..41bb41e 100644 --- a/lua.c +++ b/lua.c @@ -39,7 +39,13 @@ int lua_init_state(lua_State* L, struct application_state* state) { return 0; } + int pageview_set_content(lua_State* L) { + const char* new_content = luaL_checkstring(L, 1); + page_application_text_buffer_set_text(state, new_content); + } + lua_create_c_function(L, "__ext_pageview_append_tag", pageview_append_tag); + lua_create_c_function(L, "__ext_pageview_set_content", pageview_set_content); status = lua_pcall_file(L, "corescripts/_global.lua"); if (status != 0) return 1; diff --git a/main.c b/main.c index 25503f8..4d38969 100644 --- a/main.c +++ b/main.c @@ -12,7 +12,8 @@ static void activate(GtkApplication* app, struct application_state* state) { page_application_init(state); int status = lua_init_state(state->L, state); if (status != 0) { - printf("error: lua_init_state failed with non-zero error code %d\n", status); + fprintf(stderr, "error: lua_init_state failed with non-zero error code %d\n", status); + return; } page_application_show(state); lua_close(state->L); diff --git a/pages/test.lua b/pages/test.lua index a1bc9df..240ca44 100644 --- a/pages/test.lua +++ b/pages/test.lua @@ -1,8 +1,3 @@ -function Home() - return { - t("Hello"), - t(" wow") - } -end - -page.Commit(Home()) \ No newline at end of file +page.Commit{ + "yeah" +} \ No newline at end of file diff --git a/ui.c b/ui.c index f5aabf5..859cd95 100644 --- a/ui.c +++ b/ui.c @@ -35,4 +35,8 @@ void page_application_text_buffer_append_text(struct application_state* state, c GtkTextIter iter; gtk_text_buffer_get_end_iter(GTK_TEXT_BUFFER(state->pageview_buffer), &iter); gtk_text_buffer_insert(GTK_TEXT_BUFFER(state->pageview_buffer), &iter, display_text, -1); +} + +void page_application_text_buffer_set_text(struct application_state* state, const char* text) { + gtk_text_buffer_set_text(state->pageview_buffer, text, -1); } \ No newline at end of file diff --git a/ui.h b/ui.h index 5e19d13..0949473 100644 --- a/ui.h +++ b/ui.h @@ -18,5 +18,6 @@ struct application_state* page_application_new(); void page_application_init(struct application_state* state); void page_application_show(struct application_state* state); void page_application_text_buffer_append_text(struct application_state* state, const char* display_text); +void page_application_text_buffer_set_text(struct application_state* state, const char* text); #endif \ No newline at end of file