def test_format_mentions(): texts = { 'No Mention': 'No Mention', '@<SomeName http://some.url/twtxt.txt>': ('SomeName', 'http://some.url/twtxt.txt'), '@<Some>Shitty<Name http://some.url/twtxt.txt>': ('Some>Shitty<Name', 'http://some.url/twtxt.txt'), '@<http://some.url/twtxt.txt>': (None, 'http://some.url/twtxt.txt'), '@<SomeName>': '@<SomeName>', '@SomeName': '@SomeName' } for input, expected in texts.items(): if isinstance(expected, tuple): format_mentions( input, partial(mock_mention_format, expected_name=expected[0], expected_url=expected[1])) else: assert expected == format_mentions( input, partial(mock_mention_format, expected_name=None, expected_url=None))
def test_format_multi_mentions(): text = '@<SomeName http://url> and another @<AnotherName http://another/url> end' mentions = (('SomeName', 'http://url'), ('AnotherName', 'http://another/url')) def mock_multi_mention_format(name, url): return '@' + name format_mentions(text, mock_multi_mention_format)
def test_format_multi_mentions_incomplete(): text = '@<http://url> and another @<AnotherName http://another/url> end' mentions = ((None, 'http://url'), ('AnotherName', 'http://another/url')) def mock_multi_mention_format(name, url): if name: return '@' + name else: return '@' + url format_mentions(text, mock_multi_mention_format) text = '@<SomeName http://url> and another @<http://another/url> end' mentions = (('SomeName', 'http://url'), (None, 'http://another/url'))
def style_tweet(tweet, porcelain=False): if porcelain: return "{nick}\t{url}\t{tweet}".format(nick=tweet.source.nick, url=tweet.source.url, tweet=str(tweet)) else: styled_text = format_mentions(tweet.text) len_styling = len(styled_text) - len(click.unstyle(styled_text)) return "➤ {nick} ({time}):\n{tweet}".format( nick=click.style(tweet.source.nick, bold=True), tweet=textwrap.shorten(styled_text, 140 + len_styling), time=click.style(tweet.relative_datetime, dim=True))
def style_tweet(tweet, porcelain=False): conf = click.get_current_context().obj["conf"] limit = conf.character_limit if porcelain: return "{nick}\t{url}\t{tweet}".format(nick=tweet.source.nick, url=tweet.source.url, tweet=str(tweet)) else: styled_text = format_mentions(tweet.text) len_styling = len(styled_text) - len(click.unstyle(styled_text)) final_text = textwrap.shorten(styled_text, limit + len_styling) if limit else styled_text timestamp = tweet.absolute_datetime if conf.use_abs_time else tweet.relative_datetime return "➤ {nick} ({time}):\n{tweet}".format(nick=click.style( tweet.source.nick, bold=True), tweet=final_text, time=click.style(timestamp, dim=True))