Skip to content

Commit 9214d03

Browse files
committed
feat: format support for webui_run and webui_script
1 parent d9c86cc commit 9214d03

2 files changed

Lines changed: 123 additions & 0 deletions

File tree

include/webui_extras.h

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
WebUI Library Extras
3+
https://webui.me
4+
https://github.com/webui-dev/webui
5+
Copyright (c) 2020-2025 Hassan Draga.
6+
Licensed under MIT License.
7+
All rights reserved.
8+
Canada.
9+
*/
10+
11+
#ifndef _WEBUI_EXTRAS_H
12+
#define _WEBUI_EXTRAS_H
13+
14+
#ifdef __cplusplus
15+
extern "C" {
16+
#endif
17+
18+
#include "webui.h"
19+
20+
/**
21+
* @brief Construct a JavaScript string from a format string,
22+
* then execute it without waiting for the response. All clients.
23+
*
24+
* @param window The window number
25+
* @param fmt The JavaScript format string to be run
26+
*
27+
* @warning This function DOES NOT handle escape characters, proceed with caution.
28+
* For example, when passing string arguments that contain quotes or backslashes,
29+
* it may lead to unexpected behavior
30+
*
31+
* @note This function internally uses vsnprintf
32+
*
33+
* @example webui_run_fmt(myWindow, "alert('Hello %s');", "World");
34+
*/
35+
WEBUI_EXPORT void webui_run_fmt(size_t window, const char* fmt, ...);
36+
37+
/**
38+
* @brief Construct a JavaScript string from a format string,
39+
* then execute it and get the response back. Work only in single client mode.
40+
* Make sure your local buffer can hold the response.
41+
*
42+
* @param window The window number
43+
* @param timeout The execution timeout in seconds
44+
* @param buffer The local buffer to hold the response
45+
* @param buffer_length The local buffer size
46+
* @param fmt The JavaScript format string to be run
47+
*
48+
* @return Returns True if there is no execution error
49+
*
50+
* @warning This function DOES NOT handle escape characters, proceed with caution.
51+
* For example, when passing string arguments that contain quotes or backslashes,
52+
* it may lead to unexpected behavior
53+
*
54+
* @note This function internally uses vsnprintf
55+
*
56+
* @example bool err = webui_script_fmt(myWindow, 0, myBuffer, myBufferSize,
57+
* "return %d + %d;", 4, 6);
58+
*/
59+
WEBUI_EXPORT bool webui_script_fmt(size_t window, size_t timeout,
60+
char* buffer, size_t buffer_length,
61+
const char* fmt, ...);
62+
63+
#ifdef __cplusplus
64+
}
65+
#endif
66+
67+
#endif /*_WEBUI_EXTRAS_H */

src/webui.c

Lines changed: 56 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,37 @@ 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+
bool status;
1100+
1101+
// Precalculate actual string length
1102+
va_list args;
1103+
va_start(args, fmt);
1104+
len = vsnprintf(NULL, 0, fmt, args);
1105+
va_end(args);
1106+
1107+
if (len < 1) return false;
1108+
1109+
buf = (char*)_webui_malloc(len + 1);
1110+
if (buf == NULL) return false;
1111+
1112+
// Format and execute
1113+
va_start(args, fmt);
1114+
vsnprintf(buf, len + 1, fmt, args);
1115+
va_end(args);
1116+
1117+
status = webui_script(window, buf, timeout, buffer, buffer_length);
1118+
1119+
_webui_free_mem((void*)buf);
1120+
1121+
return status;
1122+
}
1123+
10681124
static uint32_t _webui_generate_random_uint32() {
10691125
uint32_t timestamp = (uint32_t) time(NULL);
10701126
// Get the higher 16 bits

0 commit comments

Comments
 (0)