Skip to content

Commit

Permalink
custom offset for the enriched print/logging messages
Browse files Browse the repository at this point in the history
  • Loading branch information
rsalmei committed Oct 26, 2024
1 parent e48bba8 commit 62ab72b
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 12 deletions.
4 changes: 3 additions & 1 deletion alive_progress/core/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ def _input(x):


Config = namedtuple('Config', 'title length max_cols spinner bar unknown force_tty disable manual '
'enrich_print receipt receipt_text monitor elapsed stats '
'enrich_print enrich_offset receipt receipt_text monitor elapsed stats '
'title_length spinner_length refresh_secs monitor_end elapsed_end '
'stats_end ctrl_c dual_line unit scale precision file')

Expand All @@ -155,6 +155,7 @@ def reset():
disable=False,
manual=False,
enrich_print=True,
enrich_offset=0,
receipt=True,
receipt_text=False,
monitor=True,
Expand Down Expand Up @@ -233,6 +234,7 @@ def lazy_init():
disable=_bool_input_factory(),
manual=_bool_input_factory(),
enrich_print=_bool_input_factory(),
enrich_offset=_int_input_factory(0, sys.maxsize),
receipt=_bool_input_factory(),
receipt_text=_bool_input_factory(),
monitor=_format_input_factory('count total percent'),
Expand Down
18 changes: 9 additions & 9 deletions alive_progress/core/hook_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@
ENCODING = sys.getdefaultencoding()


def buffered_hook_manager(header_template, get_pos, cond_refresh, term):
def buffered_hook_manager(header_template, get_pos, offset, cond_refresh, term):
"""Create and maintain a buffered hook manager, used for instrumenting print
statements and logging.
Args:
header_template (): the template for enriching output
get_pos (Callable[..., Any]): the container to retrieve the current position
offset (int): the offset to add to the current position
cond_refresh: Condition object to force a refresh when printing
term: the current terminal
Expand Down Expand Up @@ -121,7 +122,7 @@ def uninstall():

# internal data.
buffers = defaultdict(list)
get_header = gen_header(header_template, get_pos) if header_template else null_header
get_header = gen_header(header_template, get_pos, offset)
base = sys.stdout, sys.stderr # needed for tests.
before_handlers = {}

Expand Down Expand Up @@ -154,12 +155,11 @@ def __noop(): # pragma: no cover
pass


def gen_header(header_template, get_pos): # pragma: no cover
def inner():
return header_template.format(get_pos())
def gen_header(header_template, get_pos, offset): # pragma: no cover
def header():
return header_template.format(get_pos() + offset)

return inner
def null_header():
return ''


def null_header(): # pragma: no cover
return ''
return header if header_template else null_header
5 changes: 3 additions & 2 deletions alive_progress/core/progress.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ def alive_bar(total: Optional[int] = None, *, calibrate: Optional[int] = None, *
disable (bool): if True, completely disables all output, do not install hooks
manual (bool): set to manually control the bar position
enrich_print (bool): enriches print() and logging messages with the bar position
enrich_offset (int): the offset to apply to enrich_print
receipt (bool): prints the nice final receipt, disables if False
receipt_text (bool): set to repeat the last text message in the final receipt
monitor (bool|str): configures the monitor widget `152/200 [76%]`
Expand Down Expand Up @@ -243,8 +244,8 @@ def pause_monitoring():
term, hook_manager = terminal.get_void(), passthrough_hook_manager()
else:
term = terminal.get_term(config.file, config.force_tty, config.max_cols)
hook_manager = buffered_hook_manager(
header if config.enrich_print else '', current, cond_refresh, term)
hook_manager = buffered_hook_manager(header if config.enrich_print else '',
current, config.enrich_offset, cond_refresh, term)

if term.interactive:
thread = threading.Thread(target=run, args=_create_spinner_player(config))
Expand Down

0 comments on commit 62ab72b

Please sign in to comment.