From 14a580bc190b8a9584fc2524c6ee2e2b4b44ed2d Mon Sep 17 00:00:00 2001 From: Ivan Habunek Date: Wed, 2 Jan 2019 10:49:49 +0100 Subject: [PATCH] Make toot post prompt for input if no text is given fixes #82 --- CHANGELOG.md | 1 + toot/commands.py | 6 +++++- toot/utils.py | 16 ++++++++++++++++ 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2e1e1ac..06eaef6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ Changelog * Enable interaction with instances using http instead of https (#56) * Enable proxy usage via environment variables (#47) +* Make `toot post` prompt for input if no text is given (#82) **0.19.0 (2018-06-27)** diff --git a/toot/commands.py b/toot/commands.py index 4a74dfc..55df571 100644 --- a/toot/commands.py +++ b/toot/commands.py @@ -4,7 +4,7 @@ from toot import api, config from toot.auth import login_interactive, login_browser_interactive, create_app_interactive from toot.exceptions import ConsoleError, NotFoundError from toot.output import print_out, print_instance, print_account, print_search_results, print_timeline -from toot.utils import assert_domain_exists +from toot.utils import assert_domain_exists, multiline_input, EOF_KEY def timeline(app, user, args): @@ -56,6 +56,10 @@ def post(app, user, args): if media and not args.text: args.text = media['text_url'] + if not args.text: + print_out("Write or paste your toot. Press {} to post it.".format(EOF_KEY)) + args.text = multiline_input() + if not args.text: raise ConsoleError("You must specify either text or media to post.") diff --git a/toot/utils.py b/toot/utils.py index dc22bfb..e6f241a 100644 --- a/toot/utils.py +++ b/toot/utils.py @@ -1,5 +1,6 @@ # -*- coding: utf-8 -*- +import os import re import socket import unicodedata @@ -68,3 +69,18 @@ def trunc(text, length): return text return text[:length - 1] + '…' + + +EOF_KEY = "Ctrl-Z" if os.name == 'nt' else "Ctrl-D" + + +def multiline_input(): + """Lets user input multiple lines of text, terminated by EOF.""" + lines = [] + while True: + try: + lines.append(input()) + except EOFError: + break + + return "\n".join(lines).strip()