From 2bd8bf16d7d55fcaeed5176fbc330c36f4931dee Mon Sep 17 00:00:00 2001 From: Tyler Goodlet Date: Tue, 17 Jun 2025 17:02:21 -0400 Subject: [PATCH] Re-impl `.devx.nest_from_op()` yet again XD Apparently having lots of trouble getting the nested indenting correct.. shh This hopefully resolves it by doing the indent calcs incrementally in the order of, - `input_op: str` the "sclang" operator chars, - `nest_prefix: str` the hierarchy chars, by def our little sub-tree L: '|_', - finally the `tree_str` so there's no-overlap-/adjacency-to the `nest_prefix`. Also deprecate (kinda) the `back_from_op` param, `nest_indent` is more or less the replacement. --- tractor/_entry.py | 2 +- tractor/devx/pformat.py | 27 +++++++++++++++++---------- 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/tractor/_entry.py b/tractor/_entry.py index 400ce66d..18bee7a6 100644 --- a/tractor/_entry.py +++ b/tractor/_entry.py @@ -135,7 +135,7 @@ def _trio_main( f' loglevel: {actor.loglevel}\n' ) log.info( - 'Starting new `trio` subactor:\n' + 'Starting new `trio` subactor\n' + pformat.nest_from_op( input_op='>(', # see syntax ideas above diff --git a/tractor/devx/pformat.py b/tractor/devx/pformat.py index f2973441..a3cfa5f4 100644 --- a/tractor/devx/pformat.py +++ b/tractor/devx/pformat.py @@ -249,8 +249,6 @@ def pformat_cs( ) -# TODO: move this func to some kinda `.devx.pformat.py` eventually -# as we work out our multi-domain state-flow-syntax! def nest_from_op( input_op: str, # @@ -328,7 +326,8 @@ def nest_from_op( # NOTE: so move back-from-the-left of the `input_op` by # this amount. back_from_op: int = 0, - nest_prefix: str = '' + nest_prefix: str = '|_', + nest_indent: int = 0, ) -> str: ''' @@ -338,21 +337,29 @@ def nest_from_op( `tree_str` to nest content aligned with the ops last char. ''' + nest_indent = nest_indent or back_from_op # <- rm latter! + if ( + nest_prefix + and + nest_indent + ): + nest_prefix: str = textwrap.indent( + nest_prefix, + prefix=nest_indent*' ', + ) + + tree_str_indent: int = len(nest_prefix) indented_tree_str: str = textwrap.indent( tree_str, - prefix=' ' *( - len(input_op) - - - (back_from_op + 1) - ), + prefix=' '*tree_str_indent ) # inject any provided nesting-prefix chars # into the head of the first line. if nest_prefix: indented_tree_str: str = ( - f'{nest_prefix}' - f'{indented_tree_str[len(nest_prefix):]}' + f'{nest_prefix}{indented_tree_str[tree_str_indent:]}' ) + return ( f'{input_op}\n' f'{indented_tree_str}'