bug-ncurses
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Truncate instead of wrap


From: Virchanza
Subject: Re: Truncate instead of wrap
Date: Fri, 12 Dec 2008 04:52:58 -0800 (PST)


Here's what I got so far. It's not great but I had to throw it together
quickly. You use the function "printw_truncate" to print a truncated line to
the screen. The function "printw_truncate" in turn calls the function
"printw_truncate_no_new_line" in order to deal with strings that contain
new-line characters.

#include <string.h> /* strlen */

static int printw_truncate_no_new_line(char *str);

int printw_truncate(const char *str,...)
{
    va_list args;
    char buf[2048];

    if (strlen(str) > 1000u)  /* Hopefully 2 kB is enough for a 1 kB string
*/
        return 0;

    /* First use vsprintf */

    va_start(args,str);

    vsprintf(buf,str,args);

    /* OK, now we have the final string */

    char *start = buf;
    char *end;

    while ((end = strchr(start,'\n')))
    {
        *end = '\0';
        printw_truncate_no_new_line(start);
        printw("\n");
        start = end+1;
    }

    return printw_truncate_no_new_line(start);
}

static int printw_truncate_no_new_line(char *str)
{
    int const free_room = COLS - getcurx(stdscr) - 1;

    if (strlen(str) > free_room)
        str[free_room] = '\0';

    return printw(str);
}

Anyone wanna work together to help perfect it?
-- 
View this message in context: 
http://www.nabble.com/Truncate-instead-of-wrap-tp20914125p20975086.html
Sent from the Gnu - Ncurses mailing list archive at Nabble.com.





reply via email to

[Prev in Thread] Current Thread [Next in Thread]