Add new line formatting functionality
This commit is contained in:
parent
e81696c2d3
commit
d991e61d68
3 changed files with 47 additions and 55 deletions
2
config.h
2
config.h
|
@ -13,4 +13,4 @@ static const unsigned int pos_y = 50;
|
||||||
enum corners { TOP_LEFT, TOP_RIGHT, BOTTOM_LEFT, BOTTOM_RIGHT };
|
enum corners { TOP_LEFT, TOP_RIGHT, BOTTOM_LEFT, BOTTOM_RIGHT };
|
||||||
enum corners corner = TOP_RIGHT;
|
enum corners corner = TOP_RIGHT;
|
||||||
|
|
||||||
static const unsigned int duration = 15; /* in seconds */
|
static const unsigned int duration = 60; /* in seconds */
|
||||||
|
|
98
herbe.c
98
herbe.c
|
@ -10,6 +10,37 @@
|
||||||
Display *display;
|
Display *display;
|
||||||
Window window;
|
Window window;
|
||||||
|
|
||||||
|
int get_max_len(char *body, XftFont *font, int max_text_width)
|
||||||
|
{
|
||||||
|
int body_len = strlen(body);
|
||||||
|
XGlyphInfo info;
|
||||||
|
XftTextExtentsUtf8(display, font, (FcChar8 *)body, body_len, &info);
|
||||||
|
|
||||||
|
if (info.width < max_text_width)
|
||||||
|
return body_len;
|
||||||
|
|
||||||
|
int eol = max_text_width / font->max_advance_width;
|
||||||
|
info.width = 0;
|
||||||
|
|
||||||
|
while (info.width < max_text_width)
|
||||||
|
{
|
||||||
|
eol++;
|
||||||
|
XftTextExtentsUtf8(display, font, (FcChar8 *)body, eol, &info);
|
||||||
|
}
|
||||||
|
|
||||||
|
eol--;
|
||||||
|
|
||||||
|
int temp = eol;
|
||||||
|
|
||||||
|
while (body[eol] != ' ' && eol)
|
||||||
|
--eol;
|
||||||
|
|
||||||
|
if (eol == 0)
|
||||||
|
return temp;
|
||||||
|
else
|
||||||
|
return ++eol;
|
||||||
|
}
|
||||||
|
|
||||||
void expire()
|
void expire()
|
||||||
{
|
{
|
||||||
XEvent event;
|
XEvent event;
|
||||||
|
@ -20,14 +51,12 @@ void expire()
|
||||||
|
|
||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
if (argc != 2)
|
if (argc == 1)
|
||||||
{
|
{
|
||||||
fprintf(stderr, "Usage: herbe message\n");
|
fprintf(stderr, "Usage: herbe body\n");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
char *body = argv[1];
|
|
||||||
|
|
||||||
signal(SIGALRM, expire);
|
signal(SIGALRM, expire);
|
||||||
alarm(duration);
|
alarm(duration);
|
||||||
|
|
||||||
|
@ -57,62 +86,26 @@ int main(int argc, char *argv[])
|
||||||
|
|
||||||
XftFont *font = XftFontOpenName(display, screen, font_pattern);
|
XftFont *font = XftFontOpenName(display, screen, font_pattern);
|
||||||
|
|
||||||
|
int num_of_lines = 0;
|
||||||
int max_text_width = width - 2 * padding;
|
int max_text_width = width - 2 * padding;
|
||||||
int eols_size = 5;
|
// TODO replace hard-coded size 100
|
||||||
int *eols = malloc(eols_size * sizeof(int));
|
char words[100][max_text_width / font->max_advance_width + 2];
|
||||||
eols[0] = 0;
|
|
||||||
int remainder = strlen(body);
|
|
||||||
int num_of_lines = 1;
|
|
||||||
|
|
||||||
while (1)
|
for (int i = 1; i < argc; i++)
|
||||||
{
|
{
|
||||||
XGlyphInfo info;
|
char *body = argv[i];
|
||||||
info.width = 0;
|
|
||||||
int eol = max_text_width / font->max_advance_width;
|
for (unsigned int eol = get_max_len(body, font, max_text_width); eol <= strlen(body) && eol; body += eol, num_of_lines++, eol = get_max_len(body, font, max_text_width))
|
||||||
while (info.width < max_text_width)
|
|
||||||
{
|
{
|
||||||
eol++;
|
strncpy(words[num_of_lines], body, eol);
|
||||||
XftTextExtentsUtf8(display, font, (FcChar8 *)body + eols[num_of_lines - 1], eol, &info);
|
words[num_of_lines][eol] = '\0';
|
||||||
}
|
}
|
||||||
|
|
||||||
--eol;
|
|
||||||
|
|
||||||
if (eol >= remainder)
|
|
||||||
{
|
|
||||||
if (eols_size < num_of_lines + 1)
|
|
||||||
{
|
|
||||||
eols_size += 5;
|
|
||||||
eols = realloc(eols, eols_size * sizeof(int));
|
|
||||||
}
|
|
||||||
eols[num_of_lines] = eols[num_of_lines - 1] + remainder;
|
|
||||||
num_of_lines++;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
int temp = eol;
|
|
||||||
|
|
||||||
while (body[eols[num_of_lines - 1] + eol] != ' ' && eol)
|
|
||||||
--eol;
|
|
||||||
|
|
||||||
if (eol == 0)
|
|
||||||
eol = temp;
|
|
||||||
else
|
|
||||||
eol++;
|
|
||||||
|
|
||||||
remainder -= eol;
|
|
||||||
if (eols_size < num_of_lines + 1)
|
|
||||||
{
|
|
||||||
eols_size += 5;
|
|
||||||
eols = realloc(eols, eols_size * sizeof(int));
|
|
||||||
}
|
|
||||||
eols[num_of_lines] = eols[num_of_lines - 1] + eol;
|
|
||||||
num_of_lines++;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned int x = pos_x;
|
unsigned int x = pos_x;
|
||||||
unsigned int y = pos_y;
|
unsigned int y = pos_y;
|
||||||
unsigned int text_height = font->ascent - font->descent;
|
unsigned int text_height = font->ascent - font->descent;
|
||||||
unsigned int height = (num_of_lines - 2) * line_spacing + (num_of_lines - 1) * text_height + 2 * padding;
|
unsigned int height = (num_of_lines - 1) * line_spacing + num_of_lines * text_height + 2 * padding;
|
||||||
|
|
||||||
if (corner == TOP_RIGHT || corner == BOTTOM_RIGHT)
|
if (corner == TOP_RIGHT || corner == BOTTOM_RIGHT)
|
||||||
x = screen_width - width - border_size * 2 - pos_x;
|
x = screen_width - width - border_size * 2 - pos_x;
|
||||||
|
@ -139,14 +132,13 @@ int main(int argc, char *argv[])
|
||||||
if (event.type == Expose)
|
if (event.type == Expose)
|
||||||
{
|
{
|
||||||
XClearWindow(display, window);
|
XClearWindow(display, window);
|
||||||
for (int i = 1; i < num_of_lines; i++)
|
for (int i = 0; i < num_of_lines; i++)
|
||||||
XftDrawStringUtf8(draw, &color, font, padding, line_spacing * (i - 1) + text_height * i + padding, (FcChar8 *)body + eols[i - 1], eols[i] - eols[i - 1]);
|
XftDrawStringUtf8(draw, &color, font, padding, line_spacing * i + text_height * (i + 1) + padding, (FcChar8 *)words[i], strlen(words[i]));
|
||||||
}
|
}
|
||||||
if (event.type == ButtonPress)
|
if (event.type == ButtonPress)
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
free(eols);
|
|
||||||
XftDrawDestroy(draw);
|
XftDrawDestroy(draw);
|
||||||
XftColorFree(display, visual, colormap, &color);
|
XftColorFree(display, visual, colormap, &color);
|
||||||
XftFontClose(display, font);
|
XftFontClose(display, font);
|
||||||
|
|
2
makefile
2
makefile
|
@ -1,5 +1,5 @@
|
||||||
default:
|
default:
|
||||||
gcc herbe.c -Wall -Wextra -Werror -pedantic -lX11 -lXft -I/usr/include/freetype2 -lm -o herbe
|
gcc herbe.c -Wall -Wextra -pedantic -lX11 -lXft -I/usr/include/freetype2 -lm -o herbe
|
||||||
|
|
||||||
install: default
|
install: default
|
||||||
cp herbe /usr/local/bin
|
cp herbe /usr/local/bin
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue