Skip to content

Commit

Permalink
Merge pull request #45 from yassinebenaid/add-customization-abilities…
Browse files Browse the repository at this point in the history
…-to-theme

Add customization abilities to theme
  • Loading branch information
yassinebenaid authored Jul 2, 2024
2 parents 2ee925b + 041c512 commit b8017b9
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 44 deletions.
68 changes: 65 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ This library is especially useful for debugging and testing when the standard fm
## Why godump

- ability to pretty print values of all types
- coloreful output
- well formatted output
- unexported structs are dumped too
- pointers are followed and recursive pointers are taken in mind ([see examples](#example-3))
- customizable
- customizable, you have full ability over the output, **you can even generate HTML if you'd like to**, [see examples](#example-4)
- zero dependencies

## Get Started
Expand Down Expand Up @@ -172,7 +172,69 @@ Output:

![pointer](./demo/pointer.png)

**Note**: the `&@1` syntax means that this is an address of the value with the id of 1. which is marked as `#1`.
### Example 4.

This example emphasizes how you can generate html

```go
package main

import (
"fmt"
"net"
"net/http"

"github.com/yassinebenaid/godump"
)

// Define your custome style implementation
type CSSColor struct {
R, G, B int
}

func (c CSSColor) Apply(s string) string {
return fmt.Sprintf(`<div style="color: rgb(%d, %d, %d); display: inline-block">%s</div>`, c.R, c.G, c.B, s)
}

func main() {

var d godump.Dumper

d.Theme = godump.Theme{
String: CSSColor{138, 201, 38},// edit the theme to use your implementation
Quotes: CSSColor{112, 214, 255},
Bool: CSSColor{249, 87, 56},
Number: CSSColor{10, 178, 242},
Types: CSSColor{0, 150, 199},
Address: CSSColor{205, 93, 0},
PointerTag: CSSColor{110, 110, 110},
Nil: CSSColor{219, 57, 26},
Func: CSSColor{160, 90, 220},
Fields: CSSColor{189, 176, 194},
Chan: CSSColor{195, 154, 76},
UnsafePointer: CSSColor{89, 193, 180},
Braces: CSSColor{185, 86, 86},
}

var html = `<pre style="background: #111; padding: 10px; color: white">`
html += d.Sprint(net.Dialer{})
html += "<pre>"

// render it to browsers
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html")
fmt.Fprint(w, html)
})

http.ListenAndServe(":8000", nil)

}

```

Output:

![theme](./demo/theme.png)

For more examples, please have a look at [dumper_test](./dumper_test.go) along with [testdata](./testdata)

Expand Down
Binary file added demo/theme.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
41 changes: 25 additions & 16 deletions dumper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -684,6 +684,14 @@ func TestCanCustomizeIndentation(t *testing.T) {
checkFromFeed(t, []byte(result), "./testdata/indentation.txt")
}

type CSSColor struct {
R, G, B int
}

func (c CSSColor) Apply(s string) string {
return fmt.Sprintf(`<div style="color: rgb(%d, %d, %d); display: inline-block">%s</div>`, c.R, c.G, c.B, s)
}

func TestCanCustomizeTheme(t *testing.T) {

type User struct {
Expand All @@ -705,23 +713,24 @@ func TestCanCustomizeTheme(t *testing.T) {
}
me.bestFriend = &me

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

d.Theme = godump.Theme{
String: CSSColor{138, 201, 38},
Quotes: CSSColor{112, 214, 255},
Bool: CSSColor{249, 87, 56},
Number: CSSColor{10, 178, 242},
Types: CSSColor{0, 150, 199},
Address: CSSColor{205, 93, 0},
PointerTag: CSSColor{110, 110, 110},
Nil: CSSColor{219, 57, 26},
Func: CSSColor{160, 90, 220},
Fields: CSSColor{189, 176, 194},
Chan: CSSColor{195, 154, 76},
UnsafePointer: CSSColor{89, 193, 180},
Braces: CSSColor{185, 86, 86},
}

result := d.Sprint(me)

checkFromFeed(t, []byte(result), "./testdata/theme.txt")
Expand Down
42 changes: 21 additions & 21 deletions testdata/theme.txt
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
godump_test.User {
Name: "yassinebenaid",
Age: 22,
hobbies: []string:4:4 {
"Dev",
"Go",
"Web",
"DevOps",
},
bestFriend: &godump_test.User {#1
Name: "yassinebenaid",
Age: 22,
hobbies: []string:4:4 {
"Dev",
"Go",
"Web",
"DevOps",
},
bestFriend: &@1,
},
}
<div style="color: rgb(0, 150, 199); display: inline-block">godump_test.User</div><div style="color: rgb(185, 86, 86); display: inline-block"> {</div><div style="color: rgb(110, 110, 110); display: inline-block"></div>
<div style="color: rgb(189, 176, 194); display: inline-block">Name</div>: <div style="color: rgb(112, 214, 255); display: inline-block">"</div><div style="color: rgb(138, 201, 38); display: inline-block">yassinebenaid</div><div style="color: rgb(112, 214, 255); display: inline-block">"</div>,
<div style="color: rgb(189, 176, 194); display: inline-block">Age</div>: <div style="color: rgb(10, 178, 242); display: inline-block">22</div>,
<div style="color: rgb(189, 176, 194); display: inline-block">hobbies</div>: <div style="color: rgb(0, 150, 199); display: inline-block">[]string:4:4</div><div style="color: rgb(185, 86, 86); display: inline-block"> {</div>
<div style="color: rgb(112, 214, 255); display: inline-block">"</div><div style="color: rgb(138, 201, 38); display: inline-block">Dev</div><div style="color: rgb(112, 214, 255); display: inline-block">"</div>,
<div style="color: rgb(112, 214, 255); display: inline-block">"</div><div style="color: rgb(138, 201, 38); display: inline-block">Go</div><div style="color: rgb(112, 214, 255); display: inline-block">"</div>,
<div style="color: rgb(112, 214, 255); display: inline-block">"</div><div style="color: rgb(138, 201, 38); display: inline-block">Web</div><div style="color: rgb(112, 214, 255); display: inline-block">"</div>,
<div style="color: rgb(112, 214, 255); display: inline-block">"</div><div style="color: rgb(138, 201, 38); display: inline-block">DevOps</div><div style="color: rgb(112, 214, 255); display: inline-block">"</div>,
<div style="color: rgb(185, 86, 86); display: inline-block">}</div>,
<div style="color: rgb(189, 176, 194); display: inline-block">bestFriend</div>: <div style="color: rgb(205, 93, 0); display: inline-block">&</div><div style="color: rgb(0, 150, 199); display: inline-block">godump_test.User</div><div style="color: rgb(185, 86, 86); display: inline-block"> {</div><div style="color: rgb(110, 110, 110); display: inline-block">#1</div>
<div style="color: rgb(189, 176, 194); display: inline-block">Name</div>: <div style="color: rgb(112, 214, 255); display: inline-block">"</div><div style="color: rgb(138, 201, 38); display: inline-block">yassinebenaid</div><div style="color: rgb(112, 214, 255); display: inline-block">"</div>,
<div style="color: rgb(189, 176, 194); display: inline-block">Age</div>: <div style="color: rgb(10, 178, 242); display: inline-block">22</div>,
<div style="color: rgb(189, 176, 194); display: inline-block">hobbies</div>: <div style="color: rgb(0, 150, 199); display: inline-block">[]string:4:4</div><div style="color: rgb(185, 86, 86); display: inline-block"> {</div>
<div style="color: rgb(112, 214, 255); display: inline-block">"</div><div style="color: rgb(138, 201, 38); display: inline-block">Dev</div><div style="color: rgb(112, 214, 255); display: inline-block">"</div>,
<div style="color: rgb(112, 214, 255); display: inline-block">"</div><div style="color: rgb(138, 201, 38); display: inline-block">Go</div><div style="color: rgb(112, 214, 255); display: inline-block">"</div>,
<div style="color: rgb(112, 214, 255); display: inline-block">"</div><div style="color: rgb(138, 201, 38); display: inline-block">Web</div><div style="color: rgb(112, 214, 255); display: inline-block">"</div>,
<div style="color: rgb(112, 214, 255); display: inline-block">"</div><div style="color: rgb(138, 201, 38); display: inline-block">DevOps</div><div style="color: rgb(112, 214, 255); display: inline-block">"</div>,
<div style="color: rgb(185, 86, 86); display: inline-block">}</div>,
<div style="color: rgb(189, 176, 194); display: inline-block">bestFriend</div>: <div style="color: rgb(205, 93, 0); display: inline-block">&</div><div style="color: rgb(110, 110, 110); display: inline-block">@1</div>,
<div style="color: rgb(185, 86, 86); display: inline-block">}</div>,
<div style="color: rgb(185, 86, 86); display: inline-block">}</div>
8 changes: 4 additions & 4 deletions theme.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,22 @@ import (

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

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

// RGB implements [Style] and allow you to define your style as an RGB value.
// 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 {
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)
}

Expand Down

0 comments on commit b8017b9

Please sign in to comment.