Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove theme.go and move its content to dumper.go #46

Merged
merged 1 commit into from
Jul 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 87 additions & 0 deletions dumper.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,93 @@ import (
"strings"
)

// Style defines a general interface used for styling.
type Style interface {
Apply(string) string
}

func __(s Style, v string) string {
if s == nil {
return v
}
return s.Apply(v)
}

// RGB implements [Style] and allow you to define your style as an RGB value, it uses ANSI escape sequences under the hood.
type RGB struct {
R, G, B int
}

func (rgb RGB) Apply(v string) string {
return fmt.Sprintf("\033[38;2;%v;%v;%vm%s\033[0m", rgb.R, rgb.G, rgb.B, v)
}

// Theme allows you to define your preferred styling for [Dumper].
type Theme struct {
// String defines the style used for strings
String Style

// Quotes defines the style used for quotes (") around strings.
Quotes Style

// Bool defines the style used for boolean values.
Bool Style

// Number defines the style used for numbers, including all types of integers, floats and complex numbers.
Number Style

// Types defines the style used for defined and/or structural types, eg. slices, structs, maps...
Types Style

// Nil defines the style used for nil.
Nil Style

// Func defines the style used for functions.
Func Style

// Chan defines the style used for channels.
Chan Style

// UnsafePointer defines the style used for unsafe pointers.
UnsafePointer Style

// Address defines the style used for address symbol '&'.
Address Style

// PointerTag defines the style used for pointer tags, typically the pointer id '#x' and the recursive reference '@x'.
PointerTag Style

// Fields defines the style used for struct fields.
Fields Style

// Braces defines the style used for braces '{}' in structural types.
Braces Style
}

// DefaultTheme is the default [Theme] used by [Dump].
var DefaultTheme = Theme{
String: RGB{138, 201, 38},
Quotes: RGB{112, 214, 255},
Bool: RGB{249, 87, 56},
Number: RGB{10, 178, 242},
Types: RGB{0, 150, 199},
Address: RGB{205, 93, 0},
PointerTag: RGB{110, 110, 110},
Nil: RGB{219, 57, 26},
Func: RGB{160, 90, 220},
Fields: RGB{189, 176, 194},
Chan: RGB{195, 154, 76},
UnsafePointer: RGB{89, 193, 180},
Braces: RGB{185, 86, 86},
}

// DisableColors disables the colors globally.
//
// Deprecated: As of v0.8.0 this function only sets the [DefaultTheme] to a zero value
func DisableColors() {
DefaultTheme = Theme{}
}

// Dumper provides an elegant interface to pretty print any variable of any type in a colored and structured format.
//
// The zero value for Dumper is a themeless Dumper ready to use.
Expand Down
92 changes: 0 additions & 92 deletions theme.go

This file was deleted.