mirror of
https://github.com/ihabunek/toot
synced 2025-02-11 01:20:53 +01:00
Store temp file when using editor to post
In case of failed posting the status is not lost and the user can recover it and continue posting. fixes #311
This commit is contained in:
parent
bd8ec053b6
commit
00baabf7aa
@ -9,7 +9,7 @@ from toot.output import (print_out, print_instance, print_account, print_acct_li
|
|||||||
print_search_results, print_timeline, print_notifications,
|
print_search_results, print_timeline, print_notifications,
|
||||||
print_tag_list)
|
print_tag_list)
|
||||||
from toot.tui.utils import parse_datetime
|
from toot.tui.utils import parse_datetime
|
||||||
from toot.utils import editor_input, multiline_input, EOF_KEY
|
from toot.utils import delete_tmp_status_file, editor_input, multiline_input, EOF_KEY
|
||||||
|
|
||||||
|
|
||||||
def get_timeline_generator(app, user, args):
|
def get_timeline_generator(app, user, args):
|
||||||
@ -111,6 +111,8 @@ def post(app, user, args):
|
|||||||
else:
|
else:
|
||||||
print_out(f"Toot posted: <green>{response['url']}")
|
print_out(f"Toot posted: <green>{response['url']}")
|
||||||
|
|
||||||
|
delete_tmp_status_file()
|
||||||
|
|
||||||
|
|
||||||
def _get_status_text(text, editor):
|
def _get_status_text(text, editor):
|
||||||
isatty = sys.stdin.isatty()
|
isatty = sys.stdin.isatty()
|
||||||
|
@ -100,17 +100,52 @@ Everything below it will be ignored.
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
def editor_input(editor, initial_text):
|
def editor_input(editor: str, initial_text: str):
|
||||||
"""Lets user input text using an editor."""
|
"""Lets user input text using an editor."""
|
||||||
|
tmp_path = _tmp_status_path()
|
||||||
initial_text = (initial_text or "") + EDITOR_INPUT_INSTRUCTIONS
|
initial_text = (initial_text or "") + EDITOR_INPUT_INSTRUCTIONS
|
||||||
|
|
||||||
with tempfile.NamedTemporaryFile(suffix='.toot') as f:
|
if not _use_existing_tmp_file(tmp_path):
|
||||||
f.write(initial_text.encode())
|
with open(tmp_path, "w") as f:
|
||||||
f.flush()
|
f.write(initial_text)
|
||||||
|
f.flush()
|
||||||
|
|
||||||
subprocess.run([editor, f.name])
|
subprocess.run([editor, tmp_path])
|
||||||
|
|
||||||
f.seek(0)
|
with open(tmp_path) as f:
|
||||||
text = f.read().decode()
|
return f.read().split(EDITOR_DIVIDER)[0].strip()
|
||||||
|
|
||||||
return text.split(EDITOR_DIVIDER)[0].strip()
|
|
||||||
|
def read_char(values, default):
|
||||||
|
values = [v.lower() for v in values]
|
||||||
|
|
||||||
|
while True:
|
||||||
|
value = input().lower()
|
||||||
|
if value == "":
|
||||||
|
return default
|
||||||
|
if value in values:
|
||||||
|
return value
|
||||||
|
|
||||||
|
|
||||||
|
def delete_tmp_status_file():
|
||||||
|
try:
|
||||||
|
os.unlink(_tmp_status_path())
|
||||||
|
except FileNotFoundError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
def _tmp_status_path() -> str:
|
||||||
|
tmp_dir = tempfile.gettempdir()
|
||||||
|
return f"{tmp_dir}/.status.toot"
|
||||||
|
|
||||||
|
|
||||||
|
def _use_existing_tmp_file(tmp_path) -> bool:
|
||||||
|
from toot.output import print_out
|
||||||
|
|
||||||
|
if os.path.exists(tmp_path):
|
||||||
|
print_out(f"<cyan>Found a draft status at: {tmp_path}</cyan>")
|
||||||
|
print_out("<cyan>[O]pen (default) or [D]elete?</cyan> ", end="")
|
||||||
|
char = read_char(["o", "d"], "o")
|
||||||
|
return char == "o"
|
||||||
|
|
||||||
|
return False
|
||||||
|
Loading…
x
Reference in New Issue
Block a user