term: use textscanner.new in term.strip_ansi/1

pull/10154/head
Delyan Angelov 2021-05-20 11:24:40 +03:00
parent 888b9c020c
commit 00b3092a92
No known key found for this signature in database
GPG Key ID: 66886C0F12D595ED
1 changed files with 2 additions and 18 deletions

View File

@ -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 {