diff --git a/cpp_examples/example.cpp b/cpp_examples/example.cpp index 9defc3e..269ea9c 100644 --- a/cpp_examples/example.cpp +++ b/cpp_examples/example.cpp @@ -6,16 +6,15 @@ using namespace std; using namespace quick_ftxui; int main() { - - set_int_var("x", 5); - set_str_var("y", ""); - string source_code = R"(Vertical{ + int x = 5; + set_int_var("x", &x); + set_str_var("y"); + string source_code = R"(DashedBorder Vertical{ str z = "init" str a int o = 0 Input { "Type something...", - Password, y } RedLight Slider { @@ -26,12 +25,12 @@ int main() { 2 } Magenta Button{ - "ls", + "Chrome", System("/usr/bin/google-chrome-stable"), - Ascii, + Animated, z } - Menu{ + Green Menu{ [ "Physics", "Maths", "Chemistry", "Biology",], VerticalAnimated, o @@ -44,7 +43,7 @@ int main() { parse_qf(source_code); - cout << "x is: " << get_int("x") << "\n"; + cout << "x is: " << x << "\n"; cout << "y is: " << get_str("y") << "\n"; cout << "o is: " << get_int("o") << "\n"; cout << "z is: " << get_str("z") << "\n"; diff --git a/include/quick-ftxui.hpp b/include/quick-ftxui.hpp index 7621564..20b80fd 100644 --- a/include/quick-ftxui.hpp +++ b/include/quick-ftxui.hpp @@ -30,6 +30,8 @@ namespace quick_ftxui_ast { std::map numbers; std::map strings; +std::map ref_nums; + /////////////////////////////////////////////////////////////////////////// // The AST /////////////////////////////////////////////////////////////////////////// @@ -1087,6 +1089,10 @@ void parse_qf(std::string source_code) { } screen.Loop(main_renderer); + + for (auto It : quick_ftxui_ast::ref_nums) { + *(It.first) = quick_ftxui_ast::numbers[It.second]; + } } } else { throw std::runtime_error("Parsing failed\n"); @@ -1114,6 +1120,27 @@ void set_int_var(std::string var_name, int init_value) { } } +void set_int_var(std::string var_name, int *init_value) { + if (auto It = quick_ftxui_ast::numbers.find(var_name); + It != quick_ftxui_ast::numbers.end()) { + throw std::runtime_error("Integer variable with name " + var_name + + " already exists, please use another name"); + } else { + quick_ftxui_ast::numbers.insert({var_name, (*init_value)}); + quick_ftxui_ast::ref_nums.insert({init_value, var_name}); + } +} + +void set_int_var(std::string var_name) { + if (auto It = quick_ftxui_ast::numbers.find(var_name); + It != quick_ftxui_ast::numbers.end()) { + throw std::runtime_error("Integer variable with name " + var_name + + " already exists, please use another name"); + } else { + quick_ftxui_ast::numbers.insert({var_name, 0}); + } +} + std::string get_str(std::string var_name) { if (auto It = quick_ftxui_ast::strings.find(std::string(var_name)); It != quick_ftxui_ast::strings.end()) { @@ -1135,6 +1162,16 @@ void set_str_var(std::string var_name, std::string init_value) { } } +void set_str_var(std::string var_name) { + if (auto It = quick_ftxui_ast::strings.find(var_name); + It != quick_ftxui_ast::strings.end()) { + throw std::runtime_error("Integer variable with name " + var_name + + " already exists, please use another name"); + } else { + quick_ftxui_ast::strings.insert({var_name, ""}); + } +} + } // namespace quick_ftxui #endif // QUICK_FTXUI_HPP \ No newline at end of file