diff --git a/core/ansi/code.go b/core/ansi/code.go index 22eff9c..3727725 100644 --- a/core/ansi/code.go +++ b/core/ansi/code.go @@ -26,6 +26,7 @@ package ansi import ( "bytes" "fmt" + "io" "strings" ) @@ -198,6 +199,28 @@ func (_ Code) End () string { + + +func (c Code) Errorf (format string, params ...any) error { + return fmt.Errorf(c.Sprintf(format,params...)) } + +func (c Code) Fprint (writer io.Writer, format string, params ...any) (n int, e error) { + return fmt.Fprint(writer,c.Sprintf(format,params...)) } + +func (c Code) Fprintln (writer io.Writer, format string, params ...any) (n int, e error) { + return fmt.Fprintln(writer,c.Sprintf(format,params...)) } + +func (c Code) Print (input ...any) (n int, e error) { + return fmt.Print(c.wrap(input ...)) } + +func (c Code) Printf (format string, params ...any) { + fmt.Printf(c.Sprintf(format,params...)) } + + +func (c Code) Sprint (format string, params ...any) string { + return fmt.Sprint(c.Sprintf(format,params...)) +} + func (c Code) Sprintf (format string, params ...any) string { if len(format) == 0 && len(params) == 0 { return "" @@ -206,11 +229,7 @@ func (c Code) Sprintf (format string, params ...any) string { if c == 0 { return s } - b := bytes.Buffer{} - b.WriteString(b.String()) - b.WriteString(s) - b.WriteString(g.endEscStr) - return b.String() + return c.wrap(s) } func (c Code) Sprintln(format string, params ...any) string { @@ -218,10 +237,13 @@ func (c Code) Sprintln(format string, params ...any) string { -func (c Code) Printf (format string, params ...any) { - fmt.Printf(c.Sprintf(format,params...)) +func (c Code) wrap(str ...any) string { + b := bytes.Buffer{} + b.WriteString(c.String()) + for _,s := range str { + b.WriteString(fmt.Sprint(s)) + } + b.WriteString(g.endEscStr) + return b.String() } - - - diff --git a/core/errs/fmts/ftext/active.go b/core/errs/fmts/ftext/active.go index 9fd3cfe..29389c6 100644 --- a/core/errs/fmts/ftext/active.go +++ b/core/errs/fmts/ftext/active.go @@ -32,10 +32,11 @@ import ( const ( - errorSection int = iota - fieldsSection - causeSection - traceSection + DefaultSection int = iota + ErrorSection + FieldsSection + CauseSection + TraceSection secsSize= 4 ) @@ -67,19 +68,19 @@ func DefaultFormatter () *FmtText { func(f *FmtText) { f.FixFieldLen = 13 }, - FmtTextWithFieldFormat("*",FieldFormat{ + WithFieldFormat("*",FieldFormat{ nameTrans: []astrings.Transform{ astrings.ToKebab }, }), - FmtTextWithFieldFormat("error",FieldFormat{ + WithFieldFormat("error",FieldFormat{ nameColor: ColorGreen , }), - FmtTextWithErrorSection( + WithErrorSection( []string{ColorRed.StringColor("**********************************************************")}, []string{"**********************************************************"}, ColorMagenta), - FmtTextWithCauseSection( + WithCauseSection( []string{"- Prev Cause ---------------"}, []string(nil), ColorBlue), - FmtTextWithTraceSection( + WithTraceSection( []string{"\n* Stack Trace Information **"}, []string(nil), ColorMagenta)) }) diff --git a/core/errs/fmts/ftext/color.go b/core/errs/fmts/ftext/color.go deleted file mode 100644 index 83a31c0..0000000 --- a/core/errs/fmts/ftext/color.go +++ /dev/null @@ -1,96 +0,0 @@ -// ------------------------------------------------------------------------ -// Project active2 -// Active Thing (activething.com) git.activething.com/go -// -// File name color.go -// Created by DEV -// Modified 27/03/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 - -import "fmt" - -const ( - - ColorNone Color = 0 - ColorBlack Color = 29 + iota - ColorRed - ColorGreen - ColorYellow - ColorBlue - ColorMagenta - ColorCyan - ColorWhite -) - - -type ( - - Color uint8 -) - - -func (c Color) BeginColorString() string { - if !c.IsValid() { - return "" - } - return fmt.Sprintf("\u001B[%dm",uint8(c)) -} - -func (c Color) EndColorString () string { - if !c.IsValid() { - return "" - } - return "\x1b[0m" -} - - -func (c Color) StringColor(str string) string { - if !c.IsValid() { - return str - } - return fmt.Sprintf("\x1b[%dm%s\x1b[0m", uint8(c),str) -} - - -func (c Color) SprintfColor(fm string, params ...any) string { - if !c.IsValid() { - return fmt.Sprintf(fm,params...) - } - return fmt.Sprintf("\x1b[%dm%s\x1b[0m", uint8(c),fmt.Sprintf(fm,params...)) } - - -func (c Color) Name () string { - switch c { - case ColorBlack : return "black" - case ColorRed : return "red" - case ColorGreen : return "green" - case ColorYellow : return "yellow" - case ColorBlue : return "blue" - case ColorMagenta: return "magenta" - case ColorCyan : return "cyan" - case ColorWhite : - return "white" - default:return "" - } -} - - -func (c Color) IsValid() bool { - return c >= ColorBlack && c <= ColorWhite -} \ No newline at end of file diff --git a/core/errs/fmts/ftext/field_format.go b/core/errs/fmts/ftext/field_format.go index d765459..58c7cf8 100644 --- a/core/errs/fmts/ftext/field_format.go +++ b/core/errs/fmts/ftext/field_format.go @@ -23,13 +23,16 @@ package ftext -import "atypes/astrings" +import ( + "ansi" + "atypes/astrings" +) type ( FieldFormat struct { - nameColor Color - valueColor Color + nameColor ansi.Code + valueColor ansi.Code nameTrans astrings.Transformers valueTrans astrings.Transformers } diff --git a/core/errs/fmts/ftext/fmt.go b/core/errs/fmts/ftext/fmt.go index 3f6e1a6..f4b8c5e 100644 --- a/core/errs/fmts/ftext/fmt.go +++ b/core/errs/fmts/ftext/fmt.go @@ -24,6 +24,7 @@ package ftext import ( + "ansi" "atypes/astrings" "errs/fmts" "time" @@ -60,11 +61,6 @@ type ( IntLayout string FloatLayout string - Color Color - CodeColor Color - FieldNameColor Color - FieldValueColor Color - defFieldFormat FieldFormat fieldsFormat map[string]FieldFormat @@ -178,7 +174,11 @@ func (f *FmtText) SetFieldFormat (field string, format FieldFormat) { } -func FmtTextWithNameLenTransformer(len int) FmtTextFnc { + + + + +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) @@ -186,43 +186,31 @@ func FmtTextWithNameLenTransformer(len int) FmtTextFnc { } } - -func FmtTextWithStringTransformer(transformer ...astrings.Transform) FmtTextFnc { +func WithStringTransformer(transformer ...astrings.Transform) FmtTextFnc { return func(f *FmtText) { f.trans[stringsTrans] = append(f.trans[stringsTrans], transformer...) }} -func FmtTextWithNameTransformer (transformer ...astrings.Transform) FmtTextFnc { +func WithNameTransformer(transformer ...astrings.Transform) FmtTextFnc { return func(f *FmtText) { f.trans[samesTrans] = append(f.trans[samesTrans], transformer...) }} -func FmtTextWithValueTransformer (transformer ...astrings.Transform) FmtTextFnc { +func WithValueTransformer(transformer ...astrings.Transform) FmtTextFnc { return func(f *FmtText) { f.trans[valuesTrans] = append(f.trans[valuesTrans], transformer...) }} -func FmtTextWithErrorSection (beginLines, endLines []string, color Color) FmtTextFnc { - return FmtTextWithSection (errorSection,beginLines,endLines,color) } - -func FmtTextWithFieldsSection (beginLines, endLines []string, color Color) FmtTextFnc { - return FmtTextWithSection (fieldsSection,beginLines,endLines,color) } - -func FmtTextWithCauseSection (beginLines, endLines []string, color Color) FmtTextFnc { - return FmtTextWithSection (causeSection,beginLines,endLines,color) } - -func FmtTextWithTraceSection (beginLines, endLines []string, color Color) FmtTextFnc { - return FmtTextWithSection (traceSection,beginLines,endLines,color) } - - -func FmtTextWithSection (id int, beginLines, endLines []string, color Color) FmtTextFnc { +func WithSection(id int, beginLines, endLines []string, style ansi.Code) FmtTextFnc { + if id < DefaultSection && id > TraceSection { + return func(_ *FmtText){} + } return func(f *FmtText) { - f.secs[id].beginLines = append([]string(nil), beginLines...) - f.secs[id].endLines = append([]string(nil), endLines ...) - f.secs[id].color = color + f.secs[id].bLines = append([]string(nil), beginLines...) + f.secs[id].eLines = append([]string(nil), endLines ...) + f.secs[id].style = style } } - -func FmtTextWithFieldFormat (field string, format FieldFormat) FmtTextFnc { +func WithFieldFormat(field string, format FieldFormat) FmtTextFnc { return func(f *FmtText) { if field == "" || field == "*" { f.defFieldFormat = format diff --git a/core/errs/fmts/ftext/section.go b/core/errs/fmts/ftext/section.go index df5091d..58ae8e3 100644 --- a/core/errs/fmts/ftext/section.go +++ b/core/errs/fmts/ftext/section.go @@ -23,13 +23,14 @@ package ftext +import "ansi" type ( section struct { - beginLines []string - endLines []string - color Color + bLines []string + eLines []string + style ansi.Code } ) diff --git a/core/errs/fmts/ftext/state.go b/core/errs/fmts/ftext/state.go index fe098cc..8fd2df6 100644 --- a/core/errs/fmts/ftext/state.go +++ b/core/errs/fmts/ftext/state.go @@ -76,7 +76,7 @@ func (s *fmtTextState) addCause(err error) { s.addLevel() defer s.decLevel() - s.writeSection(causeSection,true) + s.writeSection(CauseSection,true) w := s.isWrapped if w { s.isWrapped = false @@ -85,7 +85,7 @@ func (s *fmtTextState) addCause(err error) { }() } s.format(err) - s.writeSection(causeSection,false) + s.writeSection(CauseSection,false) } func (s *fmtTextState) addTrace(trace stack.Trace) { @@ -93,11 +93,11 @@ func (s *fmtTextState) addTrace(trace stack.Trace) { return } - s.writeSection(traceSection,true) + s.writeSection(TraceSection,true) for l,f := range trace.Frames() { s.f.frameBuilder.BuildFrameFormat(s,f,l) } - s.writeSection(traceSection,false) + s.writeSection(TraceSection,false) } func (s *fmtTextState) addWrapped(err error) { @@ -187,9 +187,9 @@ func (s *fmtTextState) writeSection(sec int, begin bool) { sc := s.f.secs[sec] var ls []string if begin { - ls = sc.beginLines + ls = sc.bLines } else { - ls = sc.endLines + ls = sc.eLines } if len(ls) > 0 { for _,l := range ls { @@ -233,7 +233,7 @@ func (s *fmtTextState) format (er error) { if s.level == 0 && !s.isWrapped { - s.writeSection(errorSection,true) + s.writeSection(ErrorSection,true) } @@ -261,7 +261,7 @@ func (s *fmtTextState) format (er error) { if s.level == 0 { s.addTrace(stack.UnwrapTrace(er,true)) if !s.isWrapped { - s.writeSection(errorSection, false) + s.writeSection(ErrorSection, false) } }