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

@@ -0,0 +1,197 @@
/*
* TinyBlocks - tb_environ.c
* Categoria "Ambiente": le/define variaveis de ambiente (chave -> valor).
*
* Mantem um store em memoria, ordenado por insercao, que serve de base para a
* tela de Configuracoes do editor (enumerar e salvar/exportar como CSV).
* `Environ.Get` consulta primeiro o store e, se nao houver, o ambiente real
* do processo (getenv).
*/
#define _POSIX_C_SOURCE 200809L
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <tinyblocks/tinyblocks.h>
#include "tb_environ.h"
#define ENV_MAX 256
typedef struct { char* key; char* value; } EnvPair;
static EnvPair s_env[ENV_MAX];
static int s_env_n = 0;
static char s_err[256] = "";
/* -- Store --------------------------------------------------------------- */
static char* dup_str(const char* s) {
if (!s) s = "";
size_t n = strlen(s) + 1;
char* p = (char*)malloc(n);
if (p) memcpy(p, s, n);
return p;
}
static int env_find(const char* key) {
if (!key) return -1;
for (int i = 0; i < s_env_n; i++)
if (strcmp(s_env[i].key, key) == 0) return i;
return -1;
}
void tb_environ_reset(void) {
for (int i = 0; i < s_env_n; i++) { free(s_env[i].key); free(s_env[i].value); }
s_env_n = 0;
}
int tb_environ_count(void) { return s_env_n; }
const char* tb_environ_key(int i) {
return (i >= 0 && i < s_env_n) ? s_env[i].key : NULL;
}
const char* tb_environ_value_at(int i) {
return (i >= 0 && i < s_env_n) ? s_env[i].value : NULL;
}
bool tb_environ_has(const char* key) {
return env_find(key) >= 0;
}
const char* tb_environ_get(const char* key) {
int i = env_find(key);
if (i >= 0) return s_env[i].value;
const char* v = key ? getenv(key) : NULL;
return v ? v : "";
}
void tb_environ_set(const char* key, const char* value) {
if (!key || !key[0]) return;
if (!value) value = "";
int i = env_find(key);
if (i >= 0) {
free(s_env[i].value);
s_env[i].value = dup_str(value);
} else if (s_env_n < ENV_MAX) {
s_env[s_env_n].key = dup_str(key);
s_env[s_env_n].value = dup_str(value);
s_env_n++;
}
setenv(key, value, 1); /* mantem o ambiente do processo coerente */
}
void tb_environ_unset(const char* key) {
int i = env_find(key);
if (i < 0) return;
free(s_env[i].key);
free(s_env[i].value);
for (int j = i; j < s_env_n - 1; j++) s_env[j] = s_env[j + 1];
s_env_n--;
if (key) unsetenv(key);
}
const char* tb_environ_last_error(void) { return s_err; }
/* Escreve um campo CSV: aspas quando contem virgula, aspas ou quebra. */
static void csv_field(FILE* f, const char* s) {
if (!s) s = "";
bool quote = strpbrk(s, ",\"\n\r") != NULL;
if (!quote) { fputs(s, f); return; }
fputc('"', f);
for (const char* p = s; *p; p++) {
if (*p == '"') fputc('"', f);
fputc(*p, f);
}
fputc('"', f);
}
bool tb_environ_export_csv(const char* path) {
s_err[0] = 0;
if (!path || !path[0]) { snprintf(s_err, sizeof(s_err), "caminho vazio"); return false; }
FILE* f = fopen(path, "wb");
if (!f) { snprintf(s_err, sizeof(s_err), "nao foi possivel abrir %s", path); return false; }
fputs("chave,valor\n", f);
for (int i = 0; i < s_env_n; i++) {
csv_field(f, s_env[i].key);
fputc(',', f);
csv_field(f, s_env[i].value);
fputc('\n', f);
}
fclose(f);
return true;
}
/* -- Blocos -------------------------------------------------------------- */
static const char* in_text(TinyBlocks_Block* self, TinyBlocks* rt, uint32_t i) {
TinyBlocks_Value v = tb_block_get_input(self, rt, i);
return (v.kind == TB_VALUE_TEXT && v.as.text) ? v.as.text : "";
}
static void eval_get(TinyBlocks_Block* self, TinyBlocks* rt) {
tb_block_set_output(self, 0, tb_value_text(tb_environ_get(in_text(self, rt, 0))));
self->dirty = false;
}
static void eval_set(TinyBlocks_Block* self, TinyBlocks* rt) {
tb_environ_set(in_text(self, rt, 0), in_text(self, rt, 1));
self->dirty = false;
}
static void eval_has(TinyBlocks_Block* self, TinyBlocks* rt) {
tb_block_set_output(self, 0, tb_value_integer(tb_environ_has(in_text(self, rt, 0)) ? 1 : 0));
self->dirty = false;
}
static void eval_unset(TinyBlocks_Block* self, TinyBlocks* rt) {
tb_environ_unset(in_text(self, rt, 0));
self->dirty = false;
}
/* -- Registro ------------------------------------------------------------ */
static bool reg(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib, const char* name,
const TinyBlocks_SlotDesc* in, uint32_t ni,
const TinyBlocks_SlotDesc* out, uint32_t no,
void (*fn)(TinyBlocks_Block*, TinyBlocks*)) {
TinyBlocks_TypeBlock* t = tb_type_block_create(rt, name, in, ni, out, no, fn);
return t && tb_library_add(lib, &t->base);
}
static bool environ_init(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) {
static const TinyBlocks_Category cat = {
"Ambiente",
{ 92/255.f, 107/255.f, 192/255.f, 1.f},
{ 57/255.f, 73/255.f, 171/255.f, 1.f},
};
tb_library_set_category(lib, &cat);
static const TinyBlocks_SlotDesc get_in[] = {{"nome", TB_VALUE_TEXT}};
static const TinyBlocks_SlotDesc set_in[] = {{"nome", TB_VALUE_TEXT}, {"valor", TB_VALUE_TEXT}};
static const TinyBlocks_SlotDesc has_in[] = {{"nome", TB_VALUE_TEXT}};
static const TinyBlocks_SlotDesc unset_in[] = {{"nome", TB_VALUE_TEXT}};
static const TinyBlocks_SlotDesc val_out[] = {{"valor", TB_VALUE_TEXT}};
static const TinyBlocks_SlotDesc bool_out[] = {{"existe",TB_VALUE_INTEGER}};
return reg(rt, lib, "Environ.Get", get_in, 1, val_out, 1, eval_get)
&& reg(rt, lib, "Environ.Set", set_in, 2, NULL, 0, eval_set)
&& reg(rt, lib, "Environ.Has", has_in, 1, bool_out, 1, eval_has)
&& reg(rt, lib, "Environ.Unset", unset_in, 1, NULL, 0, eval_unset);
}
static void environ_deinit(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) {
(void)rt; (void)lib;
/* Mantem o store: o editor pode continuar lendo as settings. */
}
TINYBLOCKS_DEFINE_LIBRARY(environ,
"tinyblocks-environ",
"1.0.0",
environ_init,
environ_deinit)
const TinyBlocks_LibraryDescriptor* tb_environ_get_library(void) {
return &_tb_lib_desc_environ;
}