allow lua to clear the textbuffer

This commit is contained in:
hippoz 2021-05-03 14:51:05 +03:00
parent adba43320e
commit 50fc355a0f
No known key found for this signature in database
GPG key ID: 7C52899193467641
6 changed files with 28 additions and 24 deletions

View file

@ -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

6
lua.c
View file

@ -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;

3
main.c
View file

@ -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);

View file

@ -1,8 +1,3 @@
function Home()
return {
t("Hello"),
t(" wow")
page.Commit{
"yeah"
}
end
page.Commit(Home())

4
ui.c
View file

@ -36,3 +36,7 @@ void page_application_text_buffer_append_text(struct application_state* state, c
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);
}

1
ui.h
View file

@ -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