Skip to content

Commit 495045f

Browse files
committed
sheet: Hide row numbers on pivot groups and refactor ui args
1 parent 2ed256f commit 495045f

11 files changed

+97
-89
lines changed

app/sheet.c

+9-6
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ struct zsvsheet_opts {
4949
#define ZSVSHEET_CELL_DISPLAY_MIN_WIDTH 10
5050
static size_t zsvsheet_cell_display_width(struct zsvsheet_ui_buffer *ui_buffer,
5151
struct zsvsheet_display_dimensions *ddims) {
52-
size_t width = ddims->columns /
53-
(ui_buffer->dimensions.col_count + (ui_buffer->rownum_col_offset && !ui_buffer->has_row_num ? 1 : 0));
52+
size_t width = ddims->columns / (ui_buffer->dimensions.col_count +
53+
(ui_buffer->rownum_col_offset && !ui_buffer->no_add_row_num ? 1 : 0));
5454
return width < ZSVSHEET_CELL_DISPLAY_MIN_WIDTH ? ZSVSHEET_CELL_DISPLAY_MIN_WIDTH : width;
5555
}
5656

@@ -472,16 +472,17 @@ static zsvsheet_status zsvsheet_help_handler(struct zsvsheet_proc_context *ctx)
472472
struct zsvsheet_sheet_context *state = (struct zsvsheet_sheet_context *)ctx->subcommand_context;
473473
struct zsvsheet_display_info *di = &state->display_info;
474474
struct zsvsheet_screen_buffer_opts bopts = {
475-
.no_rownum_column = 1,
476475
.cell_buff_len = 64,
477476
.max_cell_len = 0,
478477
.rows = 256,
479478
};
479+
struct zsvsheet_opts zsvsheet_opts = {0};
480+
zsvsheet_opts.hide_row_nums = 1;
480481
struct zsvsheet_ui_buffer_opts uibopts = {
482+
.zsvsheet_opts = &zsvsheet_opts,
481483
.buff_opts = &bopts,
482484
.filename = NULL,
483485
.data_filename = NULL,
484-
.no_rownum_col_offset = 1,
485486
.write_after_open = 0,
486487
};
487488
struct zsvsheet_ui_buffer *uib = NULL;
@@ -766,8 +767,10 @@ int ZSV_MAIN_FUNC(ZSV_COMMAND)(int argc, const char *argv[], struct zsv_opts *op
766767

767768
if (handler_state.display_info.update_buffer && zsvsheet_buffer_data_filename(ub)) {
768769
struct zsvsheet_opts zsvsheet_opts = {0};
769-
if (read_data(&ub, NULL, current_ui_buffer->input_offset.row, current_ui_buffer->input_offset.col, header_span,
770-
&zsvsheet_opts, custom_prop_handler)) {
770+
struct zsvsheet_ui_buffer_opts uibopts = {0};
771+
uibopts.zsvsheet_opts = &zsvsheet_opts;
772+
if (read_data(&ub, &uibopts, current_ui_buffer->input_offset.row, current_ui_buffer->input_offset.col,
773+
header_span, custom_prop_handler)) {
771774
zsvsheet_priv_set_status(&display_dims, ZSVSHEET_STATUS_HIGH_PRIO,
772775
"Unexpected error!"); // to do: better error message
773776
continue;

app/sheet/file.c

+4-1
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,14 @@ int zsvsheet_ui_buffer_open_file_opts(struct zsvsheet_ui_buffer_opts *uibopts,
66
struct zsvsheet_screen_buffer_opts bopts = {0};
77
struct zsvsheet_ui_buffer *tmp_ui_buffer = NULL;
88

9+
if (!uibopts->zsvsheet_opts)
10+
uibopts->zsvsheet_opts = &zsvsheet_opts;
11+
912
if (!uibopts->buff_opts)
1013
uibopts->buff_opts = &bopts;
1114

1215
int err = 0;
13-
if ((err = read_data(&tmp_ui_buffer, uibopts, 0, 0, 0, &zsvsheet_opts, custom_prop_handler)) != 0 || !tmp_ui_buffer ||
16+
if ((err = read_data(&tmp_ui_buffer, uibopts, 0, 0, 0, custom_prop_handler)) != 0 || !tmp_ui_buffer ||
1417
!tmp_ui_buffer->buff_used_rows) {
1518
zsvsheet_ui_buffer_delete(tmp_ui_buffer);
1619
if (err)

app/sheet/handlers.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ struct zsvsheet_buffer_data zsvsheet_buffer_info(zsvsheet_buffer_t h) {
191191
struct zsvsheet_buffer_data d = {0};
192192
struct zsvsheet_ui_buffer *b = h;
193193
if (b) {
194-
d.has_row_num = b->has_row_num;
194+
d.has_row_num = b->no_add_row_num;
195195
}
196196
return d;
197197
}

app/sheet/pivot.c

+4-5
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,10 @@ static zsvsheet_status zsv_sqlite3_to_csv(zsvsheet_proc_context_t pctx, struct z
165165
fclose(writer_opts.stream);
166166

167167
if (tmp_fn && zsv_file_exists(tmp_fn)) {
168+
struct zsvsheet_opts zsvsheet_opts = {0};
169+
zsvsheet_opts.hide_row_nums = 1;
168170
struct zsvsheet_ui_buffer_opts uibopts = {0};
171+
uibopts.zsvsheet_opts = &zsvsheet_opts;
169172
uibopts.data_filename = tmp_fn;
170173
zst = zsvsheet_open_file_opts(pctx, &uibopts);
171174
} else {
@@ -205,11 +208,7 @@ zsvsheet_status pivot_drill_down(zsvsheet_proc_context_t ctx) {
205208
if (!zdb || !(sql_str = sqlite3_str_new(zdb->db)))
206209
zst = zsvsheet_status_memory;
207210
else if (zdb->rc == SQLITE_OK && zsv_sqlite3_add_csv(zdb, pd->data_filename, &pd->zopts, NULL) == SQLITE_OK) {
208-
if (zsvsheet_buffer_info(buff).has_row_num)
209-
sqlite3_str_appendf(sql_str, "select *");
210-
else
211-
sqlite3_str_appendf(sql_str, "select rowid as [Row #], *");
212-
sqlite3_str_appendf(sql_str, " from data where %s = %Q", pd->value_sql, pr->value);
211+
sqlite3_str_appendf(sql_str, "select rowid as [Row #], * from data where %s = %Q", pd->value_sql, pr->value);
213212
zst = zsv_sqlite3_to_csv(ctx, zdb, sqlite3_str_value(sql_str), NULL, NULL, NULL);
214213
}
215214

app/sheet/read-data.c

+15-13
Original file line numberDiff line numberDiff line change
@@ -46,20 +46,21 @@ static void get_data_index_async(struct zsvsheet_ui_buffer *uibuffp, const char
4646

4747
static int read_data(struct zsvsheet_ui_buffer **uibufferp, // a new zsvsheet_ui_buffer will be allocated
4848
struct zsvsheet_ui_buffer_opts *uibopts, // if *uibufferp == NULL and uibopts != NULL
49-
size_t start_row, size_t start_col, size_t header_span, struct zsvsheet_opts *zsvsheet_opts,
49+
size_t start_row, size_t start_col, size_t header_span,
5050
struct zsv_prop_handler *custom_prop_handler) {
51-
const char *filename = (uibufferp && *uibufferp) ? (*uibufferp)->filename : uibopts ? uibopts->filename : NULL;
51+
struct zsvsheet_opts *zsvsheet_opts = uibopts->zsvsheet_opts;
5252
struct zsv_opts opts = {0};
5353
if (uibufferp && *uibufferp)
5454
opts = (*uibufferp)->zsv_opts;
55-
else if (uibopts)
55+
else
5656
opts = uibopts->zsv_opts;
5757

5858
struct zsvsheet_ui_buffer *uibuff = uibufferp ? *uibufferp : NULL;
5959
size_t remaining_rows_to_skip = start_row;
6060
size_t remaining_header_to_skip = header_span;
6161
size_t original_row_num = 0;
6262
FILE *fp;
63+
const char *filename = NULL;
6364

6465
if (uibuff) {
6566
if (uibuff->data_filename)
@@ -68,7 +69,7 @@ static int read_data(struct zsvsheet_ui_buffer **uibufferp, // a new zsvsheet_
6869
filename = uibuff->filename;
6970
}
7071

71-
if (!filename && uibopts) {
72+
if (!filename) {
7273
if (uibopts->data_filename)
7374
filename = uibopts->data_filename;
7475
else if (uibopts->filename)
@@ -116,17 +117,17 @@ static int read_data(struct zsvsheet_ui_buffer **uibufferp, // a new zsvsheet_
116117
size_t find_len = zsvsheet_opts->find ? strlen(zsvsheet_opts->find) : 0;
117118
size_t rows_searched = 0;
118119
zsvsheet_screen_buffer_t buffer = uibuff ? uibuff->buffer : NULL;
119-
if (uibuff && uibuff->has_row_num)
120+
if (uibuff && uibuff->no_add_row_num)
120121
zsvsheet_opts->hide_row_nums = 1;
121122

122123
while (zsv_next_row(parser) == zsv_status_row &&
123124
(rows_read == 0 || rows_read < zsvsheet_screen_buffer_rows(buffer))) { // for each row
124125

125126
size_t col_count = zsv_cell_count(parser);
126-
if (uibuff == NULL && uibufferp && uibopts && col_count > 0) {
127+
if (uibuff == NULL && uibufferp && col_count > 0) {
127128
enum zsvsheet_priv_status stat;
128129
struct zsvsheet_ui_buffer *tmp_uibuff = NULL;
129-
if (!(buffer = zsvsheet_screen_buffer_new(col_count, uibopts->buff_opts, &stat)) ||
130+
if (!(buffer = zsvsheet_screen_buffer_new(col_count + 1, uibopts->buff_opts, &stat)) ||
130131
stat != zsvsheet_priv_status_ok || !(tmp_uibuff = zsvsheet_ui_buffer_new(buffer, uibopts))) {
131132
if (tmp_uibuff)
132133
zsvsheet_ui_buffer_delete(tmp_uibuff);
@@ -138,24 +139,24 @@ static int read_data(struct zsvsheet_ui_buffer **uibufferp, // a new zsvsheet_
138139
}
139140

140141
// row number
141-
size_t rownum_column_offset = 0;
142142
if (rows_read == 0 && zsvsheet_opts->hide_row_nums == 0) {
143143
// Check if we already have Row #
144144
struct zsv_cell c = zsv_get_cell(parser, 0);
145145
if (c.len == ZSVSHEET_ROWNUM_HEADER_LEN && !memcmp(c.str, ZSVSHEET_ROWNUM_HEADER, c.len)) {
146146
zsvsheet_opts->hide_row_nums = 1;
147147
if (uibuff)
148-
uibuff->has_row_num = 1;
148+
uibuff->no_add_row_num = 1;
149149
}
150150
}
151+
size_t rownum_column_offset = !zsvsheet_opts->hide_row_nums;
151152

152153
original_row_num++;
153154
if (remaining_header_to_skip > 0) {
154155
remaining_header_to_skip--;
155156
continue;
156157
}
157158
if (uibuff) {
158-
if (col_count + !buffer->opts.no_rownum_column > buffer->cols) {
159+
if (col_count > buffer->cols) {
159160
if (zsvsheet_screen_buffer_grow(buffer, col_count) != zsvsheet_priv_status_ok)
160161
return -1;
161162
}
@@ -187,7 +188,6 @@ static int read_data(struct zsvsheet_ui_buffer **uibufferp, // a new zsvsheet_
187188
sprintf(buff, "########");
188189
zsvsheet_screen_buffer_write_cell(buffer, rows_read, 0, (unsigned char *)buff);
189190
}
190-
rownum_column_offset = 1;
191191
}
192192

193193
for (size_t i = start_col; i < col_count && i + rownum_column_offset < zsvsheet_screen_buffer_cols(buffer); i++) {
@@ -261,14 +261,16 @@ static void *get_data_index(void *gdi) {
261261
static size_t zsvsheet_find_next(struct zsvsheet_ui_buffer *uib, const char *needle,
262262
struct zsvsheet_opts *zsvsheet_opts, size_t header_span,
263263
struct zsv_prop_handler *custom_prop_handler) {
264+
struct zsvsheet_ui_buffer_opts uibopts = {0};
264265
struct zsvsheet_rowcol *input_offset = &uib->input_offset;
265266
struct zsvsheet_rowcol *buff_offset = &uib->buff_offset;
266267
size_t cursor_row = uib->cursor_row;
267268
zsvsheet_opts->find = needle;
268269
zsvsheet_opts->found_rownum = 0;
270+
uibopts.zsvsheet_opts = zsvsheet_opts;
269271
// TO DO: check if it exists in current row, later column (and change 'cursor_row - 1' below to 'cursor_row')
270-
read_data(&uib, NULL, input_offset->row + buff_offset->row + header_span + cursor_row - 1, 0, header_span,
271-
zsvsheet_opts, custom_prop_handler);
272+
read_data(&uib, &uibopts, input_offset->row + buff_offset->row + header_span + cursor_row - 1, 0, header_span,
273+
custom_prop_handler);
272274
zsvsheet_opts->find = NULL;
273275
return zsvsheet_opts->found_rownum;
274276
}

app/sheet/screen_buffer.c

-5
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,6 @@ zsvsheet_screen_buffer_t zsvsheet_screen_buffer_new(size_t cols, struct zsvsheet
8080
if (opts->cell_buff_len < sizeof(void *) * 2)
8181
*stat = zsvsheet_priv_status_error;
8282
else {
83-
if (!opts->no_rownum_column)
84-
cols++;
8583
void *data = calloc(opts->rows, cols * opts->cell_buff_len);
8684
if (!data)
8785
*stat = zsvsheet_priv_status_memory;
@@ -108,9 +106,6 @@ enum zsvsheet_priv_status zsvsheet_screen_buffer_grow(zsvsheet_screen_buffer_t b
108106
size_t cell_buff_len = buff->opts.cell_buff_len;
109107
size_t old_row_len = old_cols * cell_buff_len;
110108

111-
if (!buff->opts.no_rownum_column)
112-
cols++;
113-
114109
size_t row_len = cols * cell_buff_len;
115110

116111
assert(cols > old_cols);

app/sheet/screen_buffer.h

+3-4
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,9 @@
88
typedef struct zsvsheet_screen_buffer *zsvsheet_screen_buffer_t;
99

1010
struct zsvsheet_screen_buffer_opts {
11-
size_t cell_buff_len; // default = 16. must be >= 2 * sizeof(void *)
12-
size_t max_cell_len; // length in bytes; defaults to 32767
13-
size_t rows; // rows to buffer. cannot be < 256
14-
char no_rownum_column; // reserved. TO DO: if set, omit row num column
11+
size_t cell_buff_len; // default = 16. must be >= 2 * sizeof(void *)
12+
size_t max_cell_len; // length in bytes; defaults to 32767
13+
size_t rows; // rows to buffer. cannot be < 256
1514
};
1615

1716
zsvsheet_screen_buffer_t zsvsheet_screen_buffer_new(size_t cols, struct zsvsheet_screen_buffer_opts *opts,

app/sheet/transformation.c

+2
Original file line numberDiff line numberDiff line change
@@ -238,8 +238,10 @@ enum zsvsheet_status zsvsheet_push_transformation(zsvsheet_proc_context_t ctx,
238238
goto error;
239239
}
240240

241+
struct zsvsheet_opts zsvsheet_opts = {0};
241242
struct zsvsheet_ui_buffer_opts uibopts = {0};
242243

244+
uibopts.zsvsheet_opts = &zsvsheet_opts;
243245
uibopts.data_filename = zsvsheet_transformation_filename(trn);
244246
uibopts.write_after_open = 1;
245247

app/sheet/ui_buffer.c

+9-4
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ struct zsvsheet_ui_buffer {
4141
unsigned char index_ready : 1;
4242
unsigned char rownum_col_offset : 1;
4343
unsigned char index_started : 1;
44-
unsigned char has_row_num : 1;
44+
unsigned char no_add_row_num : 1;
4545
unsigned char mutex_inited : 1;
4646
unsigned char write_in_progress : 1;
4747
unsigned char write_done : 1;
@@ -93,11 +93,11 @@ void zsvsheet_ui_buffer_delete(struct zsvsheet_ui_buffer *ub) {
9393
}
9494

9595
struct zsvsheet_ui_buffer_opts {
96+
struct zsvsheet_opts *zsvsheet_opts;
9697
struct zsvsheet_screen_buffer_opts *buff_opts;
9798
const char *filename;
9899
const char *data_filename;
99100
struct zsv_opts zsv_opts; // options to use when opening this file
100-
char no_rownum_col_offset;
101101
char write_after_open;
102102
};
103103

@@ -109,9 +109,14 @@ struct zsvsheet_ui_buffer *zsvsheet_ui_buffer_new(zsvsheet_screen_buffer_t buffe
109109
uib->buffer = buffer;
110110
uib->mutex_inited = 1;
111111
memcpy(&uib->mutex, &init, sizeof(init));
112-
if (!(uibopts && uibopts->no_rownum_col_offset))
113-
uib->rownum_col_offset = 1;
112+
uib->rownum_col_offset = !uibopts;
113+
114114
if (uibopts) {
115+
if (uibopts->zsvsheet_opts->hide_row_nums)
116+
uib->no_add_row_num = 1;
117+
else
118+
uib->rownum_col_offset = 1;
119+
115120
if (uibopts->filename && !(uib->filename = strdup(uibopts->filename))) {
116121
zsvsheet_ui_buffer_delete(uib);
117122
return NULL;
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
1-
Row # value Count
2-
1 ad 27
3-
2 ae 147
4-
3 af 27822
5-
4 ag 55
6-
5 ai 14
7-
6 al 4730
8-
7 am 896
9-
8 an 75
10-
9 ao 6170
11-
10 ar 2817
12-
11 at 4740
13-
12 au 3403
14-
13 aw 37
15-
14 az 3522
16-
15 ba 5076
17-
16 bb 165
18-
17 bd 8280
19-
18 be 5056
20-
19 bf 3248
21-
20 bg 6258
22-
21 bh 102
23-
22 bi 646
24-
23 bj 1331
25-
? for help 1
1+
value Count
2+
ad 27
3+
ae 147
4+
af 27822
5+
ag 55
6+
ai 14
7+
al 4730
8+
am 896
9+
an 75
10+
ao 6170
11+
ar 2817
12+
at 4740
13+
au 3403
14+
aw 37
15+
az 3522
16+
ba 5076
17+
bb 165
18+
bd 8280
19+
be 5056
20+
bf 3248
21+
bg 6258
22+
bh 102
23+
bi 646
24+
bj 1331
25+
? for help ad
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
1-
Row # value Count
2-
1 C1 1
3-
2 C10 1
4-
3 C100 1
5-
4 C1000 1
6-
5 C1001 1
7-
6 C1002 1
8-
7 C1003 1
9-
8 C1004 1
10-
9 C1005 1
11-
10 C1006 1
12-
11 C1007 1
13-
12 C1008 1
14-
13 C1009 1
15-
14 C101 1
16-
15 C1010 1
17-
16 C1011 1
18-
17 C1012 1
19-
18 C1013 1
20-
19 C1014 1
21-
20 C1015 1
22-
21 C1016 1
23-
22 C1017 1
24-
23 C1018 1
25-
? for help 1
1+
value Count
2+
C1 1
3+
C10 1
4+
C100 1
5+
C1000 1
6+
C1001 1
7+
C1002 1
8+
C1003 1
9+
C1004 1
10+
C1005 1
11+
C1006 1
12+
C1007 1
13+
C1008 1
14+
C1009 1
15+
C101 1
16+
C1010 1
17+
C1011 1
18+
C1012 1
19+
C1013 1
20+
C1014 1
21+
C1015 1
22+
C1016 1
23+
C1017 1
24+
C1018 1
25+
? for help C1

0 commit comments

Comments
 (0)