You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
141 lines
3.6 KiB
141 lines
3.6 KiB
// ------------------------------------------------------------------------
|
|
// Project active2
|
|
// Active Thing (activething.com) git.activething.com/go
|
|
//
|
|
// File n active.go
|
|
// Created by DEV
|
|
// Modified 05/04/2024
|
|
//
|
|
// Copyright 2024 activething.com
|
|
// ------------------------------------------------------------------------
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
// ------------------------------------------------------------------------
|
|
|
|
package ansi
|
|
|
|
import (
|
|
"bytes"
|
|
)
|
|
|
|
|
|
const (
|
|
|
|
styleAttsStart Code = 1024
|
|
|
|
bgMask Code = 0b1111110000011111
|
|
fgMask Code = 0b1111111111100000
|
|
clMask Code = 0b0000001111111111
|
|
txMask Code = 0b1111110000000000
|
|
bgShift int = 5
|
|
stShift int = 10
|
|
styleNumOps int = 3
|
|
|
|
)
|
|
|
|
|
|
var (
|
|
|
|
g = struct {
|
|
beginEscIni []byte
|
|
beginEscEnd []byte
|
|
endEsc []byte
|
|
charsEscs []byte
|
|
endEscStr string
|
|
|
|
ics []struct { n []byte; f []byte; b []byte }
|
|
itx []struct { n []byte; s []byte }
|
|
}{
|
|
|
|
beginEscIni : []byte("\x1b["),
|
|
beginEscEnd : []byte{'m'},
|
|
endEsc : []byte("\x1b[0m"),
|
|
endEscStr : "\x1b[0m",
|
|
|
|
ics : []struct { n []byte; f []byte; b []byte } {
|
|
{[]byte("none"), []byte{},[]byte{}},
|
|
{[]byte("black"), []byte{'3','0'},[]byte{'4','0'}},
|
|
{[]byte("red"), []byte{'3','1'},[]byte{'4','1'}},
|
|
{[]byte("green"), []byte{'3','2'},[]byte{'4','2'}},
|
|
{[]byte("yellow"), []byte{'3','3'},[]byte{'4','3'}},
|
|
{[]byte("blue"), []byte{'3','4'},[]byte{'4','4'}},
|
|
{[]byte("magenta"), []byte{'3','5'},[]byte{'4','5'}},
|
|
{[]byte("cyan"), []byte{'3','6'},[]byte{'4','6'}},
|
|
{[]byte("white"), []byte{'3','7'},[]byte{'4','7'}},
|
|
{[]byte("hblack"), []byte{'9','0'},[]byte{'1','0','0'}},
|
|
{[]byte("hred"), []byte{'9','1'},[]byte{'1','0','1'}},
|
|
{[]byte("hgreen"), []byte{'9','2'},[]byte{'1','0','2'}},
|
|
{[]byte("hyellow"), []byte{'9','3'},[]byte{'1','0','3'}},
|
|
{[]byte("hblue"), []byte{'9','4'},[]byte{'1','0','4'}},
|
|
{[]byte("hmagent"), []byte{'9','5'},[]byte{'1','0','5'}},
|
|
{[]byte("hcyan"), []byte{'9','6'},[]byte{'1','0','6'}},
|
|
{[]byte("hwhite"), []byte{'9','7'},[]byte{'1','0','7'}},
|
|
{[]byte("default"), []byte{'3','7'},[]byte{'4','9'}},
|
|
},
|
|
|
|
itx : []struct { n []byte; s []byte }{
|
|
{[]byte("bold-on"), []byte{'1'}},
|
|
{[]byte("underline-on"), []byte{'4'}},
|
|
{[]byte("blink-on"), []byte{'5'}},
|
|
{[]byte("bold-off"), []byte{'2','2'}},
|
|
{[]byte("underline-off"),[]byte{'2','5'}},
|
|
{[]byte("blink-off"), []byte{'2','7'}},
|
|
},
|
|
|
|
charsEscs : genCharEscapes(),
|
|
}
|
|
|
|
)
|
|
|
|
|
|
func genCharEscapes () []byte {
|
|
r := make([]byte,255)
|
|
r['@']=1
|
|
for i:=int('A'); i<=int('Z'); i++ {
|
|
r[i]=1
|
|
r[i+32]=1
|
|
}
|
|
return r
|
|
}
|
|
|
|
|
|
func Esc (code Code) string {
|
|
return code.Begin() }
|
|
|
|
func EscEnd () string {
|
|
return g.endEscStr }
|
|
|
|
|
|
func FilterEscapes (data []byte) []byte {
|
|
var buf bytes.Buffer
|
|
|
|
for i := 0; i < len(data); {
|
|
if data[i] != 0x1b {
|
|
buf.WriteByte(data[i])
|
|
i++
|
|
continue
|
|
}
|
|
// Si encuentra una secuencia de escape, salta hasta encontrar un carácter alfabético.
|
|
next := i + 1
|
|
for next < len(data) && g.charsEscs[data[next]] == 0 {
|
|
next++
|
|
}
|
|
i = next
|
|
}
|
|
|
|
return buf.Bytes()
|
|
}
|
|
|
|
|
|
|
|
|