Skip to content

Commit

Permalink
Implement configurable character_limit for tweets (closes #4)
Browse files Browse the repository at this point in the history
  • Loading branch information
buckket committed Feb 16, 2016
1 parent a4f87dd commit 3ef2e13
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 14 deletions.
3 changes: 3 additions & 0 deletions docs/user/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Here’s an example ``conf`` file, showing every currently supported option:
use_cache = True
porcelain = False
disclose_identity = False
character_limit = 140
limit_timeline = 20
timeout = 5.0
sorting = descending
Expand Down Expand Up @@ -52,6 +53,8 @@ Here’s an example ``conf`` file, showing every currently supported option:
+-------------------+-------+------------+---------------------------------------------------+
| disclose_identity | BOOL | False | include nick and twturl in twtxt’s user-agent |
+-------------------+-------+------------+---------------------------------------------------+
| character_limit | INT | None | limit amount of characters a tweet can have |
+-------------------+-------+------------+---------------------------------------------------+
| limit_timeline | INT | 20 | limit amount of tweets shown in your timeline |
+-------------------+-------+------------+---------------------------------------------------+
| timeout | FLOAT | 5.0 | maximal time a http request is allowed to take |
Expand Down
6 changes: 5 additions & 1 deletion tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ def config_dir(tmpdir_factory):
cfg.set("twtxt", "use_pager", "True")
cfg.set("twtxt", "use_cache", "False")
cfg.set("twtxt", "porcelain", "True")
cfg.set("twtxt", "character_limit", "150")
cfg.set("twtxt", "limit_timeline", "50")
cfg.set("twtxt", "timeout", "1.0")
cfg.set("twtxt", "sorting", "ascending")
Expand Down Expand Up @@ -47,6 +48,7 @@ def test_defaults():
assert empty_conf.use_pager is False
assert empty_conf.use_cache is True
assert empty_conf.porcelain is False
assert empty_conf.character_limit is None
assert empty_conf.limit_timeline == 20
assert empty_conf.timeout == 5.0
assert empty_conf.sorting == "descending"
Expand All @@ -62,6 +64,7 @@ def check_cfg(cfg):
assert cfg.use_pager is True
assert cfg.use_cache is False
assert cfg.porcelain is True
assert cfg.character_limit == 150
assert cfg.limit_timeline == 50
assert cfg.timeout == 1.0
assert cfg.sorting == "ascending"
Expand Down Expand Up @@ -96,9 +99,10 @@ def test_create_config(config_dir):
conf_r = Config.discover()
assert conf_r.nick == "bar"
assert conf_r.twtfile == "batz.txt"
assert conf_r.character_limit == 140
assert conf_r.following[0].nick == "twtxt"
assert conf_r.following[0].url == "https://buckket.org/twtxt_news.txt"
assert set(conf_r.options.keys()) == {"nick", "twtfile"}
assert set(conf_r.options.keys()) == {"nick", "twtfile", "character_limit"}

conf_r.cfg.remove_section("twtxt")
assert conf_r.options == {}
Expand Down
6 changes: 0 additions & 6 deletions tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,6 @@ def test_tweet_relative_datetime():
assert tweet.relative_datetime == "an hour ago"


def test_tweet_limited_text():
tweet = Tweet("A " * 100)
assert tweet.text == "A " * 100
assert len(tweet.limited_text) <= 140


def test_tweet_ordering():
now = datetime.now(timezone.utc)
tweet_1 = Tweet("A", now)
Expand Down
5 changes: 5 additions & 0 deletions twtxt/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ def create_config(cls, nick, twtfile, add_news):
cfg.add_section("twtxt")
cfg.set("twtxt", "nick", nick)
cfg.set("twtxt", "twtfile", twtfile)
cfg.set("twtxt", "character_limit", "140")

cfg.add_section("following")
if add_news:
Expand Down Expand Up @@ -140,6 +141,10 @@ def porcelain(self):
def disclose_identity(self):
return self.cfg.getboolean("twtxt", "disclose_identity", fallback=False)

@property
def character_limit(self):
return self.cfg.getint("twtxt", "character_limit", fallback=None)

@property
def limit_timeline(self):
return self.cfg.getint("twtxt", "limit_timeline", fallback=20)
Expand Down
6 changes: 5 additions & 1 deletion twtxt/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ def style_timeline(tweets, porcelain=False):


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,
Expand All @@ -36,9 +39,10 @@ def style_tweet(tweet, porcelain=False):
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
return "➤ {nick} ({time}):\n{tweet}".format(
nick=click.style(tweet.source.nick, bold=True),
tweet=textwrap.shorten(styled_text, 140 + len_styling),
tweet=final_text,
time=click.style(tweet.relative_datetime, dim=True))


Expand Down
6 changes: 0 additions & 6 deletions twtxt/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
:license: MIT, see LICENSE for more details.
"""

import textwrap
from datetime import datetime, timezone

import humanize
Expand Down Expand Up @@ -79,11 +78,6 @@ def relative_datetime(self):
tense = "from now" if self.created_at > now else "ago"
return "{0} {1}".format(humanize.naturaldelta(now - self.created_at), tense)

@property
def limited_text(self):
"""Shortened tweet text. (140 characters)"""
return textwrap.shorten(self.text, 140)


class Source:
"""A :class:`Source` represents a twtxt feed, remote as well as local.
Expand Down

0 comments on commit 3ef2e13

Please sign in to comment.