From fd04c55d1653805ab6974cac64a1d9d7ff00fc2e Mon Sep 17 00:00:00 2001 From: nick black Date: Sat, 23 Nov 2019 12:28:42 -0500 Subject: [PATCH] implement ncplane_putwstr #9 --- include/notcurses.h | 9 +++++++-- src/bin/demo.c | 16 ++++++++++++++++ src/lib/notcurses.c | 13 ++++++++++--- tests/notcurses.cpp | 9 ++++++--- 4 files changed, 39 insertions(+), 8 deletions(-) diff --git a/include/notcurses.h b/include/notcurses.h index 2766db054..57700369a 100644 --- a/include/notcurses.h +++ b/include/notcurses.h @@ -263,11 +263,16 @@ cell_rgb_blue(uint32_t rgb){ } static inline void -cell_set_fg(cell* c, unsigned r, unsigned g, unsigned b){ +cell_rgb_set_fg(uint64_t* channels, unsigned r, unsigned g, unsigned b){ uint64_t rgb = (r & 0xffull) << 48u; rgb |= (g & 0xffull) << 40u; rgb |= (b & 0xffull) << 32u; - c->channels = (c->channels & 0x00ffffff00000000ull) | rgb; + *channels = (*channels & 0x00ffffff00000000ull) | rgb; +} + +static inline void +cell_set_fg(cell* c, unsigned r, unsigned g, unsigned b){ + cell_rgb_set_fg(&c->channels, r, g, b); } static inline void diff --git a/src/bin/demo.c b/src/bin/demo.c index 47538af58..e208ccb73 100644 --- a/src/bin/demo.c +++ b/src/bin/demo.c @@ -1,4 +1,5 @@ #include +#include #include #include #include @@ -42,6 +43,21 @@ int main(void){ if(notcurses_render(nc)){ goto err; } + sleep(1); + const wchar_t lstr[] = L"Wovon man nicht sprechen kann, darĂ¼ber muss man schweigen."; + if(ncplane_cursor_move_yx(ncp, y / 2, (x - wcslen(lstr)) / 2)){ + goto err; + } + if(ncplane_fg_rgb8(ncp, 255, 255, 255)){ + goto err; + } + if(ncplane_putwstr(ncp, lstr) != (int)wcslen(lstr)){ + goto err; + } + if(notcurses_render(nc)){ + goto err; + } + sleep(1); if(notcurses_stop(nc)){ return EXIT_FAILURE; } diff --git a/src/lib/notcurses.c b/src/lib/notcurses.c index de80722f5..497254faf 100644 --- a/src/lib/notcurses.c +++ b/src/lib/notcurses.c @@ -40,6 +40,7 @@ typedef struct ncplane { int lenx, leny; // size of the plane, [0..len{x,y}) is addressable struct ncplane* z; // plane below us struct notcurses* nc; // our parent nc, kinda lame waste of memory FIXME + uint64_t channels; // colors when not provided an active style } ncplane; typedef struct notcurses { @@ -401,7 +402,7 @@ int ncplane_fg_rgb8(ncplane* n, int r, int g, int b){ if(r < 0 || g < 0 || b < 0){ return -1; } - cell_set_fg(&n->fb[fbcellidx(n, n->y, n->x)], r, g, b); + cell_rgb_set_fg(&n->channels, r, g, b); return 0; } @@ -550,7 +551,7 @@ int load_cell(cell* c, const wchar_t* wstr){ int copied = 0; do{ if(copied == sizeof(c->cchar) / sizeof(*c->cchar)){ - if(!wcwidth(*wstr)){ // next one *must* be a spacing char + if(*wstr != L'\0' && wcwidth(*wstr) == 0){ // next one *must* be a spacer return -1; // filled up the buffer } break; // no terminator on cells which fill the array [shrug] @@ -567,11 +568,17 @@ int ncplane_putwstr(ncplane* n, const wchar_t* wstr){ int ret = 0; // FIXME speed up this blissfully naive solution cell c; + memset(&c, 0, sizeof(c)); + uint32_t rgb = cell_fg_rgb(n->channels); + cell_set_fg(&c, cell_rgb_red(rgb), cell_rgb_green(rgb), cell_rgb_blue(rgb)); while(*wstr != L'\0'){ int wcs = load_cell(&c, wstr); - if(wcs <= 0){ + if(wcs < 0){ return -ret; } + if(wcs == 0){ + break; + } wstr += wcs; if(ncplane_putwc(n, &c)){ return -ret; diff --git a/tests/notcurses.cpp b/tests/notcurses.cpp index b1c7cddc5..51eb1168b 100644 --- a/tests/notcurses.cpp +++ b/tests/notcurses.cpp @@ -15,11 +15,16 @@ class NotcursesTest : public :: testing::Test { ASSERT_NE(nullptr, nc_); } + void TearDown() override { + if(nc_){ + EXPECT_EQ(0, notcurses_stop(nc_)); + } + } + struct notcurses* nc_; }; TEST_F(NotcursesTest, BasicLifetime) { - EXPECT_EQ(0, notcurses_stop(nc_)); } TEST_F(NotcursesTest, TermDimensions) { @@ -35,7 +40,6 @@ TEST_F(NotcursesTest, TermDimensions) { auto envx = std::stoi(strx, nullptr); EXPECT_EQ(envx, x); } - EXPECT_EQ(0, notcurses_stop(nc_)); } TEST_F(NotcursesTest, ResizeSameSize) { @@ -46,7 +50,6 @@ TEST_F(NotcursesTest, ResizeSameSize) { notcurses_term_dimyx(nc_, &newy, &newx); EXPECT_EQ(newx, x); EXPECT_EQ(newy, y); - EXPECT_EQ(0, notcurses_stop(nc_)); } // we should at least have WA_STANDOUT everywhere, i should think?