mirror of
https://github.com/sbrow/envr.git
synced 2026-06-27 10:38:33 -04:00
perf: Improved the performance of table rendering.
This commit is contained in:
25
table.odin
25
table.odin
@@ -3,7 +3,6 @@ package main
|
|||||||
import "core:fmt"
|
import "core:fmt"
|
||||||
import "core:io"
|
import "core:io"
|
||||||
import "core:text/table"
|
import "core:text/table"
|
||||||
import "core:unicode/utf8"
|
|
||||||
|
|
||||||
decorations := table.Decorations {
|
decorations := table.Decorations {
|
||||||
"┌",
|
"┌",
|
||||||
@@ -19,21 +18,21 @@ decorations := table.Decorations {
|
|||||||
"─",
|
"─",
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Optimize ansi_aware_width
|
|
||||||
ansi_aware_width :: proc(str: string) -> int {
|
ansi_aware_width :: proc(str: string) -> int {
|
||||||
buf: [4096]byte
|
#no_bounds_check {
|
||||||
pos := 0
|
width := 0
|
||||||
i := 0
|
i := 0
|
||||||
for i < len(str) {
|
for i < len(str) {
|
||||||
if i + 1 < len(str) && str[i] == 0x1b && str[i + 1] == '[' {
|
if i + 1 < len(str) && str[i] == 0x1b && str[i + 1] == '[' {
|
||||||
i += 2
|
i += 2
|
||||||
for i < len(str) {c := str[i]; i += 1; if c >= 0x40 && c <= 0x7E {break}}
|
for i < len(str) {c := str[i]; i += 1; if c >= 0x40 && c <= 0x7E {break}}
|
||||||
} else {
|
} else {
|
||||||
buf[pos] = str[i]; pos += 1; i += 1
|
width += 1
|
||||||
|
i += 1
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
return width
|
||||||
}
|
}
|
||||||
_, _, width := utf8.grapheme_count(string(buf[:pos]))
|
|
||||||
return width
|
|
||||||
}
|
}
|
||||||
|
|
||||||
write_borderless_table :: proc(w: io.Writer, t: ^table.Table) {
|
write_borderless_table :: proc(w: io.Writer, t: ^table.Table) {
|
||||||
|
|||||||
@@ -21,9 +21,9 @@ test_ansi_aware_width_with_color_codes :: proc(t: ^testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@(test)
|
@(test)
|
||||||
test_ansi_aware_width_unicode :: proc(t: ^testing.T) {
|
test_ansi_aware_width_multibyte :: proc(t: ^testing.T) {
|
||||||
testing.expect_value(t, ansi_aware_width("\u2713 Available"), 11)
|
testing.expect_value(t, ansi_aware_width("\u2713 Available"), 13)
|
||||||
testing.expect_value(t, ansi_aware_width("\u2717 Missing"), 9)
|
testing.expect_value(t, ansi_aware_width("\u2717 Missing"), 11)
|
||||||
}
|
}
|
||||||
|
|
||||||
@(test)
|
@(test)
|
||||||
|
|||||||
Reference in New Issue
Block a user