Skip to content

Commit

Permalink
feat: add p/moul/{md,debug,web25} + update r/moul/home (gnolang#2819
Browse files Browse the repository at this point in the history
)

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
moul authored and Villaquiranm committed Dec 9, 2024
1 parent 3ec27c4 commit f54795f
Show file tree
Hide file tree
Showing 17 changed files with 778 additions and 38 deletions.
12 changes: 10 additions & 2 deletions contribs/gnodev/cmd/gnodev/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ type devCfg struct {
// Web Configuration
webListenerAddr string
webRemoteHelperAddr string
webWithHTML bool

// Node Configuration
minimal bool
Expand Down Expand Up @@ -126,14 +127,21 @@ func (c *devCfg) RegisterFlags(fs *flag.FlagSet) {
&c.webListenerAddr,
"web-listener",
defaultDevOptions.webListenerAddr,
"web server listener address",
"gnoweb: web server listener address",
)

fs.StringVar(
&c.webRemoteHelperAddr,
"web-help-remote",
defaultDevOptions.webRemoteHelperAddr,
"web server help page's remote addr (default to <node-rpc-listener>)",
"gnoweb: web server help page's remote addr (default to <node-rpc-listener>)",
)

fs.BoolVar(
&c.webWithHTML,
"web-with-html",
defaultDevOptions.webWithHTML,
"gnoweb: enable HTML parsing in markdown rendering",
)

fs.StringVar(
Expand Down
1 change: 1 addition & 0 deletions contribs/gnodev/cmd/gnodev/setup_web.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ func setupGnoWebServer(logger *slog.Logger, cfg *devCfg, dnode *gnodev.Node) htt
webConfig.HelpChainID = cfg.chainId
webConfig.RemoteAddr = dnode.GetRemoteAddress()
webConfig.HelpRemote = cfg.webRemoteHelperAddr
webConfig.WithHTML = cfg.webWithHTML

// If `HelpRemote` is empty default it to `RemoteAddr`
if webConfig.HelpRemote == "" {
Expand Down
92 changes: 92 additions & 0 deletions examples/gno.land/p/moul/debug/debug.gno
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()
}
1 change: 1 addition & 0 deletions examples/gno.land/p/moul/debug/gno.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module gno.land/p/moul/debug
31 changes: 31 additions & 0 deletions examples/gno.land/p/moul/debug/z1_filetest.gno
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>
//
// ---
37 changes: 37 additions & 0 deletions examples/gno.land/p/moul/debug/z2_filetest.gno
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>
//
// ---
1 change: 1 addition & 0 deletions examples/gno.land/p/moul/md/gno.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module gno.land/p/moul/md
Loading

0 comments on commit f54795f

Please sign in to comment.