Skip to content

Commit

Permalink
feat(render): render index and namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
anarcher committed Nov 5, 2022
1 parent c35a86a commit 3271a9e
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 2 deletions.
53 changes: 51 additions & 2 deletions examples/gno.land/r/system/names/names.gno
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"std"

"gno.land/p/demo/avl"
"gno.land/p/demo/ufmt"
)

// "AddPkg" will check if r/system/names exists. If yes, it will
Expand Down Expand Up @@ -88,7 +89,55 @@ func SetInPause(namespace string, state bool) {
}

func Render(path string) string {
// TODO: by namespace.
// TODO: by address.
return "not implemented"

if path == "" {
return renderIndex()
} else if path[:2] == "n/" {
return renderNamespace(path[2:])
}
return ""
}

func renderNamespace(namespace string) string {
space := getSpace(namespace)
output := ufmt.Sprintf(`
# %s
## Admins
%s
## Editors
%s
## InPause
%s
`, namespace, renderAddresses(space.Admins), renderAddresses(space.Editors), formatBool(space.InPause))
return output

}

func renderIndex() string {
output := "## Namespaces \n"
namespaces.Iterate("", "", func(n *avl.Tree) bool {
namespace := n.Key()
space := n.Value().(*Space)
output += ufmt.Sprintf("* [%s](/r/system/names:n/%s) - admins: %d editors: %d inPause: %s \n",
namespace, namespace, len(space.Admins), len(space.Editors), formatBool(space.InPause))
return false
})

return output
}

func renderAddresses(addresses []std.Address) string {
output := ""
for _, address := range addresses {
output += ufmt.Sprintf("* %s \n", string(address))
}
return output
}
7 changes: 7 additions & 0 deletions examples/gno.land/r/system/names/utils.gno
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,10 @@ func containsAddress(addrs []std.Address, addr std.Address) bool {
}
return false
}

func formatBool(b bool) string {
if b {
return "true"
}
return "false"
}

0 comments on commit 3271a9e

Please sign in to comment.