Skip to content

Commit

Permalink
feat(js): open file api
Browse files Browse the repository at this point in the history
  • Loading branch information
std-microblock committed Feb 5, 2025
1 parent 6c85a51 commit 47e9541
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/shell/script/binding_qjs.h
Original file line number Diff line number Diff line change
Expand Up @@ -776,6 +776,8 @@ template<> struct js_bind<mb_shell::js::subproc> {
.constructor<>()
.static_fun<&mb_shell::js::subproc::run>("run")
.static_fun<&mb_shell::js::subproc::run_async>("run_async")
.static_fun<&mb_shell::js::subproc::open>("open")
.static_fun<&mb_shell::js::subproc::open_async>("open_async")
;
}

Expand Down
20 changes: 18 additions & 2 deletions src/shell/script/binding_types.cc
Original file line number Diff line number Diff line change
Expand Up @@ -503,8 +503,8 @@ subproc_result_data subproc::run(std::string cmd) {
si.hStdOutput = hWritePipe;
si.dwFlags |= STARTF_USESTDHANDLES;

if (!CreateProcessW(nullptr, utf8_to_wstring(cmd).data(), nullptr, nullptr, TRUE, 0,
nullptr, nullptr, &si, &pi)) {
if (!CreateProcessW(nullptr, utf8_to_wstring(cmd).data(), nullptr, nullptr,
TRUE, 0, nullptr, nullptr, &si, &pi)) {
CloseHandle(hReadPipe);
CloseHandle(hWritePipe);
return result;
Expand Down Expand Up @@ -797,4 +797,20 @@ menu_item_parent_item_controller::append_child_after(

return ctl;
}
void subproc::open(std::string path, std::string args) {
std::wstring wpath = utf8_to_wstring(path);
std::wstring wargs = utf8_to_wstring(args);
ShellExecuteW(nullptr, L"open", wpath.c_str(), wargs.c_str(), nullptr,
SW_SHOWNORMAL);
}
void subproc::open_async(std::string path, std::string args, std::function<void()> callback) {
std::thread([path, callback]() {
try {
open(path, args);
callback();
} catch (std::exception &e) {
std::cerr << "Error in subproc::open_async: " << e.what() << std::endl;
}
}).detach();
}
} // namespace mb_shell::js
18 changes: 18 additions & 0 deletions src/shell/script/binding_types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -963,6 +963,24 @@ export class subproc {
*/
static run_async: ((arg1: string, arg2: ((arg1: subproc_result_data) => void)) => void)


/**
* 同步打开东西
* Open something synchronously
* @param path: string, args: string
* @returns void
*/
static open: ((arg1: string, arg2: string) => void)


/**
* 异步打开东西
* Open something asynchronously
* @param path: string, args: string, callback: (() => void)
* @returns void
*/
static open_async: ((arg1: string, arg2: string, arg3: (() => void)) => void)

}

export class fs {
Expand Down
8 changes: 8 additions & 0 deletions src/shell/script/binding_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,14 @@ struct subproc {
// Run command asynchronously
static void run_async(std::string cmd,
std::function<void(subproc_result_data)> callback);

// 同步打开东西
// Open something synchronously
static void open(std::string path, std::string args = "");

// 异步打开东西
// Open something asynchronously
static void open_async(std::string path, std::string args, std::function<void()> callback);
};

// 文件系统操作
Expand Down

0 comments on commit 47e9541

Please sign in to comment.