1010from .._cache import _add_slashes , _check_relative_pathname
1111
1212
13+ def _print_wrapped_string (message , filename , end , print_wrap_limit = 100 ):
14+ """
15+ Prints a string, splitting it into two lines if it exceeds the threshold, with the second half
16+ being indented based on the initial indentation level.
17+
18+ :param message: The string to print.
19+ :type message: str
20+ :param filename: The filename to be included in the string.
21+ :type filename: str
22+ :param end:
23+ :param print_wrap_limit: The maximum length before splitting.
24+ :type print_wrap_limit: int | None
25+ """
26+
27+ if print_wrap_limit :
28+ stripped_text = message .lstrip () # Remove leading spaces for accurate tab count
29+ leading_tabs = len (message ) - len (stripped_text )
30+
31+ if len (stripped_text ) <= print_wrap_limit :
32+ print (message , end = end )
33+
34+ else :
35+ # Find a suitable split point (preferably at a space)
36+ split_index = message .find (f'"{ filename } "' ) + len (f'"{ filename } "' )
37+ if message [split_index ] == " " :
38+ split_index += 1 # Move to space if it exists
39+
40+ first_part = message [:split_index ].rstrip () # Trim trailing spaces
41+ second_part = message [split_index :].lstrip () # Trim leading spaces
42+
43+ # Print first part as is
44+ print (first_part + " ... " )
45+
46+ # Print second part with increased indentation (original + 1 extra tab)
47+ print ("\t " * (leading_tabs + 1 ) + second_part , end = end )
48+
49+ else :
50+ print (message , end = end )
51+
52+
1353def _check_saving_path (path_to_file , verbose = False , print_prefix = "" , state_verb = "Saving" ,
14- state_prep = "to" , print_suffix = "" , print_end = " ... " , belated = False ,
15- ret_info = False ):
54+ state_prep = "to" , print_suffix = "" , print_end = " ... " , print_wrap_limit = None ,
55+ belated = False , ret_info = False ):
1656 # noinspection PyShadowingNames
1757 """
1858 Verifies a specified file path before saving.
@@ -32,6 +72,11 @@ def _check_saving_path(path_to_file, verbose=False, print_prefix="", state_verb=
3272 :type print_suffix: str
3373 :param print_end: String passed to the ``end`` parameter of ``print``; defaults to ``" ... "``.
3474 :type print_end: str
75+ :param print_wrap_limit: Maximum length of the string before splitting into two lines;
76+ defaults to ``None``, which disables splitting. If the string exceeds this value,
77+ e.g. ``100``, it will be split at (before) ``state_prep`` to improve readability
78+ when printed.
79+ :type print_wrap_limit: int | None
3580 :param ret_info: Whether to return file path information; defaults to ``False``.
3681 :type ret_info: bool
3782 :return: A tuple containing the relative path and, if ``ret_info=True``, the filename.
@@ -52,6 +97,9 @@ def _check_saving_path(path_to_file, verbose=False, print_prefix="", state_verb=
5297 >>> path_to_file = cd("tests", "documents", "pyhelpers.pdf")
5398 >>> _check_saving_path(path_to_file, verbose=True); print("Passed.")
5499 Saving "pyhelpers.pdf" in "./tests/documents/" ... Passed.
100+ >>> _check_saving_path(path_to_file, verbose=True, print_wrap_limit=10); print("Passed.")
101+ Updating "pyhelpers.pdf" ...
102+ in "./tests/documents/" ... Passed.
55103 >>> path_to_file = "C:\\ Windows\\ pyhelpers.pdf"
56104 >>> _check_saving_path(path_to_file, verbose=True); print("Passed.")
57105 Saving "pyhelpers.pdf" to "C:/Windows/" ... Passed.
@@ -90,12 +138,15 @@ def _check_saving_path(path_to_file, verbose=False, print_prefix="", state_verb=
90138
91139 if (rel_dir_path == rel_dir_path .parent or rel_dir_path == abs_path_to_file .parent ) and (
92140 rel_dir_path .absolute ().drive == pathlib .Path .cwd ().drive ):
93- msg = f'{ print_prefix } { state_verb } "{ filename } "{ print_suffix } '
94- print (msg , end = end )
141+ message = f'{ print_prefix } { state_verb } "{ filename } "{ print_suffix } '
142+ print (message , end = end )
143+
95144 else :
96- msg = (f'{ print_prefix } { state_verb } "{ filename } " '
97- f'{ state_prep } { _add_slashes (rel_dir_path )} { print_suffix } ' )
98- print (msg , end = end )
145+ message = (f'{ print_prefix } { state_verb } "{ filename } " '
146+ f'{ state_prep } { _add_slashes (rel_dir_path )} { print_suffix } ' )
147+
148+ _print_wrapped_string (
149+ message = message , filename = filename , end = end , print_wrap_limit = print_wrap_limit )
99150
100151 if ret_info :
101152 return rel_dir_path , filename
0 commit comments