Skip to content

Commit

Permalink
Configurable default connector style
Browse files Browse the repository at this point in the history
  • Loading branch information
mar10 committed Feb 22, 2023
1 parent c15a20d commit 61adf84
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 9 deletions.
8 changes: 6 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
# Changelog

## 0.4.0 (unreleased)
## 0.4.1 (Unreleased)

## 0.4.0 (2023-02-22)

- BREAKING: Rename `node.move()` -> `node.move_to()`
- New `node.copy_to()`
- New `tree.copy_to()` and `node.copy_to()`
- New `tree.children` property
- Configurable default connector style


## 0.3.0 (2022-08-01)
Expand Down
15 changes: 11 additions & 4 deletions nutree/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,25 +86,32 @@ def __init__(self, value=None):

#: Node connector prefixes, for use with ``format(style=...)`` argument.
CONNECTORS = {
"space1": (" ", " ", " ", " "),
"space2": (" ", " ", " ", " "),
"space3": (" ", " ", " ", " "),
"space4": (" ", " | ", " ", " "),
"ascii11": (" ", "|", "`", "-"),
"ascii21": (" ", "| ", "` ", "- "),
"ascii22": (" ", "| ", "`-", "+-"),
"ascii32": (" ", "| ", "`- ", "+- "),
"ascii42": (" ", " | ", " `- ", " +- "),
"ascii43": (" ", "| ", "`-- ", "+-- "),
"lines11": (" ", "│", "└", "├"),
"lines21": (" ", "│ ", "└ ", "├ "),
"lines22": (" ", "│ ", "└─", "├─"),
"lines32": (" ", "│ ", "└─ ", "├─ "),
"lines42": (" ", " │ ", " └─ ", " ├─ "),
"lines43": (" ", " │ ", " └──", " ├──"),
"lines43": (" ", "│ ", "└── ", "├── "),
"lines43r": (" ", " │ ", " └──", " ├──"),
"round11": (" ", "│", "╰", "├"),
"round21": (" ", "│ ", "╰ ", "├ "),
"round22": (" ", "│ ", "╰─", "├─"),
"round32": (" ", "│ ", "╰─ ", "├─ "),
"round42": (" ", " │ ", " ╰─ ", " ├─ "),
"round43": (" ", "│ ", "╰── ", "├── "),
"round43r": (" ", " │ ", " ╰──", " ├──"),
}

#: Default connector prefixes ``format(style=...)`` argument.
DEFAULT_CONNECTOR_STYLE = "round43"


def call_mapper(fn, node: "Node", data: dict) -> Any:
"""Call the function and normalize result and exceptions.
Expand Down
3 changes: 1 addition & 2 deletions nutree/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

from .common import (
CONNECTORS,
DEFAULT_CONNECTOR_STYLE,
AmbiguousMatchError,
ItemIdType,
IterMethod,
Expand Down Expand Up @@ -1138,7 +1137,7 @@ def _is_last(p):
def _render_lines(self, *, repr=None, style=None, add_self=True):
if type(style) not in (list, tuple):
try:
style = CONNECTORS[style or DEFAULT_CONNECTOR_STYLE]
style = CONNECTORS[style or self.tree.default_connector_style]
except KeyError:
raise ValueError(
f"Invalid style {style!r}. Expected: {'|'.join(CONNECTORS.keys())}"
Expand Down
3 changes: 3 additions & 0 deletions nutree/tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ class Tree:
Trees expose methods to iterate, search, copy, filter, serialize, etc.
"""

#: Default connector prefixes ``format(style=...)`` argument.
default_connector_style = "round43"

def __init__(self, name: str = None, *, factory=None, calc_data_id=None):
self._lock = threading.RLock()
self.name = str(id(self) if name is None else name)
Expand Down
2 changes: 1 addition & 1 deletion nutree/typed_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@ def iterator(
# def _render_lines(self, *, repr=None, style=None, add_self=True):
# if type(style) not in (list, tuple):
# try:
# style = CONNECTORS[style or DEFAULT_CONNECTOR_STYLE]
# style = CONNECTORS[style or self.tree.default_connector_style]
# except KeyError:
# raise ValueError(
# f"Invalid style '{style}'. Expected: {'|'.join(CONNECTORS.keys())}"
Expand Down
4 changes: 4 additions & 0 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ def test_add_child(self):
""",
)

# for style in CONNECTORS.keys():
# print(tree.format(repr="{node.data}", style=style))
# raise

def test_meta(aelf):
tree = fixture.create_tree()
node = tree.first_child()
Expand Down

0 comments on commit 61adf84

Please sign in to comment.