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.
98 lines
2.2 KiB
98 lines
2.2 KiB
// ------------------------------------------------------------------------
|
|
// Project active2
|
|
// Active Thing (activething.com) git.activething.com/go
|
|
//
|
|
// File name fmt.go
|
|
// Created by DEV
|
|
// Modified 12/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 fmx
|
|
|
|
import (
|
|
"atypes/astrings"
|
|
"errs/err_fmt"
|
|
)
|
|
|
|
type (
|
|
|
|
TextFmt struct {
|
|
options err_fmt.BuilderOption
|
|
|
|
vLayouts layouts
|
|
sections sections
|
|
defField fieldFormat
|
|
fields map[string]*fieldFormat
|
|
|
|
stringTrans astrings.Transformers
|
|
|
|
fbOnError ,
|
|
fbOnUnwrap,
|
|
fbOnCause err_fmt.BuilderErrorFallback
|
|
|
|
frameBuilder err_fmt.FrameBuilder
|
|
}
|
|
|
|
TextFmtFnc func(f *TextFmt)
|
|
)
|
|
|
|
|
|
|
|
func NewTextFmt (options ...TextFmtFnc) *TextFmt {
|
|
fm := &TextFmt{
|
|
options : err_fmt.BuilderOption(0).SetDefaults(),
|
|
|
|
vLayouts : *newLayouts(),
|
|
sections : sections{},
|
|
|
|
|
|
fbOnError : err_fmt.MakeErrorFallback(fbBuildError),
|
|
fbOnUnwrap: err_fmt.MakeErrorFallback(fbUnwrapError),
|
|
fbOnCause : err_fmt.MakeErrorFallback(fbUnwrapError),
|
|
|
|
frameBuilder: err_fmt.BuildFrameFnc(defaultFrameBuilder),
|
|
}
|
|
for _,o := range options {
|
|
o(fm)
|
|
}
|
|
return fm
|
|
}
|
|
|
|
|
|
|
|
func (f *TextFmt) Format (err error) []byte {
|
|
if err == nil {
|
|
return nil
|
|
}
|
|
st := &state{
|
|
f : f,
|
|
}
|
|
|
|
fe := st.writeError(err,f.options.ShowWrapped(),f.options.ShowCause(),f.options.ShowTrace(),0)
|
|
if fe == nil {
|
|
return st.b.Bytes()
|
|
}
|
|
st.reset()
|
|
if fe = st.writeError(fe,false,true,false,0); fe == nil{
|
|
return st.b.Bytes()
|
|
}
|
|
|
|
return []byte(f.fbOnError(err))
|
|
}
|
|
|
|
|