Selaa lähdekoodia

* Support for ANSI escape codes in the input:

http://zoy.org/~sam/toilet-ansi.png
      http://zoy.org/~sam/toilet-ansi2.png
  * Empty lines are currently broken.
pull/1/head
Sam Hocevar sam 18 vuotta sitten
vanhempi
commit
c325cb1c81
4 muutettua tiedostoa jossa 74 lisäystä ja 30 poistoa
  1. +11
    -4
      src/figlet.c
  2. +59
    -23
      src/render.c
  3. +3
    -2
      src/term.c
  4. +1
    -1
      src/toilet.h

+ 11
- 4
src/figlet.c Näytä tiedosto

@@ -32,7 +32,7 @@
#define STD_GLYPHS (127 - 32) #define STD_GLYPHS (127 - 32)
#define EXT_GLYPHS (STD_GLYPHS + 7) #define EXT_GLYPHS (STD_GLYPHS + 7)


static int feed_figlet(context_t *, uint32_t);
static int feed_figlet(context_t *, uint32_t, uint32_t);
static int flush_figlet(context_t *); static int flush_figlet(context_t *);
static int end_figlet(context_t *); static int end_figlet(context_t *);


@@ -50,7 +50,7 @@ int init_figlet(context_t *cx)
return 0; return 0;
} }


static int feed_figlet(context_t *cx, uint32_t ch)
static int feed_figlet(context_t *cx, uint32_t ch, uint32_t attr)
{ {
unsigned int c, w, h, x, y; unsigned int c, w, h, x, y;


@@ -90,14 +90,21 @@ static int feed_figlet(context_t *cx, uint32_t ch)
if(cx->y + h > cx->h) if(cx->y + h > cx->h)
cx->h = cx->y + h; cx->h = cx->y + h;


if(attr)
cucul_set_attr(cx->cv, attr);
cucul_set_canvas_size(cx->cv, cx->w, cx->h); cucul_set_canvas_size(cx->cv, cx->w, cx->h);


/* Render our char (FIXME: create a rect-aware cucul_blit_canvas?) */ /* Render our char (FIXME: create a rect-aware cucul_blit_canvas?) */
for(y = 0; y < h; y++) for(y = 0; y < h; y++)
for(x = 0; x < w; x++) for(x = 0; x < w; x++)
{ {
uint32_t tmp = cucul_get_char(cx->image, x, y + c * cx->height);
cucul_put_char(cx->cv, cx->x + x, cx->y + y, tmp);
uint32_t tmpch = cucul_get_char(cx->image, x, y + c * cx->height);
//uint32_t tmpat = cucul_get_attr(cx->image, x, y + c * cx->height);
/* FIXME: this could be changed to cucul_put_attr() when the
* function is fixed in libcucul */
//cucul_set_attr(cx->cv, tmpat);
cucul_put_char(cx->cv, cx->x + x, cx->y + y, tmpch);
//cucul_put_attr(cx->cv, cx->x + x, cx->y + y, tmpat);
} }


/* Advance cursor */ /* Advance cursor */


+ 59
- 23
src/render.c Näytä tiedosto

@@ -46,47 +46,83 @@ int render_init(context_t *cx)


int render_stdin(context_t *cx) int render_stdin(context_t *cx)
{ {
char buf[10];
unsigned int i = 0, len;
uint32_t ch;
cucul_canvas_t *cv;
char *line;
unsigned int i, len;

/* FIXME: we can't read longer lines */
len = 1024;
line = malloc(len);
cv = cucul_create_canvas(0, 0);


/* Read from stdin */ /* Read from stdin */
while(!feof(stdin)) while(!feof(stdin))
{ {
buf[i++] = getchar();
buf[i] = '\0';

ch = cucul_utf8_to_utf32(buf, &len);

if(!len)
continue;
if(!fgets(line, len, stdin))
break;


cx->feed(cx, ch);
i = 0;
cucul_set_canvas_size(cv, 0, 0);
cucul_import_memory(cv, line, strlen(line), "utf8");
for(i = 0; i < cucul_get_canvas_width(cv); i++)
{
uint32_t ch = cucul_get_char(cv, i, 0);
uint32_t at = cucul_get_attr(cv, i, 0);
cx->feed(cx, ch, at);
if(cucul_utf32_is_fullwidth(ch)) i++;
}


if(ch == '\n')
render_flush(cx);
render_flush(cx);
} }


free(line);

return 0; return 0;
} }


int render_list(context_t *cx, unsigned int argc, char *argv[]) int render_list(context_t *cx, unsigned int argc, char *argv[])
{ {
unsigned int i, j;
cucul_canvas_t *cv;
unsigned int i, j, len;
char *parser = NULL;


for(i = 0; i < argc; i++)
cv = cucul_create_canvas(0, 0);

for(j = 0; j < argc; )
{ {
/* Read from commandline */
unsigned int len;
char *cr;


if(i)
cx->feed(cx, ' ');
if(!parser)
{
if(j)
cx->feed(cx, ' ', 0);
parser = argv[j];
}

cr = strchr(parser, '\n');
if(cr)
len = (cr - parser) + 1;
else
len = strlen(parser);

cucul_set_canvas_size(cv, 0, 0);
cucul_import_memory(cv, parser, len, "utf8");
for(i = 0; i < cucul_get_canvas_width(cv); i++)
{
uint32_t ch = cucul_get_char(cv, i, 0);
uint32_t at = cucul_get_attr(cv, i, 0);
cx->feed(cx, ch, at);
if(cucul_utf32_is_fullwidth(ch)) i++;
}


for(j = 0; argv[i][j];)
if(cr)
{
parser += len;
render_flush(cx);
}
else
{ {
cx->feed(cx, cucul_utf8_to_utf32(argv[i] + j, &len));
j += len;
parser = NULL;
j++;
} }
} }




+ 3
- 2
src/term.c Näytä tiedosto

@@ -26,7 +26,7 @@
#include "toilet.h" #include "toilet.h"
#include "render.h" #include "render.h"


static int feed_tiny(context_t *, uint32_t);
static int feed_tiny(context_t *, uint32_t, uint32_t);
static int flush_tiny(context_t *); static int flush_tiny(context_t *);
static int end_tiny(context_t *); static int end_tiny(context_t *);


@@ -42,7 +42,7 @@ int init_tiny(context_t *cx)
return 0; return 0;
} }


static int feed_tiny(context_t *cx, uint32_t ch)
static int feed_tiny(context_t *cx, uint32_t ch, uint32_t attr)
{ {
switch(ch) switch(ch)
{ {
@@ -79,6 +79,7 @@ static int feed_tiny(context_t *cx, uint32_t ch)
cx->eh = cx->eh + cx->eh / 2; cx->eh = cx->eh + cx->eh / 2;
} }


cucul_set_attr(cx->cv, attr);
cucul_set_canvas_size(cx->cv, cx->ew, cx->eh); cucul_set_canvas_size(cx->cv, cx->ew, cx->eh);


cucul_put_char(cx->cv, cx->x, cx->y, ch); cucul_put_char(cx->cv, cx->x, cx->y, ch);


+ 1
- 1
src/toilet.h Näytä tiedosto

@@ -28,7 +28,7 @@ struct toilet_context
unsigned int w, h, ew, eh, x, y, lines; unsigned int w, h, ew, eh, x, y, lines;


/* Render methods */ /* Render methods */
int (*feed)(struct toilet_context *, uint32_t);
int (*feed)(struct toilet_context *, uint32_t, uint32_t);
int (*flush)(struct toilet_context *); int (*flush)(struct toilet_context *);
int (*end)(struct toilet_context *); int (*end)(struct toilet_context *);




Ladataan…
Peruuta
Tallenna