You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

326 lines
6.1 KiB

// ------------------------------------------------------------------------
// 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 ftext
import (
"ansi"
"bytes"
"errors"
"errs/fmts"
"errs/stack"
"fmt"
"reflect"
"strings"
"time"
)
type (
fmtTextState struct {
f *Formatter
buffer bytes.Buffer
level int
section int
indent string
error error
isWrapped bool
}
)
func newFmtTextState(f *Formatter) *fmtTextState {
return &fmtTextState{ f : f } }
func (s *fmtTextState) reset() {
s.buffer.Reset()
s.isWrapped = false
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.writeSection(CauseSection,true)
w := s.isWrapped
if w {
s.isWrapped = false
defer func(){
s.isWrapped = true
}()
}
s.format(err)
s.writeSection(CauseSection,false)
}
func (s *fmtTextState) addTrace(trace stack.Trace) {
if trace == nil || !s.f.ShowTrace() {
return
}
s.writeSection(TraceSection,true)
for l,f := range trace.Frames() {
s.f.frameBuilder.BuildFrameFormat(s,f,l)
}
s.writeSection(TraceSection,false)
}
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(fmts.FieldNameError, s.f.trans[valuesTrans].Apply(code))
}
func (s *fmtTextState) AddType (ter string ) {
if s.isWrapped {
return
}
s.writeField(fmts.FieldNameType,ter)
}
func (s *fmtTextState) AddString (str string) {
s.writeString(s.f.trans[stringsTrans].Apply(str))
s.buffer.WriteByte('\n')
}
func (s *fmtTextState) AddStrings(str []string) {
for _,st := range str {
s.writeString(s.f.trans[stringsTrans].Apply(st))
s.buffer.WriteByte('\n')
}
}
func (s *fmtTextState) AddFields (build func (b fmts.FormatFieldBuilder)) {
if s.f.ShowFields() && build != nil {
build(s)
}
}
func (s *fmtTextState) AddFieldString (name string, value string ) {
value = s.f.trans[valuesTrans].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) {
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 {
n = ff.NameTrans.Apply (n)
v = ff.ValueTrans.Apply(v)
if s.f.ShowAnsi() {
cn = ff.NameAnsi
cv = ff.ValueAnsi
}
defer func(){
if ff.BuildAfter != nil {
ff.BuildAfter(s)
}
}()
}
s.buffer.WriteString(s.indent)
s.buffer.WriteString(cn.Sprint(n))
s.buffer.WriteString(cn.Sprint(s.f.FieldSep))
s.buffer.WriteString(cv.Sprint(v))
s.buffer.WriteByte('\n')
}
func (s *fmtTextState) writeSection(sec int, begin bool) {
sc := s.f.secs[sec]
if begin && sc.BuildBefore != nil {
sc.BuildBefore(s)
return
}
if sc.BuildAfter != nil {
sc.BuildAfter(s)
}
}
func (s *fmtTextState) writeString(str string) {
s.buffer.WriteString(str)
}
func (s *fmtTextState) format (er error) {
if er == nil {
return
}
dn := false
defer func(d *bool, s *fmtTextState){
if !(*d) {
/*
if s.error == nil {
var ne error
if re := recover(); re != nil {
var is bool
if ne,is = re.(error); !is {
ne = fmt.Errorf("%#v",re)
}
}
s.error = *fmts.NewErrFormatBuilder(ne)
} else {
s.error = *fmts.NewErrFormatBuilder(s.error)
}
*/
}
}(&dn, s)
if s.level == 0 && !s.isWrapped {
s.writeSection(ErrorSection,true)
}
if fm,is := er.(fmts.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.isWrapped {
s.addTrace(stack.UnwrapTrace(er,true))
if !s.isWrapped {
s.writeSection(ErrorSection, false)
}
}
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)
}

Powered by TurnKey Linux.