Debian Codegraph @

tapset/ansi.stp

raw
1 # ANSI escape sequences tapset
2 # Copyright (C) 2009 Red Hat, Inc., Eugene Teo <[email protected]>
3 #
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License version 2 as
6 # published by the Free Software Foundation.
7 #
8 # Based on some previous work done by Masami Hiramatsu for stapgames.
9 # Reference: http://en.wikipedia.org/wiki/ANSI_escape_code
10 #
11
12 /**
13 * sfunction ansi_clear_screen - Move cursor to top left and clear screen.
14 *
15 * Description: Sends ansi code for moving cursor to top left and then the
16 * ansi code for clearing the screen from the cursor position to the end.
17 **/
18 function ansi_clear_screen() {
19 print("\033[1;1H\033[J")
20 }
21
22 /**
23 * sfunction ansi_set_color - Set the ansi Select Graphic Rendition mode.
24 * @fg: Foreground color to set.
25 *
26 * Description: Sends ansi code for Select Graphic Rendition mode for the
27 * given forground color. Black (30), Blue (34), Green (32), Cyan (36),
28 * Red (31), Purple (35), Brown (33), Light Gray (37).
29 */
30 function ansi_set_color(fg:long) {
31 printf("\033[%dm", fg)
32 }
33
34 /**
35 * sfunction ansi_set_color - Set the ansi Select Graphic Rendition mode.
36 * @fg: Foreground color to set.
37 * @bg: Background color to set.
38 *
39 * Description: Sends ansi code for Select Graphic Rendition mode for the
40 * given forground color, Black (30), Blue (34), Green (32), Cyan (36),
41 * Red (31), Purple (35), Brown (33), Light Gray (37) and the given
42 * background color, Black (40), Red (41), Green (42), Yellow (43),
43 * Blue (44), Magenta (45), Cyan (46), White (47).
44 */
45 function ansi_set_color(fg:long, bg:long) {
46 printf("\033[%d;%dm", bg, fg)
47 }
48 function ansi_set_color2(fg:long, bg:long) {
49 ansi_set_color(fg, bg);
50 }
51
52 /**
53 * sfunction ansi_set_color - Set the ansi Select Graphic Rendition mode.
54 * @fg: Foreground color to set.
55 * @bg: Background color to set.
56 * @attr: Color attribute to set.
57 *
58 * Description: Sends ansi code for Select Graphic Rendition mode for the
59 * given forground color, Black (30), Blue (34), Green (32), Cyan (36),
60 * Red (31), Purple (35), Brown (33), Light Gray (37), the given
61 * background color, Black (40), Red (41), Green (42), Yellow (43),
62 * Blue (44), Magenta (45), Cyan (46), White (47) and the color attribute
63 * All attributes off (0), Intensity Bold (1), Underline Single (4),
64 * Blink Slow (5), Blink Rapid (6), Image Negative (7).
65 */
66 function ansi_set_color(fg:long, bg:long, attr:long) {
67 attr_str = attr ? sprintf(";%dm", attr) : "m"
68 printf("\033[%d;%d%s", bg, fg, attr_str)
69 }
70 function ansi_set_color3(fg:long, bg:long, attr:long) {
71 ansi_set_color(fg, bg, attr);
72 }
73
74 /**
75 * sfunction ansi_reset_color - Resets Select Graphic Rendition mode.
76 *
77 * Description: Sends ansi code to reset foreground, background and color
78 * attribute to default values.
79 */
80 function ansi_reset_color() {
81 ansi_set_color(0, 0, 0)
82 }
83
84 /**
85 * sfunction ansi_new_line - Move cursor to new line.
86 *
87 * Description: Sends ansi code new line.
88 */
89 function ansi_new_line() {
90 printf("\12")
91 }
92
93 /**
94 * sfunction ansi_cursor_move - Move cursor to new coordinates.
95 * @x: Row to move the cursor to.
96 * @y: Colomn to move the cursor to.
97 *
98 * Description: Sends ansi code for positioning the cursor at row x
99 * and column y. Coordinates start at one, (1,1) is the top-left corner.
100 */
101 function ansi_cursor_move(x:long, y:long) {
102 printf("\033[%d;%dH", y, x)
103 }
104
105 /**
106 * sfunction ansi_cursor_hide - Hides the cursor.
107 *
108 * Description: Sends ansi code for hiding the cursor.
109 */
110 function ansi_cursor_hide() {
111 print("\033[>5I")
112 }
113
114 /**
115 * sfunction ansi_cursor_save - Saves the cursor position.
116 *
117 * Description: Sends ansi code for saving the current cursor position.
118 */
119 function ansi_cursor_save() {
120 print("\033[s")
121 }
122
123 /**
124 * sfunction ansi_cursor_restore - Restores a previously saved cursor position.
125 *
126 * Description: Sends ansi code for restoring the current cursor position
127 * previously saved with ansi_cursor_save().
128 */
129 function ansi_cursor_restore() {
130 print("\033[u")
131 }
132
133 /**
134 * sfunction ansi_cursor_show - Shows the cursor.
135 *
136 * Description: Sends ansi code for showing the cursor.
137 */
138 function ansi_cursor_show() {
139 print("\033[>5h")
140 }