mirror of
https://github.com/sbrow/envr.git
synced 2026-06-27 18:48:33 -04:00
feat: Added --color flag.
This commit is contained in:
69
colors.odin
69
colors.odin
@@ -1,5 +1,6 @@
|
||||
package main
|
||||
|
||||
import "core:io"
|
||||
import "core:terminal/ansi"
|
||||
|
||||
COLOR_HEADINGS ::
|
||||
@@ -15,3 +16,71 @@ COLOR_TABLE_HEADING :: ansi.CSI + ansi.FG_BRIGHT_GREEN + ansi.SGR
|
||||
|
||||
ANSI_RESET :: ansi.CSI + ansi.RESET + ansi.SGR
|
||||
|
||||
ANSI_Strip_State :: enum { Normal, GotESC, InCSI }
|
||||
|
||||
ANSI_Strip_Data :: struct {
|
||||
inner: io.Writer,
|
||||
state: ANSI_Strip_State,
|
||||
}
|
||||
|
||||
ansi_strip_proc :: proc(
|
||||
stream_data: rawptr,
|
||||
mode: io.Stream_Mode,
|
||||
p: []byte,
|
||||
offset: i64,
|
||||
whence: io.Seek_From,
|
||||
) -> (n: i64, err: io.Error) {
|
||||
data := cast(^ANSI_Strip_Data) stream_data
|
||||
|
||||
#partial switch mode {
|
||||
case .Write:
|
||||
start := 0
|
||||
for i in 0..<len(p) {
|
||||
b := p[i]
|
||||
|
||||
switch data.state {
|
||||
case .Normal:
|
||||
if b == 0x1b {
|
||||
if i > start {
|
||||
io.write(data.inner, p[start:i])
|
||||
}
|
||||
data.state = .GotESC
|
||||
}
|
||||
|
||||
case .GotESC:
|
||||
if b == '[' {
|
||||
data.state = .InCSI
|
||||
} else {
|
||||
start = i
|
||||
data.state = .Normal
|
||||
}
|
||||
|
||||
case .InCSI:
|
||||
if b >= 0x40 && b <= 0x7E {
|
||||
start = i + 1
|
||||
data.state = .Normal
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if data.state == .Normal && len(p) > start {
|
||||
io.write(data.inner, p[start:])
|
||||
}
|
||||
|
||||
n = i64(len(p))
|
||||
return
|
||||
|
||||
case .Flush:
|
||||
return 0, io.flush(data.inner)
|
||||
case .Close:
|
||||
return 0, io.close(data.inner)
|
||||
case:
|
||||
return data.inner.procedure(data.inner.data, mode, p, offset, whence)
|
||||
}
|
||||
}
|
||||
|
||||
make_ansi_strip_writer :: proc(inner: io.Writer) -> io.Writer {
|
||||
data := new(ANSI_Strip_Data, context.temp_allocator)
|
||||
data.inner = inner
|
||||
return io.Writer{procedure = ansi_strip_proc, data = rawptr(data)}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user