-
-
Notifications
You must be signed in to change notification settings - Fork 475
Add helper for formatting long warning messages #2563
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 2 commits
d345b65
072fcc0
748b6d0
ebee442
b8c9b3a
8e13003
907659c
f91e1e9
d5501f8
ca633ab
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| import textwrap | ||
|
|
||
|
|
||
| def format_message_to_asterisk_box( | ||
| message: str, | ||
| title: str = "", | ||
| width=80, | ||
| border_char="*", | ||
| padding=2, | ||
| ) -> str: | ||
| """Format a message to an asterisk box. | ||
|
|
||
| Args: | ||
| message (str): The message to format. | ||
| title (str, optional): The title of the box. Defaults to None. | ||
| width (int, optional): The width of the box. Defaults to 80. | ||
| border_char (str, optional): The character to use for the border. | ||
| Defaults to "*". | ||
| padding (int, optional): The number of spaces to pad the border. | ||
| Defaults to 2. | ||
|
|
||
| Returns: | ||
| str: The formatted message. | ||
| """ | ||
|
|
||
| if not message: | ||
| return "" | ||
|
|
||
| # Create border line | ||
| border_line = border_char * width | ||
| # create lines array with opening line of the box | ||
| lines = [border_line] | ||
|
|
||
| # if title exists, format title in the box | ||
| if title: | ||
| # Wrap the title to lines to fit the width of the box | ||
| wrapped_title_lines = textwrap.wrap( | ||
| title.strip(), | ||
| width=width - (padding * 2) - 4, | ||
| ) | ||
|
|
||
| # width of title line inside the box | ||
| inner_width = width - (padding * 2) | ||
|
|
||
| for line in wrapped_title_lines: | ||
| # Center each line within the available space | ||
| padded_line = line.center(inner_width) | ||
| # add line to the box | ||
| lines.append(f"**{padded_line}**") | ||
|
|
||
| # closing line of title in the box | ||
| lines.append(border_line) | ||
|
|
||
| # split the message into paragraphs | ||
| paragraphs = message.split("\n") | ||
|
||
| for paragraph in paragraphs: | ||
| if paragraph.strip(): # Non-empty paragraph | ||
| # Wrap paragraph to lines to fit the width of the box | ||
| wrapped_lines = textwrap.wrap(paragraph, width=width) | ||
| lines.extend(wrapped_lines) | ||
otekraden marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| else: # Empty paragraph (preserve blank lines) | ||
| lines.append("") | ||
|
|
||
| # closing line of message | ||
| lines.append(border_line) | ||
|
|
||
| # merge lines into a single string and return | ||
| return "\n".join(lines) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| from briefcase.utils import format_message_to_asterisk_box | ||
|
|
||
| name = "Anton" | ||
| issue_idx = 2559 | ||
|
|
||
| source_msg = ( | ||
| f"Hi there! My name is {name}, and I'm a Python developer.\n\n" | ||
| "This is my very first experience in open source development. " | ||
| "I like your project, and I'm excited to contribute to it." | ||
| ) | ||
| source_title = ( | ||
| f"INFO: I TRIED TO SOLVE THIS ISSUE #{issue_idx}. " | ||
| "IT SEEMED TO ME RATHER SIMPLE, " | ||
| "BUT I GOT VERY GOOD EXPERIENCE" | ||
| ) | ||
|
|
||
|
|
||
| def test_format_message_to_asterisk_box_empty_string_title(): | ||
| # Test with empty message and title | ||
| msg = format_message_to_asterisk_box("") | ||
|
|
||
| assert msg == "" | ||
|
|
||
|
|
||
| def test_format_message_to_asterisk_box_width_80_default(): | ||
| # Test with width 80: default | ||
| msg = format_message_to_asterisk_box( | ||
| source_msg, | ||
| title=source_title, | ||
| ) | ||
| output_msg_width_80_default = ( | ||
| "*" * 79 | ||
| + """* | ||
| ** INFO: I TRIED TO SOLVE THIS ISSUE #2559. IT SEEMED TO ME RATHER SIMPLE, ** | ||
| ** BUT I GOT VERY GOOD EXPERIENCE ** | ||
| ******************************************************************************** | ||
| Hi there! My name is Anton, and I'm a Python developer. | ||
|
|
||
| This is my very first experience in open source development. I like your | ||
| project, and I'm excited to contribute to it. | ||
| ********************************************************************************""" | ||
| ) | ||
| assert msg == output_msg_width_80_default | ||
|
|
||
|
|
||
| def test_format_message_to_asterisk_box_width_40_default(): | ||
| # Test with width 40: very narrow | ||
| msg = format_message_to_asterisk_box( | ||
| source_msg, | ||
| title=source_title, | ||
| width=40, | ||
| ) | ||
| output_msg_width_40 = """**************************************** | ||
| ** INFO: I TRIED TO SOLVE THIS ** | ||
| ** ISSUE #2559. IT SEEMED TO ME ** | ||
| ** RATHER SIMPLE, BUT I GOT VERY ** | ||
| ** GOOD EXPERIENCE ** | ||
| **************************************** | ||
| Hi there! My name is Anton, and I'm a | ||
| Python developer. | ||
|
|
||
| This is my very first experience in open | ||
| source development. I like your project, | ||
| and I'm excited to contribute to it. | ||
| ****************************************""" | ||
| assert msg == output_msg_width_40 | ||
|
|
||
|
|
||
| def test_format_message_to_asterisk_box_empty_title(): | ||
| # Test with width 80: default | ||
| msg = format_message_to_asterisk_box( | ||
| source_msg, | ||
| ) | ||
| output_msg_width_80_default = ( | ||
| "*" * 79 | ||
| + """* | ||
| Hi there! My name is Anton, and I'm a Python developer. | ||
|
|
||
| This is my very first experience in open source development. I like your | ||
| project, and I'm excited to contribute to it. | ||
| ********************************************************************************""" | ||
| ) | ||
| assert msg == output_msg_width_80_default |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We use the Sphinx docstring format; check any other source file for examples of use.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Got it!
I applied this format.