Skip to content

Commit

Permalink
use cwd as workdir in CLI, use JSON path as workdir in GUI
Browse files Browse the repository at this point in the history
  • Loading branch information
matyalatte committed Dec 16, 2024
1 parent 5b8a7b4 commit c3d123c
Show file tree
Hide file tree
Showing 3 changed files with 259 additions and 7 deletions.
7 changes: 2 additions & 5 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -214,11 +214,8 @@ int main(int argc, char* argv[]) noexcept {
args.emplace_back(argv[i]);
#endif
}
char *exe_path_cstr = envuGetExecutablePath();
char *exe_dir = envuGetDirectory(exe_path_cstr);
envuSetCwd(exe_dir);
noex::string exe_path = envuStr(exe_path_cstr);
envuFree(exe_dir);

noex::string exe_path = envuStr(envuGetExecutablePath());

// Launch GUI if no args.
if (argc <= 1) return main_app();
Expand Down
21 changes: 19 additions & 2 deletions src/main_frame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,13 @@ void MainFrame::Initialize(const rapidjson::Document& definition,
bool ignore_external_json = false;

bool exists_external_json = true;
if (!json_path) {
noex::string workdir;
if (json_path) {
char* full_json_path = envuGetFullPath(json_path);
workdir = envuStr(envuGetDirectory(full_json_path));
envuFree(full_json_path);
} else {
workdir = envuStr(envuGetDirectory(exe_path.c_str()));
// Find gui_definition.json
json_path = DEF_JSON;
if (!envuFileExists(json_path)) {
Expand Down Expand Up @@ -62,7 +68,7 @@ void MainFrame::Initialize(const rapidjson::Document& definition,
if (!result.ok)
m_definition.SetObject();
} else {
result = { false, "gui_definition.json not found." };
result = { false, noex::string(json_path) + " not found." };
}
}
}
Expand Down Expand Up @@ -91,6 +97,17 @@ void MainFrame::Initialize(const rapidjson::Document& definition,
uiMainStep(1); // Need uiMainStep before using uiMsgBox
#endif

// Set working directory
if (workdir.empty()) {
// Failed to determine a workdir.
char* cwd = envuGetCwd();
PrintFmt("[LoadDefinition] CWD: %s\n", cwd);
envuFree(cwd);
} else {
PrintFmt("[LoadDefinition] CWD: %s\n", workdir.c_str());
envuSetCwd(workdir.c_str());
}

if (ignore_external_json) {
noex::string msg = noex::string("WARNING: Using embedded JSON. ") +
json_path + " was ignored.\n";
Expand Down
238 changes: 238 additions & 0 deletions test.tuw
Original file line number Diff line number Diff line change
@@ -0,0 +1,238 @@
{
"recommended": "0.5.0",
"minimum_required": "0.5.0",
"gui": [
{
"label": "IDs",
"command": "echo hello 1>&2",
"components": []
},
{
"label": "utf.py",
"command": "python\\python utf.py",
"codepage": "utf-8",
"components": []
},
{
"label": "Components Minimal",
"show_success_dialog": false,
"command": "echo file: %-% & echo folder: %-% & echo combo: %-% & echo check: %-% & echo check_array: %-% & echo textbox: %-% & echo int: %-% & echo float: %-%",
"components": [
{
"type": "static_text",
"label": "All components with their required keys."
},
{
"type": "file",
"label": "Some file path"
},
{
"type": "folder",
"label": "Some folder path"
},
{
"type": "radio",
"label": "combo box",
"items": [
{
"label": "value1"
},
{
"label": "value2"
},
{
"label": "value3"
}
]
},
{
"type": "check",
"label": "flag!"
},
{
"type": "check_array",
"label": "options",
"items": [
{
"label": "flag1"
},
{
"label": "flag2"
},
{
"label": "flag3"
}
]
},
{
"type": "text",
"label": "text box"
},
{
"type": "int",
"label": "Int picker"
},
{
"type": "float",
"label": "Float picker"
}
]
},
{
"label": "Components Optional",
"command": "echo file: %file% & echo folder: %folder% & echo combo: %combo% & echo check: %check% & echo check_array: %options% & echo textbox: %text% & echo int: %integer% & echo float: %double%",
"components": [
{
"type": "static_text",
"label": "All components with their optional keys.",
"platforms": ["win", "linux", "mac"]
},
{
"type": "file",
"label": "PNG or JPG",
"id": "file",
"extension": "png or jpg (*.png;*.jpg)|*.png;*.jpg|all|*",
"placeholder": "Drop a file here!",
"button": "...",
"default": "test.txt",
"add_quotes": true,
"tooltip": "Tooltip!",
"platforms": ["win", "linux", "mac"],
"validator": {
"exist_error": "",
"wildcard_error": "",
"regex_error": "",
"not_empty_error": ""
}
},
{
"type": "folder",
"label": "Folder path",
"id": "folder",
"placeholder": "Drop a folder here!",
"button": "Browse",
"default": "testdir",
"add_quotes": true,
"tooltip": "Tooltip?",
"platforms": ["win", "linux", "mac"]
},
{
"type": "radio",
"label": "Combo box",
"id": "combo",
"items": [
{
"label": "item1",
"value": "value1"
},
{
"label": "item2",
"value": "value2"
},
{
"label": "item3",
"value": "value3"
}
],
"default": 2,
"tooltip": "choose one of items.",
"platforms": ["win", "linux", "mac"]
},
{
"type": "check",
"label": "Checkbox",
"id": "check",
"value": "flag!",
"default": true,
"tooltip": "check this flag!",
"platforms": ["win", "linux", "mac"]
},
{
"type": "check_array",
"label": "Options",
"id": "options",
"items": [
{
"label": "item1",
"value": " --f1",
"default": false,
"tooltip": "flag1"
},
{
"label": "item2",
"value": " --f2",
"default": true,
"tooltip": "flag2"
},
{
"label": "item3",
"value": " --f3",
"default": false,
"tooltip": "flag3"
}
],
"platforms": ["win", "linux", "mac"]
},
{
"type": "text",
"label": "Text box",
"id": "text",
"placeholder": "type here!",
"default": "remove this text!",
"tooltip": "text box.",
"platforms": ["win", "linux", "mac"]
},
{
"type": "int",
"label": "Int picker",
"id": "integer",
"default": 10,
"min": -50,
"max": 50,
"inc": 10,
"wrap": true,
"tooltip": "int!",
"platforms": ["win", "linux", "mac"]
},
{
"type": "float",
"label": "Float picker",
"id": "double",
"default": 0.01,
"min": 0,
"max": 1,
"digits": 2,
"inc": 0.01,
"wrap": false,
"tooltip": "float!",
"platforms": ["win", "linux", "mac"]
}
]
},
{
"label": "GUI Optional",
"window_name": "Window Title Here",
"command_win": "color.bat",
"command_linux": "echo home: %__HOME__%; echo cwd: %__CWD__%; echo percent: %%; echo sample message!",
"command_mac": "echo home: %__HOME__%; echo cwd: %__CWD__%; echo percent: %%; echo sample message!",
"show_success_dialog": true,
"button": "Run!",
"show_last_line": true,
"check_exit_code": true,
"exit_success": 0,
"components": [
{
"type": "static_text",
"label": "show_last_line can show a message."
}
]
}
],
"help": [
{
"type": "url",
"label": "test",
"url":"http://test.com"
}
]
}

0 comments on commit c3d123c

Please sign in to comment.