incluidas las librerias de transformacion de strings

master
DEV 3 years ago
parent 272f7147ab
commit fb0c527744

@ -1,8 +1,8 @@
package main
import (
"ansi"
"bytes"
"errors"
"errs"
"errs/stack"
"fmt"
)
@ -60,16 +60,16 @@ func (c MiCustomErr) Unwrap () error {
func main() {
c1 := ansi.Code(0).SetStyle(ansi.StyleBold|ansi.StyleUnderline)
fmt.Printf("%s TexTo con Atts %s\n",c1,c1.End())
e := errs.ErrCode("Mi-Error")
i := errs.NewErrInfo(e,
errs.ErrInfoWithMessage("Esto es un mensage"),
errs.ErrInfoWithCause(errors.New("Un error desconocido")),
errs.ErrInfoWithStackTrace(2),
)
c1 = c1.DelStyle(ansi.StyleUnderline)
s:= fmt.Sprintf("%s TexTo con Atts %s\n",c1,c1.End())
fmt.Print(s)
b := &bytes.Buffer{}
w := ansi.NewNonEscapeWriter(b)
w.Write([]byte(s))
fmt.Print(string(b.Bytes()))
fmt.Print(i.String())
}

@ -194,7 +194,10 @@ func (c Code) String() string {
return string(append(g.beginEscIni, append(r, g.beginEscEnd...)...))
}
func (_ Code) End () string {
func (c Code) End () string {
if c == 0 {
return ""
}
return g.endEscStr }

@ -24,6 +24,7 @@
package ftext
import (
"ansi"
"atypes/astrings"
"errs/fmts"
"errs/stack"
@ -38,7 +39,7 @@ const (
CauseSection
TraceSection
secsSize= 4
secsSize= 5
)
const (
@ -65,37 +66,55 @@ var (
func DefaultFormatter () *FmtText {
G.buildDefault.Do(func() {
G.fmtDefault = NewFmtText(
func(f *FmtText) {
f.FixFieldLen = 13
},
WithFieldFormat("*",FieldFormat{
nameTrans: []astrings.Transform{ astrings.ToKebab },
nameTrans: []astrings.Transform{
astrings.ToKebab,
func(s string) string { return astrings.Pad(s,14,' ',true) },
},
}),
WithFieldFormat("error",FieldFormat{
nameColor: ColorGreen ,
nameAnsi : ansi.Code(0).SetFgColor(ansi.ColorGreen).SetStyle(ansi.StyleBold) ,
valueTrans : []astrings.Transform {astrings.ToKebab },
}),
WithErrorSection(
[]string{ColorRed.StringColor("**********************************************************")},
[]string{"**********************************************************"}, ColorMagenta),
WithCauseSection(
WithSection(
ErrorSection,
[]string{"**********************************************************"},
[]string{"**********************************************************"},
ansi.Code(0).SetFgColor(ansi.ColorHRed)),
WithSection(
CauseSection,
[]string{"- Prev Cause ---------------"},
[]string(nil), ColorBlue),
WithTraceSection(
[]string(nil),
ansi.Code(0).SetFgColor(ansi.ColorBlue)),
WithSection(
TraceSection,
[]string{"\n* Stack Trace Information **"},
[]string(nil), ColorMagenta))
[]string(nil),
ansi.Code(0).SetFgColor(ansi.ColorGreen)))
})
return G.fmtDefault
}
func defaultFrameBuilder (builder fmts.FormatBuilder, frame stack.Frame, num int) {
builder.AddString(ColorBlue.SprintfColor("%d %s.%s",num,frame.Package,frame.FuncName))
builder.AddString(ColorMagenta.SprintfColor(" %.90s:%d",frame.File,frame.LineNumber))
func buildDefaultFrameBuilder (f *FmtText) fmts.FrameFormatBuilder {
fm := f
return fmts.BuildFrameFormatFnc(func (builder fmts.FormatBuilder, frame stack.Frame, num int) {
var c1, c2, c3, c4 ansi.Code
if fm.ShowAnsi(){
c1 = c1.SetFgColor(ansi.ColorBlue)
c2 = c2.SetFgColor(ansi.ColorMagenta)
c3 = c3.SetFgColor(ansi.ColorYellow)
c4 = c4.SetFgColor(ansi.ColorRed)
}
builder.AddString(c1.Sprintf("%d %s.%s", num, frame.Package, frame.FuncName))
builder.AddString(c2.Sprintf(" %.90s:%d", frame.File, frame.LineNumber))
f, e := frame.SourceLine()
if e == nil {
builder.AddString(ColorYellow.SprintfColor("%s",f))
builder.AddString(c3.Sprintf("%s", f))
} else {
builder.AddString(ColorRed.SprintfColor("error %s", e.Error()))
builder.AddString(c4.Sprintf("error %s", e.Error()))
}
})
}

@ -31,8 +31,8 @@ import (
type (
FieldFormat struct {
nameColor ansi.Code
valueColor ansi.Code
nameAnsi ansi.Code
valueAnsi ansi.Code
nameTrans astrings.Transformers
valueTrans astrings.Transformers
}
@ -41,9 +41,11 @@ type (
func (f FieldFormat) Clone () FieldFormat {
return FieldFormat{
nameColor : f.nameColor,
valueColor: f.valueColor,
nameAnsi: f.nameAnsi,
valueAnsi: f.valueAnsi,
nameTrans : append(astrings.Transformers(nil),f.nameTrans...),
valueTrans: append(astrings.Transformers(nil),f.valueTrans...),
}
}

@ -27,6 +27,7 @@ import (
"ansi"
"atypes/astrings"
"errs/fmts"
"strings"
"time"
)
@ -48,14 +49,11 @@ const (
type (
FmtText struct {
Show uint8
showOption
Indent string
FieldSep string
FixFieldLen int
MaxStringLen int
TimeLayout string
DateLayout string
IntLayout string
@ -67,10 +65,9 @@ type (
secs [secsSize]section
trans [transSize]astrings.Transformers
frameBuilder fmts.FrameFormatBuilder
builders *fmts.TypeFormatBuilders
extractors fmts.FieldExtractors
builders *fmts.TypeFormatBuilders
}
FmtTextFnc func (fm *FmtText)
@ -80,7 +77,7 @@ type (
func NewFmtText(options ...FmtTextFnc) *FmtText {
f := &FmtText{
Show : FormatShowCause | FormatShowWrapped | FormatShowDetails | FormatShowDetails | FormatShowTrace,
showOption : showDefault,
Indent : " ",
FieldSep : " : ",
@ -89,17 +86,14 @@ func NewFmtText(options ...FmtTextFnc) *FmtText {
IntLayout : "%d",
FloatLayout: "%-10s: %.2f",
frameBuilder: fmts.BuildFrameFormatFnc(defaultFrameBuilder),
builders : fmts.NewTypeBuilders(),
extractors : fmts.DefaultsExtractors(),
}
for _,o := range options {
o(f)
}
if f.FixFieldLen > 0 {
f.trans[samesTrans] = append(f.trans[samesTrans],func(s string)string {
return astrings.Pad(s,f.FixFieldLen,' ',true)
})
if f.frameBuilder == nil {
f.frameBuilder = buildDefaultFrameBuilder(f)
}
return f
@ -131,41 +125,10 @@ func (f *FmtText) Format (err error) []byte {
}
func (f *FmtText) ShowWrapped () bool {
return f.Show&FormatShowWrapped !=0 }
func (f *FmtText) ShowType () bool {
return f.Show&FormatShowType !=0 }
func (f *FmtText) ShowColors () bool {
return f.Show&FormatShowColors !=0 }
func (f *FmtText) ShowIndent () bool {
return f.Show&FormatShowIndent !=0 }
func (f *FmtText) ShowDetails() bool {
return f.Show&FormatShowDetails !=0 }
func (f *FmtText) ShowCause () bool {
return f.Show&FormatShowCause !=0 }
func (f *FmtText) ShowTrace () bool {
return f.Show&FormatShowTrace !=0 }
func (f *FmtText) ShowFieldEmpty () bool {
return f.Show&FormatShowFieldEmpty !=0 }
func (f *FmtText) SetFieldFormat (field string, format FieldFormat) {
if field == "" || field == "*" {
f.defFieldFormat.nameColor = format.nameColor
f.defFieldFormat.valueColor= format.valueColor
f.defFieldFormat.nameTrans = append(astrings.Transformers(nil),format.nameTrans ...)
f.defFieldFormat.valueTrans= append(astrings.Transformers(nil),format.valueTrans...)
f.defFieldFormat = format
return
}
if f.fieldsFormat == nil {
f.fieldsFormat = make(map[string]FieldFormat)
@ -175,30 +138,6 @@ func (f *FmtText) SetFieldFormat (field string, format FieldFormat) {
func WithNameLenTransformer(len int) FmtTextFnc {
return func(f *FmtText) {
f.trans[samesTrans] = append(f.trans[samesTrans],func(s string)string {
return astrings.Pad(s,len,' ',true)
})
}
}
func WithStringTransformer(transformer ...astrings.Transform) FmtTextFnc {
return func(f *FmtText) {
f.trans[stringsTrans] = append(f.trans[stringsTrans], transformer...) }}
func WithNameTransformer(transformer ...astrings.Transform) FmtTextFnc {
return func(f *FmtText) {
f.trans[samesTrans] = append(f.trans[samesTrans], transformer...) }}
func WithValueTransformer(transformer ...astrings.Transform) FmtTextFnc {
return func(f *FmtText) {
f.trans[valuesTrans] = append(f.trans[valuesTrans], transformer...) }}
func WithSection(id int, beginLines, endLines []string, style ansi.Code) FmtTextFnc {
if id < DefaultSection && id > TraceSection {
return func(_ *FmtText){}
@ -206,7 +145,7 @@ func WithSection(id int, beginLines, endLines []string, style ansi.Code) FmtText
return func(f *FmtText) {
f.secs[id].bLines = append([]string(nil), beginLines...)
f.secs[id].eLines = append([]string(nil), endLines ...)
f.secs[id].style = style
f.secs[id].ansi = style
}
}
@ -217,7 +156,14 @@ func WithFieldFormat(field string, format FieldFormat) FmtTextFnc {
return
}
if f.fieldsFormat == nil {
f.fieldsFormat=map[string]FieldFormat{field:format}
f.fieldsFormat = make(map[string]FieldFormat)
}
if strings.Index(field,",") == -1 {
f.fieldsFormat[field]=format
return
}
for _,fn := range strings.SplitAfter(field,",") {
f.fieldsFormat[fn]=format
}
}
}

@ -30,7 +30,7 @@ type (
section struct {
bLines []string
eLines []string
style ansi.Code
ansi ansi.Code
}
)

@ -0,0 +1,72 @@
// ------------------------------------------------------------------------
// Project active2
// Active Thing (activething.com) git.activething.com/go
//
// File name show.go
// Created by DEV
// Modified 07/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 ftext
const (
ShowWrapped showOption = 1 << iota
ShowType
ShowAnsi
ShowIndent
ShowDetails
ShowCause
ShowTrace
ShowFieldEmpty
showDefault = ShowWrapped | ShowType | ShowAnsi | ShowIndent | ShowDetails | ShowCause | ShowTrace
)
type (
showOption uint8
)
func (s showOption) ShowWrapped () bool {
return s&ShowWrapped !=0 }
func (s showOption) ShowType () bool {
return s&ShowType !=0 }
func (s showOption) ShowAnsi () bool {
return s&ShowAnsi !=0 }
func (s showOption) ShowIndent () bool {
return s&ShowIndent !=0 }
func (s showOption) ShowDetails() bool {
return s&ShowDetails !=0 }
func (s showOption) ShowCause () bool {
return s&ShowCause !=0 }
func (s showOption) ShowTrace () bool {
return s&ShowTrace !=0 }
func (s showOption) ShowFieldEmpty () bool {
return s&ShowFieldEmpty !=0 }

@ -24,6 +24,7 @@
package ftext
import (
"ansi"
"bytes"
"errors"
"errs/fmts"
@ -113,6 +114,9 @@ func (s *fmtTextState) addWrapped(err error) {
func (s *fmtTextState) AddCode (code string) {
if s.isWrapped {
return
@ -164,20 +168,28 @@ func (s *fmtTextState) AddFieldDate (name string, value time.Time) {
func (s *fmtTextState) writeField (name string, value string) {
name = s.f.defFieldFormat.nameTrans.Apply(name)
value= s.f.defFieldFormat.valueTrans.Apply(value)
cn := s.f.defFieldFormat.nameColor
cv := s.f.defFieldFormat.valueColor
var cn,cv ansi.Code
n := s.f.defFieldFormat.nameTrans.Apply(name)
v := s.f.defFieldFormat.valueTrans.Apply(value)
if s.f.ShowAnsi() {
cn = s.f.defFieldFormat.nameAnsi
cv = s.f.defFieldFormat.valueAnsi
}
if ff,hs := s.f.fieldsFormat[name]; hs {
name = ff.nameTrans.Apply (name)
value= ff.valueTrans.Apply(value)
cn = ff.nameColor
cv = ff.valueColor
n = ff.nameTrans.Apply (n)
v = ff.valueTrans.Apply(v)
if s.f.ShowAnsi() {
cn = ff.nameAnsi
cv = ff.valueAnsi
}
}
s.buffer.WriteString(s.indent)
s.buffer.WriteString(cn.StringColor(name))
s.buffer.WriteString(cn.StringColor(s.f.FieldSep))
s.buffer.WriteString(cv.StringColor(value))
s.buffer.WriteString(cn.Sprint(n))
s.buffer.WriteString(cn.Sprint(s.f.FieldSep))
s.buffer.WriteString(cv.Sprint(v))
s.buffer.WriteByte('\n')
}
@ -193,8 +205,7 @@ func (s *fmtTextState) writeSection(sec int, begin bool) {
}
if len(ls) > 0 {
for _,l := range ls {
s.writeString(l)
s.buffer.WriteByte('\n')
s.writeString(sc.ansi.Sprintln(l))
}
}
}

Loading…
Cancel
Save

Powered by TurnKey Linux.