From bd4a3729ddebf4241adf43bd81aecf72811836de Mon Sep 17 00:00:00 2001 From: Itai Liba Date: Sun, 17 Sep 2023 07:31:52 -0700 Subject: [PATCH] Fix line wrapping bug where words were split between lines. --- cowsay/main.py | 16 +++------------- 1 file changed, 3 insertions(+), 13 deletions(-) diff --git a/cowsay/main.py b/cowsay/main.py index 6bc7a12..4369289 100644 --- a/cowsay/main.py +++ b/cowsay/main.py @@ -1,4 +1,5 @@ import re +import textwrap from .characters import CHARS @@ -12,21 +13,10 @@ class CowsayError(LookupError): pass -def wrap_lines(lines: list, max_width: int = 49) -> list: - - new_lines = [] - for line in lines: - for line_part in [ - line[i: i+max_width] for i in range(0, len(line), max_width) - ]: - new_lines.append(line_part) - return new_lines - - def generate_bubble(text: str) -> list: - lines = [line.strip() for line in text.split('\n')] - lines = wrap_lines([line for line in lines if line]) + text = textwrap.fill(text, width=49) + lines = [line.strip() for line in text.splitlines()] text_width = max([len(line) for line in lines]) output = [" " + "_" * text_width]