parent
d11c5133d7
commit
864923cee7
@ -1,173 +0,0 @@
|
||||
// ------------------------------------------------------------------------
|
||||
// Project active2
|
||||
// Active Thing (activething.com) git.activething.com/go
|
||||
//
|
||||
// File name format_text.go
|
||||
// Created by DEV
|
||||
// Modified 30/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 fmts
|
||||
|
||||
import (
|
||||
"atypes/astrings"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
|
||||
FormatShowWrapped uint8 = 1 << iota
|
||||
FormatShowType
|
||||
FormatShowColors
|
||||
FormatShowIndent
|
||||
FormatShowDetails
|
||||
FormatShowCause
|
||||
FormatShowTrace
|
||||
FormatShowFieldEmpty
|
||||
)
|
||||
|
||||
|
||||
|
||||
|
||||
type (
|
||||
|
||||
FmtText struct {
|
||||
Show uint8
|
||||
|
||||
Indent string
|
||||
FieldSep string
|
||||
|
||||
FixFieldLen int
|
||||
MaxStringLen int
|
||||
|
||||
TimeLayout string
|
||||
DateLayout string
|
||||
IntLayout string
|
||||
FloatLayout string
|
||||
|
||||
Color FmtTextColor
|
||||
CodeColor FmtTextColor
|
||||
FieldNameColor FmtTextColor
|
||||
FieldValueColor FmtTextColor
|
||||
|
||||
Lines [4]*fmtTextSection
|
||||
|
||||
StringTransformers astrings.Transformers
|
||||
ValueTransformers astrings.Transformers
|
||||
NameTransformers astrings.Transformers
|
||||
|
||||
builders *TypeFormatBuilders
|
||||
extractors FieldExtractors
|
||||
}
|
||||
|
||||
FmtTextFnc func (fm *FmtText)
|
||||
)
|
||||
|
||||
|
||||
|
||||
func NewFmtText(options ...FmtTextFnc) *FmtText {
|
||||
f := &FmtText{
|
||||
Show : FormatShowCause | FormatShowWrapped | FormatShowDetails | FormatShowDetails,
|
||||
Indent : " ",
|
||||
FieldSep : " : ",
|
||||
|
||||
TimeLayout : time.RFC822Z,
|
||||
DateLayout : time.RFC822Z,
|
||||
IntLayout : "%d",
|
||||
FloatLayout: "%-10s: %.2f",
|
||||
|
||||
builders : NewTypeBuilders(),
|
||||
extractors : DefaultsExtractors(),
|
||||
}
|
||||
for _,o := range options {
|
||||
o(f)
|
||||
}
|
||||
if f.FixFieldLen > 0 {
|
||||
f.NameTransformers = append(f.NameTransformers,func(s string)string {
|
||||
return astrings.Pad(s,f.FixFieldLen,' ',true)
|
||||
})
|
||||
}
|
||||
|
||||
return f
|
||||
|
||||
}
|
||||
|
||||
|
||||
func (f *FmtText) Format (err error) []byte {
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
st := newFmtTextState(f)
|
||||
defer st.reset()
|
||||
|
||||
st.format(err)
|
||||
if st.error != nil {
|
||||
e := st.error
|
||||
st.reset()
|
||||
st.format(e)
|
||||
}
|
||||
|
||||
return st.buffer.Bytes()
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
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 FmtTextWithNameLenTransformer(len int) FmtTextFnc {
|
||||
return func(f *FmtText) {
|
||||
f.NameTransformers = append(f.NameTransformers,func(s string)string {
|
||||
return astrings.Pad(s,len,' ',true)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func FmtTextWithNameTransformer (transformer ...astrings.Transform) FmtTextFnc {
|
||||
return func(f *FmtText) {
|
||||
f.NameTransformers = append(f.NameTransformers,transformer...)
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,61 +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 fmts
|
||||
|
||||
import "fmt"
|
||||
|
||||
const (
|
||||
|
||||
FormatColorBlack FmtTextColor = iota + 30
|
||||
FormatColorRed
|
||||
FormatColorGreen
|
||||
FormatColorYellow
|
||||
FormatColorBlue
|
||||
FormatColorMagenta
|
||||
FormatColorCyan
|
||||
FormatColorWhite
|
||||
)
|
||||
|
||||
|
||||
type (
|
||||
|
||||
FmtTextColor uint8
|
||||
)
|
||||
|
||||
|
||||
func (c FmtTextColor) BeginColorString() string {
|
||||
return fmt.Sprintf("\u001B[%dm",uint8(c))
|
||||
}
|
||||
|
||||
func (_ FmtTextColor) EndColorString () string {
|
||||
return "\x1b[0m" }
|
||||
|
||||
|
||||
func (c FmtTextColor) Sprintf(fm string, params ...any) string {
|
||||
return fmt.Sprintf("\x1b[%dm%s\x1b[0m", uint8(c),fmt.Sprintf(fm,params...)) }
|
||||
|
||||
|
||||
func (c FmtTextColor) IsValid() bool {
|
||||
return c >= FormatColorBlack && c <= FormatColorWhite
|
||||
}
|
||||
@ -1,36 +0,0 @@
|
||||
// ------------------------------------------------------------------------
|
||||
// Project active2
|
||||
// Active Thing (activething.com) git.activething.com/go
|
||||
//
|
||||
// File name fmt_lines.go
|
||||
// Created by DEV
|
||||
// Modified 03/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 fmts
|
||||
|
||||
|
||||
type (
|
||||
|
||||
fmtTextSection struct {
|
||||
startLines []string
|
||||
endLines []string
|
||||
color FmtTextColor
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
@ -1,247 +0,0 @@
|
||||
// ------------------------------------------------------------------------
|
||||
// Project active2
|
||||
// Active Thing (activething.com) git.activething.com/go
|
||||
//
|
||||
// File name format_text_builder.go
|
||||
// Created by DEV
|
||||
// Modified 30/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 fmts
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"errs/stack"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
|
||||
type (
|
||||
|
||||
fmtTextState struct {
|
||||
f *FmtText
|
||||
buffer bytes.Buffer
|
||||
level int
|
||||
indent string
|
||||
error error
|
||||
isWrapped bool
|
||||
}
|
||||
|
||||
)
|
||||
|
||||
func newFmtTextState(f *FmtText) *fmtTextState {
|
||||
return &fmtTextState{ f : f } }
|
||||
|
||||
|
||||
|
||||
func (s *fmtTextState) reset() {
|
||||
s.buffer.Reset()
|
||||
s.level = 0
|
||||
s.error = nil
|
||||
}
|
||||
|
||||
|
||||
|
||||
func (s *fmtTextState) AddError (err error) {
|
||||
if err == nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
func (s *fmtTextState) addCause(err error) {
|
||||
if err == nil || !s.f.ShowCause() {
|
||||
return
|
||||
}
|
||||
s.addLevel()
|
||||
defer s.decLevel()
|
||||
|
||||
s.writeString(s.indent)
|
||||
s.writeString(s.f.StringOnCause)
|
||||
w := s.isWrapped
|
||||
if w {
|
||||
s.isWrapped = false
|
||||
defer func(){
|
||||
s.isWrapped = true
|
||||
}()
|
||||
}
|
||||
s.format(err)
|
||||
}
|
||||
|
||||
func (s *fmtTextState) addTrace(trace stack.Trace) {
|
||||
if trace == nil || !s.f.ShowTrace() {
|
||||
return
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func (s *fmtTextState) addWrapped(err error) {
|
||||
if err == nil || !s.f.ShowWrapped() {
|
||||
return
|
||||
}
|
||||
if !s.isWrapped {
|
||||
s.isWrapped = true
|
||||
defer func(st *fmtTextState) { s.isWrapped = false }(s)
|
||||
}
|
||||
s.format(err)
|
||||
}
|
||||
|
||||
|
||||
|
||||
func (s *fmtTextState) AddCode (code string) {
|
||||
if s.isWrapped {
|
||||
return
|
||||
}
|
||||
s.writeField("error", s.f.ValueTransformers.Apply(code))
|
||||
}
|
||||
|
||||
func (s *fmtTextState) AddType (ter string ) {
|
||||
if s.isWrapped {
|
||||
return
|
||||
}
|
||||
s.writeField("error-type",ter)
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
func (s *fmtTextState) AddString (str string) {
|
||||
s.writeString(s.f.StringTransformers.Apply(str))
|
||||
}
|
||||
|
||||
func (s *fmtTextState) AddStrings(str []string) {
|
||||
for _,st := range str {
|
||||
s.writeString(s.f.StringTransformers.Apply(st))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
func (s *fmtTextState) AddFieldString (name string, value string ) {
|
||||
value = s.f.ValueTransformers.Apply(value)
|
||||
if len(value) == 0 && !s.f.ShowFieldEmpty() {
|
||||
return
|
||||
}
|
||||
s.writeField(name,value) }
|
||||
|
||||
func (s *fmtTextState) AddFieldInt (name string, value int ) {
|
||||
s.writeField(name,fmt.Sprintf(s.f.IntLayout,value)) }
|
||||
|
||||
func (s *fmtTextState) AddFieldFloat (name string, value float64 ) {
|
||||
s.writeField(name,fmt.Sprintf(s.f.FloatLayout,value)) }
|
||||
|
||||
func (s *fmtTextState) AddFieldTime (name string, value time.Time) {
|
||||
s.writeField(name,value.Format(s.f.TimeLayout)) }
|
||||
|
||||
func (s *fmtTextState) AddFieldDate (name string, value time.Time) {
|
||||
s.writeField(name,value.Format(s.f.DateLayout)) }
|
||||
|
||||
|
||||
func (s *fmtTextState) writeField (name string, value string) {
|
||||
name = s.f.NameTransformers.Apply (name)
|
||||
s.writeString(fmt.Sprintf("%s%s%s%s\n",s.indent,name,s.f.FieldSep,value))
|
||||
}
|
||||
|
||||
|
||||
func (s *fmtTextState) writeString(str string) {
|
||||
s.buffer.WriteString(str)
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
func (s *fmtTextState) format (er error) {
|
||||
|
||||
dn := false
|
||||
defer func(d *bool, s *fmtTextState){
|
||||
if !(*d) {
|
||||
if s.error == nil {
|
||||
var ie error
|
||||
if re := recover(); re != nil {
|
||||
if ne,is := re.(error); is {
|
||||
ie = ne
|
||||
} else {
|
||||
ie = fmt.Errorf(G.CodeBuilderError,"%v",re)
|
||||
}
|
||||
}
|
||||
s.error = *NewErrFormat(G.CodeBuilderError,ie)
|
||||
} else {
|
||||
s.error = *NewErrFormat(G.CodeBuilderError,s.error)
|
||||
}
|
||||
}
|
||||
}(&dn, s)
|
||||
|
||||
|
||||
if fm,is := er.(Formattable); is {
|
||||
fm.BuildFormat(s)
|
||||
} else {
|
||||
rt := reflect.TypeOf(er)
|
||||
if rt.Kind() == reflect.Pointer {
|
||||
rt = rt.Elem()
|
||||
}
|
||||
|
||||
if fb := s.f.builders.TypeFormatBuilder(rt); fb != nil {
|
||||
fb.BuildFormatError(s,er)
|
||||
} else {
|
||||
s.f.builders.Set(rt,s.f.extractors.BuildAndCreate(s,er))
|
||||
}
|
||||
}
|
||||
|
||||
s.addWrapped(errors.Unwrap(er))
|
||||
|
||||
if cs,is := er.(interface{ Cause() error }); is {
|
||||
s.addCause(cs.Cause())
|
||||
}
|
||||
|
||||
if s.level == 0 {
|
||||
s.addTrace(stack.UnwrapTrace(er,true))
|
||||
}
|
||||
|
||||
|
||||
dn=true
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
func (s *fmtTextState) decLevel() {
|
||||
s.level--
|
||||
if s.level <= 0 {
|
||||
s.indent = ""
|
||||
return
|
||||
}
|
||||
if s.level == 1 {
|
||||
s.indent = s.f.Indent
|
||||
return
|
||||
}
|
||||
s.indent = strings.Repeat(s.f.Indent,s.level)
|
||||
}
|
||||
|
||||
func (s *fmtTextState) addLevel() {
|
||||
s.level++
|
||||
if s.level == 1 {
|
||||
s.indent = s.f.Indent
|
||||
return
|
||||
}
|
||||
s.indent = strings.Repeat(s.f.Indent,s.level)
|
||||
}
|
||||
|
||||
|
||||
|
||||
@ -1,40 +0,0 @@
|
||||
// ------------------------------------------------------------------------
|
||||
// Project active2
|
||||
// Active Thing (activething.com) git.activething.com/go
|
||||
//
|
||||
// File name sections.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 text
|
||||
|
||||
|
||||
type (
|
||||
|
||||
sections struct {
|
||||
err *section
|
||||
fields *section
|
||||
cause *section
|
||||
trace *section
|
||||
}
|
||||
|
||||
)
|
||||
|
||||
|
||||
|
||||
|
||||
@ -1,37 +0,0 @@
|
||||
// ------------------------------------------------------------------------
|
||||
// Project active2
|
||||
// Active Thing (activething.com) git.activething.com/go
|
||||
//
|
||||
// File name transformers.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 text
|
||||
|
||||
import "atypes/astrings"
|
||||
|
||||
type (
|
||||
|
||||
transformers struct {
|
||||
strings astrings.Transformers
|
||||
fieldName astrings.Transformers
|
||||
fieldValue astrings.Transformers
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
Loading…
Reference in new issue