Skip to content

Commit 85d0fd3

Browse files
committed
feat: format support for webui_run and webui_script
1 parent d9c86cc commit 85d0fd3

3 files changed

Lines changed: 102 additions & 0 deletions

File tree

include/webui.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1029,6 +1029,18 @@ WEBUI_EXPORT bool webui_set_tls_certificate(const char* certificate_pem, const c
10291029
*/
10301030
WEBUI_EXPORT void webui_run(size_t window, const char* script);
10311031

1032+
/**
1033+
* @brief Run formatted JavaScript without waiting for the response. All clients.
1034+
*
1035+
* @param window The window number
1036+
* @param format The JavaScript format to be run
1037+
*
1038+
* @warning This function DOES NOT handle escape characters, proceed with caution.
1039+
*
1040+
* @example webui_run_fmt(myWindow, "alert('Hello %s');", "World");
1041+
*/
1042+
WEBUI_EXPORT void webui_run_fmt(size_t window, const char* format, ...);
1043+
10321044
/**
10331045
* @brief Run JavaScript without waiting for the response. Single client.
10341046
*
@@ -1056,6 +1068,26 @@ WEBUI_EXPORT void webui_run_client(webui_event_t* e, const char* script);
10561068
WEBUI_EXPORT bool webui_script(size_t window, const char* script, size_t timeout,
10571069
char* buffer, size_t buffer_length);
10581070

1071+
/**
1072+
* @brief Run formatted JavaScript and get the response back. Work only in single client mode.
1073+
* Make sure your local buffer can hold the response.
1074+
*
1075+
* @param window The window number
1076+
* @param timeout The execution timeout in seconds
1077+
* @param buffer The local buffer to hold the response
1078+
* @param buffer_length The local buffer size
1079+
* @param format The JavaScript format to be run
1080+
*
1081+
* @return Returns True if there is no execution error
1082+
*
1083+
* @warning This function DOES NOT handle escape characters, proceed with caution.
1084+
*
1085+
* @example bool err = webui_script_fmt(myWindow, 0, myBuffer, myBufferSize,
1086+
* "return %d + %d;", 4, 6);
1087+
*/
1088+
WEBUI_EXPORT bool webui_script_fmt(size_t window, size_t timeout,
1089+
char* buffer, size_t buffer_length, const char* format, ...);
1090+
10591091
/**
10601092
* @brief Run JavaScript and get the response back. Single client.
10611093
* Make sure your local buffer can hold the response.

include/webui.hpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,12 +398,27 @@ namespace webui {
398398
webui_run(webui_window, script.data());
399399
}
400400

401+
// Quickly run a JavaScript from a formatted string (no response waiting).
402+
// Note that this function **DOES NOT** handle escape characters
403+
template<typename... Args>
404+
void run_fmt(const std::string_view fmt, Args... args) const {
405+
webui_run_fmt(webui_window, fmt.data(), args...);
406+
}
407+
401408
// Run a JavaScript, and get the response back (Make sure your local buffer can hold the response).
402409
bool script(const std::string_view script, unsigned int timeout,
403410
char* buffer, size_t buffer_length) const {
404411
return webui_script(webui_window, script.data(), timeout, buffer, buffer_length);
405412
}
406413

414+
// Run a JavaScript with formatting, and get the response back (Make sure your local buffer can hold the response).
415+
// Note that this function **DOES NOT** handle escape characters
416+
template <typename... Args>
417+
bool script_fmt(unsigned int timeout, char* buffer, size_t buffer_length,
418+
const std::string_view fmt, Args... args) const {
419+
return webui_script_fmt(webui_window, timeout, buffer, buffer_length, fmt.data(), args...);
420+
}
421+
407422
// Chose between Deno and Nodejs runtime for .js and .ts files.
408423
void set_runtime(unsigned int runtime) const {
409424
webui_set_runtime(webui_window, runtime);

src/webui.c

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -847,6 +847,31 @@ void webui_run(size_t window, const char* script) {
847847
_webui_send_all(win, 0, WEBUI_CMD_JS_QUICK, script, js_len);
848848
}
849849

850+
void webui_run_fmt(size_t window, const char* fmt, ...) {
851+
int len;
852+
char* buf;
853+
854+
// Precalculate actual string length
855+
va_list args;
856+
va_start(args, fmt);
857+
len = vsnprintf(NULL, 0, fmt, args);
858+
va_end(args);
859+
860+
if (len < 1) return;
861+
862+
buf = (char*)_webui_malloc(len + 1);
863+
if (buf == NULL) return;
864+
865+
// Format and execute
866+
va_start(args, fmt);
867+
vsnprintf(buf, len + 1, fmt, args);
868+
va_end(args);
869+
870+
webui_run(window, buf);
871+
872+
_webui_free_mem((void*)buf);
873+
}
874+
850875
void webui_set_close_handler_wv(size_t window, bool(*close_handler)(size_t window)) {
851876

852877
// Initialization
@@ -1065,6 +1090,36 @@ bool webui_script(size_t window, const char* script, size_t timeout,
10651090
return webui_script_client(&e, script, timeout, buffer, buffer_length);
10661091
}
10671092

1093+
bool webui_script_fmt(
1094+
size_t window, size_t timeout,
1095+
char* buffer, size_t buffer_length,
1096+
const char* fmt, ...) {
1097+
int len;
1098+
char* buf;
1099+
1100+
// Precalculate actual string length
1101+
va_list args;
1102+
va_start(args, fmt);
1103+
len = vsnprintf(NULL, 0, fmt, args);
1104+
va_end(args);
1105+
1106+
if (len < 1) return false;
1107+
1108+
buf = (char*)_webui_malloc(len + 1);
1109+
if (buf == NULL) return false;
1110+
1111+
// Format and execute
1112+
va_start(args, fmt);
1113+
vsnprintf(buf, len + 1, fmt, args);
1114+
va_end(args);
1115+
1116+
bool status = webui_script(window, buf, timeout, buffer, buffer_length);
1117+
1118+
_webui_free_mem((void*)buf);
1119+
1120+
return status;
1121+
}
1122+
10681123
static uint32_t _webui_generate_random_uint32() {
10691124
uint32_t timestamp = (uint32_t) time(NULL);
10701125
// Get the higher 16 bits

0 commit comments

Comments
 (0)