Skip to content

Commit

Permalink
[Scripting] Added interfacing of C++ references with QF variables (#22)
Browse files Browse the repository at this point in the history
* Added ability to use C++ references with QF ints
* Added ability to use C++ std::string references with QF strs
* Updated README to reflect new changes in scripting
* Fixing failing builds due to redefinitions
* Updated roadmap for v0.2 release

Signed-off-by: Vedant <[email protected]>
  • Loading branch information
vrnimje committed Sep 21, 2023
1 parent b214d0a commit f39030d
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 53 deletions.
88 changes: 47 additions & 41 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,15 +158,15 @@ https://github.com/vrnimje/quick-ftxui/assets/103848930/715c821b-b259-4e2b-ab25-

* #### Variables

Used as hooks, so that we can obtain the input given to the Terminal UI. Currenlty, supports `int` and `str` data types
Used as hooks, so that we can obtain the input given to the Terminal UI. Currently, supports `int` and `str` data types

Check out the examples in [this directory](./examples/)

**Note:** To run these examples, build this repository with the steps mentioned [here](#build-instructions)

* #### Embedded scripting supported in C++

Supports basic scripting functionality, with functions to add & access the quick-ftxui variables.
Supports basic scripting functionality, with functions (or hooks) to add & access the quick-ftxui variables. We can also use C++ references to integers and string data types to access the values inside the QF script. See the example given below.

[Full Example](./cpp_examples/example.cpp)

Expand All @@ -179,46 +179,52 @@ https://github.com/vrnimje/quick-ftxui/assets/103848930/715c821b-b259-4e2b-ab25-
using namespace quick_ftxui;

int main() {

set_int_var("x", 5);
set_str_var("y", "");
string source_code = R"(Vertical{
str z = "init"
str a
int o = 0
Input {
"Type something...",
Password,
y
}
Slider {
"Test: ",
x,
0,
100,
2
}
Button{
"ls",
System("/usr/bin/google-chrome-stable"),
Ascii,
z
}
Menu{
[ "Physics", "Maths", "Chemistry", "Biology",],
VerticalAnimated,
o
}
Button {
"Exit",
"Exit"
}
})";

parse_qf(source_code);
int x = 5;
string y = "Init value";
set_int_var("x", &x);
set_str_var("y", &y);
string source_code = R"(Border Vertical{
str z = "init"
str a
int o = 0
Input {
"Type something...",
y
}
RedLight Slider {
"Test: ",
x,
0,
100,
2
}
Magenta Button{
"Chrome",
System("/usr/bin/google-chrome-stable"),
Animated,
z
}
Green Menu{
[ "Physics", "Maths", "Chemistry", "Biology",],
VerticalAnimated,
o
}
Button {
"Exit",
"Exit"
}
})";

parse_qf(source_code);

cout << "Slider value is: " << x << "\n";
cout << "User input is: " << y << "\n";
cout << "Option no. selected in Menu is: " << get_int("o") + 1 << "\n";
cout << "Chrome debug msgs are: " << get_str("z") << "\n";
}
```

**Note:** To run this above example, build this repository with examples, steps [given here](#build-with-examples)
**Note:** To run this example, build this repository with examples, steps [given here](#build-with-examples)

## Build instructions:
~~~bash
Expand All @@ -242,7 +248,7 @@ ninja

- [x] Adding color (component wise)
- [x] Adding FTXUI DOM elements (like seperator, border)
- [ ] Adding a way to use C++ defined variables directly, instead of depending on script-variables
- [x] Adding a way to use C++ defined variables directly, instead of depending on script-variables
- [ ] Adding detailed user documentation

## Dependencies
Expand Down
24 changes: 12 additions & 12 deletions cpp_examples/example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ 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;
string y = "Init value";
set_int_var("x", &x);
set_str_var("y", &y);
string source_code = R"(Border Vertical{
str z = "init"
str a
int o = 0
Input {
"Type something...",
Password,
y
}
RedLight Slider {
Expand All @@ -26,12 +26,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
Expand All @@ -44,8 +44,8 @@ int main() {

parse_qf(source_code);

cout << "x is: " << get_int("x") << "\n";
cout << "y is: " << get_str("y") << "\n";
cout << "o is: " << get_int("o") << "\n";
cout << "z is: " << get_str("z") << "\n";
cout << "Slider value is: " << x << "\n";
cout << "User input is: " << y << "\n";
cout << "Option no. selected in Menu is: " << get_int("o") + 1 << "\n";
cout << "Chrome debug msgs are: " << get_str("z") << "\n";
}
56 changes: 56 additions & 0 deletions include/quick-ftxui.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ namespace quick_ftxui_ast {
std::map<std::string, int> numbers;
std::map<std::string, std::string> strings;

std::map<int *, std::string> ref_nums;
std::map<std::string *, std::string> ref_strs;

///////////////////////////////////////////////////////////////////////////
// The AST
///////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -1087,6 +1090,17 @@ 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];
}

for (auto It : quick_ftxui_ast::ref_nums) {
*(It.first) = quick_ftxui_ast::numbers[It.second];
}
for (auto It : quick_ftxui_ast::ref_strs) {
*(It.first) = quick_ftxui_ast::strings[It.second];
}
}
} else {
throw std::runtime_error("Parsing failed\n");
Expand Down Expand Up @@ -1114,6 +1128,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()) {
Expand All @@ -1135,6 +1170,27 @@ 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, ""});
}
}

void set_str_var(std::string var_name, std::string *init_value) {
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, (*init_value)});
quick_ftxui_ast::ref_strs.insert({init_value, var_name});
}
}

} // namespace quick_ftxui

#endif // QUICK_FTXUI_HPP

0 comments on commit f39030d

Please sign in to comment.