Merge pull request #400 from MihailRis/syntax-hightlighting
Text Formatting
This commit is contained in:
@@ -31,9 +31,16 @@ static int l_add_command(lua::State* L) {
|
||||
|
||||
static int l_execute(lua::State* L) {
|
||||
auto prompt = lua::require_string(L, 1);
|
||||
auto result = engine->getCommandsInterpreter()->execute(prompt);
|
||||
lua::pushvalue(L, result);
|
||||
return 1;
|
||||
try {
|
||||
auto result = engine->getCommandsInterpreter()->execute(prompt);
|
||||
lua::pushvalue(L, result);
|
||||
return 1;
|
||||
} catch (const parsing_error& err) {
|
||||
if (std::string(err.what()).find("unknown command ") == 0) {
|
||||
throw;
|
||||
}
|
||||
throw std::runtime_error(err.errorLog());
|
||||
}
|
||||
}
|
||||
|
||||
static int l_set(lua::State* L) {
|
||||
|
||||
@@ -12,6 +12,8 @@
|
||||
#include "graphics/ui/elements/TrackBar.hpp"
|
||||
#include "graphics/ui/elements/UINode.hpp"
|
||||
#include "graphics/ui/gui_util.hpp"
|
||||
#include "graphics/ui/markdown.hpp"
|
||||
#include "graphics/core/Font.hpp"
|
||||
#include "items/Inventories.hpp"
|
||||
#include "util/stringutil.hpp"
|
||||
#include "world/Level.hpp"
|
||||
@@ -299,6 +301,22 @@ static int p_get_line_numbers(UINode* node, lua::State* L) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int p_get_syntax(UINode* node, lua::State* L) {
|
||||
if (auto box = dynamic_cast<TextBox*>(node)) {
|
||||
return lua::pushstring(L, box->getSyntax());
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int p_get_markup(UINode* node, lua::State* L) {
|
||||
if (auto box = dynamic_cast<TextBox*>(node)) {
|
||||
return lua::pushstring(L, box->getMarkup());
|
||||
} else if (auto label = dynamic_cast<Label*>(node)) {
|
||||
return lua::pushstring(L, label->getMarkup());
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int p_get_src(UINode* node, lua::State* L) {
|
||||
if (auto image = dynamic_cast<Image*>(node)) {
|
||||
return lua::pushstring(L, image->getTexture());
|
||||
@@ -420,6 +438,8 @@ static int l_gui_getattr(lua::State* L) {
|
||||
{"lineNumbers", p_get_line_numbers},
|
||||
{"lineAt", p_get_line_at},
|
||||
{"linePos", p_get_line_pos},
|
||||
{"syntax", p_get_syntax},
|
||||
{"markup", p_get_markup},
|
||||
{"src", p_get_src},
|
||||
{"value", p_get_value},
|
||||
{"min", p_get_min},
|
||||
@@ -512,6 +532,18 @@ static void p_set_line_numbers(UINode* node, lua::State* L, int idx) {
|
||||
box->setShowLineNumbers(lua::toboolean(L, idx));
|
||||
}
|
||||
}
|
||||
static void p_set_syntax(UINode* node, lua::State* L, int idx) {
|
||||
if (auto box = dynamic_cast<TextBox*>(node)) {
|
||||
box->setSyntax(lua::require_string(L, idx));
|
||||
}
|
||||
}
|
||||
static void p_set_markup(UINode* node, lua::State* L, int idx) {
|
||||
if (auto box = dynamic_cast<TextBox*>(node)) {
|
||||
box->setMarkup(lua::require_string(L, idx));
|
||||
} else if (auto label = dynamic_cast<Label*>(node)) {
|
||||
label->setMarkup(lua::require_string(L, idx));
|
||||
}
|
||||
}
|
||||
static void p_set_src(UINode* node, lua::State* L, int idx) {
|
||||
if (auto image = dynamic_cast<Image*>(node)) {
|
||||
image->setTexture(lua::require_string(L, idx));
|
||||
@@ -612,6 +644,8 @@ static int l_gui_setattr(lua::State* L) {
|
||||
{"text", p_set_text},
|
||||
{"editable", p_set_editable},
|
||||
{"lineNumbers", p_set_line_numbers},
|
||||
{"syntax", p_set_syntax},
|
||||
{"markup", p_set_markup},
|
||||
{"src", p_set_src},
|
||||
{"caret", p_set_caret},
|
||||
{"value", p_set_value},
|
||||
@@ -694,6 +728,25 @@ static int l_gui_getviewport(lua::State* L) {
|
||||
return lua::pushvec2(L, engine->getGUI()->getContainer()->getSize());
|
||||
}
|
||||
|
||||
static int l_gui_clear_markup(lua::State* L) {
|
||||
auto lang = lua::require_string(L, 1);
|
||||
std::string text = lua::require_string(L, 2);
|
||||
if (std::strcmp(lang, "md") == 0) {
|
||||
auto [processed, _] = markdown::process(text, true);
|
||||
text = std::move(processed);
|
||||
}
|
||||
return lua::pushstring(L, text);
|
||||
}
|
||||
|
||||
static int l_gui_escape_markup(lua::State* L) {
|
||||
auto lang = lua::require_string(L, 1);
|
||||
std::string text = lua::require_string(L, 2);
|
||||
if (std::strcmp(lang, "md") == 0) {
|
||||
text = std::move(markdown::escape<char>(text));
|
||||
}
|
||||
return lua::pushstring(L, text);
|
||||
}
|
||||
|
||||
const luaL_Reg guilib[] = {
|
||||
{"get_viewport", lua::wrap<l_gui_getviewport>},
|
||||
{"getattr", lua::wrap<l_gui_getattr>},
|
||||
@@ -701,5 +754,8 @@ const luaL_Reg guilib[] = {
|
||||
{"get_env", lua::wrap<l_gui_get_env>},
|
||||
{"str", lua::wrap<l_gui_str>},
|
||||
{"get_locales_info", lua::wrap<l_gui_get_locales_info>},
|
||||
{"clear_markup", lua::wrap<l_gui_clear_markup>},
|
||||
{"escape_markup", lua::wrap<l_gui_escape_markup>},
|
||||
{"__reindex", lua::wrap<l_gui_reindex>},
|
||||
{NULL, NULL}};
|
||||
{NULL, NULL}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user