term: use textscanner.new in term.strip_ansi/1
parent
888b9c020c
commit
00b3092a92
|
@ -1,6 +1,7 @@
|
||||||
module term
|
module term
|
||||||
|
|
||||||
import os
|
import os
|
||||||
|
import strings.textscanner
|
||||||
|
|
||||||
const (
|
const (
|
||||||
default_columns_size = 80
|
default_columns_size = 80
|
||||||
|
@ -63,28 +64,11 @@ pub fn colorize(cfn fn (string) string, s string) string {
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
struct AnsiScanner {
|
|
||||||
mut:
|
|
||||||
pos int
|
|
||||||
input string
|
|
||||||
}
|
|
||||||
|
|
||||||
fn (mut ss AnsiScanner) next() int {
|
|
||||||
if ss.pos < ss.input.len {
|
|
||||||
opos := ss.pos
|
|
||||||
ss.pos++
|
|
||||||
return ss.input[opos]
|
|
||||||
}
|
|
||||||
return -1
|
|
||||||
}
|
|
||||||
|
|
||||||
// strip_ansi removes any ANSI sequences in the `text`
|
// strip_ansi removes any ANSI sequences in the `text`
|
||||||
pub fn strip_ansi(text string) string {
|
pub fn strip_ansi(text string) string {
|
||||||
// This is a port of https://github.com/kilobyte/colorized-logs/blob/master/ansi2txt.c
|
// This is a port of https://github.com/kilobyte/colorized-logs/blob/master/ansi2txt.c
|
||||||
// \e, [, 1, m, a, b, c, \e, [, 2, 2, m => abc
|
// \e, [, 1, m, a, b, c, \e, [, 2, 2, m => abc
|
||||||
mut input := AnsiScanner{
|
mut input := textscanner.new(text)
|
||||||
input: text
|
|
||||||
}
|
|
||||||
mut output := []byte{cap: text.len}
|
mut output := []byte{cap: text.len}
|
||||||
mut ch := 0
|
mut ch := 0
|
||||||
for ch != -1 {
|
for ch != -1 {
|
||||||
|
|
Loading…
Reference in New Issue