source: toilet/trunk/src/term.c @ 1385

Last change on this file since 1385 was 1385, checked in by Sam Hocevar, 15 years ago
  • 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.
  • Property svn:keywords set to Id
File size: 2.2 KB
Line 
1/*
2 *  TOIlet        The Other Implementation’s letters
3 *  Copyright (c) 2006 Sam Hocevar <sam@zoy.org>
4 *                All Rights Reserved
5 *
6 *  $Id: term.c 1385 2006-11-13 01:02:05Z sam $
7 *
8 *  This program is free software; you can redistribute it and/or
9 *  modify it under the terms of the Do What The Fuck You Want To
10 *  Public License, Version 2, as published by Sam Hocevar. See
11 *  http://sam.zoy.org/wtfpl/COPYING for more details.
12 */
13
14/*
15 * This file contains text to canvas rendering functions.
16 */
17
18#include "config.h"
19
20#if defined(HAVE_INTTYPES_H)
21#   include <inttypes.h>
22#endif
23#include <stdlib.h>
24#include <cucul.h>
25
26#include "toilet.h"
27#include "render.h"
28
29static int feed_tiny(context_t *, uint32_t, uint32_t);
30static int flush_tiny(context_t *);
31static int end_tiny(context_t *);
32
33int init_tiny(context_t *cx)
34{
35    cx->ew = 16;
36    cx->eh = 2;
37
38    cx->feed = feed_tiny;
39    cx->flush = flush_tiny;
40    cx->end = end_tiny;
41
42    return 0;
43}
44
45static int feed_tiny(context_t *cx, uint32_t ch, uint32_t attr)
46{
47    switch(ch)
48    {
49        case (uint32_t)'\r':
50            return 0;
51        case (uint32_t)'\n':
52            cx->x = 0;
53            cx->y++;
54            return 0;
55        case (uint32_t)'\t':
56            cx->x = (cx->x & ~7) + 8;
57            return 0;
58    }
59
60    /* Check whether we reached the end of the screen */
61    if(cx->x && cx->x + 1 > cx->term_width)
62    {
63        cx->x = 0;
64        cx->y++;
65    }
66
67    /* Check whether the current canvas is large enough */
68    if(cx->x + 1 > cx->w)
69    {
70        cx->w = cx->x + 1 < cx->term_width ? cx->x + 1 : cx->term_width;
71        if(cx->w > cx->ew)
72            cx->ew = cx->ew + cx->ew / 2;
73    }
74
75    if(cx->y + 1 > cx->h)
76    {
77        cx->h = cx->y + 1;
78        if(cx->h > cx->eh)
79            cx->eh = cx->eh + cx->eh / 2;
80    }
81
82    cucul_set_attr(cx->cv, attr);
83    cucul_set_canvas_size(cx->cv, cx->ew, cx->eh);
84
85    cucul_put_char(cx->cv, cx->x, cx->y, ch);
86    cx->x++;
87
88    return 0;
89}
90
91static int flush_tiny(context_t *cx)
92{
93    cx->torender = cx->cv;
94    cucul_set_canvas_size(cx->torender, cx->w, cx->h);
95
96    cx->ew = 16;
97    cx->eh = 2;
98    cx->x = cx->y = 0;
99    cx->w = cx->h = 0;
100    cx->cv = cucul_create_canvas(cx->ew, cx->eh);
101
102    return 0;
103}
104
105static int end_tiny(context_t *cx)
106{
107    return 0;
108}
109
Note: See TracBrowser for help on using the repository browser.