Files
armorpaint/paint/sources/ui/tab_console.c
T

197 lines
5.7 KiB
C
Raw Normal View History

2026-03-09 13:17:15 +01:00
2026-06-04 11:00:05 +02:00
#include "../global.h"
2026-03-09 13:17:15 +01:00
2026-05-06 19:54:13 +02:00
bool tab_console_prompt_entered = false;
2026-03-09 23:02:08 +01:00
void tab_console_draw_export_on_file_picked(char *path) {
char *str = string_array_join(console_last_traces, "\n");
char *f = ui_files_filename;
if (string_equals(f, "")) {
2026-03-15 11:37:45 +01:00
f = string_copy(tr("untitled"));
2026-03-09 23:02:08 +01:00
}
2026-03-10 21:00:00 +01:00
path = string("%s%s%s", path, PATH_SEP, f);
2026-03-09 23:02:08 +01:00
if (!ends_with(path, ".txt")) {
2026-03-10 21:00:00 +01:00
path = string("%s.txt", path);
2026-03-09 23:02:08 +01:00
}
iron_file_save_bytes(path, sys_string_to_buffer(str), 0);
}
2026-05-06 19:54:13 +02:00
void tab_console_run_done(char *s) {
2026-05-07 14:14:47 +02:00
console_log(tr("Script updated."));
if (starts_with(s, "```c")) {
s = substring(s, 5, string_length(s) - 4);
2026-05-06 19:54:13 +02:00
}
2026-05-07 14:14:47 +02:00
g_project->script_datas->buffer[0] = string_copy(s);
ui_base_hwnds->buffer[TAB_AREA_SIDEBAR0]->redraws = 2;
// any_array_t *parts = string_split(s, "\n\n");
// for (i32 i = 0; i < parts->length; ++i) {
// console_log(parts->buffer[i]);
// if (i < parts->length - 1) {
// console_log(""); // new-line
// }
// }
2026-05-06 19:54:13 +02:00
}
2026-05-07 13:24:29 +02:00
#if defined(IRON_WINDOWS) || defined(IRON_LINUX) || defined(IRON_MACOS)
void tab_console_run_button_on_next_frame(void *_) {
box_preferences_htab->i = PREFERENCES_TAB_NEURAL;
box_preferences_show();
}
bool tab_console_run_button(ui_handle_t *h_input, bool press_run) {
char *url = box_preferneces_model_url_from_name("Qwen");
char *file_name = box_preferences_file_name_from_url(url);
bool found = box_preferences_model_exists(file_name);
if (iron_exec_async_done == 0) {
ui_icon_button(tr("Processing..."), ICON_STOP, UI_ALIGN_CENTER);
}
else if (!found && ui_icon_button(tr("Setup"), ICON_COG, UI_ALIGN_CENTER)) {
sys_notify_on_next_frame(&tab_console_run_button_on_next_frame, NULL);
}
else if (found && (ui_icon_button(tr("Run"), ICON_PLAY, UI_ALIGN_CENTER) || press_run)) {
2026-05-07 14:14:47 +02:00
char *prompt = h_input->text;
2026-05-07 13:24:29 +02:00
if (!tab_console_prompt_entered) {
tab_console_prompt_entered = true;
text_to_text_node_clear();
2026-05-07 14:14:47 +02:00
buffer_t *api_blob = data_get_blob("api.h");
char *api = sys_buffer_to_string(api_blob);
char *guide = "Write C code only. Do not chain statements - declare intermediary variables. Place the code inside 'void main()' function.";
prompt = string("%s\n%s\n%s", api, guide, prompt);
2026-05-07 13:24:29 +02:00
}
2026-05-07 14:14:47 +02:00
2026-05-07 13:24:29 +02:00
console_log(string(">%s", h_input->text));
2026-05-07 14:14:47 +02:00
text_to_text_node_run(prompt, tab_console_run_done);
2026-05-07 13:24:29 +02:00
h_input->text = "";
return true;
}
return false;
}
#endif
2026-03-06 12:56:38 +01:00
void tab_console_draw(ui_handle_t *htab) {
2026-03-15 11:37:45 +01:00
char *title = console_message_timer > 0 ? string("%s ", console_message) : tr("Console");
2026-03-10 21:00:00 +01:00
i32 color = console_message_timer > 0 ? console_message_color : -1;
2026-03-06 12:56:38 +01:00
2026-06-08 11:42:44 +02:00
if (ui_tab(htab, title, false, color, false) && g_ui->_window_h > ui_statusbar_default_h * UI_SCALE()) {
2026-03-06 12:56:38 +01:00
ui_begin_sticky();
2026-03-10 21:00:00 +01:00
#if defined(IRON_WINDOWS) || defined(IRON_LINUX) || defined(IRON_MACOS) // Copy
2026-03-06 12:56:38 +01:00
f32_array_t *row = f32_array_create_from_raw(
(f32[]){
-100,
-100,
-100,
},
3);
2026-03-10 21:00:00 +01:00
#else
2026-03-06 12:56:38 +01:00
f32_array_t *row = f32_array_create_from_raw(
(f32[]){
-100,
-100,
},
2);
2026-03-10 21:00:00 +01:00
#endif
2026-03-06 12:56:38 +01:00
ui_row(row);
2026-03-13 17:44:47 +01:00
ui_handle_t *h_input = ui_handle(__ID__);
2026-03-15 11:37:45 +01:00
if (ui_icon_button(tr("Clear"), ICON_ERASE, UI_ALIGN_CENTER)) {
2026-03-06 12:56:38 +01:00
gc_unroot(console_last_traces);
2026-03-08 08:39:29 +01:00
console_last_traces = any_array_create_from_raw((void *[]){}, 0);
2026-03-06 12:56:38 +01:00
gc_root(console_last_traces);
2026-05-06 19:54:13 +02:00
h_input->text = "";
tab_console_prompt_entered = false;
2026-03-06 12:56:38 +01:00
}
2026-03-15 11:37:45 +01:00
if (ui_icon_button(tr("Export"), ICON_EXPORT, UI_ALIGN_CENTER)) {
2026-03-09 23:02:08 +01:00
ui_files_show("txt", true, false, &tab_console_draw_export_on_file_picked);
2026-03-06 12:56:38 +01:00
}
2026-03-10 21:00:00 +01:00
#if defined(IRON_WINDOWS) || defined(IRON_LINUX) || defined(IRON_MACOS)
2026-03-15 11:37:45 +01:00
if (ui_icon_button(tr("Copy"), ICON_COPY, UI_ALIGN_CENTER)) {
2026-03-07 21:12:59 +01:00
char *str = string_array_join(console_last_traces, "\n");
2026-03-06 12:56:38 +01:00
iron_copy_to_clipboard(str);
}
2026-03-10 21:00:00 +01:00
#endif
2026-03-06 12:56:38 +01:00
ui_end_sticky();
2026-06-08 15:35:04 +02:00
draw_font_t *_font = g_font;
i32 _font_size = g_ui->font_size;
i32 _element_offset = g_theme->ELEMENT_OFFSET;
g_theme->ELEMENT_OFFSET = 0;
draw_font_t *f = data_get_font("font_mono.ttf");
2026-06-08 11:42:44 +02:00
ui_set_font(g_ui, f);
g_ui->font_size = math_floor(15 * UI_SCALE());
2026-03-06 12:56:38 +01:00
for (i32 i = 0; i < console_last_traces->length; ++i) {
2026-05-07 13:24:29 +02:00
char *t = console_last_traces->buffer[i];
i32 len = string_length(t);
2026-06-08 11:42:44 +02:00
float max_w = g_ui->_w;
2026-05-07 13:24:29 +02:00
uint32_t bg = starts_with(t, ">") ? 0x22000000 : 0x00000000;
2026-06-08 11:42:44 +02:00
if (draw_string_width(f, g_ui->font_size, t) <= max_w) {
2026-05-07 13:24:29 +02:00
ui_text(t, UI_ALIGN_LEFT, bg);
2026-05-06 19:54:13 +02:00
continue;
}
// Word wrap
i32 start = 0;
while (start < len) {
i32 end = start;
i32 last_space = -1;
2026-06-08 11:42:44 +02:00
while (end < len && draw_sub_string_width(f, g_ui->font_size, t, start, end + 1) <= max_w) {
2026-05-06 19:54:13 +02:00
if (t[end] == ' ') {
last_space = end;
}
end++;
}
if (end == len) {
2026-05-07 13:24:29 +02:00
ui_text(substring(t, start, end), UI_ALIGN_LEFT, bg);
2026-05-06 19:54:13 +02:00
break;
}
if (last_space > start) {
2026-05-07 13:24:29 +02:00
ui_text(substring(t, start, last_space), UI_ALIGN_LEFT, bg);
2026-05-06 19:54:13 +02:00
start = last_space + 1;
}
else {
2026-05-07 13:24:29 +02:00
ui_text(substring(t, start, end > start ? end : start + 1), UI_ALIGN_LEFT, bg);
2026-05-06 19:54:13 +02:00
start = end > start ? end : start + 1;
}
}
2026-03-06 12:56:38 +01:00
}
2026-03-13 17:44:47 +01:00
2026-06-08 15:35:04 +02:00
g_theme->ELEMENT_OFFSET = _element_offset;
2026-05-07 13:24:29 +02:00
2026-03-13 17:44:47 +01:00
row = f32_array_create_from_raw(
(f32[]){
0.9,
0.1,
},
2);
ui_row(row);
ui_text_input(h_input, "", UI_ALIGN_LEFT, true, false);
2026-06-08 11:42:44 +02:00
bool press_run = h_input->changed && g_ui->is_return_down;
2026-03-13 17:44:47 +01:00
2026-06-08 11:42:44 +02:00
ui_set_font(g_ui, _font);
g_ui->font_size = _font_size;
2026-03-13 17:44:47 +01:00
2026-05-07 13:24:29 +02:00
#if defined(IRON_WINDOWS) || defined(IRON_LINUX) || defined(IRON_MACOS)
tab_console_run_button(h_input, press_run);
#else
if (ui_icon_button(tr("Run"), ICON_PLAY, UI_ALIGN_CENTER) || press_run) {
console_log(string(">%s", h_input->text));
2026-03-23 23:20:02 +01:00
minic_ctx_free(minic_eval(string("float main() { %s }", h_input->text)));
2026-03-13 17:44:47 +01:00
h_input->text = "";
}
2026-05-07 13:24:29 +02:00
#endif
2026-03-06 12:56:38 +01:00
}
}