commit e8b9e63c0e2eb0ad86b212e16d3c8b5fcbf4c3c2
parent 747d234bea0b52f7b0518d4769bb2991edfe915f
Author: Claudio Alessi <smoppy@gmail.com>
Date: Mon, 29 Feb 2016 19:26:05 +0100
Introduce stfl_putitem().
This centralizes item format and make mode functions a bit more UI agnostic.
Diffstat:
M | core.c | | | 60 | +++++++++++++++++++++++++++++++----------------------------- |
1 file changed, 31 insertions(+), 29 deletions(-)
diff --git a/core.c b/core.c
@@ -70,6 +70,7 @@ void *ecalloc(size_t nmemb, size_t size);
void cleanupview(View *v);
void cleanupitems(Item *i);
Item *getitem(void);
+void stfl_putitem(Item *item);
void flagas(const Arg *arg);
void apply(const Arg *arg);
void quit(const Arg *arg);
@@ -260,10 +261,30 @@ mysql_items(MYSQL_RES *res, Item **items) {
}
void
+stfl_putitem(Item *item) {
+ char t[32]; /* XXX */
+ char txt[256]; /* XXX */
+
+ if(item->nfields == 1)
+ snprintf(txt, sizeof txt, "listitem text:\"%s\"", item->fields[0]);
+ else {
+ strncpy(txt, "listitem text:\"", sizeof txt);
+ for(int i = 0; i < item->nfields; ++i) {
+ snprintf(t, sizeof t, "%8.16s", item->fields[i]);
+ if(i)
+ strncat(txt, " | ", sizeof txt);
+ /* XXX What if strlen(all_fields) > sizeof txt? */
+ strncat(txt, t, sizeof txt);
+ }
+ strncat(txt, "\"", sizeof txt);
+ }
+ stfl_modify(selview->form, L"items", L"append", stfl_ipool_towc(ipool, txt));
+}
+
+void
databases(void) {
MYSQL_RES *res;
Item *item;
- char txt[256];
if(!(res = mysql_exec("show databases")))
die("databases");
@@ -271,15 +292,12 @@ databases(void) {
cleanupitems(selview->items);
selview->nitems = mysql_items(res, &selview->items);
mysql_free_result(res);
-
if(!selview->form)
selview->form = stfl_create(L"<items.stfl>");
stfl_modify(selview->form, L"items", L"replace_inner", L"vbox"); /* clear */
- for(item = selview->items; item; item = item->next) {
- snprintf(txt, sizeof txt, "listitem text:\"%s\"", item->fields[0]);
- stfl_modify(selview->form, L"items", L"append", stfl_ipool_towc(ipool, txt));
- }
+ for(item = selview->items; item; item = item->next)
+ stfl_putitem(item);
stfl_set(selview->form, L"pos", 0);
}
@@ -287,7 +305,6 @@ void
tables(void) {
MYSQL_RES *res;
Item *item;
- char txt[256];
if(!(res = mysql_exec("show tables")))
die("tables\n");
@@ -295,15 +312,12 @@ tables(void) {
cleanupitems(selview->items);
selview->nitems = mysql_items(res, &selview->items);
mysql_free_result(res);
-
if(!selview->form)
selview->form = stfl_create(L"<items.stfl>");
stfl_modify(selview->form, L"items", L"replace_inner", L"vbox"); /* clear */
- for(item = selview->items; item; item = item->next) {
- snprintf(txt, sizeof txt, "listitem text:\"%s\"", item->fields[0]);
- stfl_modify(selview->form, L"items", L"append", stfl_ipool_towc(ipool, txt));
- }
+ for(item = selview->items; item; item = item->next)
+ stfl_putitem(item);
stfl_set(selview->form, L"pos", 0);
}
@@ -311,33 +325,21 @@ void
records(void) {
Item *item;
MYSQL_RES *res;
- char txt[512];
- char t[32];
+ char t[32]; /* XXX */
- snprintf(txt, sizeof txt, "select * from `%s`", selitem->fields[0]);
- if(!(res = mysql_exec(txt)))
+ snprintf(t, sizeof t, "select * from `%s`", selitem->fields[0]);
+ if(!(res = mysql_exec(t)))
die("records\n");
cleanupitems(selview->items);
selview->nitems = mysql_items(res, &selview->items);
mysql_free_result(res);
-
if(!selview->form)
selview->form = stfl_create(L"<items.stfl>");
stfl_modify(selview->form, L"items", L"replace_inner", L"vbox"); /* clear */
- for(item = selview->items; item; item = item->next) {
- strncpy(txt, "listitem text:\"", sizeof txt);
- for(int i = 0; i < item->nfields; ++i) {
- snprintf(t, sizeof t, "%8.16s", item->fields[i]);
- if(i)
- strncat(txt, " | ", sizeof txt);
- /* XXX What if strlen(all_fields) > sizeof txt? */
- strncat(txt, t, sizeof txt);
- }
- strncat(txt, "\"", sizeof txt);
- stfl_modify(selview->form, L"items", L"append", stfl_ipool_towc(ipool, txt));
- }
+ for(item = selview->items; item; item = item->next)
+ stfl_putitem(item);
stfl_set(selview->form, L"pos", 0);
}