formatter.go: Truncate function and number type names
This commit is contained in:
parent
b92b19e1dd
commit
1c4ffd7d64
30
formatter.go
30
formatter.go
@ -11,16 +11,42 @@ type FmtOpt uint16
|
||||
const (
|
||||
TTY FmtOpt = 1 << iota
|
||||
MultiLine
|
||||
Truncate
|
||||
Base2
|
||||
Base8
|
||||
Base10
|
||||
Base16
|
||||
)
|
||||
|
||||
const (
|
||||
TruncateEllipsis = "(...)"
|
||||
MinTruncateSize = 10
|
||||
TruncateSize = MinTruncateSize + 15
|
||||
)
|
||||
|
||||
func TruncateString(s string) (trunc string) {
|
||||
finalPart := len(s) - (MinTruncateSize - len(TruncateEllipsis))
|
||||
trunc = s[0:len(s)-MinTruncateSize] + TruncateEllipsis + s[finalPart:]
|
||||
return
|
||||
}
|
||||
|
||||
type Formatter interface {
|
||||
ToString(options FmtOpt) string
|
||||
}
|
||||
|
||||
func getFormatted(v any, opt FmtOpt) (text string) {
|
||||
if v == nil {
|
||||
text = "(nil)"
|
||||
} else if s, ok := v.(string); ok {
|
||||
text = s
|
||||
} else if formatter, ok := v.(Formatter); ok {
|
||||
text = formatter.ToString(opt)
|
||||
} else {
|
||||
text = fmt.Sprintf("%v", v)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
type Typer interface {
|
||||
TypeName() string
|
||||
}
|
||||
@ -28,6 +54,10 @@ type Typer interface {
|
||||
func getTypeName(v any) (name string) {
|
||||
if typer, ok := v.(Typer); ok {
|
||||
name = typer.TypeName()
|
||||
} else if IsInteger(v) {
|
||||
name = "integer"
|
||||
} else if IsFloat(v) {
|
||||
name = "float"
|
||||
} else {
|
||||
name = fmt.Sprintf("%T", v)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user