forked from gnolang/gno
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
) Related with https://hackmd.io/a8k09_TeQUu6WawvkpDDpw?both - [x] `p/moul/web25` - displays a link suggesting to view the realm from an external webui, but avoid recursive printing when seen from the external webui - [x] `p/moul/md` - minimal `markdown` helpers library - [x] `p/moul/debug` - displays useful informations when adding `?debug=1` - [x] update `r/moul/home` - [x] markdown table with links example - [x] svg example - [x] use of the new `p/moul/...` packages - [x] Release a static web25 interface somewhere (https://github.com/moul/gno-moul-home-web25) --------- Signed-off-by: moul <[email protected]>
- Loading branch information
1 parent
3ec27c4
commit f54795f
Showing
17 changed files
with
778 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
// Package debug provides utilities for logging and displaying debug information | ||
// within Gno realms. It supports conditional rendering of logs and metadata, | ||
// toggleable via query parameters. | ||
// | ||
// Key Features: | ||
// - Log collection and display using Markdown formatting. | ||
// - Metadata display for realm path, address, and height. | ||
// - Collapsible debug section for cleaner presentation. | ||
// - Query-based debug toggle using `?debug=1`. | ||
package debug | ||
|
||
import ( | ||
"std" | ||
"time" | ||
|
||
"gno.land/p/demo/ufmt" | ||
"gno.land/p/moul/md" | ||
"gno.land/p/moul/mdtable" | ||
"gno.land/p/moul/realmpath" | ||
) | ||
|
||
// Debug encapsulates debug information, including logs and metadata. | ||
type Debug struct { | ||
Logs []string | ||
HideMetadata bool | ||
} | ||
|
||
// Log appends a new line of debug information to the Logs slice. | ||
func (d *Debug) Log(line string) { | ||
d.Logs = append(d.Logs, line) | ||
} | ||
|
||
// Render generates the debug content as a collapsible Markdown section. | ||
// It conditionally renders logs and metadata if enabled via the `?debug=1` query parameter. | ||
func (d Debug) Render(path string) string { | ||
if realmpath.Parse(path).Query.Get("debug") != "1" { | ||
return "" | ||
} | ||
|
||
var content string | ||
|
||
if d.Logs != nil { | ||
content += md.H3("Logs") | ||
content += md.BulletList(d.Logs) | ||
} | ||
|
||
if !d.HideMetadata { | ||
content += md.H3("Metadata") | ||
table := mdtable.Table{ | ||
Headers: []string{"Key", "Value"}, | ||
} | ||
table.Append([]string{"`std.CurrentRealm().PkgPath()`", string(std.CurrentRealm().PkgPath())}) | ||
table.Append([]string{"`std.CurrentRealm().Addr()`", string(std.CurrentRealm().Addr())}) | ||
table.Append([]string{"`std.PrevRealm().PkgPath()`", string(std.PrevRealm().PkgPath())}) | ||
table.Append([]string{"`std.PrevRealm().Addr()`", string(std.PrevRealm().Addr())}) | ||
table.Append([]string{"`std.GetHeight()`", ufmt.Sprintf("%d", std.GetHeight())}) | ||
table.Append([]string{"`time.Now().Format(time.RFC3339)`", time.Now().Format(time.RFC3339)}) | ||
content += table.String() | ||
} | ||
|
||
if content == "" { | ||
return "" | ||
} | ||
|
||
return md.CollapsibleSection("debug", content) | ||
} | ||
|
||
// Render displays metadata about the current realm but does not display logs. | ||
// This function uses a default Debug struct with metadata enabled and no logs. | ||
func Render(path string) string { | ||
return Debug{}.Render(path) | ||
} | ||
|
||
// IsEnabled checks if the `?debug=1` query parameter is set in the given path. | ||
// Returns true if debugging is enabled, otherwise false. | ||
func IsEnabled(path string) bool { | ||
req := realmpath.Parse(path) | ||
return req.Query.Get("debug") == "1" | ||
} | ||
|
||
// ToggleURL modifies the given path's query string to toggle the `?debug=1` parameter. | ||
// If debugging is currently enabled, it removes the parameter. | ||
// If debugging is disabled, it adds the parameter. | ||
func ToggleURL(path string) string { | ||
req := realmpath.Parse(path) | ||
if IsEnabled(path) { | ||
req.Query.Del("debug") | ||
} else { | ||
req.Query.Add("debug", "1") | ||
} | ||
return req.String() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module gno.land/p/moul/debug |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package main | ||
|
||
import "gno.land/p/moul/debug" | ||
|
||
func main() { | ||
println("---") | ||
println(debug.Render("")) | ||
println("---") | ||
println(debug.Render("?debug=1")) | ||
println("---") | ||
} | ||
|
||
// Output: | ||
// --- | ||
// | ||
// --- | ||
// <details><summary>debug</summary> | ||
// | ||
// ### Metadata | ||
// | Key | Value | | ||
// | --- | --- | | ||
// | `std.CurrentRealm().PkgPath()` | | | ||
// | `std.CurrentRealm().Addr()` | g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm | | ||
// | `std.PrevRealm().PkgPath()` | | | ||
// | `std.PrevRealm().Addr()` | g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm | | ||
// | `std.GetHeight()` | 123 | | ||
// | `time.Now().Format(time.RFC3339)` | 2009-02-13T23:31:30Z | | ||
// | ||
// </details> | ||
// | ||
// --- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package main | ||
|
||
import "gno.land/p/moul/debug" | ||
|
||
func main() { | ||
var d debug.Debug | ||
d.Log("hello world!") | ||
d.Log("foobar") | ||
println("---") | ||
println(d.Render("")) | ||
println("---") | ||
println(d.Render("?debug=1")) | ||
println("---") | ||
} | ||
|
||
// Output: | ||
// --- | ||
// | ||
// --- | ||
// <details><summary>debug</summary> | ||
// | ||
// ### Logs | ||
// - hello world! | ||
// - foobar | ||
// ### Metadata | ||
// | Key | Value | | ||
// | --- | --- | | ||
// | `std.CurrentRealm().PkgPath()` | | | ||
// | `std.CurrentRealm().Addr()` | g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm | | ||
// | `std.PrevRealm().PkgPath()` | | | ||
// | `std.PrevRealm().Addr()` | g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm | | ||
// | `std.GetHeight()` | 123 | | ||
// | `time.Now().Format(time.RFC3339)` | 2009-02-13T23:31:30Z | | ||
// | ||
// </details> | ||
// | ||
// --- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module gno.land/p/moul/md |
Oops, something went wrong.