//The core printing function, prints characters to the array, and sets colors
inline int printstring(WINDOW *win, char *fmt)
{
    int size = strlen(fmt);
    if(size>0)
    {
        int j, i, p=win->cursorx;
        int x = (win->line[win->cursory].width_in_bytes==win->width)?win->cursorx:cursorx_to_position(win->line[win->cursory].chars, win->cursorx, &p);

        if(p!=x)//so we start inside a wide character, erase it for good
        {
            const char* ts = win->line[win->cursory].chars+p;
            int len = ANY_LENGTH;
            unsigned tc = UTF8_getch(&ts, &len);
            int tw = mk_wcwidth((wchar_t)tc);
            erease_utf8_by_cw(win->line[win->cursory].chars+p, tw, tw, win->width*4-p-1);
            x = p+tw-1;
        }
        for (j=0; j<size; j++){
            if (!(fmt[j]==10)){//check that this isnt a newline char
                const char* utf8str = fmt+j;
                int len = ANY_LENGTH;
                unsigned ch = UTF8_getch(&utf8str, &len);
                int cw = mk_wcwidth((wchar_t)ch);
                len = ANY_LENGTH-len;
                if(cw<1) cw = 1;
                if(len<1) len = 1;
                if (win->cursorx+cw <= win->width && win->cursory <= win->height - 1) {
                    win->line[win->cursory].width_in_bytes += erease_utf8_by_cw(win->line[win->cursory].chars+x, cw, len, win->width*4-x-1);
                    for(i=0; i<len; i++)
                    {
                        win->line[win->cursory].chars[x+i]=fmt[j+i];
                    }
                    for(i=0; i<cw; i++)
                    {
                        win->line[win->cursory].FG[win->cursorx]=win->FG;
                        win->line[win->cursory].BG[win->cursorx]=win->BG;
                        addedchar(win);
                    }
                    win->line[win->cursory].touched=true;
                    j += len-1;
                    x+=len;
                } else if (win->cursory <= win->height - 1) {
                    // don't write outside the window, but don't abort if there are still lines to write.
                    //j += len-1;
                    //x+=len;
                } else {
                    return 0; //if we try and write anything outside the window, abort completely
                }
            } else {// if the character is a newline, make sure to move down a line
                if (newline(win)==0){
                    return 0;
                }
                x = 0;
            }
        }
    }
    win->draw=true;
    return 1;
}
std::string trim_to(const std::string &text, size_t length) {
    const size_t width = utf8_width(text.c_str());
    if(width <= length) {
        return text;
    }
    const size_t bytes_offset = cursorx_to_position(text.c_str(), length - 1, NULL, -1);
    return text.substr(0, bytes_offset) + "…";
}
int waddch(WINDOW *win, const chtype ch)
{
    char charcode;
    charcode=ch;

    switch (ch){        //LINE_NESW  - X for on, O for off
        case LINE_XOXO:   //#define LINE_XOXO 4194424
            charcode=LINE_XOXO_C;
            break;
        case LINE_OXOX:   //#define LINE_OXOX 4194417
            charcode=LINE_OXOX_C;
            break;
        case LINE_XXOO:   //#define LINE_XXOO 4194413
            charcode=LINE_XXOO_C;
            break;
        case LINE_OXXO:   //#define LINE_OXXO 4194412
            charcode=LINE_OXXO_C;
            break;
        case LINE_OOXX:   //#define LINE_OOXX 4194411
            charcode=LINE_OOXX_C;
            break;
        case LINE_XOOX:   //#define LINE_XOOX 4194410
            charcode=LINE_XOOX_C;
            break;
        case LINE_XXOX:   //#define LINE_XXOX 4194422
            charcode=LINE_XXOX_C;
            break;
        case LINE_XXXO:   //#define LINE_XXXO 4194420
            charcode=LINE_XXXO_C;
            break;
        case LINE_XOXX:   //#define LINE_XOXX 4194421
            charcode=LINE_XOXX_C;
            break;
        case LINE_OXXX:   //#define LINE_OXXX 4194423
            charcode=LINE_OXXX_C;
            break;
        case LINE_XXXX:   //#define LINE_XXXX 4194414
            charcode=LINE_XXXX_C;
            break;
        default:
            charcode = (char)ch;
            break;
        }


    int cury=win->cursory;
    int p = win->cursorx;
    int curx=(win->line[cury].width_in_bytes==win->width)?win->cursorx:cursorx_to_position(win->line[cury].chars, win->cursorx, &p);

    if(curx!=p)
    {
        const char* ts = win->line[cury].chars+p;
        int len = ANY_LENGTH;
        unsigned tc = UTF8_getch(&ts, &len);
        int tw = mk_wcwidth((wchar_t)tc);
        win->line[cury].width_in_bytes += erease_utf8_by_cw(win->line[cury].chars+p, tw, tw, win->width*4-p-1);
        curx = p+tw-1;
    }
    else if(win->line[cury].width_in_bytes!=win->width)
    {
        win->line[cury].width_in_bytes += erease_utf8_by_cw(win->line[cury].chars+p, 1, 1, win->width*4-p-1);
    }

//if (win2 > -1){
   win->line[cury].chars[curx]=charcode;
   win->line[cury].FG[win->cursorx]=win->FG;
   win->line[cury].BG[win->cursorx]=win->BG;


    win->draw=true;
    addedchar(win);
    return 1;
  //  else{
  //  win2=win2+1;

}