Exemple #1
0
static const char *
str_8bit_fit_to_term (const char *text, int width, align_crt_t just_mode)
{
    static char result[BUF_MEDIUM];
    char *actual;
    size_t remain;
    int ident = 0;
    size_t length;
    size_t pos = 0;

    length = strlen (text);
    actual = result;
    remain = sizeof (result);

    if ((int) length <= width)
    {
        switch (HIDE_FIT (just_mode))
        {
        case J_CENTER_LEFT:
        case J_CENTER:
            ident = (width - length) / 2;
            break;
        case J_RIGHT:
            ident = width - length;
            break;
        default:
            break;
        }

        if ((int) remain <= ident)
            goto finally;
        memset (actual, ' ', ident);
        actual += ident;
        remain -= ident;

        for (; pos < length && remain > 1; pos++, actual++, remain--)
            actual[0] = char_isprint (text[pos]) ? text[pos] : '.';

        if (width - length - ident > 0)
        {
            if (remain <= width - length - ident)
                goto finally;
            memset (actual, ' ', width - length - ident);
            actual += width - length - ident;
        }
    }
    else if (IS_FIT (just_mode))
    {
        for (; pos + 1 <= (gsize) width / 2 && remain > 1; actual++, pos++, remain--)
            actual[0] = char_isprint (text[pos]) ? text[pos] : '.';

        if (remain <= 1)
            goto finally;
        actual[0] = '~';
        actual++;
        remain--;

        pos += length - width + 1;
        for (; pos < length && remain > 1; pos++, actual++, remain--)
            actual[0] = char_isprint (text[pos]) ? text[pos] : '.';
    }
    else
    {
        switch (HIDE_FIT (just_mode))
        {
        case J_CENTER:
            ident = (length - width) / 2;
            break;
        case J_RIGHT:
            ident = length - width;
            break;
        default:
            break;
        }

        pos += ident;
        for (; pos < (gsize) (ident + width) && remain > 1; pos++, actual++, remain--)
            actual[0] = char_isprint (text[pos]) ? text[pos] : '.';
    }

  finally:
    if (actual >= result + sizeof (result))
        actual = result + sizeof (result) - 1;
    actual[0] = '\0';
    return result;
}
Exemple #2
0
static const char *
str_ascii_fit_to_term (const char *text, int width, align_crt_t just_mode)
{
    static char result[BUF_MEDIUM];
    char *actual;
    size_t remain;
    int ident = 0;
    size_t length;
    size_t pos = 0;

    length = strlen (text);
    actual = result;
    remain = sizeof (result);

    if ((int) length <= width)
    {
        switch (HIDE_FIT (just_mode))
        {
        case J_CENTER_LEFT:
        case J_CENTER:
            ident = (width - length) / 2;
            break;
        case J_RIGHT:
            ident = width - length;
            break;
        }

        /* add space before text */
        if ((int) remain <= ident)
            goto finally;
        memset (actual, ' ', ident);
        actual += ident;
        remain -= ident;

        /* copy all characters */
        for (; pos < (gsize) length && remain > 1; pos++, actual++, remain--)
        {
            actual[0] = isascii ((unsigned char) text[pos]) ? text[pos] : '?';
            actual[0] = g_ascii_isprint ((gchar) actual[0]) ? actual[0] : '.';
        }

        /* add space after text */
        if (width - length - ident > 0)
        {
            if (remain <= width - length - ident)
                goto finally;
            memset (actual, ' ', width - length - ident);
            actual += width - length - ident;
        }
    }
    else if (IS_FIT (just_mode))
    {
        /* copy prefix of text, that is not wider than width / 2 */
        for (; pos + 1 <= (gsize) width / 2 && remain > 1; actual++, pos++, remain--)
        {
            actual[0] = isascii ((unsigned char) text[pos]) ? text[pos] : '?';
            actual[0] = g_ascii_isprint ((gchar) actual[0]) ? actual[0] : '.';
        }

        if (remain <= 1)
            goto finally;
        actual[0] = '~';
        actual++;
        remain--;

        pos += length - width + 1;

        /* copy suffix of text */
        for (; pos < length && remain > 1; pos++, actual++, remain--)
        {
            actual[0] = isascii ((unsigned char) text[pos]) ? text[pos] : '?';
            actual[0] = g_ascii_isprint ((gchar) actual[0]) ? actual[0] : '.';
        }
    }
    else
    {
        switch (HIDE_FIT (just_mode))
        {
        case J_CENTER:
            ident = (length - width) / 2;
            break;
        case J_RIGHT:
            ident = length - width;
            break;
        }

        /* copy substring text, substring start from ident and take width
         * characters from text */
        pos += ident;
        for (; pos < (gsize) (ident + width) && remain > 1; pos++, actual++, remain--)
        {
            actual[0] = isascii ((unsigned char) text[pos]) ? text[pos] : '?';
            actual[0] = g_ascii_isprint ((gchar) actual[0]) ? actual[0] : '.';
        }

    }

finally:
    actual[0] = '\0';
    return result;
}
Exemple #3
0
static const char *
str_utf8_fit_to_term (const char *text, int width, align_crt_t just_mode)
{
    static char result[BUF_MEDIUM * 6];
    const struct term_form *pre_form;
    struct utf8_tool tool;

    pre_form = str_utf8_make_make_term_form (text, (size_t) (-1));
    tool.cheked = pre_form->text;
    tool.actual = result;
    tool.remain = sizeof (result);
    tool.compose = 0;

    if (pre_form->width <= (gsize)width)
    {
	tool.ident = 0;
	switch (HIDE_FIT (just_mode))
	{
	case J_CENTER_LEFT:
	case J_CENTER:
	    tool.ident = (width - pre_form->width) / 2;
	    break;
	case J_RIGHT:
	    tool.ident = width - pre_form->width;
	    break;
	}

	utf8_tool_insert_space (&tool, tool.ident);
	utf8_tool_copy_chars_to_end (&tool);
	utf8_tool_insert_space (&tool, width - pre_form->width - tool.ident);
    }
    else
    {
	if (IS_FIT (just_mode))
	{
	    tool.ident = 0;
	    utf8_tool_copy_chars_to (&tool, width / 2);
	    utf8_tool_insert_char (&tool, '~');

	    tool.ident = 0;
	    utf8_tool_skip_chars_to (&tool, pre_form->width - width + 1);
	    utf8_tool_copy_chars_to_end (&tool);
	    utf8_tool_insert_space (&tool,
				    width - (pre_form->width - tool.ident +
					     1));
	}
	else
	{
	    tool.ident = 0;
	    switch (HIDE_FIT (just_mode))
	    {
	    case J_CENTER:
		tool.ident = (width - pre_form->width) / 2;
		break;
	    case J_RIGHT:
		tool.ident = width - pre_form->width;
		break;
	    }

	    utf8_tool_skip_chars_to (&tool, 0);
	    utf8_tool_insert_space (&tool, tool.ident);
	    utf8_tool_copy_chars_to (&tool, width);
	    utf8_tool_insert_space (&tool, width - tool.ident);
	}
    }

    tool.actual[0] = '\0';
    if (tool.compose)
	utf8_tool_compose (result, sizeof (result));
    return result;
}