From 6c058bb0903fdcd5366e4e67d93b4ef7c816c349 Mon Sep 17 00:00:00 2001 From: DEV Date: Sun, 7 Apr 2024 02:47:41 +0200 Subject: [PATCH] incluidas las librerias de transformacion de strings --- app/code.gold | 241 +++++++++++++++++++++ app/code.gold2 | 231 ++++++++++++++++++++ app/main.go | 27 ++- core/ansi/active.go | 66 +++--- core/ansi/code-text-info.go | 35 --- core/ansi/code.go | 93 ++++---- core/ansi/{code_color.go => color.go} | 18 +- core/ansi/non_escape_reader.go | 2 +- core/ansi/non_escape_writer.go | 34 ++- core/ansi/{code-color-info.go => style.go} | 25 ++- core/bins/active.go | 16 +- core/errs/active.go | 11 +- 12 files changed, 655 insertions(+), 144 deletions(-) create mode 100644 app/code.gold create mode 100644 app/code.gold2 delete mode 100644 core/ansi/code-text-info.go rename core/ansi/{code_color.go => color.go} (81%) rename core/ansi/{code-color-info.go => style.go} (80%) diff --git a/app/code.gold b/app/code.gold new file mode 100644 index 0000000..580e745 --- /dev/null +++ b/app/code.gold @@ -0,0 +1,241 @@ +// ------------------------------------------------------------------------ +// Project active2 +// Active Thing (activething.com) git.activething.com/go +// +// File name code.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 "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 +) + + +type ( + + Code uint16 +) + + +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,string(g.itx[i].name)) + } + dc <<= 1 + } + } + r = append(r,strings.Join([]string{c.FgColorName(),c.BgColorName()},":")) + return strings.Join(r,";") +} + + + +func (c Code) Clear () Code { + return 0 } + +func (c Code) ClearColor () Code { + return c & txMask } + +func (c Code) ClearText () Code { + return c & clMask } + + +func (c Code) FgColor() Code { + return (c & clMask) & bgMask } + +func (c Code) SetFgColor(color Code) Code { + return (c & fgMask) | color& bgMask } + +func (c Code) FgColorName () string { + return string(g.ics[(c & clMask) & bgMask].name) } + + +func (c Code) BgColor() Code { + return ((c & clMask) & fgMask) >> bgShift } + +func (c Code) SetBgColor(color Code) Code { + return (c & bgMask) | (color<< bgShift) & fgMask } + +func (c Code) BgColorName () string { + return string(g.ics[((c & clMask) & fgMask) >> bgShift].name) } + + + +func (c Code) SetColors(fg, bg Code) Code { + return c.SetFgColor(fg).SetBgColor(bg) } + + + + + +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) SetBold (on bool) Code { + if on { + return (c &^ TextBoldOff) | TextBoldOn + } + return (c &^ TextBoldOn) | TextBoldOff +} + +func (c Code) SetBlink (on bool) Code { + if on { + return (c &^ TextBlinkOff) | TextBlinkOn + } + return (c &^ TextBlinkOn) | TextBlinkOff +} + +func (c Code) SetUnderline (on bool) Code { + if on { + return (c &^ TextUnderlineOff) | TextUnderlineOn + } + return (c &^ TextUnderlineOn) | TextUnderlineOff +} + + + +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, g.itx[i].esc...) + } + 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, g.ics[f].fgEsc...) + } + if b != 0 { + if f > 0 { + r=append(r,';') + } + r = append(r, g.ics[b].bgEsc...) + } + 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 { + if c == 0 { + return "" + } + r := c.escape() + return string(append(g.beginEscIni, append(r, g.beginEscEnd...)...)) +} diff --git a/app/code.gold2 b/app/code.gold2 new file mode 100644 index 0000000..74ded16 --- /dev/null +++ b/app/code.gold2 @@ -0,0 +1,231 @@ +// ------------------------------------------------------------------------ +// Project active2 +// Active Thing (activething.com) git.activething.com/go +// +// File n code.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 "strings" + + +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 +) + + +type ( + + Code uint16 +) + + +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,string(g.itx[i].n)) + } + dc <<= 1 + } + } + r = append(r,strings.Join([]string{c.FgColor().Name(),c.BgColor().Name()},":")) + return strings.Join(r,";") +} + + + +func (c Code) Clear () Code { + return 0 } + +func (c Code) ClearColor () Code { + return c & txMask } + +func (c Code) ClearText () Code { + return c & clMask } + + +func (c Code) FgColor() Color { + return Color((c & clMask) & bgMask) } + +func (c Code) SetFgColor(color Color) Code { + if !color.IsValid() { + return c + } + return (c & fgMask) | Code(color)& bgMask +} + +func (c Code) ResetFgColor() Code { + return (c & fgMask) | Code(colorDefault)& bgMask } + + +func (c Code) BgColor() Color { + return Color(((c & clMask) & fgMask) >> bgShift) } + +func (c Code) SetBgColor(color Color) Code { + if !color.IsValid() { + return c + } + return (c & bgMask) | (Code(color)<< bgShift) & fgMask +} + +func (c Code) ResetBgColor() Code { + return (c & bgMask) | (Code(colorDefault)<< bgShift) & fgMask } + + + + +func (c Code) SetColors(fg, bg Color) Code { + return c.SetFgColor(fg).SetBgColor(bg) } + + + + + +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) SetBold (on bool) Code { + if on { + return (c &^ TextBoldOff) | TextBoldOn + } + return (c &^ TextBoldOn) | TextBoldOff +} + +func (c Code) SetBlink (on bool) Code { + if on { + return (c &^ TextBlinkOff) | TextBlinkOn + } + return (c &^ TextBlinkOn) | TextBlinkOff +} + +func (c Code) SetUnderline (on bool) Code { + if on { + return (c &^ TextUnderlineOff) | TextUnderlineOn + } + return (c &^ TextUnderlineOn) | TextUnderlineOff +} + + + +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,';') + } else { + r = append(r,'0',';') + } + r = append(r, g.itx[i].s...) + } + 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, g.ics[f].f...) + } + if b != 0 { + if f > 0 { + r=append(r,';') + } + r = append(r, g.ics[b].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) EscapeValues () string { + return string(c.escape()) +} + +func (c Code) Esc() string { + if c == 0 { + return g.endEscStr + } + r := c.escape() + return string(append(g.beginEscIni, append(r, g.beginEscEnd...)...)) +} + diff --git a/app/main.go b/app/main.go index 4d38758..d7278b6 100644 --- a/app/main.go +++ b/app/main.go @@ -59,18 +59,29 @@ func (c MiCustomErr) Unwrap () error { func main() { - result := GenerateBinaryString() - fmt.Println(result) + c1 := ansi.Code(0).SetBold(true) + c2 := c1.SetBold(false) + //fmt.Printf("C1: %s C2: %s\n",c1.EscapeValues(),c2.EscapeValues()) + fmt.Printf("%sCon Atributo Bold -> %ssin atributo Bold\n",c1.Esc(),c2.Esc()) - c := ansi.ColorRed.SetBgColor(ansi.ColorHWhite).SetBlink(true).SetBold(true) - fmt.Printf("Attributes %s\n",c.Atts()) - s := fmt.Sprintf("%sColor Red y White \n",c.Escape()) + c1 = c1.Clear().SetUnderline(true) + c2 = c1.SetUnderline(false) + fmt.Printf("C1: %s C2: %s\n",c1.EscapeValues(),c2.EscapeValues()) + fmt.Printf("%sCon Atributo -> %ssin atributo \n",c1.Esc(),c2.Esc()) - sf := ansi.FilterEscapes([]byte(s)) + c1 = c1.Clear().SetBlink(true).SetBgColor(ansi.ColorRed) - fmt.Printf("Filtrados %s\n",sf) - fmt.Printf("No filtrados %s\n",s) + c1 = c1.Clear().SetBlink(false).SetFgColor(ansi.ColorGreen) + fmt.Printf("C1: %s \n",c1.EscapeValues()) + fmt.Println(c1) + c1 = c1.SetStyle(ansi.StyleBold) + fmt.Printf("C1: %s \n",c1.EscapeValues()) + fmt.Println(c1) + + c1 = c1.SetStyle(ansi.StyleBlink) + fmt.Printf("C1: %s \n",c1.EscapeValues()) + fmt.Println(c1) } diff --git a/core/ansi/active.go b/core/ansi/active.go index 5617322..fba6bb5 100644 --- a/core/ansi/active.go +++ b/core/ansi/active.go @@ -2,7 +2,7 @@ // Project active2 // Active Thing (activething.com) git.activething.com/go // -// File name active.go +// File n active.go // Created by DEV // Modified 05/04/2024 // @@ -36,8 +36,8 @@ var ( charsEscs []byte endEscStr string - ics []colorInfo - itx []textInfo + ics []struct { n []byte; f []byte; b []byte } + itx []struct { n []byte; s []byte } }{ beginEscIni : []byte("\x1b["), @@ -45,37 +45,37 @@ var ( endEsc : []byte("\x1b[0m"), endEscStr : "\x1b[0m", - ics : []colorInfo{ - {[]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'}}, + 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 : []textInfo{ + 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','1'}}, - {[]byte("underline-off"), []byte{'2','4'}}, - {[]byte("blink-off"), []byte{'2','5'}}, + {[]byte("bold-off"), []byte{'2','2'}}, + {[]byte("underline-off"),[]byte{'2','5'}}, + {[]byte("blink-off"), []byte{'2','7'}}, }, - charsEscs: genCharEscapes(), + charsEscs : genCharEscapes(), } ) @@ -83,21 +83,19 @@ var ( func genCharEscapes () []byte { r := make([]byte,255) - for i:=int('a'); i<=int('c'); i++ { - r[i]=1 - } + r['@']=1 for i:=int('A'); i<=int('Z'); i++ { r[i]=1 + r[i+32]=1 } - r[int('@')]=1 return r } -func BeginEscape (code Code) string { - return code.Escape() } +func Esc (code Code) string { + return code.Esc() } -func EndEscape () string { +func EscEnd () string { return g.endEscStr } diff --git a/core/ansi/code-text-info.go b/core/ansi/code-text-info.go deleted file mode 100644 index ad7cdbc..0000000 --- a/core/ansi/code-text-info.go +++ /dev/null @@ -1,35 +0,0 @@ -// ------------------------------------------------------------------------ -// Project active2 -// Active Thing (activething.com) git.activething.com/go -// -// File name text-info.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 - - - -type ( - - textInfo struct { - name []byte - esc []byte - } -) - diff --git a/core/ansi/code.go b/core/ansi/code.go index c887a6e..257515a 100644 --- a/core/ansi/code.go +++ b/core/ansi/code.go @@ -2,7 +2,7 @@ // Project active2 // Active Thing (activething.com) git.activething.com/go // -// File name code.go +// File n code.go // Created by DEV // Modified 06/04/2024 // @@ -23,30 +23,11 @@ package ansi -import "strings" - -const ( - - ColorNone Code = iota - ColorBlack - ColorRed - ColorGreen - ColorYellow - ColorBlue - ColorMagenta - ColorCyan - ColorWhite - ColorHBlack - ColorHRed - ColorHGreen - ColorHYellow - ColorHBlue - ColorHMagenta - ColorHCyan - ColorHWhite - ColorDefault +import ( + "strings" ) + const ( TextBoldOn Code = 1024 << iota @@ -61,7 +42,8 @@ const ( fgMask Code = 0b1111111111100000 clMask Code = 0b0000001111111111 txMask Code = 0b1111110000000000 - bgShift int = 5 + bgShift int = 5 + stShift int = 10 ) @@ -81,12 +63,12 @@ func (c Code) Atts () string { dc := TextBoldOn for i:=0; i<6; i++ { if m & dc != 0 { - r = append(r,string(g.itx[i].name)) + r = append(r,string(g.itx[i].n)) } dc <<= 1 } } - r = append(r,strings.Join([]string{c.FgColorName(),c.BgColorName()},":")) + r = append(r,strings.Join([]string{c.FgColor().Name(),c.BgColor().Name()},":")) return strings.Join(r,";") } @@ -102,32 +84,49 @@ func (c Code) ClearText () Code { return c & clMask } -func (c Code) FgColor() Code { - return (c & clMask) & bgMask } +func (c Code) FgColor() Color { + return Color((c & clMask) & bgMask) } -func (c Code) SetFgColor(color Code) Code { - return (c & fgMask) | color& bgMask } +func (c Code) SetFgColor(color Color) Code { + if !color.IsValid() { + return c + } + return (c & fgMask) | Code(color)& bgMask +} -func (c Code) FgColorName () string { - return string(g.ics[(c & clMask) & bgMask].name) } +func (c Code) ResetFgColor() Code { + return (c & fgMask) | Code(colorDefault)& bgMask } -func (c Code) BgColor() Code { - return ((c & clMask) & fgMask) >> bgShift } +func (c Code) BgColor() Color { + return Color(((c & clMask) & fgMask) >> bgShift) } -func (c Code) SetBgColor(color Code) Code { - return (c & bgMask) | (color<< bgShift) & fgMask } +func (c Code) SetBgColor(color Color) Code { + if !color.IsValid() { + return c + } + return (c & bgMask) | (Code(color)<< bgShift) & fgMask +} + +func (c Code) ResetBgColor() Code { + return (c & bgMask) | (Code(colorDefault)<< bgShift) & fgMask } -func (c Code) BgColorName () string { - return string(g.ics[((c & clMask) & fgMask) >> bgShift].name) } -func (c Code) SetColors(fg, bg Code) Code { +func (c Code) SetColors(fg, bg Color) Code { return c.SetFgColor(fg).SetBgColor(bg) } +func (c Code) SetStyle (sty Style) Code { + v := Code(sty) + s := c >> 10 + if s != 0 { + v = (((s >> 3) & ^ v) << 3) | v + } + return v << 10 | (c & clMask) +} func (c Code) IsBoldOn () bool { @@ -182,8 +181,10 @@ func (c Code) scapeText () []byte { if m & dc != 0 { if r != nil { r = append(r,';') + } else { + r = append(r,'0',';') } - r = append(r, g.itx[i].esc...) + r = append(r, g.itx[i].s...) } dc <<= 1 } @@ -200,13 +201,13 @@ func (c Code) scapeColor () []byte { f := m & bgMask b := (m & fgMask) >> bgShift if f != 0 { - r = append(r, g.ics[f].fgEsc...) + r = append(r, g.ics[f].f...) } if b != 0 { if f > 0 { r=append(r,';') } - r = append(r, g.ics[b].bgEsc...) + r = append(r, g.ics[b].b...) } return r } @@ -227,11 +228,15 @@ func (c Code) escape () []byte { return append(st,append([]byte{';'},sc...)...) } +func (c Code) EscapeValues () string { + return string(c.escape()) +} -func (c Code) Escape() string { +func (c Code) Esc() string { if c == 0 { - return "" + return g.endEscStr } r := c.escape() return string(append(g.beginEscIni, append(r, g.beginEscEnd...)...)) } + diff --git a/core/ansi/code_color.go b/core/ansi/color.go similarity index 81% rename from core/ansi/code_color.go rename to core/ansi/color.go index 57088f9..acd241b 100644 --- a/core/ansi/code_color.go +++ b/core/ansi/color.go @@ -2,7 +2,7 @@ // Project active2 // Active Thing (activething.com) git.activething.com/go // -// File name code_color.go +// File n code_color.go // Created by DEV // Modified 06/04/2024 // @@ -26,7 +26,7 @@ package ansi const ( - ColorNone AnsiColor = iota + ColorNone Color = iota ColorBlack ColorRed ColorGreen @@ -43,13 +43,23 @@ const ( ColorHMagenta ColorHCyan ColorHWhite - ColorDefault + colorDefault ) type ( - AnsiColor uint8 + Color uint8 ) +func (c Color) Name () string { + if c.IsValid() { + return string(g.ics[c].n) + } + return string(g.ics[0].n) +} + +func (c Color) IsValid () bool { + return c >= ColorNone && c <= colorDefault +} \ No newline at end of file diff --git a/core/ansi/non_escape_reader.go b/core/ansi/non_escape_reader.go index 388ccc9..3c3a159 100644 --- a/core/ansi/non_escape_reader.go +++ b/core/ansi/non_escape_reader.go @@ -2,7 +2,7 @@ // Project active2 // Active Thing (activething.com) git.activething.com/go // -// File name non_escape_reader.go +// File n non_escape_reader.go // Created by DEV // Modified 06/04/2024 // diff --git a/core/ansi/non_escape_writer.go b/core/ansi/non_escape_writer.go index 2771123..453d218 100644 --- a/core/ansi/non_escape_writer.go +++ b/core/ansi/non_escape_writer.go @@ -2,7 +2,7 @@ // Project active2 // Active Thing (activething.com) git.activething.com/go // -// File name non_colorable_writer.go +// File n non_colorable_writer.go // Created by DEV // Modified 06/04/2024 // @@ -24,6 +24,7 @@ package ansi import ( + "bytes" "io" ) @@ -41,6 +42,33 @@ func NewNonEscapeWriter(w io.Writer) *NonEscapeWriter { -func (w *NonEscapeWriter) Write (data []byte) (c int, e error) { - return w.writer.Write(FilterEscapes(data)) } +func (w *NonEscapeWriter) Write(data []byte) (n int, e error) { + var pt bytes.Buffer + var ef bool + for _, b := range data { + if b == 0x1b { + ef = true + continue + } + + if ef && b == 0x5b { + ef = false + continue + } + + if ef { + ef = g.charsEscs[b] == 0 + continue + } + + pt.WriteByte(b) + } + + _, e = pt.WriteTo(w.writer) + if e != nil { + return n, e + } + + return len(data), nil +} \ No newline at end of file diff --git a/core/ansi/code-color-info.go b/core/ansi/style.go similarity index 80% rename from core/ansi/code-color-info.go rename to core/ansi/style.go index 7b5b64b..ad019ff 100644 --- a/core/ansi/code-color-info.go +++ b/core/ansi/style.go @@ -2,7 +2,7 @@ // Project active2 // Active Thing (activething.com) git.activething.com/go // -// File name color.go +// File name style.go // Created by DEV // Modified 06/04/2024 // @@ -23,17 +23,28 @@ package ansi +const ( + StyleBold Style = 1 << iota + StyleUnderline + StyleBlink + //StyleNonBold + //StyleNonUnderline + //StyleNonBlink -type ( + StyleNone Style = 0 +) - colorInfo struct { - name []byte - fgEsc []byte - bgEsc []byte +type ( - } + Style uint8 ) + + +func (s Style) IsValid () bool { + return true +} + diff --git a/core/bins/active.go b/core/bins/active.go index 60ee8ea..a3d6f2b 100644 --- a/core/bins/active.go +++ b/core/bins/active.go @@ -23,6 +23,8 @@ package bins +import "errs" + const ( minBitSize = 6 @@ -45,7 +47,7 @@ var ( ErrNegativeRead errs.ErrCode ErrUnreadInvalid errs.ErrCode - bufferPool *BufferPool + }{ ErrOutOfRange: errs.ErrCode("core:bins.out-of-range"), @@ -55,6 +57,14 @@ var ( ErrNegativeRead: errs.ErrCode("core:bins.negative-read"), ErrUnreadInvalid: errs.ErrCode("core:bins.unread-invalid"), + + } + + g = struct { + + bufferPool *BufferPool + + }{ bufferPool: NewBufferPool(), } ) @@ -62,9 +72,9 @@ var ( func AcquireBuffer () *Buffer { - return G.bufferPool.Get() } + return g.bufferPool.Get() } func ReleaseBuffer (buffer *Buffer) { - G.bufferPool.Put(buffer) } + g.bufferPool.Put(buffer) } diff --git a/core/errs/active.go b/core/errs/active.go index 7419e04..2b5c177 100644 --- a/core/errs/active.go +++ b/core/errs/active.go @@ -46,7 +46,6 @@ const ( var ( G = struct { - mux sync.RWMutex Formatter fmts.Formatter @@ -54,18 +53,20 @@ var ( ErrParamInvalid ErrCode ErrParamOutOfRange ErrCode - stackTraceRType reflect.Type }{ ErrParamNil : ErrCode("param-CodePanicError"), ErrParamInvalid : ErrCode("param-invalid"), ErrParamOutOfRange : ErrCode("param-out-of-range"), - - stackTraceRType : reflect.TypeOf(stack.Trace(nil)), - Formatter : ftext.DefaultFormatter(), + } + g = struct { + mux sync.RWMutex + stackTraceRType reflect.Type + }{ + stackTraceRType : reflect.TypeOf(stack.Trace(nil)), } )