2 * ansilib.c - ANSI escape sequences library
4 * http://en.wikipedia.org/wiki/ANSI_escape_code
6 * This file is part of ktap by Jovi Zhangwei.
8 * Copyright (C) 2012-2013 Jovi Zhangwei <jovi.zhangwei@gmail.com>.
10 * ktap is free software; you can redistribute it and/or modify it
11 * under the terms and conditions of the GNU General Public License,
12 * version 2, as published by the Free Software Foundation.
14 * ktap is distributed in the hope it will be useful, but WITHOUT
15 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
19 * You should have received a copy of the GNU General Public License along with
20 * this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
24 #include "../include/ktap_types.h"
29 * function ansi.clear_screen - Move cursor to top left and clear screen.
31 * Description: Sends ansi code for moving cursor to top left and then the
32 * ansi code for clearing the screen from the cursor position to the end.
35 static int ktap_lib_clear_screen(ktap_state *ks)
37 kp_printf(ks, "\033[1;1H\033[J");
42 * function ansi.set_color - Set the ansi Select Graphic Rendition mode.
43 * @fg: Foreground color to set.
45 * Description: Sends ansi code for Select Graphic Rendition mode for the
46 * given forground color. Black (30), Blue (34), Green (32), Cyan (36),
47 * Red (31), Purple (35), Brown (33), Light Gray (37).
50 static int ktap_lib_set_color(ktap_state *ks)
54 kp_arg_check(ks, 1, KTAP_TNUMBER);
56 fg = nvalue(kp_arg(ks, 1));
57 kp_printf(ks, "\033[%dm", fg);
62 * function ansi.set_color2 - Set the ansi Select Graphic Rendition mode.
63 * @fg: Foreground color to set.
64 * @bg: Background color to set.
66 * Description: Sends ansi code for Select Graphic Rendition mode for the
67 * given forground color, Black (30), Blue (34), Green (32), Cyan (36),
68 * Red (31), Purple (35), Brown (33), Light Gray (37) and the given
69 * background color, Black (40), Red (41), Green (42), Yellow (43),
70 * Blue (44), Magenta (45), Cyan (46), White (47).
72 static int ktap_lib_set_color2(ktap_state *ks)
76 kp_arg_check(ks, 1, KTAP_TNUMBER);
77 kp_arg_check(ks, 2, KTAP_TNUMBER);
79 fg = nvalue(kp_arg(ks, 1));
80 bg = nvalue(kp_arg(ks, 2));
81 kp_printf(ks, "\033[%d;%dm", fg, bg);
86 * function ansi.set_color3 - Set the ansi Select Graphic Rendition mode.
87 * @fg: Foreground color to set.
88 * @bg: Background color to set.
89 * @attr: Color attribute to set.
91 * Description: Sends ansi code for Select Graphic Rendition mode for the
92 * given forground color, Black (30), Blue (34), Green (32), Cyan (36),
93 * Red (31), Purple (35), Brown (33), Light Gray (37), the given
94 * background color, Black (40), Red (41), Green (42), Yellow (43),
95 * Blue (44), Magenta (45), Cyan (46), White (47) and the color attribute
96 * All attributes off (0), Intensity Bold (1), Underline Single (4),
97 * Blink Slow (5), Blink Rapid (6), Image Negative (7).
99 static int ktap_lib_set_color3(ktap_state *ks)
103 kp_arg_check(ks, 1, KTAP_TNUMBER);
104 kp_arg_check(ks, 2, KTAP_TNUMBER);
105 kp_arg_check(ks, 3, KTAP_TNUMBER);
107 fg = nvalue(kp_arg(ks, 1));
108 bg = nvalue(kp_arg(ks, 2));
109 attr = nvalue(kp_arg(ks, 3));
112 kp_printf(ks, "\033[%d;%d;%dm", fg, bg, attr);
114 kp_printf(ks, "\033[%d;%dm", fg, bg);
120 * function ansi.reset_color - Resets Select Graphic Rendition mode.
122 * Description: Sends ansi code to reset foreground, background and color
123 * attribute to default values.
125 static int ktap_lib_reset_color(ktap_state *ks)
127 kp_printf(ks, "\033[0;0m");
132 * function ansi.new_line - Move cursor to new line.
134 * Description: Sends ansi code new line.
136 static int ktap_lib_new_line (ktap_state *ks)
138 kp_printf(ks, "\12");
142 static const ktap_Reg ansi_funcs[] = {
143 {"clear_screen", ktap_lib_clear_screen},
144 {"set_color", ktap_lib_set_color},
145 {"set_color2", ktap_lib_set_color2},
146 {"set_color3", ktap_lib_set_color3},
147 {"reset_color", ktap_lib_reset_color},
148 {"new_line", ktap_lib_new_line},
152 void kp_init_ansilib(ktap_state *ks)
154 kp_register_lib(ks, "ansi", ansi_funcs);