ncplane_puttext() unit test + fix #691

This commit is contained in:
nick black 2020-06-16 02:12:11 -04:00
parent 97517d0473
commit cfe764bd57
No known key found for this signature in database
GPG Key ID: 5F43400C21CBFACC
2 changed files with 41 additions and 38 deletions

View File

@ -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 // FIXME use the more advanced unicode functionality to break lines
if(iswspace(w)){ if(iswspace(w)){
if(x == 0){
text += consumed;
linestart = text;
continue; // don't emit leading whitespace, or count it
}else{
breaker = text; breaker = text;
} }
}
if(x + width > dimx){ if(x + width > dimx){
break; break;
} }
x += width; x += width;
text += consumed; 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; // 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. // print what we can and move along. if *text is nul, we're done.
if(!*text || breaker == NULL){ if(!*text || breaker == NULL){
breaker = text; breaker = text + 1;
} }else{
int carrycols = 0;
carrycols = text - breaker; carrycols = text - breaker;
}
//fprintf(stderr, "exited at %d (%d) looking at [%.*s]\n", x, dimx, (int)(breaker - linestart), linestart);
if(breaker != linestart){ if(breaker != linestart){
totalcols += (breaker - linestart); totalcols += (breaker - linestart);
const int xpos = ncplane_align(n, align, x); 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; x = carrycols;
if(breaker == text + 1){
linestart = breaker;
}else{
linestart = breaker + 1; linestart = breaker + 1;
}
++y; // FIXME scrolling!??!! ++y; // FIXME scrolling!??!!
}while(*text); }while(*text);
if(bytes){ if(bytes){

View File

@ -18,13 +18,9 @@ TEST_CASE("TextLayout") {
CHECK(0 < ncplane_puttext(sp, 0, NCALIGN_LEFT, str, &bytes)); CHECK(0 < ncplane_puttext(sp, 0, NCALIGN_LEFT, str, &bytes));
CHECK(0 == notcurses_render(nc_)); CHECK(0 == notcurses_render(nc_));
CHECK(bytes == strlen(str)); CHECK(bytes == strlen(str));
char* line = ncplane_contents(sp, 0, 0, 1, 20); char* line = ncplane_contents(sp, 0, 0, 2, 20);
REQUIRE(line); REQUIRE(line);
CHECK(0 == strcmp(line, "this is going to be")); CHECK(0 == strcmp(line, "this is going to bebroken up"));
free(line);
line = ncplane_contents(sp, 1, 0, 1, 20);
REQUIRE(line);
CHECK(0 == strcmp(line, "broken up"));
free(line); free(line);
ncplane_destroy(sp); ncplane_destroy(sp);
} }
@ -36,13 +32,9 @@ TEST_CASE("TextLayout") {
CHECK(0 < ncplane_puttext(sp, 0, NCALIGN_RIGHT, str, &bytes)); CHECK(0 < ncplane_puttext(sp, 0, NCALIGN_RIGHT, str, &bytes));
CHECK(0 == notcurses_render(nc_)); CHECK(0 == notcurses_render(nc_));
CHECK(bytes == strlen(str)); CHECK(bytes == strlen(str));
char* line = ncplane_contents(sp, 0, 0, 1, 20); char* line = ncplane_contents(sp, 0, 0, 2, 20);
REQUIRE(line); REQUIRE(line);
CHECK(0 == strcmp(line, "this is going to be")); CHECK(0 == strcmp(line, "this is going to bebroken up"));
free(line);
line = ncplane_contents(sp, 1, 0, 1, 20);
REQUIRE(line);
CHECK(0 == strcmp(line, "broken up"));
free(line); free(line);
ncplane_destroy(sp); ncplane_destroy(sp);
} }
@ -54,13 +46,9 @@ TEST_CASE("TextLayout") {
CHECK(0 < ncplane_puttext(sp, 0, NCALIGN_CENTER, str, &bytes)); CHECK(0 < ncplane_puttext(sp, 0, NCALIGN_CENTER, str, &bytes));
CHECK(0 == notcurses_render(nc_)); CHECK(0 == notcurses_render(nc_));
CHECK(bytes == strlen(str)); CHECK(bytes == strlen(str));
char* line = ncplane_contents(sp, 0, 0, 1, 20); char* line = ncplane_contents(sp, 0, 0, 2, 20);
REQUIRE(line); REQUIRE(line);
CHECK(0 == strcmp(line, "this is going to be")); CHECK(0 == strcmp(line, "this is going to bebroken up"));
free(line);
line = ncplane_contents(sp, 1, 0, 1, 20);
REQUIRE(line);
CHECK(0 == strcmp(line, "broken up"));
free(line); free(line);
ncplane_destroy(sp); ncplane_destroy(sp);
} }
@ -74,13 +62,9 @@ TEST_CASE("TextLayout") {
CHECK(0 < ncplane_puttext(sp, 0, NCALIGN_CENTER, boundstr, &bytes)); CHECK(0 < ncplane_puttext(sp, 0, NCALIGN_CENTER, boundstr, &bytes));
CHECK(0 == notcurses_render(nc_)); CHECK(0 == notcurses_render(nc_));
CHECK(bytes == strlen(boundstr)); CHECK(bytes == strlen(boundstr));
char* line = ncplane_contents(sp, 0, 0, 1, 10); char* line = ncplane_contents(sp, 0, 0, -1, -1);
REQUIRE(line); REQUIRE(line);
CHECK(0 == strcmp(line, "my nuclear")); CHECK(0 == strcmp(line, "my nucleararms"));
free(line);
line = ncplane_contents(sp, 1, 0, 1, 10);
REQUIRE(line);
CHECK(0 == strcmp(line, "arms"));
free(line); free(line);
ncplane_destroy(sp); ncplane_destroy(sp);
} }
@ -94,17 +78,25 @@ TEST_CASE("TextLayout") {
CHECK(0 < ncplane_puttext(sp, 0, NCALIGN_CENTER, boundstr, &bytes)); CHECK(0 < ncplane_puttext(sp, 0, NCALIGN_CENTER, boundstr, &bytes));
CHECK(0 == notcurses_render(nc_)); CHECK(0 == notcurses_render(nc_));
CHECK(bytes == strlen(boundstr)); CHECK(bytes == strlen(boundstr));
char* line = ncplane_contents(sp, 0, 0, 1, 10); char* line = ncplane_contents(sp, 0, 0, -1, -1);
REQUIRE(line); REQUIRE(line);
CHECK(0 == strcmp(line, "my")); CHECK(0 == strcmp(line, "mygraspingarms"));
free(line); free(line);
line = ncplane_contents(sp, 1, 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); REQUIRE(line);
CHECK(0 == strcmp(line, "grasping")); CHECK(0 == strcmp(line, "mythermonuclear arms"));
free(line);
line = ncplane_contents(sp, 2, 0, 1, 10);
REQUIRE(line);
CHECK(0 == strcmp(line, "arms"));
free(line); free(line);
ncplane_destroy(sp); ncplane_destroy(sp);
} }