1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
|
#include "assets.h"
#include "output.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <lua.h>
static int push_field(lua_State *L, struct asset *a, const char *field)
{
int n = 0;
if (strcmp(field, "symbol") == 0) {
lua_pushstring(L, a->symbol);
++n;
} else if (strcmp(field, "type") == 0) {
lua_pushstring(L, ASSET_TYPE_NAMES[a->type]);
++n;
} else if (strcmp(field, "usd_price") == 0) {
if (a->usd && a->type != TOTAL) {
lua_pushnumber(L, a->usd_pr);
++n;
}
} else if (strcmp(field, "usd_change") == 0) {
if (a->usd && a->type != TOTAL) {
lua_pushnumber(L, a->usd_ch);
++n;
}
} else if (strcmp(field, "usd_total") == 0) {
if (a->usd) {
lua_pushnumber(L, a->usd_tot);
++n;
}
} else if (strcmp(field, "btc_price") == 0) {
if (a->btc && a->type != TOTAL) {
lua_pushnumber(L, a->btc_pr);
++n;
}
} else if (strcmp(field, "btc_change") == 0) {
if (a->btc && a->type != TOTAL) {
lua_pushnumber(L, a->btc_ch);
++n;
}
} else if (strcmp(field, "btc_total") == 0) {
if (a->btc) {
lua_pushnumber(L, a->btc_tot);
++n;
}
}
return n;
}
static int decolour_len(const char *str)
{
int len = strlen(str);
while ((str = strchr(str, '\033'))) {
char *ptr = strchr(str, 'm') + 1;
len -= ptr - str;
str = ptr;
}
return len;
}
void output(struct asset **assets, int assets_len, lua_State *L)
{
lua_getglobal(L, "columns");
int len = lua_rawlen(L, -1);
lua_pop(L, 1);
int *widths = calloc(len, sizeof(int));
char ***out = calloc(assets_len, sizeof(char **));
for (int i = 0; i < assets_len; ++i) {
out[i] = calloc(len, sizeof(char *));
lua_getglobal(L, "columns");
lua_pushnil(L);
int j = 0;
while (lua_next(L, -2)) {
lua_rawgeti(L, -1, 1);
const char *x = lua_tostring(L, -1);
lua_pop(L, 1);
char *y;
lua_rawgeti(L, -1, 2);
if (push_field(L, assets[i], x) > 0) {
lua_pushlightuserdata(L, assets[i]);
lua_pcall(L, 2, 1, 0);
y = strdup(lua_tostring(L, -1));
} else {
y = strdup("");
}
lua_pop(L, 1);
int dlen = decolour_len(y);
if (dlen > widths[j])
widths[j] = dlen;
out[i][j++] = y;
lua_pop(L, 1);
}
lua_pop(L, 1);
}
for (int i = 0; i < assets_len; ++i) {
for (int j = 0; j < len; ++j) {
printf("%*s%s", widths[j] - decolour_len(out[i][j]) + 2, "",
out[i][j]);
}
printf("\n");
}
free(widths);
for (int i = 0; i < assets_len; ++i) {
for (int j = 0; j < len; ++j) {
free(out[i][j]);
}
free(out[i]);
}
free(out);
}
int push_field_lua(lua_State *L) {
struct asset *a = lua_touserdata(L, 1);
const char *field = lua_tostring(L, 2);
return push_field(L, a, field);
}
|