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.

129 lines
3.9 KiB

// ------------------------------------------------------------------------
// Project active2
// Active Thing (activething.com) git.activething.com/go
//
// File name active.go
// Created by DEV
// Modified 16/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 ftext
import (
"ansi"
"atypes/astrings"
"errs/stack"
"fmt"
"reflect"
"strings"
"sync"
)
var (
g = struct {
buildDefault sync.Once
formatter *TextFormatter
}{
}
)
func DefaultFormatter () *TextFormatter {
g.buildDefault.Do(func() {
g.formatter = NewTextFormatter(
WithNameTransformers(
astrings.TransformFnc(func (s string) string {
return astrings.ToKebab(s) }),
astrings.TransformFnc(func (s string) string {
return astrings.Pad(s,8,' ',true) }),
),
WithSectionsFormat(func(c *Sections){
c.Main.Set(0,
func (b *TextBuilder){
b.AddTextAnsi("# ERROR #####################################################\n",ansi.Code(0).SetFgColor(ansi.ColorHRed).SetStyle(ansi.StyleBold)) },
func (b *TextBuilder){
b.AddTextAnsi("#############################################################\n",ansi.Code(0).SetFgColor(ansi.ColorHRed).SetStyle(ansi.StyleBold)) },)
c.Head.Set(ansi.Code(0).SetFgColor(ansi.ColorHRed),nil,nil)
c.Fields.Set(ansi.Code(0).SetFgColor(ansi.ColorGreen),
nil,
nil)
c.Causes.Set(ansi.Code(0).SetFgColor(ansi.ColorCyan),
func(b *TextBuilder){
b.AddTextAnsi("\n* Prev Causes ***********************************************\n",ansi.Code(0).SetStyle(ansi.StyleBold)) },
nil)
c.Cause.Set(ansi.Code(0).SetFgColor(ansi.ColorCyan),
func(b *TextBuilder) {
if b.level == 1 {
b.setAnsi(ansi.Code(0).SetFgColor(ansi.ColorMagenta))
} else if b.level == 3 {
b.setAnsi(ansi.Code(0).SetFgColor(ansi.ColorGreen))
}
b.AddTextAnsi(fmt.Sprintf("-- %d --------------------------------------------------------\n",b.Level()),0) },
nil)
c.Trace.Set(ansi.Code(0).SetFgColor(ansi.ColorHYellow).SetBgColor(ansi.ColorBlack),
func(b *TextBuilder){
b.AddTextAnsi("\n# Stack Trace ###############################################\n",b.ansiC.SetStyle(ansi.StyleBold).SetFgColor(ansi.ColorHYellow)) },
nil)
}),
)
})
return g.formatter
}
func defaultFrameBuilder (builder *TextBuilder, frame stack.Frame, num int) {
if strings.Index(frame.Package,"runtime") != -1 {
return
}
var c1, c2, c3, c4 ansi.Code
if builder.Options().ShowAnsi(){
c1 = c1.SetFgColor(ansi.ColorBlue)//.SetBgColor(ansi.ColorHBlack)
c2 = c2.SetFgColor(ansi.ColorMagenta)
c3 = c3.SetFgColor(ansi.ColorWhite)
c4 = c3.SetFgColor(ansi.ColorHWhite)
}
builder.AddText(c4.Sprintf(" %d %s.%s \n",num,frame.Package, frame.FuncName))
builder.AddText(c2.Sprintf(" %.90s:%d\n", frame.File, frame.LineNumber))
f, e := frame.SourceLine()
if e == nil {
builder.AddTextAnsi(fmt.Sprintf(" %s\n", f),c3)
} else {
builder.AddTextAnsi(fmt.Sprintf(" error %s\n", e.Error()),c3)
}
}
func defaultFallback (b *TextBuilder, err, cause error) {
b.AddText(fmt.Sprintf("Formatter error: Cant format error type (%s)\nValue : %#v \nCause : %s\n",reflect.TypeOf(err).String(),err,cause.Error()))
}

Powered by TurnKey Linux.