dankamongmen / notcurses

blingful character graphics/TUI library. definitely not curses.
https://nick-black.com/dankwiki/index.php/Notcurses
Other
3.56k stars 112 forks source link

ncplane_puttext causes infinite plane resizing #2771

Open lokxii opened 6 months ago

lokxii commented 6 months ago

I have this following code that causes the problem

int main() {
    setlocale(LC_ALL, "");
    notcurses* nc = NULL;
    notcurses_options opt = {
        .loglevel = NCLOGLEVEL_TRACE,
        .flags = NCOPTION_NO_QUIT_SIGHANDLERS | NCOPTION_SUPPRESS_BANNERS
    };
    if ((nc = notcurses_init(&opt, NULL)) == NULL) {
        exit(EXIT_FAILURE);
    }

    ncplane* stdplane = notcurses_stdplane(nc);
    ncplane_options testplane_opt = {
        .rows = 1,
        .cols = 48,
        .flags = NCPLANE_OPTION_AUTOGROW | NCPLANE_OPTION_VSCROLL
    };
    ncplane* n = ncplane_create(stdplane, &testplane_opt);
    std::string text = "おなかがすいたし眠いのでナイトシティに帰ります\n私は最寄駅のことをナイトシティと2分前から呼んでいます";
    float cols = ncplane_puttext(
        n, 0, NCALIGN_LEFT, text.c_str(), NULL);

    notcurses_render(nc);

    ncinput ni;
    while (notcurses_get_blocking(nc, &ni) == -1 || ni.id == 27) {}
    notcurses_stop(nc);

    return 0;
}

I am using notcurses just compiled from latest commit on master branch, an Kitty terminal v0.31.0 When I remove the number 2 from the string, ncplane_puttext prints the string properly.

lokxii commented 6 months ago

I can only tell that some calculations are wrong in this function to cause *bytes not being properly set, causing the do while loop in ncplane_puttext() to loop infinitely

lokxii commented 6 months ago

ncplane_puttext() can print text normally when I add one more 1 column wide character to the second line of the string. For example

    std::string text = "おなかがすいたし眠いのでナイトシティに帰ります\n私は最寄駅のことをナイトシティと22分前から呼んでいます";
dankamongmen commented 6 months ago

thanks for the report! i'll look into this as soon as i can, hopefully very soon.

lokxii commented 6 months ago

I replaced my use of ncplane_puttext() with ncplane_putstr_yx() because I only need to print align to left. Then I realized why do we need ncplane_puttext() when ncplane_putstr*() basically do all the jobs of ncplane_puttext()?

dankamongmen commented 6 months ago

I replaced my use of ncplane_puttext() with ncplane_putstr_yx() because I only need to print align to left. Then I realized why do we need ncplane_puttext() when ncplane_putstr*() basically do all the jobs of ncplane_puttext()?

ncplane_puttext() does way more than ncplane_putstr(). it's intended for putting multiple lines of text.

lokxii commented 6 months ago

Interesting. Cuz I used ncplane_putstr_yx() to print that multiline string to give the expected result of ncplane_puttext()

Edit: sorry, pressed the wrong button closing the commit

dankamongmen commented 6 months ago

Interesting. Cuz I used ncplane_putstr_yx() to print that multiline string to give the expected result of ncplane_puttext()

yeah, ncplane_putstr() will move to the next line, but it won't intelligently split lines so that words aren't broken across lines etc.

Edit: sorry, pressed the wrong button closing the commit

no worries! i definitely want to get this resolved either way.