Skip to content

Commit

Permalink
Merge pull request #46 from matyalatte/refactoring
Browse files Browse the repository at this point in the history
Refactoring and v0.7.1 update
  • Loading branch information
matyalatte authored Sep 14, 2024
2 parents 5bf7242 + deab667 commit f6652f8
Show file tree
Hide file tree
Showing 16 changed files with 236 additions and 285 deletions.
2 changes: 2 additions & 0 deletions docs/changelog.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
ver 0.7.1
- Added support for C-style comments and trailing commas in JSON files.
- Added support for .jsonc files.
- Added "codepage" option to support UTF-8 outputs on Windows.
- Added support for colorized outputs on Linux.
- Fixed a bug that there is no limit on the buffer size of the console window on Linux.
- Optimized some functions to reduce the binary size.

ver 0.7.0
- Added options for string validation.
Expand Down
20 changes: 15 additions & 5 deletions examples/all_keys/gui_definition.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"recommended": "0.7.0",
"minimum_required": "0.7.0",
"recommended": "0.7.1",
"minimum_required": "0.7.1",
"gui": [
{
"label": "Components Minimal",
Expand Down Expand Up @@ -249,6 +249,7 @@
"show_last_line": true,
"check_exit_code": true,
"exit_success": 0,
"codepage": "default",
"components": [
{
"type": "static_text",
Expand All @@ -260,8 +261,17 @@
"help": [
{
"type": "url",
"label": "test",
"url":"example.com"
"label": "open example.com",
"url": "example.com"
},
{
"type": "file",
"label": "open gui_definition.json",
"path": "gui_definition.json"
}
]
], // Trailing comma
}
// Single-line comment
/*
* Multi-line comments
*/
3 changes: 0 additions & 3 deletions examples/other_features/codepage/README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
# UTF-8 Outputs on Windows

> [!WARNING]
> Released builds do not support this feature yet.
Tuw expects user's default locale for stdout on Windows. If you want to use UTF-8 outputs on Windows, you should use `"codepage": "utf8"` (or `"codepage": "utf-8"`) as an option.

```json
Expand Down
3 changes: 0 additions & 3 deletions examples/tips/comments/README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
# JSON with Comments

> [!WARNING]
> Released builds do not support this feature yet.
Tuw supports the [JSON with Comments](https://code.visualstudio.com/docs/languages/json#_json-with-comments) format. It allows C-style comments (`//` and `/**/`) and trailing commas in addition to the standard JSON format. You can also use `.jsonc` as a file extension to let code editors know that it uses the extended JSON format.

```jsonc
Expand Down
8 changes: 4 additions & 4 deletions include/component.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,16 @@ class Component {
virtual ~Component();
virtual std::string GetRawString() { return "";}
std::string GetString();
std::string const GetID();
const std::string& GetID() const { return m_id; }

virtual void SetConfig(const rapidjson::Value& config) { UNUSED(config); }
virtual void GetConfig(rapidjson::Document& config) { UNUSED(config); }

bool HasString() { return m_has_string; }
bool IsWide() { return m_is_wide; }
bool HasString() const { return m_has_string; }
bool IsWide() const { return m_is_wide; }

bool Validate(bool* redraw_flag);
std::string GetValidationError();
const std::string& GetValidationError() const;
void PutErrorWidget(uiBox* box);

static Component* PutComponent(uiBox* box, const rapidjson::Value& j);
Expand Down
3 changes: 3 additions & 0 deletions include/json_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ struct JsonResult {

#define JSON_RESULT_OK { true, "" }

// Max binary size for JSON files.
#define JSON_SIZE_MAX 128 * 1024

JsonResult LoadJson(const std::string& file, rapidjson::Document& json);
JsonResult SaveJson(rapidjson::Document& json, const std::string& file);
std::string JsonToString(rapidjson::Document& json);
Expand Down
10 changes: 5 additions & 5 deletions include/tuw_constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ namespace tuw_constants {
" CLI tools\n";
constexpr char TOOL_NAME[] = "Tuw";
constexpr char AUTHOR[] = "matyalatte";
constexpr char VERSION[] = "0.7.0";
constexpr int VERSION_INT = 700;
constexpr char VERSION[] = "0.7.1";
constexpr int VERSION_INT = 701;

#ifdef _WIN32
constexpr char OS[] = "win";
#define TUW_CONSTANTS_OS "win"
const int GRID_COMP_XSPACE = 2;
const int GRID_MAIN_SPACE = 7;
const int BOX_MAIN_SPACE = 6;
Expand All @@ -23,7 +23,7 @@ namespace tuw_constants {
const int BTN_WIDTH = 90;
const int BTN_HEIGHT = 24;
#elif defined(__TUW_UNIX__)
constexpr char OS[] = "linux";
#define TUW_CONSTANTS_OS "linux"
const int GRID_COMP_XSPACE = 4;
const int GRID_MAIN_SPACE = 12;
const int BOX_MAIN_SPACE = 12;
Expand All @@ -32,7 +32,7 @@ namespace tuw_constants {
const int BTN_WIDTH = 84;
const int BTN_HEIGHT = -1;
#else
constexpr char OS[] = "mac";
#define TUW_CONSTANTS_OS "mac"
const int GRID_COMP_XSPACE = 4;
const int GRID_MAIN_SPACE = 12;
const int BOX_MAIN_SPACE = 12;
Expand Down
2 changes: 1 addition & 1 deletion include/validator.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@ class Validator {
~Validator() {}
void Initialize(const rapidjson::Value& j);
bool Validate(const std::string& str);
std::string GetError() { return m_error_msg; }
const std::string& GetError() const { return m_error_msg; }
};
26 changes: 11 additions & 15 deletions src/component.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Component::Component(const rapidjson::Value& j) {
m_is_wide = false;
m_label = j["label"].GetString();
m_id = json_utils::GetString(j, "id", "");
if (m_id == "") {
if (m_id.empty()) {
uint32_t hash = Fnv1Hash32(j["label"].GetString());
m_id = "_" + std::to_string(hash);
}
Expand All @@ -46,10 +46,6 @@ std::string Component::GetString() {
return str;
}

std::string const Component::GetID() {
return m_id;
}

bool Component::Validate(bool* redraw_flag) {
// Main frame should run Fit() after this function.
bool validate = m_validator.Validate(GetRawString());
Expand All @@ -75,7 +71,7 @@ bool Component::Validate(bool* redraw_flag) {
return validate;
}

std::string Component::GetValidationError() {
const std::string& Component::GetValidationError() const {
return m_validator.GetError();
}

Expand Down Expand Up @@ -239,7 +235,7 @@ class Filter {
name = n;
}
void AddPattern(const char* pattern) {
patterns.push_back(pattern);
patterns.emplace_back(pattern);
}
uiFileDialogParamsFilter ToLibuiFilter() {
return {
Expand Down Expand Up @@ -267,7 +263,7 @@ class FilterList {
bool is_reading_pattern = false;
Filter* filter = new Filter();
for (const char c : ext) {
if (c == "|"[0]) {
if (c == '|') {
filter_buf[i] = 0;
if (is_reading_pattern) {
filter->AddPattern(&filter_buf[start]);
Expand All @@ -278,7 +274,7 @@ class FilterList {
}
is_reading_pattern = !is_reading_pattern;
start = i + 1;
} else if (is_reading_pattern && (c == ";"[0])) {
} else if (is_reading_pattern && (c == ';')) {
filter_buf[i] = 0;
filter->AddPattern(&filter_buf[start]);
start = i + 1;
Expand Down Expand Up @@ -311,7 +307,7 @@ class FilterList {
}

void AddFilter(Filter* f) {
filters.push_back(f);
filters.emplace_back(f);
}

size_t GetSize() {
Expand Down Expand Up @@ -398,7 +394,7 @@ ComboBox::ComboBox(uiBox* box, const rapidjson::Value& j)
const char* label = i["label"].GetString();
uiComboboxAppend(combo, label);
const char* value = json_utils::GetString(i, "value", label);
values.push_back(value);
values.emplace_back(value);
}
uiBox* hbox = uiNewHorizontalBox();
uiBoxAppend(hbox, uiControl(combo), 0);
Expand Down Expand Up @@ -441,7 +437,7 @@ RadioButtons::RadioButtons(uiBox* box, const rapidjson::Value& j)
const char* label = i["label"].GetString();
uiRadioButtonsAppend(radio, label);
const char* value = json_utils::GetString(i, "value", label);
values.push_back(value);
values.emplace_back(value);
}
uiBox* hbox = uiNewHorizontalBox();
uiBoxAppend(hbox, uiControl(radio), 0);
Expand Down Expand Up @@ -526,9 +522,9 @@ CheckArray::CheckArray(uiBox* box, const rapidjson::Value& j)
uiControlSetTooltip(uiControl(check),
json_utils::GetString(i, "tooltip", ""));
}
checks->push_back(check);
checks->emplace_back(check);
const char* value = json_utils::GetString(i, "value", label);
values.push_back(value);
values.emplace_back(value);
id++;
}
uiBoxAppend(box, uiControl(check_array_box), 0);
Expand All @@ -537,7 +533,7 @@ CheckArray::CheckArray(uiBox* box, const rapidjson::Value& j)
}

std::string CheckArray::GetRawString() {
std::string str = "";
std::string str;
std::vector<uiCheckbox*> checks;
checks = *(std::vector<uiCheckbox*>*)m_widget;
for (size_t i = 0; i < checks.size(); i++) {
Expand Down
Loading

0 comments on commit f6652f8

Please sign in to comment.