From 37116210fe1ae3350e03689710ee233326938dcf Mon Sep 17 00:00:00 2001 From: liquidaty Date: Mon, 16 Dec 2024 20:56:03 -0800 Subject: [PATCH] update sheet ext cell attr API to use zsv attrs instead of ncurses definitions (#339) * update sheet ext cell attr API to use zsv attrs instead of ncurses definitions --- app/cli.c | 1 + app/ext_example/mysheet_extension.c | 7 ++++--- app/sheet.c | 2 +- app/{ => sheet}/curses.h | 6 +++--- app/sheet/handlers.c | 22 ++++++++++++++++++++-- app/sheet/handlers_internal.h | 8 ++++++-- app/sheet/screen_buffer.c | 4 +++- app/sheet/ui_buffer.c | 3 +-- include/zsv/ext.h | 17 +++++++++++++++-- 9 files changed, 54 insertions(+), 16 deletions(-) rename app/{ => sheet}/curses.h (78%) diff --git a/app/cli.c b/app/cli.c index 40fb1b75..cc71173b 100644 --- a/app/cli.c +++ b/app/cli.c @@ -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; diff --git a/app/ext_example/mysheet_extension.c b/app/ext_example/mysheet_extension.c index 318be95b..2111572c 100644 --- a/app/ext_example/mysheet_extension.c +++ b/app/ext_example/mysheet_extension.c @@ -15,7 +15,7 @@ #include #include #include -#include "../curses.h" +// #include "../curses.h" /** * This is an example to demonstrate various extension capabilities @@ -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; } diff --git a/app/sheet.c b/app/sheet.c index 0a0c6972..a34ed39d 100644 --- a/app/sheet.c +++ b/app/sheet.c @@ -16,7 +16,7 @@ #include -#include "curses.h" +#include "sheet/curses.h" #include #include diff --git a/app/curses.h b/app/sheet/curses.h similarity index 78% rename from app/curses.h rename to app/sheet/curses.h index 6d5f03f6..399116d9 100644 --- a/app/curses.h +++ b/app/sheet/curses.h @@ -5,10 +5,10 @@ #include #endif // HAVE_NCURSESW #else -#if __has_include() -#include -#elif __has_include() +#if __has_include() #include +#elif __has_include() +#include #else #error Cannot find ncurses include file! #endif diff --git a/app/sheet/handlers.c b/app/sheet/handlers.c index 6dc71c25..4b6306c8 100644 --- a/app/sheet/handlers.c +++ b/app/sheet/handlers.c @@ -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; @@ -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; +}; diff --git a/app/sheet/handlers_internal.h b/app/sheet/handlers_internal.h index dfbf5758..b639fe45 100644 --- a/app/sheet/handlers_internal.h +++ b/app/sheet/handlers_internal.h @@ -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); @@ -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); diff --git a/app/sheet/screen_buffer.c b/app/sheet/screen_buffer.c index e8399ad7..6008ed35 100644 --- a/app/sheet/screen_buffer.c +++ b/app/sheet/screen_buffer.c @@ -1,11 +1,13 @@ #include "screen_buffer.h" +#include // 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 }; diff --git a/app/sheet/ui_buffer.c b/app/sheet/ui_buffer.c index ebf30d1f..421bb529 100644 --- a/app/sheet/ui_buffer.c +++ b/app/sheet/ui_buffer.c @@ -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; diff --git a/include/zsv/ext.h b/include/zsv/ext.h index 8cf65d46..392399a2 100644 --- a/include/zsv/ext.h +++ b/include/zsv/ext.h @@ -13,6 +13,7 @@ #define ZSV_EXTENSION_ID_MAX_LEN 8 #include +#include #include "common.h" #include "ext/sheet.h" #include "utils/sql.h" @@ -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); @@ -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