Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce overwrite highlight functionality to sheet #357

Open
wants to merge 19 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions app/sheet/handlers.c
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,8 @@ zsvsheet_cell_attr_t zsvsheet_cell_profile_attrs(enum zsvsheet_cell_profile_t t)
#else
return A_ITALIC | A_BOLD;
#endif
case zsvsheet_cell_attr_profile_overwritten:
return A_UNDERLINE;
default:
break;
}
Expand Down
10 changes: 9 additions & 1 deletion app/sheet/read-data.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <string.h>
#include <pthread.h>
#include <zsv/utils/prop.h>
#include "handlers_internal.h"
#include "sheet_internal.h"
#include "screen_buffer.h"
#include "../utils/index.h"
Expand Down Expand Up @@ -192,8 +193,15 @@ static int read_data(struct zsvsheet_ui_buffer **uibufferp, // a new zsvsheet_

for (size_t i = start_col; i < col_count && i + rownum_column_offset < zsvsheet_screen_buffer_cols(buffer); i++) {
struct zsv_cell c = zsv_get_cell(parser, i);
if (c.len)
if (c.len) {
zsvsheet_screen_buffer_write_cell_w_len(buffer, rows_read, i + rownum_column_offset, c.str, c.len);
if (c.overwritten)
buffer->overwrite_attrs[rows_read * buffer->cols + (i + rownum_column_offset)] =
zsvsheet_cell_profile_attrs(zsvsheet_cell_attr_profile_overwritten);
else
buffer->overwrite_attrs[rows_read * buffer->cols + (i + rownum_column_offset)] =
zsvsheet_cell_attr_profile_none;
}
}

rows_read++;
Expand Down
10 changes: 10 additions & 0 deletions app/sheet/screen_buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ struct zsvsheet_screen_buffer {
struct zsvsheet_screen_buffer_opts opts;
unsigned char *data;
zsvsheet_cell_attr_t *cell_attrs; // used for per-cell attron() and attroff()
zsvsheet_cell_attr_t *overwrite_attrs;
// to do: add hooks for extension
};

Expand Down Expand Up @@ -60,6 +61,7 @@ void zsvsheet_screen_buffer_delete(zsvsheet_screen_buffer_t buff) {
}
}
free(buff->cell_attrs);
free(buff->overwrite_attrs);
free(buff->data);
free(buff);
}
Expand Down Expand Up @@ -96,6 +98,8 @@ zsvsheet_screen_buffer_t zsvsheet_screen_buffer_new(size_t cols, struct zsvsheet
buff->cols = cols;
buff->data = data;
buff->opts = *opts;

buff->overwrite_attrs = calloc(buff->opts.rows, buff->cols * sizeof(*buff->overwrite_attrs));
return buff;
}
}
Expand Down Expand Up @@ -181,6 +185,12 @@ enum zsvsheet_priv_status zsvsheet_screen_buffer_write_cell(zsvsheet_screen_buff
}

int zsvsheet_screen_buffer_cell_attrs(zsvsheet_screen_buffer_t buff, size_t row, size_t col) {
if (buff->overwrite_attrs) {
size_t offset = row * buff->cols + col;
if (buff->overwrite_attrs[offset] != zsvsheet_cell_attr_profile_none) {
return buff->overwrite_attrs[offset];
}
}
if (buff->cell_attrs) {
size_t offset = row * buff->cols + col;
return buff->cell_attrs[offset];
Expand Down
25 changes: 23 additions & 2 deletions app/test/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ TARGETS=$(addprefix ${BUILD_DIR}/bin/zsv_,$(addsuffix ${EXE},${SOURCES}))
TESTS=test-blank-leading-rows $(addprefix test-,${SOURCES}) test-rm test-mv test-2json-help test-paste

ifeq ($(ZSVSHEET_BUILD),1)
SOURCES+=sheet
SOURCES+=sheet overwrite
CFLAGS+=-DZSVSHEET_BUILD
CFLAGS+=${CFLAGS_NCURSES}
LDFLAGS+=${LDFLAGS_NCURSES}
Expand Down Expand Up @@ -626,7 +626,7 @@ test-sheet-cleanup:
@rm -f tmux-*.log
@tmux kill-server || printf ''

test-sheet-all: test-sheet-1 test-sheet-2 test-sheet-3 test-sheet-4 test-sheet-5 test-sheet-6 test-sheet-7 test-sheet-8 test-sheet-9 test-sheet-10 test-sheet-11 test-sheet-12 test-sheet-13 test-sheet-14 test-sheet-subcommand test-sheet-prop-cmd-opt
test-sheet-all: test-sheet-1 test-sheet-2 test-sheet-3 test-sheet-4 test-sheet-5 test-sheet-6 test-sheet-7 test-sheet-8 test-sheet-9 test-sheet-10 test-sheet-11 test-sheet-12 test-sheet-13 test-sheet-14 test-sheet-apply-overwrites test-sheet-overwrites-underlined test-sheet-subcommand test-sheet-prop-cmd-opt
@(for SESSION in $^; do ! tmux kill-session -t "$$SESSION" 2>/dev/null; done && ${TEST_PASS} || ${TEST_FAIL})

TMUX_TERM=xterm-256color
Expand Down Expand Up @@ -759,6 +759,27 @@ test-sheet-15: ${BUILD_DIR}/bin/zsv_sheet${EXE} ${TIMINGS_CSV}
tmux send-keys -t $@ -N 4096 "k" && \
${EXPECT} $@ && ${TEST_PASS} || ${TEST_FAIL})

test-sheet-apply-overwrites: ${BUILD_DIR}/bin/zsv_sheet${EXE} ${BUILD_DIR}/bin/zsv_overwrite${EXE} worldcitiespop_mil.csv ${TIMINGS_CSV}
@${TEST_INIT}
@${BUILD_DIR}/bin/zsv_overwrite${EXE} worldcitiespop_mil.csv clear
@${BUILD_DIR}/bin/zsv_overwrite${EXE} worldcitiespop_mil.csv add 3-3 14
@${BUILD_DIR}/bin/zsv_overwrite${EXE} worldcitiespop_mil.csv add 8-3 27
@echo 'set-option default-terminal "${TMUX_TERM}"' > ~/.tmux.conf
@(tmux new-session -x 80 -y 25 -d -s $@ "${PREFIX} $< worldcitiespop_mil.csv --apply-overwrites" && \
${EXPECT} $@ && ${TEST_PASS} || ${TEST_FAIL})

test-sheet-overwrites-underlined: ${BUILD_DIR}/bin/zsv_sheet${EXE} ${BUILD_DIR}/bin/zsv_overwrite${EXE}
@${TEST_INIT}
@echo 'set-option default-terminal "${TMUX_TERM}"' > ~/.tmux.conf
# Clear and set up overwrites
@${BUILD_DIR}/bin/zsv_overwrite${EXE} worldcitiespop_mil.csv clear
@${BUILD_DIR}/bin/zsv_overwrite${EXE} worldcitiespop_mil.csv add 3-3 14
@${BUILD_DIR}/bin/zsv_overwrite${EXE} worldcitiespop_mil.csv add 8-3 27
# Configure tmux to capture output
@tmux kill-session | echo "no"
@(tmux new-session -d -s $@ -x 80 -y 25 "${PREFIX} stdbuf -o0 $< worldcitiespop_mil.csv --apply-overwrites" \
${EXPECT} $@ && ${TEST_PASS} || ${TEST_FAIL})

test-sheet-subcommand:\
test-sheet-subcommand-open-file-prompt test-sheet-subcommand-open-file-argument\
test-sheet-subcommand-filter-file-prompt test-sheet-subcommand-filter-file-argument
Expand Down
25 changes: 25 additions & 0 deletions app/test/expected/test-sheet-apply-overwrites.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
Row # Country City AccentCit Region Populatio Latitude Longitude
1 ir sarmaj-e Sarmaj-e 13 34.3578 47.5207
2 ad aixirival Aixirival 06 42.466666 1.5
3 mm mokho-atw Mokho-atw 09 18.033333 96.75
4 id selingon Selingon 14 -8.8374 116.4914
5 ir berimvand Berimvand 13 34.2953 47.1096
6 pl chomiaza Chomiaza 73 52.7508 17.841793
7 mg itona Itona 02 -23.86666 47.216666
8 ad andorra-v Andorra-V 07 42.5 1.5166667
9 mx la tortug La Tortug 27 25.75 -108.3333
10 us asaph Asaph PA 41.770833 -77.40527
11 ad andorre-v Andorre-V 07 42.5 1.5166667
12 ru dolmatova Dolmatova 71 57.436791 63.279522
13 ro escu Escu 13 47.133333 23.533333
14 us la presa La Presa CA 35119 32.708055 -116.9963
15 pk makam khu Makam Khu 04 33.650863 72.551536
16 ad aubinya Aubinyà 06 42.45 1.5
17 mm kiosong Kiosöng 11 22.583333 97.05
18 tr donencay Dönençay 28 40.266667 38.583333
19 it roncaglia Roncaglia 05 45.05 9.8
20 ml kourmouss Kourmouss 04 14.75 -5.033333
21 id lamogo Lamogo 38 -4.3945 119.9028
22 ad casas vil Casas Vil 03 42.533333 1.5666667
23 ru otdeleniy Otdeleniy 86 51.726473 39.714345
? for help 1
1 change: 1 addition & 0 deletions app/test/expected/test-sheet-overwrites-underlined.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(BRow # Country City AccentCit Region Populatio Latitude Longitude 1 (Birsarmaj-e Sarmaj-e 1334.3578 47.52072adaixirival Aixirival 0642.466666 1.53mmmokho-atw Mokho-atw 0918.033333 96.754idselingon Selingon (B14 (B-8.8374 116.49145irberimvand Berimvand 1334.2953 47.10966plchomiaza Chomiaza 7352.7508 17.8417937mgitona Itona 02-23.86666 47.2166668adandorra-v Andorra-V 0742.51.51666679mxla tortug La Tortug (B27 (B25.75 -108.333310usasaph Asaph PA41.770833 -77.4052711adandorre-v Andorre-V 0742.51.516666712rudolmatova Dolmatova 7157.436791 63.27952213roescuEscu1347.133333 23.53333314usla presa La Presa CA35119 32.708055 -116.996315pkmakam khu Makam Khu 0433.650863 72.55153616adaubinya Aubinyà 0642.45 1.517mmkiosong Kiosöng 1122.583333 97.0518trdonencay Dönençay 2840.266667 38.58333319itroncaglia Roncaglia 0545.05 9.820mlkourmouss Kourmouss 0414.75 -5.03333321idlamogo Lamogo 38-4.3945 119.902822adcasas vil Casas Vil 0342.533333 1.566666723ruotdeleniy Otdeleniy 8651.726473 39.714345(B Country City AccentCity Region Population Latitude Longitude  (Birsarmaj-e hoseynkhani Sarmaj-e Hoseynkhani 1334.357847.5207  adaixirivali Aixirivali0642.46666671.5  mmmokho-atwinywa Mokho-atwinywa0918.033333396.75  idselingon Selingon (B14 (B-8.8374116.4914  irberimvand Berimvand1334.295347.1096  plchomiaza Chomiaza 7352.750817.841793  mgitona Itona 02-23.866666747.2166667  adandorra-vieille Andorra-Vieille0742.51.5166667  mxla tortuga La Tortuga(B27 (B25.75-108.333333  usasaph Asaph PA41.7708333-77.4052778  adandorre-vieille Andorre-Vieille0742.51.5166667  rudolmatovaDolmatova 7157.43679163.279522  ro  escu Escu 1347.13333323.533333  usla presaLa Presa CA3511932.7080556-116.9963889  pkmakam khurd Makam Khurd0433.65086372.551536  adaubinya Aubinyà0642.451.5  mmkiosong Kiosöng1122.583333397.05  trdonencay Dönençay 2840.26666738.583333  itroncaglia Roncaglia0545.059.8  mlkourmoussana Kourmoussana0414.75-5.0333333  idlamogo Lamogo 38-4.3945119.9028  adcasas vila Casas Vila0342.53333331.5666667  ruotdeleniye yuzhnoye so Otdeleniye Yuzhnoye So 8651.72647339.71434524adcertesCertés0642.46666671.525usmoundMoundLA32.3391667-91.023888926adel puiEl Pui0442.551.516666727adels bonsEls Bons0342.53333331.583333328ruroshinaRoshina4654.64109643.54723429adels plansEls Plans0242.58333331.633333330ruzelenaya roshchaZelënaya Roshcha5552.61666754.3531mxtejalpaTejalpa2118.35-98.36666732adel vilarEl Vilar0242.56666671.633tdnarweytNarweyt0215.133333321.9534idbabakanmeulitBabakanmeulit30-7.5209108.073835mxrancho san miguelRancho San Miguel1025.4-105.11666736aderczErcz0442.56666671.537aderezErez0442.56666671.538idcigaleuhCigaleuh30-6.415833107.61027839jpushitakiUshitaki0341.283333140.840mlnkalamedoNkalamédo0712.1369444-7.744722241laban huai leukBan Huai Leuk1317.875556101.21944442adfontanedaFontaneda0642.451.466666743krchinbolChinbol1337.7376127.226644adjuverriJuverri0642.43333331.545uaarginchikArginchik1145.23789434.64808346ilezuzEzuz0130.79244534.47267147adl'aldosaL'Aldosa0242.58333331.633333348khphumi trabietPhumi Trabiet0610.8666667104.683333349mxrancho el anconRancho El Ancón0626.616667-105.21666750idberuk wetanBeruk Wetan07-7.695833111.12027851adlas escadasLas Escadas0842.51.533333352mgandabotokanaAndabotokana03-17.566666745.6833333(B? for help (B1
Expand Down
6 changes: 4 additions & 2 deletions app/utils/overwrite.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ enum zsv_status zsv_overwrite_context_delete(void *h) {
fclose(ctx->csv.f);
if (ctx->csv.parser)
zsv_delete(ctx->csv.parser);
free(ctx->src);
free(ctx);
// free(ctx->src);
// free(ctx);
return zsv_status_ok;
}

Expand Down Expand Up @@ -174,6 +174,8 @@ static enum zsv_status zsv_overwrite_init_sqlite3(struct zsv_overwrite_ctx *ctx,

enum zsv_status zsv_overwrite_open(void *h) {
struct zsv_overwrite_ctx *ctx = h;
if (!ctx)
return zsv_status_error;
if (!ctx->src)
return zsv_status_ok;
char ok = 0;
Expand Down
1 change: 1 addition & 0 deletions include/zsv/ext.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ typedef uint32_t zsvsheet_cell_attr_t;
enum zsvsheet_cell_profile_t {
zsvsheet_cell_attr_profile_none = 0,
zsvsheet_cell_attr_profile_link,
zsvsheet_cell_attr_profile_overwritten,
};

struct zsv_ext_callbacks {
Expand Down