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,<caminho>" 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.
This commit is contained in:
1 parent
7fc3a18234
commit
a14c7f9937
7 files changed
+139
-1088
No files matched your search
@@ -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;
|
||||
|
||||
@@ -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
|
||||
* "# <meta_key>,<meta_value>" (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 */
|
||||
@@ -12,6 +12,8 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdint.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
#include <tinyblocks/tinyblocks.h>
|
||||
#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) {
|
||||
|
||||
Reference in new issue
Block a user