-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
179 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,3 +18,4 @@ dist | |
|
||
# Created by unit tests | ||
.pytest_cache/ | ||
/.gtm/ |
Empty file.
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,89 @@ | ||
from ovos_config import Configuration | ||
from rich.text import Text | ||
from textual.app import App, ComposeResult | ||
from textual.widgets import Header, Footer, Tree | ||
from textual.widgets.tree import TreeNode | ||
|
||
|
||
class TreeApp(App): | ||
BINDINGS = [ | ||
("a", "add", "Add node"), | ||
("c", "clear", "Clear"), | ||
("t", "toggle_root", "Toggle root"), | ||
] | ||
|
||
def compose(self) -> ComposeResult: | ||
yield Header() | ||
yield Footer() | ||
yield Tree("Root") | ||
|
||
@classmethod | ||
def add_json(cls, node: TreeNode, json_data: object) -> None: | ||
"""Adds JSON data to a node. | ||
Args: | ||
node (TreeNode): A Tree node. | ||
json_data (object): An object decoded from JSON. | ||
""" | ||
|
||
from rich.highlighter import ReprHighlighter | ||
|
||
highlighter = ReprHighlighter() | ||
|
||
def add_node(name: str, node: TreeNode, data: object) -> None: | ||
"""Adds a node to the tree. | ||
Args: | ||
name (str): Name of the node. | ||
node (TreeNode): Parent node. | ||
data (object): Data associated with the node. | ||
""" | ||
if isinstance(data, dict): | ||
node.set_label(Text(f"{{}} {name}")) | ||
for key, value in data.items(): | ||
new_node = node.add("") | ||
add_node(key, new_node, value) | ||
elif isinstance(data, list): | ||
node.set_label(Text(f"[] {name}")) | ||
for index, value in enumerate(data): | ||
new_node = node.add("") | ||
add_node(str(index), new_node, value) | ||
else: | ||
node.allow_expand = False | ||
if name: | ||
label = Text.assemble( | ||
Text.from_markup(f"[b]{name}[/b]="), highlighter(repr(data)) | ||
) | ||
else: | ||
label = Text(repr(data)) | ||
node.set_label(label) | ||
|
||
add_node("JSON", node, json_data) | ||
|
||
def on_mount(self) -> None: | ||
"""Load some JSON when the app starts.""" | ||
self.json_data = Configuration() | ||
self.action_add() | ||
self.action_toggle_root() | ||
|
||
def action_add(self) -> None: | ||
"""Add a node to the tree.""" | ||
tree = self.query_one(Tree) | ||
json_node = tree.root.add("JSON") | ||
self.add_json(json_node, self.json_data) | ||
tree.root.expand() | ||
|
||
def action_clear(self) -> None: | ||
"""Clear the tree (remove all nodes).""" | ||
tree = self.query_one(Tree) | ||
tree.clear() | ||
|
||
def action_toggle_root(self) -> None: | ||
"""Toggle the root node.""" | ||
tree = self.query_one(Tree) | ||
tree.show_root = not tree.show_root | ||
|
||
|
||
if __name__ == "__main__": | ||
app = TreeApp() | ||
app.run() |
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,89 @@ | ||
from ovos_config import Configuration | ||
from rich.text import Text | ||
from textual.app import App, ComposeResult | ||
from textual.widgets import Header, Footer, Tree | ||
from textual.widgets.tree import TreeNode | ||
|
||
|
||
class TreeApp(App): | ||
BINDINGS = [ | ||
("a", "add", "Add node"), | ||
("c", "clear", "Clear"), | ||
("t", "toggle_root", "Toggle root"), | ||
] | ||
|
||
def compose(self) -> ComposeResult: | ||
yield Header() | ||
yield Footer() | ||
yield Tree("Root") | ||
|
||
@classmethod | ||
def add_json(cls, node: TreeNode, json_data: object) -> None: | ||
"""Adds JSON data to a node. | ||
Args: | ||
node (TreeNode): A Tree node. | ||
json_data (object): An object decoded from JSON. | ||
""" | ||
|
||
from rich.highlighter import ReprHighlighter | ||
|
||
highlighter = ReprHighlighter() | ||
|
||
def add_node(name: str, node: TreeNode, data: object) -> None: | ||
"""Adds a node to the tree. | ||
Args: | ||
name (str): Name of the node. | ||
node (TreeNode): Parent node. | ||
data (object): Data associated with the node. | ||
""" | ||
if isinstance(data, dict): | ||
node.set_label(Text(f"{{}} {name}")) | ||
for key, value in data.items(): | ||
new_node = node.add("") | ||
add_node(key, new_node, value) | ||
elif isinstance(data, list): | ||
node.set_label(Text(f"[] {name}")) | ||
for index, value in enumerate(data): | ||
new_node = node.add("") | ||
add_node(str(index), new_node, value) | ||
else: | ||
node.allow_expand = False | ||
if name: | ||
label = Text.assemble( | ||
Text.from_markup(f"[b]{name}[/b]="), highlighter(repr(data)) | ||
) | ||
else: | ||
label = Text(repr(data)) | ||
node.set_label(label) | ||
|
||
add_node("JSON", node, json_data) | ||
|
||
def on_mount(self) -> None: | ||
"""Load some JSON when the app starts.""" | ||
self.json_data = Configuration() | ||
self.action_add() | ||
self.action_toggle_root() | ||
|
||
def action_add(self) -> None: | ||
"""Add a node to the tree.""" | ||
tree = self.query_one(Tree) | ||
json_node = tree.root.add("JSON") | ||
self.add_json(json_node, self.json_data) | ||
tree.root.expand() | ||
|
||
def action_clear(self) -> None: | ||
"""Clear the tree (remove all nodes).""" | ||
tree = self.query_one(Tree) | ||
tree.clear() | ||
|
||
def action_toggle_root(self) -> None: | ||
"""Toggle the root node.""" | ||
tree = self.query_one(Tree) | ||
tree.show_root = not tree.show_root | ||
|
||
|
||
if __name__ == "__main__": | ||
app = TreeApp() | ||
app.run() |