Adiciona tinyblocks-environ e tela de Configuracoes (salvar/exportar CSV)

tinyblocks-environ (categoria "Ambiente"):
- blocos Environ.Get/Set/Has/Unset (store chave->valor; Get cai no getenv).
- API C enumeravel (count/key/value_at/get/has/set/unset) e
  tb_environ_export_csv(path) com escape de CSV; tb_environ_reset.
- library.toml com metadados; Makefile proprio (.a/.so).

Editor:
- biblioteca de settings no store da environ, com valores iniciais
  (autor, projeto, tema, escala, autosave).
- tela "Configurações" (botao Config no menu): lista as chaves, edita o
  valor da linha (Enter confirma, Esc cancela), "+ Nova" cria chave,
  Delete remove, campo de caminho + botoes "Salvar" e "Exportar CSV".
- Ctrl/scroll e foco de texto integrados ao mesmo fluxo do libman.

Build/testes:
- environ entra em CAT_LIBS (compila no editor, no harness e no libman).
- make test-environ (store, fallback getenv, escape CSV, blocos).
- harness: passo 18 cobre abrir Configuracoes, editar valor, salvar CSV
  e verificar o conteudo; Esc fecha.
This commit is contained in:
mguinhos committed 2026-10-20 20:36:14 -03:00
1 parent 3120d2d797
commit b29908e65b
9 files changed
+714 -7

No files matched your search

+24
View File
@@ -385,6 +385,30 @@ int main(void) {
key(TB_KEY_ESCAPE, 0, 0);
frame(0, 0, 0, 0, 0, 0, NULL);
/* 18. Tela de Configuracoes (settings da tinyblocks-environ) */
remove("tinyblocks-settings.csv");
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("autor", R_ANY, 0, &x, &y), "listas as settings do store (autor)");
click(200, 156); /* 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) */
frame(0, 0, 0, 0, 0, 0, NULL);
{
FILE* sf = fopen("tinyblocks-settings.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, "chave,valor") != NULL, "settings salvas em CSV (cabecalho)");
CHECK(strstr(sbuf, "autor,maria") != NULL, "valor editado gravado no CSV");
remove("tinyblocks-settings.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");
printf("comandos de desenho no último frame: %u\n", ncmd);
tinyblocks_deinit(&rt);
printf(fails ? "\n%d FALHA(S)\n" : "\nTUDO OK\n", fails);
+97
View File
@@ -0,0 +1,97 @@
/*
* environ_test.c - testa tinyblocks-environ:
* store chave/valor, getenv de fallback, exportacao CSV e os blocos
* Environ.Get / Environ.Set / Environ.Has / Environ.Unset.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <tinyblocks/tinyblocks.h>
#include <tb_environ.h>
static int fails = 0;
#define CHECK(c, m) do { if (c) printf("ok %s\n", m); else { printf("FAIL %s\n", m); fails++; } } while (0)
static void set_def(TinyBlocks_Block* b, uint32_t i, const char* v) {
TinyBlocks_Value vv = tb_value_text(v);
tb_block_set_default(b, i, vv);
tb_value_free(&vv);
}
int main(void) {
TinyBlocks* rt;
tinyblocks_init(&rt, NULL);
tb_library_register(rt, tb_environ_get_library());
/* -- Tipos registrados --- */
CHECK(tb_rt_find_type(rt, "Environ.Get") != NULL, "bloco Environ.Get registrado");
CHECK(tb_rt_find_type(rt, "Environ.Set") != NULL, "bloco Environ.Set registrado");
CHECK(tb_rt_find_type(rt, "Environ.Has") != NULL, "bloco Environ.Has registrado");
CHECK(tb_rt_find_type(rt, "Environ.Unset") != NULL, "bloco Environ.Unset registrado");
/* -- Store chave/valor --- */
tb_environ_reset();
CHECK(tb_environ_count() == 0, "store vazio apos reset");
tb_environ_set("a", "1");
tb_environ_set("b", "dois");
tb_environ_set("a", "3"); /* sobrescreve */
CHECK(tb_environ_count() == 2, "set cria e sobrescreve sem duplicar");
CHECK(strcmp(tb_environ_get("a"), "3") == 0, "get le valor sobrescrito");
CHECK(strcmp(tb_environ_get("b"), "dois") == 0, "get le segundo valor");
CHECK(strcmp(tb_environ_key(1), "b") == 0, "ordem de insercao preservada");
CHECK(tb_environ_has("b") && !tb_environ_has("z"), "has distingue presente/ausente");
/* Fallback para o ambiente real do processo */
const char* home = getenv("HOME");
if (home) CHECK(strcmp(tb_environ_get("HOME"), home) == 0, "get faz fallback para getenv");
tb_environ_unset("b");
CHECK(tb_environ_count() == 1, "unset remove a chave");
/* -- Exportacao CSV (com campo que precisa de aspas) --- */
tb_environ_set("com,quota", "ele disse \"oi\"");
const char* path = "build/environ_test.csv";
CHECK(tb_environ_export_csv(path), "export_csv grava o arquivo");
FILE* f = fopen(path, "rb");
char buf[512] = "";
if (f) { size_t n = fread(buf, 1, sizeof(buf) - 1, f); buf[n] = 0; fclose(f); }
CHECK(strstr(buf, "chave,valor") != NULL, "CSV tem cabecalho chave,valor");
CHECK(strstr(buf, "\"com,quota\"") != NULL, "campo com virgula vem entre aspas");
CHECK(strstr(buf, "\"com,quota\",\"ele disse \"\"oi\"\"\"") != NULL, "CSV escapa aspas corretamente");
remove(path);
/* -- Blocos --- */
TinyBlocks_TypeBlock* tset = tb_rt_find_type(rt, "Environ.Set");
TinyBlocks_Block* bset = tb_block_create(rt, tset);
set_def(bset, 0, "nome_do_bloco");
set_def(bset, 1, "TinyBlocks");
tb_block_eval(bset, rt);
TinyBlocks_TypeBlock* tget = tb_rt_find_type(rt, "Environ.Get");
TinyBlocks_Block* bget = tb_block_create(rt, tget);
set_def(bget, 0, "nome_do_bloco");
tb_block_eval(bget, rt);
CHECK(bget->outputs[0].kind == TB_VALUE_TEXT &&
strcmp(bget->outputs[0].as.text, "TinyBlocks") == 0,
"Environ.Set -> Environ.Get transporta o valor");
TinyBlocks_TypeBlock* thas = tb_rt_find_type(rt, "Environ.Has");
TinyBlocks_Block* bhas = tb_block_create(rt, thas);
set_def(bhas, 0, "nome_do_bloco");
tb_block_eval(bhas, rt);
CHECK(bhas->outputs[0].as.integer == 1, "Environ.Has retorna 1 para chave existente");
TinyBlocks_TypeBlock* tunset = tb_rt_find_type(rt, "Environ.Unset");
TinyBlocks_Block* bunset = tb_block_create(rt, tunset);
set_def(bunset, 0, "nome_do_bloco");
tb_block_eval(bunset, rt);
CHECK(!tb_environ_has("nome_do_bloco"), "Environ.Unset remove a chave");
tinyblocks_deinit(&rt);
if (fails) printf("\n%d FALHA(S)\n", fails);
else printf("\nTUDO OK\n");
return fails != 0;
}