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.

224 lines
5.1 KiB

// ------------------------------------------------------------------------
// 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 ftext
import (
"ansi"
"atypes/astrings"
"errs/fmts"
"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
defFieldFormat FieldFormat
fieldsFormat map[string]FieldFormat
secs [secsSize]section
trans [transSize]astrings.Transformers
frameBuilder fmts.FrameFormatBuilder
builders *fmts.TypeFormatBuilders
extractors fmts.FieldExtractors
}
FmtTextFnc func (fm *FmtText)
)
func NewFmtText(options ...FmtTextFnc) *FmtText {
f := &FmtText{
Show : FormatShowCause | FormatShowWrapped | FormatShowDetails | FormatShowDetails | FormatShowTrace,
Indent : " ",
FieldSep : " : ",
TimeLayout : time.RFC822Z,
DateLayout : time.RFC822Z,
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)
})
}
return f
}
func (f *FmtText) Format (err error) []byte {
if err == nil {
return nil
}
st := newFmtTextState(f)
defer func (s *fmtTextState) {
s.reset()
}(st)
st.format(err)
if st.error != nil {
e := st.error
st.reset()
st.format(e)
} else if st.level != 0 {
st.reset()
st.format(*fmts.NewErrFormatLevel())
}
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 (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...)
}
if f.fieldsFormat == nil {
f.fieldsFormat = make(map[string]FieldFormat)
}
f.fieldsFormat[field]=format
}
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){}
}
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
}
}
func WithFieldFormat(field string, format FieldFormat) FmtTextFnc {
return func(f *FmtText) {
if field == "" || field == "*" {
f.defFieldFormat = format
return
}
if f.fieldsFormat == nil {
f.fieldsFormat=map[string]FieldFormat{field:format}
}
}
}

Powered by TurnKey Linux.