v / vlib / term / term.v
233 lines · 213 sloc · 5.95 KB · 9e012480b2fe7146ff1280899d829d09762a2d3b
Raw
1@[has_globals]
2module term
3
4import os
5import strings.textscanner
6
7const default_columns_size = 80
8const default_rows_size = 25
9
10// Coord - used by term.get_cursor_position and term.set_cursor_position.
11pub struct Coord {
12pub mut:
13 x int
14 y int
15}
16
17__global can_show_color_on_stdout_cache = 0
18__global can_show_color_on_stderr_cache = 0
19
20// can_show_color_on_stdout returns true, if colors are allowed in stdout.
21// It returns false otherwise.
22pub fn can_show_color_on_stdout() bool {
23 match can_show_color_on_stdout_cache {
24 1 { return true }
25 -1 { return false }
26 else {}
27 }
28
29 status := supports_escape_sequences(1)
30 can_show_color_on_stdout_cache = if status { 1 } else { -1 }
31 return status
32}
33
34// can_show_color_on_stderr returns true, if colors are allowed in stderr.
35// It returns false otherwise.
36pub fn can_show_color_on_stderr() bool {
37 match can_show_color_on_stderr_cache {
38 1 { return true }
39 -1 { return false }
40 else {}
41 }
42
43 status := supports_escape_sequences(2)
44 can_show_color_on_stderr_cache = if status { 1 } else { -1 }
45 return status
46}
47
48// failed returns a bold white on red version of the string `s`.
49// If colors are not allowed, returns the string `s`
50pub fn failed(s string) string {
51 if can_show_color_on_stdout() {
52 return bg_red(bold(white(s)))
53 }
54 return s
55}
56
57// ok_message returns a colored string with green color.
58// If colors are not allowed, returns a given string.
59pub fn ok_message(s string) string {
60 if can_show_color_on_stdout() {
61 return green('${s}')
62 }
63 return s
64}
65
66// fail_message returns a colored string with red color.
67// If colors are not allowed, returns a given string.
68pub fn fail_message(s string) string {
69 return failed('${s}')
70}
71
72// warn_message returns a colored string with yellow color.
73// If colors are not allowed, returns a given string.
74pub fn warn_message(s string) string {
75 if can_show_color_on_stdout() {
76 return bright_yellow('${s}')
77 }
78 return s
79}
80
81// colorize returns a colored string by running the specified `cfn` over the message `s`, but only if colored stdout is supported by the terminal.
82// Example: term.colorize(term.yellow, 'the message')
83pub fn colorize(cfn fn (string) string, s string) string {
84 if can_show_color_on_stdout() {
85 return cfn(s)
86 }
87 return s
88}
89
90// ecolorize returns a colored string by running the specified `cfn` over the message `s`, but only if colored stderr is supported by the terminal.
91// Example: term.ecolorize(term.bright_red, 'the message')
92pub fn ecolorize(cfn fn (string) string, s string) string {
93 if can_show_color_on_stderr() {
94 return cfn(s)
95 }
96 return s
97}
98
99// strip_ansi removes any ANSI sequences in the `text`.
100pub fn strip_ansi(text string) string {
101 // This is a port of https://github.com/kilobyte/colorized-logs/blob/master/ansi2txt.c
102 // \e, [, 1, m, a, b, c, \e, [, 2, 2, m => abc
103 mut input := textscanner.new(text)
104 mut output := []u8{cap: text.len}
105 mut ch := 0
106 for ch != -1 {
107 ch = input.next()
108 if ch == 27 {
109 ch = input.next()
110 if ch == `[` {
111 for {
112 ch = input.next()
113 if ch in [`;`, `?`] || (ch >= `0` && ch <= `9`) {
114 continue
115 }
116 break
117 }
118 } else if ch == `]` {
119 ch = input.next()
120 if ch >= `0` && ch <= `9` {
121 for {
122 ch = input.next()
123 if ch == -1 || ch == 7 {
124 break
125 }
126 if ch == 27 {
127 ch = input.next()
128 break
129 }
130 }
131 }
132 } else if ch == `%` {
133 ch = input.next()
134 }
135 } else if ch != -1 {
136 output << u8(ch)
137 }
138 }
139 return output.bytestr()
140}
141
142// h_divider returns a horizontal divider line with a dynamic width, that depends on the current terminal settings.
143// If an empty string is passed in, print enough spaces to make a new line.
144pub fn h_divider(divider string) string {
145 cols, _ := get_terminal_size()
146 mut result := ''
147 if divider.len > 0 {
148 result = divider.repeat(1 + (cols / divider.len))
149 } else {
150 result = ' '.repeat(1 + cols)
151 }
152 return result[0..cols]
153}
154
155// header_left returns a horizontal divider line with a title text on the left.
156// e.g: term.header_left('TITLE', '=')
157// ==== TITLE =========================
158pub fn header_left(text string, divider string) string {
159 plain_text := strip_ansi(text)
160 xcols, _ := get_terminal_size() // can get 0 in lldb/gdb
161 cols := imax(1, xcols)
162 relement := if divider.len > 0 { divider } else { ' ' }
163 hstart := relement.repeat(4)[0..4]
164 remaining_cols := imax(0, (cols - (hstart.len + 1 + plain_text.len + 1)))
165 hend := relement.repeat((remaining_cols + 1) / relement.len)[0..remaining_cols]
166 return '${hstart} ${text} ${hend}'
167}
168
169// header returns a horizontal divider line with a centered text in the middle.
170// e.g: term.header('TEXT', '=')
171// =============== TEXT ===============
172pub fn header(text string, divider string) string {
173 if text.len == 0 {
174 return h_divider(divider)
175 }
176 xcols, _ := get_terminal_size()
177 cols := imax(1, xcols)
178 tlimit := imax(1, if cols > text.len + 2 + 2 * divider.len {
179 text.len
180 } else {
181 cols - 3 - 2 * divider.len
182 })
183 tlimit_aligned := if (tlimit % 2) != (cols % 2) { tlimit + 1 } else { tlimit }
184 tstart := imax(0, (cols - tlimit_aligned) / 2)
185 mut ln := ''
186 if divider.len > 0 {
187 ln = divider.repeat(1 + cols / divider.len)[0..cols]
188 } else {
189 ln = ' '.repeat(1 + cols)
190 }
191 if ln.len == 1 {
192 return ln + ' ' + text[0..tlimit] + ' ' + ln
193 }
194 return ln[0..tstart] + ' ' + text[0..tlimit] + ' ' + ln[tstart + tlimit + 2..cols]
195}
196
197fn imax(x int, y int) int {
198 return if x > y { x } else { y }
199}
200
201@[manualfree]
202fn supports_escape_sequences(fd int) bool {
203 vcolors_override := os.getenv('VCOLORS')
204 defer {
205 unsafe { vcolors_override.free() }
206 }
207 if vcolors_override == 'always' {
208 return true
209 }
210 if vcolors_override == 'never' {
211 return false
212 }
213 env_term := os.getenv('TERM')
214 defer {
215 unsafe { env_term.free() }
216 }
217 if env_term == 'dumb' {
218 return false
219 }
220 $if windows {
221 env_conemu := os.getenv('ConEmuANSI')
222 defer {
223 unsafe { env_conemu.free() }
224 }
225 if env_conemu == 'ON' {
226 return true
227 }
228 // 4 is enable_virtual_terminal_processing
229 return (os.is_atty(fd) & 0x0004) > 0
230 } $else {
231 return os.is_atty(fd) > 0
232 }
233}
234