diff --git a/app/main.go b/app/main.go index 00bbfa7..18ccae2 100644 --- a/app/main.go +++ b/app/main.go @@ -4,6 +4,7 @@ import ( "ansi" "errs/stack" "fmt" + "os" ) @@ -44,80 +45,15 @@ func (c MiCustomErr) Unwrap () error { func main() { - /* - for c := ftext.ColorBlack; c <= ftext.ColorWhite; c++ { - fmt.Print(c.SprintfColor("Code %d %s ******\n",c,c.Name())) - } - - - fmt.Printf("\n \x1b[32;43m sdasd \x1b[0m \n" ) - fmt.Printf("\n \x1b[92;43m sdasd \x1b[0m \n" ) - fmt.Printf("\n \x1b[1;4;32;43m sdasd \x1b[0m \n" ) - fmt.Printf("\n \x1b[1;4;92;103m sdasd \x1b[0m \n" ) - fmt.Printf("\n \x1b[1;4;5;92;43m sdasd \x1b[0m \n" ) - - */ - - c := ansi.ColorBlue.SetBgColor(ansi.ColorGreen).SetBlink(true) - //fg,bg := c.Colors() - fmt.Printf("Con attributos %s con klinkin con colores %s:%s\n",c.Atts(),c.FgColorName(),c.BgColorName()) - - fmt.Printf("%sEsto es una muestra%s\n",c.Escape(),c.Atts()) - c = c.Clear() - fmt.Printf("Con attributos %s con klinkin con colores %s:%s\n",c.Atts(),c.FgColorName(),c.BgColorName()) - /* - fmt.Print(c.Escape()) - fmt.Printf("Con attributos %s Con Underline \n",c.Atts()) - c = c.SetUnderlineOff().SetBoldOn() - fmt.Print(c.Escape()) - fmt.Printf("Con attributos %s Sin Underline \n",c.Atts()) - c = c.SetBlinkOn().SetBackground(ansi.ColorCyan) - fmt.Print(c.Escape()) - fmt.Printf("Con attributos %s con klinkin \n",c.Atts()) - c = c.SetBlinkOff() - fmt.Print(c.Escape()) - fmt.Printf("Con attributos %s con klinkin \n",c.Atts()) - - - c = c.SetBlinkOff() - fmt.Print(c.Escape()) + c := ansi.ColorRed.SetBgColor(ansi.ColorHWhite).SetBlink(true) + fmt.Printf("Attributes %s\n",c.Atts()) + s := fmt.Sprintf("%sColor Red y White \n",c.Escape()) - fmt.Printf("Con attributos %s con klinkin con colores %s:%s\n",c.Atts(),fg.ForegroundName(),bg.BackgroundName()) - - - */ - /* - ercode := errs.ErrCode("error->err-code") - //fmt.Print(errs.ToFormat(ercode,nil)) - //fmt.Print("\n") - - erpanic := errs.NewErrPanic(errs.PanicRecoveredError("error->err-mensaje-panico")) - fmt.Print(errs.ToFormat(erpanic,nil)) - fmt.Print("\n") - - panicInfo := erpanic.WrapWithInfo(errs.ErrInfoWithMessage("Un mensaje de panico")) - fmt.Print(errs.ToFormat(panicInfo,nil)) - fmt.Print("\n") - - ercause := ercode.WrapWithCause(errs.ErrCode("causa-2").WrapWithCause(errs.ErrCode("causa-1"))) - fmt.Print(errs.ToFormat(ercause,nil)) - fmt.Print("\n") - - erInfo := errs.ErrCode("err-code-with-info").WrapWithInfo(errs.ErrInfoWithMessage("Esto es un mensaje")) - fmt.Print(errs.ToFormat(erInfo,nil)) - fmt.Print("\n") - - fmt.Print(string(errs.G.Formatter.Format(&MiCustomErr{error:errs.ErrCode("UnErrorCU") }))) - fmt.Print("\n") -*/ -// fmt.Print(errs.ToFormat(errs.NewErrParam("MiParamatro","invalid paearmtro %s con valor %v","MiParam",343).WrapWithTrace(2),nil)) + w := ansi.NewNonEscapeWriter(os.Stdout) + os.Stdout.WriteString(s) - /* - ertrace := errs.ErrCode("err-code-with-trace").ToErrTrace(0) - fmt.Print(ertrace.StringColor()) - fmt.Print("\n") - */ + w.Write([]byte(s)) } diff --git a/core/ansi/ansi_color.go b/core/ansi/ansi_color.go deleted file mode 100644 index 70df08a..0000000 --- a/core/ansi/ansi_color.go +++ /dev/null @@ -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...)...)) -} - -*/ \ No newline at end of file diff --git a/core/ansi/color/color.go b/core/ansi/color/color.go deleted file mode 100644 index a588524..0000000 --- a/core/ansi/color/color.go +++ /dev/null @@ -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 -) - - diff --git a/core/ansi/non_escape_writer.go b/core/ansi/non_escape_writer.go new file mode 100644 index 0000000..c9d7703 --- /dev/null +++ b/core/ansi/non_escape_writer.go @@ -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 +} +