Skip to content

Commit

Permalink
update sheet ext cell attr API to use zsv attrs instead of ncurses de…
Browse files Browse the repository at this point in the history
…finitions (#339)

* update sheet ext cell attr API to use zsv attrs instead of ncurses definitions
  • Loading branch information
liquidaty authored Dec 17, 2024
1 parent bf5f4a5 commit 3711621
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 16 deletions.
1 change: 1 addition & 0 deletions app/cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,7 @@ static struct zsv_ext_callbacks *zsv_ext_callbacks_init(struct zsv_ext_callbacks
e->ext_sheet_buffer_set_ctx = zsvsheet_buffer_set_ctx;
e->ext_sheet_buffer_get_ctx = zsvsheet_buffer_get_ctx;
e->ext_sheet_buffer_set_cell_attrs = zsvsheet_buffer_set_cell_attrs;
e->ext_sheet_cell_profile_attrs = zsvsheet_cell_profile_attrs;
e->ext_sheet_buffer_get_zsv_opts = zsvsheet_buffer_get_zsv_opts;
e->ext_sheet_buffer_on_newline = zsvsheet_buffer_on_newline;
e->ext_sheet_buffer_get_selected_cell = zsvsheet_buffer_get_selected_cell;
Expand Down
7 changes: 4 additions & 3 deletions app/ext_example/mysheet_extension.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#include <zsv/utils/writer.h>
#include <zsv/utils/file.h>
#include <zsv/utils/prop.h>
#include "../curses.h"
// #include "../curses.h"

/**
* This is an example to demonstrate various extension capabilities
Expand Down Expand Up @@ -120,13 +120,14 @@ static struct pivot_row *get_pivot_row_data(struct pivot_data *pd, size_t row_ix
}

// TO DO: return zsvsheet_status
static enum zsv_ext_status get_cell_attrs(void *pdh, int *attrs, size_t start_row, size_t row_count, size_t cols) {
static enum zsv_ext_status get_cell_attrs(void *pdh, zsvsheet_cell_attr_t *attrs, size_t start_row, size_t row_count,
size_t cols) {
struct pivot_data *pd = pdh;
size_t end_row = start_row + row_count;
if (end_row > pd->rows.used)
end_row = pd->rows.used;
for (size_t i = start_row; i < end_row; i++)
attrs[i * cols] = A_ITALIC | A_BOLD | A_ITALIC;
attrs[i * cols] = zsv_cb.ext_sheet_cell_profile_attrs(zsvsheet_cell_attr_profile_link);
return zsv_ext_status_ok;
}

Expand Down
2 changes: 1 addition & 1 deletion app/sheet.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

#include <zsv.h>

#include "curses.h"
#include "sheet/curses.h"

#include <locale.h>
#include <wchar.h>
Expand Down
6 changes: 3 additions & 3 deletions app/curses.h → app/sheet/curses.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
#include <ncurses/ncurses.h>
#endif // HAVE_NCURSESW
#else
#if __has_include(<curses.h>)
#include <curses.h>
#elif __has_include(<ncursesw/curses.h>)
#if __has_include(<ncursesw/curses.h>)
#include <ncursesw/curses.h>
#elif __has_include(<curses.h>)
#include <curses.h>
#else
#error Cannot find ncurses include file!
#endif
Expand Down
22 changes: 20 additions & 2 deletions app/sheet/handlers.c
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,9 @@ enum zsv_ext_status zsvsheet_buffer_get_ctx(zsvsheet_buffer_t h, void **ctx_out)

/** Set callback for fetching cell attributes **/
void zsvsheet_buffer_set_cell_attrs(zsvsheet_buffer_t h,
enum zsv_ext_status (*get_cell_attrs)(void *ext_ctx, int *, size_t start_row,
size_t row_count, size_t col_count)) {
enum zsv_ext_status (*get_cell_attrs)(void *ext_ctx, zsvsheet_cell_attr_t *,
size_t start_row, size_t row_count,
size_t col_count)) {
if (h) {
struct zsvsheet_ui_buffer *buff = h;
buff->get_cell_attrs = get_cell_attrs;
Expand Down Expand Up @@ -194,3 +195,20 @@ struct zsvsheet_buffer_data zsvsheet_buffer_info(zsvsheet_buffer_t h) {
}
return d;
}

/**
* Get the corresponding cell attributes for the given profile
*/
zsvsheet_cell_attr_t zsvsheet_cell_profile_attrs(enum zsvsheet_cell_profile_t t) {
switch (t) {
case zsvsheet_cell_attr_profile_link:
#ifndef A_ITALIC
return A_BOLD;
#else
return A_ITALIC | A_BOLD;
#endif
default:
break;
}
return zsvsheet_cell_attr_profile_none;
};
8 changes: 6 additions & 2 deletions app/sheet/handlers_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,9 @@ zsvsheet_status zsvsheet_buffer_get_selected_cell(zsvsheet_buffer_t h, struct zs
* Set custom cell attributes
*/
void zsvsheet_buffer_set_cell_attrs(zsvsheet_buffer_t h,
enum zsv_ext_status (*get_cell_attrs)(void *ext_ctx, int *, size_t start_row,
size_t row_count, size_t col_count));
enum zsv_ext_status (*get_cell_attrs)(void *ext_ctx, zsvsheet_cell_attr_t *,
size_t start_row, size_t row_count,
size_t col_count));

/** Get zsv_opts use to open the buffer's data file **/
struct zsv_opts zsvsheet_buffer_get_zsv_opts(zsvsheet_buffer_t h);
Expand All @@ -122,3 +123,6 @@ enum zsvsheet_status zsvsheet_push_transformation(zsvsheet_proc_context_t ctx,
#endif

struct zsvsheet_buffer_data zsvsheet_buffer_info(zsvsheet_buffer_t buff);

/** cell formatting **/
zsvsheet_cell_attr_t zsvsheet_cell_profile_attrs(enum zsvsheet_cell_profile_t);
4 changes: 3 additions & 1 deletion app/sheet/screen_buffer.c
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
#include "screen_buffer.h"

#include <zsv/ext.h> // zsvsheet_cell_attr_t

struct zsvsheet_screen_buffer {
size_t cols;
size_t long_cell_count;
struct zsvsheet_screen_buffer_opts opts;
unsigned char *data;
int *cell_attrs; // used for per-cell attron() and attroff()
zsvsheet_cell_attr_t *cell_attrs; // used for per-cell attron() and attroff()
// to do: add hooks for extension
};

Expand Down
3 changes: 1 addition & 2 deletions app/sheet/ui_buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ struct zsvsheet_ui_buffer {
zsvsheet_status (*on_newline)(zsvsheet_proc_context_t);
void (*ext_on_close)(void *);

enum zsv_ext_status (*get_cell_attrs)(void *ext_ctx, int *attrs, size_t start_row, size_t row_count,
size_t col_count);
enum zsv_ext_status (*get_cell_attrs)(void *, zsvsheet_cell_attr_t *, size_t, size_t, size_t);

unsigned char index_ready : 1;
unsigned char rownum_col_offset : 1;
Expand Down
17 changes: 15 additions & 2 deletions include/zsv/ext.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#define ZSV_EXTENSION_ID_MAX_LEN 8

#include <stdio.h>
#include <stdint.h>
#include "common.h"
#include "ext/sheet.h"
#include "utils/sql.h"
Expand Down Expand Up @@ -90,6 +91,12 @@ struct zsvsheet_buffer_data {
unsigned char _ : 7;
};

typedef uint32_t zsvsheet_cell_attr_t;
enum zsvsheet_cell_profile_t {
zsvsheet_cell_attr_profile_none = 0,
zsvsheet_cell_attr_profile_link,
};

struct zsv_ext_callbacks {
void (*set_row_handler)(zsv_parser handle, void (*row)(void *ctx));
void (*set_context)(zsv_parser handle, void *ctx);
Expand Down Expand Up @@ -247,12 +254,18 @@ struct zsv_ext_callbacks {
*/
enum zsv_ext_status (*ext_sheet_buffer_get_ctx)(zsvsheet_buffer_t h, void **ctx_out);

/**
*
*/
zsvsheet_cell_attr_t (*ext_sheet_cell_profile_attrs)(enum zsvsheet_cell_profile_t);

/**
* Set custom cell attributes
*/
void (*ext_sheet_buffer_set_cell_attrs)(zsvsheet_buffer_t h,
enum zsv_ext_status (*get_cell_attrs)(void *pdh, int *attrs, size_t start_row,
size_t row_count, size_t cols));
enum zsv_ext_status (*get_cell_attrs)(void *pdh, zsvsheet_cell_attr_t *attrs,
size_t start_row, size_t row_count,
size_t cols));

/**
* Set custom handler on Enter key press
Expand Down

0 comments on commit 3711621

Please sign in to comment.