From a14c7f993713a57f08ab358c1447492a7ba8e75b Mon Sep 17 00:00:00 2001 From: mguinhos Date: Tue, 20 Oct 2026 20:44:16 -0300 Subject: [PATCH] Configuracoes: caminho do arquivo de config + toggle da variavel de ambiente - tb_environ_config_path: caminho padrao ($XDG_CONFIG_HOME/tinyblocks/ settings.csv ou $HOME/.config/tinyblocks/settings.csv). - tb_environ_env_path: le TINYBLOCKS_SETTINGS. - tb_environ_export_csv_meta: escreve uma linha de metadados "# arquivo_config," antes da tabela; export_csv agora tambem cria as pastas do caminho (mkdir -p). - Editor: o CSV de settings passa a ser salvo no caminho do arquivo de configuracao (mostrado no campo) e inclui esse caminho no proprio CSV. - Nova linha de opcoes com o toggle "Detectar caminho de TINYBLOCKS_SETTINGS" (checkbox): ligado usa a variavel de ambiente, desligado usa o caminho padrao; mostra o valor atual (ou "nao definida"). - Testes: test-environ cobre config_path/env_path/export_csv_meta e a criacao de pastas; harness passa a ligar o toggle com TINYBLOCKS_SETTINGS e confere a linha de metadados no CSV. --- aa.json | 754 ------------------ libraries/tinyblocks-editor/src/tb_editor.c | 52 +- .../tinyblocks-environ/include/tb_environ.h | 18 +- libraries/tinyblocks-environ/src/tb_environ.c | 40 +- teste.json | 322 -------- tests/editor_harness.c | 20 +- tests/environ_test.c | 21 + 7 files changed, 139 insertions(+), 1088 deletions(-) delete mode 100644 aa.json delete mode 100644 teste.json diff --git a/aa.json b/aa.json deleted file mode 100644 index dcdf444..0000000 --- a/aa.json +++ /dev/null @@ -1,754 +0,0 @@ -{ - "format": "tinyblocks-project", - "version": 1, - "cur_tab": 1, - "tabs": [ - { - "name": "Código", - "composite": false, - "comp_id": -1, - "color": 0, - "ws_sx": 0, - "ws_sy": 0, - "blocks": [ - { - "i": 0, - "type": "Events.WhenRun", - "x": 40, - "y": 40, - "parent": -1, - "prel": -1, - "next": 7, - "sub": [ - -1, - -1 - ], - "args": [ - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1 - ], - "in_text": [ - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "" - ], - "lit": "" - }, - { - "i": 1, - "type": "Variables.Set", - "x": 42, - "y": 183, - "parent": 7, - "prel": 0, - "next": 2, - "sub": [ - -1, - -1 - ], - "args": [ - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1 - ], - "in_text": [ - "x", - "0", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "" - ], - "lit": "" - }, - { - "i": 2, - "type": "Control.Repeat", - "x": 0, - "y": 0, - "parent": 1, - "prel": 0, - "next": 6, - "sub": [ - 3, - -1 - ], - "args": [ - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1 - ], - "in_text": [ - "5", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "" - ], - "lit": "" - }, - { - "i": 3, - "type": "Variables.Change", - "x": 0, - "y": 0, - "parent": 2, - "prel": 1, - "next": 4, - "sub": [ - -1, - -1 - ], - "args": [ - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1 - ], - "in_text": [ - "x", - "1", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "" - ], - "lit": "" - }, - { - "i": 4, - "type": "IO.Print", - "x": 0, - "y": 0, - "parent": 3, - "prel": 0, - "next": -1, - "sub": [ - -1, - -1 - ], - "args": [ - -1, - 5, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1 - ], - "in_text": [ - "x vale", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "" - ], - "lit": "" - }, - { - "i": 5, - "type": "Variables.Get", - "x": 0, - "y": 0, - "parent": 4, - "prel": 4, - "next": -1, - "sub": [ - -1, - -1 - ], - "args": [ - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1 - ], - "in_text": [ - "x", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "" - ], - "lit": "" - }, - { - "i": 6, - "type": "Stage.Clear", - "x": 51, - "y": 90, - "parent": 2, - "prel": 0, - "next": 9, - "sub": [ - -1, - -1 - ], - "args": [ - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1 - ], - "in_text": [ - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "" - ], - "lit": "" - }, - { - "i": 7, - "type": "Stage.Clear", - "x": 55, - "y": 109, - "parent": 0, - "prel": 0, - "next": 1, - "sub": [ - -1, - -1 - ], - "args": [ - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1 - ], - "in_text": [ - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "" - ], - "lit": "" - }, - { - "i": 8, - "type": "Stage.Text", - "x": 117, - "y": 406, - "parent": 9, - "prel": 0, - "next": -1, - "sub": [ - -1, - -1 - ], - "args": [ - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1 - ], - "in_text": [ - "Olá!", - "-40", - "0", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "" - ], - "lit": "" - }, - { - "i": 9, - "type": "Stage.Clear", - "x": 43, - "y": 115, - "parent": 6, - "prel": 0, - "next": 8, - "sub": [ - -1, - -1 - ], - "args": [ - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1 - ], - "in_text": [ - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "" - ], - "lit": "" - } - ], - "top": [ - 0 - ] - }, - { - "name": "Código 2", - "composite": false, - "comp_id": -1, - "color": 0, - "ws_sx": 0, - "ws_sy": 0, - "blocks": [ - { - "i": 0, - "type": "Events.WhenRun", - "x": 229, - "y": 55, - "parent": -1, - "prel": -1, - "next": 1, - "sub": [ - -1, - -1 - ], - "args": [ - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1 - ], - "in_text": [ - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "" - ], - "lit": "" - }, - { - "i": 1, - "type": "Stage.Clear", - "x": 222, - "y": 103, - "parent": 0, - "prel": 0, - "next": 2, - "sub": [ - -1, - -1 - ], - "args": [ - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1 - ], - "in_text": [ - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "" - ], - "lit": "" - }, - { - "i": 2, - "type": "Stage.Background", - "x": 243, - "y": 137, - "parent": 1, - "prel": 0, - "next": 3, - "sub": [ - -1, - -1 - ], - "args": [ - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1 - ], - "in_text": [ - "255", - "255", - "255", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "" - ], - "lit": "" - }, - { - "i": 3, - "type": "Stage.Rect", - "x": 219, - "y": 345, - "parent": 2, - "prel": 0, - "next": -1, - "sub": [ - -1, - -1 - ], - "args": [ - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1 - ], - "in_text": [ - "0", - "0", - "120", - "80", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "" - ], - "lit": "" - } - ], - "top": [ - 0 - ] - }, - { - "name": "aa", - "composite": true, - "comp_id": 0, - "color": 0, - "ws_sx": 0, - "ws_sy": 0, - "blocks": [], - "top": [] - } - ], - "user_blocks": [ - { - "name": "aa", - "color": 0, - "tab": 2, - "inputs": [], - "outputs": [], - "parts": [] - } - ], - "variables": [] -} diff --git a/libraries/tinyblocks-editor/src/tb_editor.c b/libraries/tinyblocks-editor/src/tb_editor.c index 365d79b..a35fa5d 100644 --- a/libraries/tinyblocks-editor/src/tb_editor.c +++ b/libraries/tinyblocks-editor/src/tb_editor.c @@ -438,6 +438,7 @@ typedef struct { bool settings_open; int settings_sel; /* linha selecionada (-1 = nenhuma) */ bool settings_path_focus; /* campo de caminho do CSV em foco */ + bool settings_use_env; /* caminho vindo de TINYBLOCKS_SETTINGS */ bool settings_value_edit; /* editando o valor da linha sel. */ bool settings_new_key; /* digitando o nome de uma nova chave */ char settings_path[256]; @@ -3110,12 +3111,25 @@ static void libman_scroll_by(Ed* ed, float dy) { * Configuracoes (settings) - store da tinyblocks-environ, salvo/exportado em CSV * === */ #define ST_HEAD_H 64 -#define ST_TOOL_Y ST_HEAD_H +#define ST_OPT_Y ST_HEAD_H +#define ST_OPT_H 40 +#define ST_TOOL_Y (ST_HEAD_H + ST_OPT_H) #define ST_TOOL_H 64 #define ST_MARGIN 40 #define ST_ROW_H 40 #define ST_LIST_Y (ST_TOOL_Y + ST_TOOL_H + 6) #define ST_BTN_H 40 +#define ST_ENV_LABEL "Detectar caminho de TINYBLOCKS_SETTINGS" + +/* Recalcula o caminho do config: da variavel de ambiente (se o toggle + * estiver ligado e a variavel existir) ou o caminho padrao do sistema. */ +static void settings_sync_path(Ed* ed) { + if (ed->settings_use_env) { + const char* e = tb_environ_env_path(); + if (e[0]) { snprintf(ed->settings_path, sizeof(ed->settings_path), "%s", e); return; } + } + tb_environ_config_path(ed->settings_path, sizeof(ed->settings_path)); +} static void settings_show(Ed* ed) { ed->settings_open = true; @@ -3125,7 +3139,7 @@ static void settings_show(Ed* ed) { ed->settings_new_key = false; ed->settings_scroll = 0; ed->settings_hover = -1; - if (!ed->settings_path[0]) strcpy(ed->settings_path, "tinyblocks-settings.csv"); + settings_sync_path(ed); ed->libman_open = false; ed->path_input_active = false; ed->ctx_menu_open = false; @@ -3159,7 +3173,8 @@ static void st_button(TinyBlocks_RenderableBlock* rb, float x, float y, float w, } static void settings_export(Ed* ed, const char* path) { - bool ok = tb_environ_export_csv(path); + if (!path || !path[0]) path = ed->settings_path; + bool ok = tb_environ_export_csv_meta(path, "arquivo_config", path); if (ok) snprintf(ed->hot_status, sizeof(ed->hot_status), "Configuracoes salvas (CSV): %.200s", path); else snprintf(ed->hot_status, sizeof(ed->hot_status), @@ -3190,6 +3205,24 @@ static void settings_render(Ed* ed, TinyBlocks_RenderableBlock* rb) { { const char* t = "Configurações"; rb_text_c(rb, (WIN_W - tw(t, 20)) * 0.5f, by, t, COL_WHITE, 20); } + /* Opcoes: toggle de deteccao do caminho pela variavel de ambiente */ + rb_fill_rect(rb, 0, ST_OPT_Y, WIN_W, ST_OPT_H, COL_FLY); + { + float cb = ST_OPT_Y + (ST_OPT_H - 20) * 0.5f; + rb_fill_rrect(rb, ST_MARGIN, cb, 20, 20, 4, + ed->settings_use_env ? COL_MENU : COL_WHITE); + rb_stroke_rect(rb, ST_MARGIN, cb, 20, 20, 2.f, COL_MENU); + if (ed->settings_use_env) { + rb_line(rb, ST_MARGIN + 4, cb + 10, ST_MARGIN + 8, cb + 15, 2.5f, COL_WHITE); + rb_line(rb, ST_MARGIN + 8, cb + 15, ST_MARGIN + 16, cb + 5, 2.5f, COL_WHITE); + } + rb_text_c(rb, ST_MARGIN + 30, ST_OPT_Y + ST_OPT_H*0.5f, ST_ENV_LABEL, COL_TEXT, FS_UI); + const char* e = tb_environ_env_path(); + const char* hint = e[0] ? e : "TINYBLOCKS_SETTINGS nao definida"; + rb_text_c(rb, WIN_W - ST_MARGIN - tw(hint, FS_SM), ST_OPT_Y + ST_OPT_H*0.5f, + hint, e[0] ? COL_TEXT_DIM : COL_ERR_MENU, FS_SM); + } + /* Barra de ferramentas */ rb_fill_rect(rb, 0, ST_TOOL_Y, WIN_W, ST_TOOL_H, COL_PANEL); { @@ -3258,6 +3291,14 @@ static void settings_render(Ed* ed, TinyBlocks_RenderableBlock* rb) { static void settings_click(Ed* ed, float mx, float my) { if (my < ST_HEAD_H) { if (mx < 170) settings_close(ed); return; } + if (my < ST_OPT_Y + ST_OPT_H) { + if (in_rect(mx, my, ST_MARGIN, ST_OPT_Y, 540, ST_OPT_H)) { + ed->settings_use_env = !ed->settings_use_env; + settings_sync_path(ed); + } + return; + } + if (my < ST_TOOL_Y + ST_TOOL_H) { float px, py, pw, ph; settings_path_rect(ed, &px, &py, &pw, &ph); if (in_rect(mx, my, px, py, pw, ph)) { @@ -3268,7 +3309,7 @@ static void settings_click(Ed* ed, float mx, float my) { float x, y, w, h; settings_btn_rect(ed, i, &x, &y, &w, &h); if (!in_rect(mx, my, x, y, w, h)) continue; ed->settings_path_focus = false; - if (i == 0) settings_export(ed, "tinyblocks-settings.csv"); + if (i == 0) settings_export(ed, ed->settings_path); else if (i == 1) settings_export(ed, ed->settings_path); else { ed->settings_new_key = true; ed->settings_key[0] = 0; ed->settings_value_edit = false; ed->settings_sel = -1; @@ -3998,7 +4039,8 @@ static bool editor_instance_init(TinyBlocks_Block* self, TinyBlocks* rt) { ed->ctx_menu_bi = -1; ed->path_mode = PM_NONE; ed->settings_sel = -1; - strcpy(ed->settings_path, "tinyblocks-settings.csv"); + ed->settings_use_env = false; + settings_sync_path(ed); ed->tab_n = 1; ed->cur_tab = 0; diff --git a/libraries/tinyblocks-environ/include/tb_environ.h b/libraries/tinyblocks-environ/include/tb_environ.h index 1049ad8..18f7055 100644 --- a/libraries/tinyblocks-environ/include/tb_environ.h +++ b/libraries/tinyblocks-environ/include/tb_environ.h @@ -25,8 +25,24 @@ TB_API bool tb_environ_has(const char* key); TB_API void tb_environ_set(const char* key, const char* value); TB_API void tb_environ_unset(const char* key); -/* Exporta/enumera como CSV "chave,valor". Retorna false em erro de IO. */ +/* Exporta/enumera como CSV "chave,valor". Retorna false em erro de IO. + * As pastas do caminho sao criadas se necessario. */ TB_API bool tb_environ_export_csv(const char* path); + +/* Como tb_environ_export_csv, mas escreve antes uma linha de metadados + * "# ," (ex.: o caminho do arquivo de configuracao). */ +TB_API bool tb_environ_export_csv_meta(const char* path, + const char* meta_key, + const char* meta_value); + +/* Caminho padrao do arquivo de configuracao do editor: + * $XDG_CONFIG_HOME/tinyblocks/settings.csv ou + * $HOME/.config/tinyblocks/settings.csv ou ./tinyblocks-settings.csv */ +TB_API void tb_environ_config_path(char* buf, size_t cap); + +/* Valor da variavel de ambiente TINYBLOCKS_SETTINGS ("" se ausente). */ +TB_API const char* tb_environ_env_path(void); + TB_API const char* tb_environ_last_error(void); #endif /* TINYBLOCKS_ENVIRON_H */ diff --git a/libraries/tinyblocks-environ/src/tb_environ.c b/libraries/tinyblocks-environ/src/tb_environ.c index bc22308..4880d22 100644 --- a/libraries/tinyblocks-environ/src/tb_environ.c +++ b/libraries/tinyblocks-environ/src/tb_environ.c @@ -12,6 +12,8 @@ #include #include #include +#include +#include #include #include "tb_environ.h" @@ -107,11 +109,29 @@ static void csv_field(FILE* f, const char* s) { fputc('"', f); } -bool tb_environ_export_csv(const char* path) { +/* Cria as pastas do caminho (como mkdir -p do diretorio pai). */ +static void mkparents(const char* path) { + char tmp[1024]; + snprintf(tmp, sizeof(tmp), "%s", path); + for (char* p = tmp + 1; *p; p++) { + if (*p == '/') { *p = 0; mkdir(tmp, 0755); *p = '/'; } + } +} + +bool tb_environ_export_csv_meta(const char* path, const char* meta_key, + const char* meta_value) { s_err[0] = 0; if (!path || !path[0]) { snprintf(s_err, sizeof(s_err), "caminho vazio"); return false; } + mkparents(path); FILE* f = fopen(path, "wb"); if (!f) { snprintf(s_err, sizeof(s_err), "nao foi possivel abrir %s", path); return false; } + if (meta_key && meta_key[0]) { + fputs("# ", f); + csv_field(f, meta_key); + fputc(',', f); + csv_field(f, meta_value ? meta_value : ""); + fputc('\n', f); + } fputs("chave,valor\n", f); for (int i = 0; i < s_env_n; i++) { csv_field(f, s_env[i].key); @@ -123,6 +143,24 @@ bool tb_environ_export_csv(const char* path) { return true; } +bool tb_environ_export_csv(const char* path) { + return tb_environ_export_csv_meta(path, NULL, NULL); +} + +void tb_environ_config_path(char* buf, size_t cap) { + if (!buf || cap == 0) return; + const char* xdg = getenv("XDG_CONFIG_HOME"); + const char* home = getenv("HOME"); + if (xdg && xdg[0]) snprintf(buf, cap, "%s/tinyblocks/settings.csv", xdg); + else if (home && home[0]) snprintf(buf, cap, "%s/.config/tinyblocks/settings.csv", home); + else snprintf(buf, cap, "%s", "tinyblocks-settings.csv"); +} + +const char* tb_environ_env_path(void) { + const char* e = getenv("TINYBLOCKS_SETTINGS"); + return (e && e[0]) ? e : ""; +} + /* -- Blocos -------------------------------------------------------------- */ static const char* in_text(TinyBlocks_Block* self, TinyBlocks* rt, uint32_t i) { diff --git a/teste.json b/teste.json deleted file mode 100644 index 4df88b8..0000000 --- a/teste.json +++ /dev/null @@ -1,322 +0,0 @@ -{ - "format": "tinyblocks-project", - "version": 1, - "cur_tab": 0, - "tabs": [ - { - "name": "Código", - "composite": false, - "comp_id": -1, - "color": 0, - "ws_sx": 0, - "ws_sy": 0, - "blocks": [ - { - "i": 0, - "type": "Events.WhenRun", - "x": 40, - "y": 40, - "parent": -1, - "prel": -1, - "next": 1, - "sub": [ - -1, - -1 - ], - "args": [ - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1 - ], - "in_text": [ - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "" - ], - "lit": "" - }, - { - "i": 1, - "type": "Variables.Set", - "x": 0, - "y": 0, - "parent": 0, - "prel": 0, - "next": 2, - "sub": [ - -1, - -1 - ], - "args": [ - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1 - ], - "in_text": [ - "x", - "0", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "" - ], - "lit": "" - }, - { - "i": 2, - "type": "Control.Repeat", - "x": 0, - "y": 0, - "parent": 1, - "prel": 0, - "next": -1, - "sub": [ - 3, - -1 - ], - "args": [ - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1 - ], - "in_text": [ - "5", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "" - ], - "lit": "" - }, - { - "i": 3, - "type": "Variables.Change", - "x": 0, - "y": 0, - "parent": 2, - "prel": 1, - "next": 4, - "sub": [ - -1, - -1 - ], - "args": [ - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1 - ], - "in_text": [ - "x", - "1", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "" - ], - "lit": "" - }, - { - "i": 4, - "type": "IO.Print", - "x": 0, - "y": 0, - "parent": 3, - "prel": 0, - "next": -1, - "sub": [ - -1, - -1 - ], - "args": [ - -1, - 5, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1 - ], - "in_text": [ - "x vale", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "" - ], - "lit": "" - }, - { - "i": 5, - "type": "Variables.Get", - "x": 0, - "y": 0, - "parent": 4, - "prel": 4, - "next": -1, - "sub": [ - -1, - -1 - ], - "args": [ - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1, - -1 - ], - "in_text": [ - "x", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "", - "" - ], - "lit": "" - } - ], - "top": [ - 0 - ] - } - ], - "user_blocks": [], - "variables": [] -} diff --git a/tests/editor_harness.c b/tests/editor_harness.c index 8e68586..958227a 100644 --- a/tests/editor_harness.c +++ b/tests/editor_harness.c @@ -386,28 +386,38 @@ int main(void) { frame(0, 0, 0, 0, 0, 0, NULL); /* 18. Tela de Configuracoes (settings da tinyblocks-environ) */ - remove("tinyblocks-settings.csv"); + remove("build/settings_test.csv"); + setenv("TINYBLOCKS_SETTINGS", "build/settings_test.csv", 1); click(1231, 22); /* botao Config (WIN_W=1280) */ frame(0, 0, 0, 0, 0, 0, NULL); CHECK(find("Configurações", R_ANY, 0, &x, &y), "botao Config abre a tela de configuracoes"); + CHECK(find("Detectar caminho de TINYBLOCKS_SETTINGS", R_ANY, 0, &x, &y), + "mostra o toggle da variavel de ambiente"); CHECK(find("autor", R_ANY, 0, &x, &y), "listas as settings do store (autor)"); - click(200, 156); /* seleciona a linha 'autor' */ + click(50, 84); /* liga o toggle de deteccao pelo env */ + frame(0, 0, 0, 0, 0, 0, NULL); + CHECK(find("build/settings_test.csv", R_ANY, 0, &x, &y), + "toggle passa a usar o caminho de TINYBLOCKS_SETTINGS"); + click(200, 194); /* seleciona a linha 'autor' */ type("maria"); key(TB_KEY_RETURN, 0, 0); frame(0, 0, 0, 0, 0, 0, NULL); - click(922, 96); /* botao Salvar (CSV) */ + click(922, 136); /* botao Salvar (CSV no arquivo de config) */ frame(0, 0, 0, 0, 0, 0, NULL); { - FILE* sf = fopen("tinyblocks-settings.csv", "rb"); + FILE* sf = fopen("build/settings_test.csv", "rb"); char sbuf[512] = ""; if (sf) { size_t sn = fread(sbuf, 1, sizeof(sbuf)-1, sf); sbuf[sn] = 0; fclose(sf); } + CHECK(strstr(sbuf, "# arquivo_config,build/settings_test.csv") != NULL, + "CSV inclui o caminho do arquivo de configuracao"); CHECK(strstr(sbuf, "chave,valor") != NULL, "settings salvas em CSV (cabecalho)"); CHECK(strstr(sbuf, "autor,maria") != NULL, "valor editado gravado no CSV"); - remove("tinyblocks-settings.csv"); + remove("build/settings_test.csv"); } key(TB_KEY_ESCAPE, 0, 0); /* fecha a tela */ frame(0, 0, 0, 0, 0, 0, NULL); CHECK(!find("Configurações", R_ANY, 0, &x, &y), "Esc fecha as configuracoes"); + unsetenv("TINYBLOCKS_SETTINGS"); printf("comandos de desenho no último frame: %u\n", ncmd); tinyblocks_deinit(&rt); diff --git a/tests/environ_test.c b/tests/environ_test.c index 349a0ad..01b9273 100644 --- a/tests/environ_test.c +++ b/tests/environ_test.c @@ -62,6 +62,27 @@ int main(void) { CHECK(strstr(buf, "\"com,quota\",\"ele disse \"\"oi\"\"\"") != NULL, "CSV escapa aspas corretamente"); remove(path); + /* -- Caminho de configuracao + metadados + criacao de pastas --- */ + { + char cp[512]; + tb_environ_config_path(cp, sizeof(cp)); + CHECK(strstr(cp, "tinyblocks/settings.csv") != NULL, "config_path aponta para .../tinyblocks/settings.csv"); + + setenv("TINYBLOCKS_SETTINGS", "/tmp/tb-cfg.csv", 1); + CHECK(strcmp(tb_environ_env_path(), "/tmp/tb-cfg.csv") == 0, "env_path le TINYBLOCKS_SETTINGS"); + unsetenv("TINYBLOCKS_SETTINGS"); + CHECK(tb_environ_env_path()[0] == 0, "env_path vazio sem a variavel"); + + CHECK(tb_environ_export_csv_meta("build/sub/dir/cfg.csv", "arquivo_config", "build/sub/dir/cfg.csv"), + "export_csv_meta cria as pastas e grava"); + FILE* mf = fopen("build/sub/dir/cfg.csv", "rb"); + char mbuf[512] = ""; + if (mf) { size_t mn = fread(mbuf, 1, sizeof(mbuf)-1, mf); mbuf[mn] = 0; fclose(mf); } + CHECK(strncmp(mbuf, "# arquivo_config,build/sub/dir/cfg.csv", 37) == 0, + "linha de metadados com o caminho de configuracao"); + CHECK(strstr(mbuf, "chave,valor") != NULL, "tabela CSV vem apos os metadados"); + } + /* -- Blocos --- */ TinyBlocks_TypeBlock* tset = tb_rt_find_type(rt, "Environ.Set"); TinyBlocks_Block* bset = tb_block_create(rt, tset);