def wrap_lines(lines): for line in lines: line, color = parse_line(line) if line: for wrapped in wc_wrap(line, text_width): yield wrapped, color else: yield "", color
def print_status(status, width): reblog = status['reblog'] content = reblog['content'] if reblog else status['content'] media_attachments = reblog['media_attachments'] if reblog else status[ 'media_attachments'] in_reply_to = status['in_reply_to_id'] time = status['created_at'] time = datetime.strptime(time, "%Y-%m-%dT%H:%M:%S.%fZ") time = time.strftime('%Y-%m-%d %H:%M%z') username = "******" + status['account']['acct'] spacing = width - wcswidth(username) - wcswidth(time) display_name = status['account']['display_name'] if display_name: spacing -= wcswidth(display_name) + 1 print_out("{}{}{}{}".format( "<green>{}</green> ".format(display_name) if display_name else "", "<blue>{}</blue>".format(username), " " * spacing, "<yellow>{}</yellow>".format(time), )) for paragraph in parse_html(content): print_out("") for line in paragraph: for subline in wc_wrap(line, width): print_out(highlight_hashtags(subline)) if media_attachments: print_out("\nMedia:") for attachment in media_attachments: url = attachment['text_url'] or attachment['url'] for line in wc_wrap(url, width): print_out(line) print_out("\n{}{}{}".format( "ID <yellow>{}</yellow> ".format(status['id']), "↲ In reply to <yellow>{}</yellow> ".format(in_reply_to) if in_reply_to else "", "↻ Reblogged <blue>@{}</blue> ".format(reblog['account']['acct']) if reblog else "", ))
def test_wc_wrap_hard_wrap(): lorem = ( "☕☕☕☕☕voluptaseospraesentiumet🎸🎸🎸🎸🎸.Quaeratnihil" "voluptatemexcepturireiciendissapientevoluptatenatus.") assert list(wc_wrap(lorem, 50)) == [ #01234567890123456789012345678901234567890123456789 # noqa "☕☕☕☕☕voluptaseospraesentiumet🎸🎸🎸🎸🎸.Quaer", "atnihilvoluptatemexcepturireiciendissapientevolupt", "atenatus.", ]
def size_as_drawn(lines, screen_width): """Get the bottom-right corner of some text as would be drawn by draw_lines""" y = 0 x = 0 for line in lines: wrapped = list(wc_wrap(line, screen_width)) if len(wrapped) > 0: for wrapped_line in wrapped: x = len(wrapped_line) y += 1 else: x = 0 y += 1 return y - 1, x - 1 if x != 0 else 0
def test_wc_wrap_plain_text(): lorem = ( "Eius voluptas eos praesentium et tempore. Quaerat nihil voluptatem " "excepturi reiciendis sapiente voluptate natus. Tenetur occaecati " "velit dicta dolores. Illo reiciendis nulla ea. Facilis nostrum non " "qui inventore sit.") assert list(wc_wrap(lorem, 50)) == [ #01234567890123456789012345678901234567890123456789 # noqa "Eius voluptas eos praesentium et tempore. Quaerat", "nihil voluptatem excepturi reiciendis sapiente", "voluptate natus. Tenetur occaecati velit dicta", "dolores. Illo reiciendis nulla ea. Facilis nostrum", "non qui inventore sit.", ]
def test_wc_wrap_plain_text_wrap_on_any_whitespace(): lorem = ( "Eius\t\tvoluptas\teos\tpraesentium\tet\ttempore.\tQuaerat\tnihil\tvoluptatem\t" "excepturi\nreiciendis\n\nsapiente\nvoluptate\nnatus.\nTenetur\noccaecati\n" "velit\rdicta\rdolores.\rIllo\rreiciendis\rnulla\r\r\rea.\rFacilis\rnostrum\rnon\r" "qui\u2003inventore\u2003\u2003sit." # em space ) assert list(wc_wrap(lorem, 50)) == [ #01234567890123456789012345678901234567890123456789 # noqa "Eius voluptas eos praesentium et tempore. Quaerat", "nihil voluptatem excepturi reiciendis sapiente", "voluptate natus. Tenetur occaecati velit dicta", "dolores. Illo reiciendis nulla ea. Facilis nostrum", "non qui inventore sit.", ]