parent
3faea971c4
commit
f48a17de36
@ -1,306 +0,0 @@
|
||||
// ------------------------------------------------------------------------
|
||||
// Project active2
|
||||
// Active Thing (activething.com) git.activething.com/go
|
||||
//
|
||||
// File name ansi_color.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 (
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const (
|
||||
|
||||
ColorNone Code = iota
|
||||
ColorBlack
|
||||
ColorRed
|
||||
ColorGreen
|
||||
ColorYellow
|
||||
ColorBlue
|
||||
ColorMagenta
|
||||
ColorCyan
|
||||
ColorWhite
|
||||
ColorHBlack
|
||||
ColorHRed
|
||||
ColorHGreen
|
||||
ColorHYellow
|
||||
ColorHBlue
|
||||
ColorHMagenta
|
||||
ColorHCyan
|
||||
ColorHWhite
|
||||
ColorDefault
|
||||
)
|
||||
|
||||
const (
|
||||
|
||||
TextBoldOn Code = 1024 << iota
|
||||
TextUnderlineOn
|
||||
TextBlinkOn
|
||||
TextBoldOff
|
||||
TextUnderlineOff
|
||||
TextBlinkOff
|
||||
|
||||
//bgMask Code = 0b0000000000011111
|
||||
bgMask Code = 0b1111110000011111
|
||||
fgMask Code = 0b1111111111100000
|
||||
clMask Code = 0b0000001111111111
|
||||
txMask Code = 0b1111110000000000
|
||||
bgShift int = 5
|
||||
)
|
||||
|
||||
var (
|
||||
|
||||
|
||||
fc = [][]byte{
|
||||
{},
|
||||
{'3','0'},
|
||||
{'3','1'},
|
||||
{'3','2'},
|
||||
{'3','3'},
|
||||
{'3','4'},
|
||||
{'3','5'},
|
||||
{'3','6'},
|
||||
{'3','7'},
|
||||
{'9','0'},
|
||||
{'9','1'},
|
||||
{'9','2'},
|
||||
{'9','3'},
|
||||
{'9','4'},
|
||||
{'9','5'},
|
||||
{'9','6'},
|
||||
{'9','7'},
|
||||
{'3','9'},
|
||||
}
|
||||
bc = [][]byte{
|
||||
{},
|
||||
{'4','0'},
|
||||
{'4','1'},
|
||||
{'4','2'},
|
||||
{'4','3'},
|
||||
{'4','4'},
|
||||
{'4','5'},
|
||||
{'4','6'},
|
||||
{'4','7'},
|
||||
{'1','0','0'},
|
||||
{'1','0','1'},
|
||||
{'1','0','2'},
|
||||
{'1','0','3'},
|
||||
{'1','0','4'},
|
||||
{'1','0','5'},
|
||||
{'1','0','6'},
|
||||
{'1','0','7'},
|
||||
{'4','9'},
|
||||
}
|
||||
tx = [][]byte{
|
||||
{'1'},
|
||||
{'4'},
|
||||
{'5'},
|
||||
{'2','1'},
|
||||
{'2','4'},
|
||||
{'2','5'}}
|
||||
|
||||
colorNames = []string {
|
||||
"none",
|
||||
"black",
|
||||
"red",
|
||||
"green",
|
||||
"yellow",
|
||||
"blue",
|
||||
"magenta",
|
||||
"cyan",
|
||||
"white",
|
||||
"hblack",
|
||||
"hred",
|
||||
"hgreen",
|
||||
"hyellow",
|
||||
"hblue",
|
||||
"hmagenta",
|
||||
"hcyan",
|
||||
"hwhite",
|
||||
"default",
|
||||
}
|
||||
|
||||
attNames = []string {
|
||||
"bold",
|
||||
"underline",
|
||||
"blink",
|
||||
"not-bold",
|
||||
"not-underline",
|
||||
"not-blink",
|
||||
}
|
||||
)
|
||||
|
||||
type Code uint16
|
||||
|
||||
|
||||
func (c Code) IsBoldOn () bool {
|
||||
return c & TextBoldOn != 0 }
|
||||
|
||||
func (c Code) IsBoldOff() bool {
|
||||
return c & TextBoldOff != 0 }
|
||||
|
||||
func (c Code) IsBlinkOn() bool {
|
||||
return c & TextBlinkOn != 0 }
|
||||
|
||||
func (c Code) IsBlinkOff() bool {
|
||||
return c & TextBlinkOff != 0 }
|
||||
|
||||
func (c Code) IsUnderlineOn () bool {
|
||||
return c & TextUnderlineOn != 0 }
|
||||
|
||||
func (c Code) IsUnderlineOff() bool {
|
||||
return c & TextUnderlineOff != 0 }
|
||||
|
||||
func (c Code) SetBoldOn () Code {
|
||||
return (c &^ TextBoldOff) | TextBoldOn }
|
||||
|
||||
func (c Code) SetBoldOff() Code {
|
||||
return (c &^ TextBoldOn) | TextBoldOff }
|
||||
|
||||
func (c Code) SetBlinkOn () Code {
|
||||
return (c &^ TextBlinkOff) | TextBlinkOn }
|
||||
|
||||
func (c Code) SetBlinkOff() Code {
|
||||
return (c &^ TextBlinkOn) | TextBlinkOff }
|
||||
|
||||
func (c Code) SetUnderlineOn () Code {
|
||||
return (c &^ TextUnderlineOff) | TextUnderlineOn }
|
||||
|
||||
func (c Code) SetUnderlineOff() Code {
|
||||
return (c &^ TextUnderlineOn) | TextUnderlineOff }
|
||||
|
||||
|
||||
func (c Code) Atts () string {
|
||||
if c == 0 {
|
||||
return ""
|
||||
}
|
||||
var r []string
|
||||
m := c & txMask
|
||||
if m != 0 {
|
||||
dc := TextBoldOn
|
||||
for i:=0; i<6; i++ {
|
||||
if m & dc != 0 {
|
||||
r = append(r,attNames[i])
|
||||
}
|
||||
dc <<= 1
|
||||
}
|
||||
}
|
||||
return strings.Join(append(r,c.ForegroundName(),c.BackgroundName()),";")
|
||||
}
|
||||
|
||||
|
||||
|
||||
func (c Code) SetColors(fg, bg Code) Code {
|
||||
return fg.SetForeground(fg).SetBackground(bg) }
|
||||
|
||||
func (c Code) Colors() (fg, bg Code) {
|
||||
return (c & clMask) & bgMask, ((c & clMask) & fgMask) >> bgShift }
|
||||
|
||||
|
||||
|
||||
func (c Code) FgColor() Code {
|
||||
return (c & clMask) & bgMask }
|
||||
|
||||
func (c Code) SetForeground(fg Code) Code {
|
||||
return (c & fgMask) | fg & bgMask }
|
||||
|
||||
func (c Code) ForegroundName () string {
|
||||
return colorNames[c.FgColor()] }
|
||||
|
||||
|
||||
func (c Code) Background() Code {
|
||||
h := ((c & clMask) & fgMask) >> bgShift
|
||||
return h
|
||||
//return ((c & clMask) & fgMask) >> bgShift
|
||||
}
|
||||
|
||||
func (c Code) SetBackground(bg Code) Code {
|
||||
return (c & bgMask) | (bg << bgShift) & fgMask }
|
||||
|
||||
func (c Code) BackgroundName () string {
|
||||
return colorNames[c.Background()] }
|
||||
|
||||
|
||||
|
||||
func (c Code) scapeText () []byte {
|
||||
m := c & txMask
|
||||
if m == 0 {
|
||||
return nil
|
||||
}
|
||||
var r []byte
|
||||
dc := TextBoldOn
|
||||
for i:=0; i<6; i++ {
|
||||
if m & dc != 0 {
|
||||
if r != nil {
|
||||
r = append(r,';')
|
||||
}
|
||||
r = append(r,tx[i]...)
|
||||
}
|
||||
dc <<= 1
|
||||
}
|
||||
return r
|
||||
}
|
||||
|
||||
|
||||
func (c Code) scapeColor () []byte {
|
||||
m := c & clMask
|
||||
if m == 0 {
|
||||
return nil
|
||||
}
|
||||
var r []byte
|
||||
f := m & bgMask
|
||||
b := (m & fgMask) >> bgShift
|
||||
if f != 0 {
|
||||
r = append(r,fc[f]...)
|
||||
}
|
||||
if b != 0 {
|
||||
if f > 0 {
|
||||
r=append(r,';')
|
||||
}
|
||||
r = append(r,bc[b]...)
|
||||
}
|
||||
return r
|
||||
}
|
||||
|
||||
func (c Code) escape () []byte {
|
||||
if c == 0 {
|
||||
return []byte{'0'}
|
||||
}
|
||||
st := c.scapeText ()
|
||||
sc := c.scapeColor()
|
||||
if len(sc) == 0 {
|
||||
return st
|
||||
}
|
||||
if len(st) == 0 {
|
||||
return sc
|
||||
}
|
||||
return append(st,append([]byte{';'},sc...)...)
|
||||
}
|
||||
|
||||
|
||||
func (c Code) Escape() string {
|
||||
r := c.escape()
|
||||
fmt.Printf("Escape all %s \n",string(r))
|
||||
return string(append(G.beginEscape, append(r, G.endEscape...)...))
|
||||
}
|
||||
|
||||
*/
|
||||
@ -1,95 +0,0 @@
|
||||
// ------------------------------------------------------------------------
|
||||
// Project active2
|
||||
// Active Thing (activething.com) git.activething.com/go
|
||||
//
|
||||
// File name color.go
|
||||
// Created by DEV
|
||||
// Modified 04/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 color
|
||||
|
||||
|
||||
const (
|
||||
|
||||
ColorTextOff __Color = 0
|
||||
|
||||
ColorTextBold __Color = 1
|
||||
ColorTextUnderline __Color = 4
|
||||
ColorTextBlink __Color = 5
|
||||
|
||||
ColorTextBoldOff __Color = 21
|
||||
ColorTextUnderlineOff __Color = 24
|
||||
ColorTextBlinkOff __Color = 25
|
||||
|
||||
|
||||
ColorFBlack __Color = 30
|
||||
ColorFRed __Color = 31
|
||||
ColorFGreen __Color = 32
|
||||
ColorFYellow __Color = 33
|
||||
ColorFBlue __Color = 34
|
||||
ColorFMagenta __Color = 35
|
||||
ColorFCyan __Color = 36
|
||||
ColorFWhite __Color = 37
|
||||
|
||||
ColorFDefault __Color = 39
|
||||
|
||||
ColorFLGray __Color = 90
|
||||
ColorFLRed __Color = 91
|
||||
ColorFLGreen __Color = 92
|
||||
ColorFLYellow __Color = 93
|
||||
ColorFLBlue __Color = 94
|
||||
ColorFLMagenta __Color = 95
|
||||
ColorFLCyan __Color = 96
|
||||
ColorFLWhite __Color = 97
|
||||
|
||||
|
||||
ColorBBlack __Color = 40
|
||||
ColorBRed __Color = 41
|
||||
ColorBGreen __Color = 42
|
||||
ColorBYellow __Color = 43
|
||||
ColorBBlue __Color = 44
|
||||
ColorBMagenta __Color = 45
|
||||
ColorBCyan __Color = 46
|
||||
ColorBWhite __Color = 47
|
||||
|
||||
ColorBDefault __Color = 49
|
||||
|
||||
ColorBLGray __Color = 100
|
||||
ColorBLRed __Color = 101
|
||||
ColorBLGreen __Color = 102
|
||||
ColorBLYellow __Color = 103
|
||||
ColorBLBlue __Color = 104
|
||||
ColorBLMagenta __Color = 105
|
||||
ColorBLCyan __Color = 106
|
||||
ColorBLWhite __Color = 107
|
||||
)
|
||||
|
||||
const (
|
||||
|
||||
ColorBeginSeq string = "\x1b["
|
||||
ColorMiddleSeq string = "m"
|
||||
ColorEndSeq string = "\x1b[0m"
|
||||
)
|
||||
|
||||
|
||||
type (
|
||||
|
||||
__Color uint16
|
||||
)
|
||||
|
||||
|
||||
@ -0,0 +1,88 @@
|
||||
// ------------------------------------------------------------------------
|
||||
// Project active2
|
||||
// Active Thing (activething.com) git.activething.com/go
|
||||
//
|
||||
// File name non_colorable_writer.go
|
||||
// Created by DEV
|
||||
// Modified 06/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"
|
||||
"io"
|
||||
)
|
||||
|
||||
type (
|
||||
|
||||
NonEscapeWriter struct {
|
||||
writer io.Writer
|
||||
}
|
||||
|
||||
)
|
||||
|
||||
|
||||
func NewNonEscapeWriter(w io.Writer) *NonEscapeWriter {
|
||||
return &NonEscapeWriter{ writer: w } }
|
||||
|
||||
|
||||
|
||||
func (w *NonEscapeWriter) Write (data []byte) (c int, e error) {
|
||||
var buf bytes.Buffer
|
||||
er := bytes.NewReader(data)
|
||||
|
||||
for {
|
||||
c, err := er.ReadByte()
|
||||
if err == io.EOF {
|
||||
break
|
||||
}
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
if c != 0x1b {
|
||||
buf.WriteByte(c)
|
||||
continue
|
||||
}
|
||||
|
||||
// Si encuentra una secuencia de escape, salta hasta encontrar un carácter alfabético.
|
||||
for {
|
||||
c, err := er.ReadByte()
|
||||
if err == io.EOF {
|
||||
break
|
||||
}
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
// Si encuentra un carácter alfabético, sale del bucle.
|
||||
if ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z') || c == '@' {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Escribe el contenido sin secuencias de escape en el escritor de salida.
|
||||
n, err := w.writer.Write(buf.Bytes())
|
||||
if err != nil {
|
||||
return n, err
|
||||
}
|
||||
|
||||
return len(data), nil
|
||||
}
|
||||
|
||||
Loading…
Reference in new issue