ncplane_puttext() unit test + fix #691

pull/723/head
nick black 4 years ago
parent 97517d0473
commit cfe764bd57
No known key found for this signature in database
GPG Key ID: 5F43400C21CBFACC

@ -1531,22 +1531,29 @@ int ncplane_puttext(ncplane* n, int y, ncalign_e align, const char* text, size_t
}
// FIXME use the more advanced unicode functionality to break lines
if(iswspace(w)){
if(x == 0){
text += consumed;
linestart = text;
continue; // don't emit leading whitespace, or count it
}else{
breaker = text;
}
}
if(x + width > dimx){
break;
}
x += width;
text += consumed;
}
//fprintf(stderr, "exited at %d (%d) looking at %.*s\n", x, dimx, breaker - linestart, linestart);
int carrycols = 0;
// if we have no breaker, we got a word that was longer than our line;
// print what we can and move along. if *text is nul, we're done.
if(!*text || breaker == NULL){
breaker = text;
}
int carrycols = 0;
breaker = text + 1;
}else{
carrycols = text - breaker;
}
//fprintf(stderr, "exited at %d (%d) looking at [%.*s]\n", x, dimx, (int)(breaker - linestart), linestart);
if(breaker != linestart){
totalcols += (breaker - linestart);
const int xpos = ncplane_align(n, align, x);
@ -1559,7 +1566,11 @@ int ncplane_puttext(ncplane* n, int y, ncalign_e align, const char* text, size_t
}
}
x = carrycols;
if(breaker == text + 1){
linestart = breaker;
}else{
linestart = breaker + 1;
}
++y; // FIXME scrolling!??!!
}while(*text);
if(bytes){

@ -18,13 +18,9 @@ TEST_CASE("TextLayout") {
CHECK(0 < ncplane_puttext(sp, 0, NCALIGN_LEFT, str, &bytes));
CHECK(0 == notcurses_render(nc_));
CHECK(bytes == strlen(str));
char* line = ncplane_contents(sp, 0, 0, 1, 20);
char* line = ncplane_contents(sp, 0, 0, 2, 20);
REQUIRE(line);
CHECK(0 == strcmp(line, "this is going to be"));
free(line);
line = ncplane_contents(sp, 1, 0, 1, 20);
REQUIRE(line);
CHECK(0 == strcmp(line, "broken up"));
CHECK(0 == strcmp(line, "this is going to bebroken up"));
free(line);
ncplane_destroy(sp);
}
@ -36,13 +32,9 @@ TEST_CASE("TextLayout") {
CHECK(0 < ncplane_puttext(sp, 0, NCALIGN_RIGHT, str, &bytes));
CHECK(0 == notcurses_render(nc_));
CHECK(bytes == strlen(str));
char* line = ncplane_contents(sp, 0, 0, 1, 20);
char* line = ncplane_contents(sp, 0, 0, 2, 20);
REQUIRE(line);
CHECK(0 == strcmp(line, "this is going to be"));
free(line);
line = ncplane_contents(sp, 1, 0, 1, 20);
REQUIRE(line);
CHECK(0 == strcmp(line, "broken up"));
CHECK(0 == strcmp(line, "this is going to bebroken up"));
free(line);
ncplane_destroy(sp);
}
@ -54,13 +46,9 @@ TEST_CASE("TextLayout") {
CHECK(0 < ncplane_puttext(sp, 0, NCALIGN_CENTER, str, &bytes));
CHECK(0 == notcurses_render(nc_));
CHECK(bytes == strlen(str));
char* line = ncplane_contents(sp, 0, 0, 1, 20);
REQUIRE(line);
CHECK(0 == strcmp(line, "this is going to be"));
free(line);
line = ncplane_contents(sp, 1, 0, 1, 20);
char* line = ncplane_contents(sp, 0, 0, 2, 20);
REQUIRE(line);
CHECK(0 == strcmp(line, "broken up"));
CHECK(0 == strcmp(line, "this is going to bebroken up"));
free(line);
ncplane_destroy(sp);
}
@ -74,13 +62,9 @@ TEST_CASE("TextLayout") {
CHECK(0 < ncplane_puttext(sp, 0, NCALIGN_CENTER, boundstr, &bytes));
CHECK(0 == notcurses_render(nc_));
CHECK(bytes == strlen(boundstr));
char* line = ncplane_contents(sp, 0, 0, 1, 10);
REQUIRE(line);
CHECK(0 == strcmp(line, "my nuclear"));
free(line);
line = ncplane_contents(sp, 1, 0, 1, 10);
char* line = ncplane_contents(sp, 0, 0, -1, -1);
REQUIRE(line);
CHECK(0 == strcmp(line, "arms"));
CHECK(0 == strcmp(line, "my nucleararms"));
free(line);
ncplane_destroy(sp);
}
@ -94,17 +78,25 @@ TEST_CASE("TextLayout") {
CHECK(0 < ncplane_puttext(sp, 0, NCALIGN_CENTER, boundstr, &bytes));
CHECK(0 == notcurses_render(nc_));
CHECK(bytes == strlen(boundstr));
char* line = ncplane_contents(sp, 0, 0, 1, 10);
REQUIRE(line);
CHECK(0 == strcmp(line, "my"));
free(line);
line = ncplane_contents(sp, 1, 0, 1, 10);
char* line = ncplane_contents(sp, 0, 0, -1, -1);
REQUIRE(line);
CHECK(0 == strcmp(line, "grasping"));
CHECK(0 == strcmp(line, "mygraspingarms"));
free(line);
line = ncplane_contents(sp, 2, 0, 1, 10);
ncplane_destroy(sp);
}
// lay out text where a word is longer than the plane
SUBCASE("LayoutCrossBoundary") {
auto sp = ncplane_new(nc_, 3, 10, 0, 0, nullptr);
REQUIRE(sp);
size_t bytes;
const char boundstr[] = "my thermonuclear arms";
CHECK(0 < ncplane_puttext(sp, 0, NCALIGN_CENTER, boundstr, &bytes));
CHECK(0 == notcurses_render(nc_));
CHECK(bytes == strlen(boundstr));
char* line = ncplane_contents(sp, 0, 0, -1, -1);
REQUIRE(line);
CHECK(0 == strcmp(line, "arms"));
CHECK(0 == strcmp(line, "mythermonuclear arms"));
free(line);
ncplane_destroy(sp);
}

Loading…
Cancel
Save