mirror of
https://github.com/dankamongmen/notcurses.git
synced 2024-11-18 03:25:55 +00:00
ncplane_puttext() unit test + fix #691
This commit is contained in:
parent
97517d0473
commit
cfe764bd57
@ -1531,7 +1531,13 @@ 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)){
|
||||||
breaker = text;
|
if(x == 0){
|
||||||
|
text += consumed;
|
||||||
|
linestart = text;
|
||||||
|
continue; // don't emit leading whitespace, or count it
|
||||||
|
}else{
|
||||||
|
breaker = text;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if(x + width > dimx){
|
if(x + width > dimx){
|
||||||
break;
|
break;
|
||||||
@ -1539,14 +1545,15 @@ int ncplane_puttext(ncplane* n, int y, ncalign_e align, const char* text, size_t
|
|||||||
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{
|
||||||
|
carrycols = text - breaker;
|
||||||
}
|
}
|
||||||
int carrycols = 0;
|
//fprintf(stderr, "exited at %d (%d) looking at [%.*s]\n", x, dimx, (int)(breaker - linestart), linestart);
|
||||||
carrycols = text - breaker;
|
|
||||||
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;
|
||||||
linestart = breaker + 1;
|
if(breaker == text + 1){
|
||||||
|
linestart = breaker;
|
||||||
|
}else{
|
||||||
|
linestart = breaker + 1;
|
||||||
|
}
|
||||||
++y; // FIXME scrolling!??!!
|
++y; // FIXME scrolling!??!!
|
||||||
}while(*text);
|
}while(*text);
|
||||||
if(bytes){
|
if(bytes){
|
||||||
|
@ -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);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user