2017-04-21 20:23:48 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
2019-03-02 02:04:45 +01:00
|
|
|
import os
|
2019-04-24 14:05:28 +02:00
|
|
|
import webbrowser
|
2017-04-21 20:23:48 +02:00
|
|
|
|
2019-01-03 17:08:02 +01:00
|
|
|
from toot import __version__, api
|
2018-01-22 10:07:18 +01:00
|
|
|
|
2017-12-30 13:32:52 +01:00
|
|
|
from toot.exceptions import ConsoleError
|
2018-06-12 12:03:49 +02:00
|
|
|
from toot.ui.parsers import parse_status
|
2019-02-28 02:10:27 +01:00
|
|
|
from toot.ui.utils import draw_horizontal_divider, draw_lines, size_as_drawn
|
2019-04-23 13:31:17 +02:00
|
|
|
from toot.wcstring import fit_text
|
2017-04-24 16:25:34 +02:00
|
|
|
|
2017-09-09 09:54:13 +02:00
|
|
|
# Attempt to load curses, which is not available on windows
|
|
|
|
try:
|
|
|
|
import curses
|
2018-01-22 10:07:18 +01:00
|
|
|
import curses.panel
|
2019-02-27 01:29:34 +01:00
|
|
|
import curses.textpad
|
2019-02-14 15:47:40 +01:00
|
|
|
except ImportError:
|
2017-09-09 09:54:13 +02:00
|
|
|
raise ConsoleError("Curses is not available on this platform")
|
|
|
|
|
2017-04-21 20:23:48 +02:00
|
|
|
|
|
|
|
class Color:
|
2018-01-04 13:17:34 +01:00
|
|
|
@classmethod
|
|
|
|
def setup_palette(class_):
|
2018-01-06 17:32:27 +01:00
|
|
|
curses.init_pair(1, curses.COLOR_WHITE, curses.COLOR_BLACK)
|
|
|
|
curses.init_pair(2, curses.COLOR_BLUE, curses.COLOR_BLACK)
|
|
|
|
curses.init_pair(3, curses.COLOR_GREEN, curses.COLOR_BLACK)
|
|
|
|
curses.init_pair(4, curses.COLOR_YELLOW, curses.COLOR_BLACK)
|
|
|
|
curses.init_pair(5, curses.COLOR_RED, curses.COLOR_BLACK)
|
2018-01-21 14:30:05 +01:00
|
|
|
curses.init_pair(6, curses.COLOR_CYAN, curses.COLOR_BLACK)
|
|
|
|
curses.init_pair(7, curses.COLOR_MAGENTA, curses.COLOR_BLACK)
|
|
|
|
curses.init_pair(8, curses.COLOR_WHITE, curses.COLOR_BLUE)
|
|
|
|
curses.init_pair(9, curses.COLOR_WHITE, curses.COLOR_RED)
|
2018-01-06 17:32:27 +01:00
|
|
|
|
|
|
|
class_.WHITE = curses.color_pair(1)
|
|
|
|
class_.BLUE = curses.color_pair(2)
|
|
|
|
class_.GREEN = curses.color_pair(3)
|
|
|
|
class_.YELLOW = curses.color_pair(4)
|
|
|
|
class_.RED = curses.color_pair(5)
|
2018-01-21 14:30:05 +01:00
|
|
|
class_.CYAN = curses.color_pair(6)
|
|
|
|
class_.MAGENTA = curses.color_pair(7)
|
|
|
|
class_.WHITE_ON_BLUE = curses.color_pair(8)
|
|
|
|
class_.WHITE_ON_RED = curses.color_pair(9)
|
2018-01-06 17:32:27 +01:00
|
|
|
|
2018-01-21 15:45:07 +01:00
|
|
|
class_.HASHTAG = class_.BLUE | curses.A_BOLD
|
|
|
|
|
2018-01-06 17:32:27 +01:00
|
|
|
|
2018-01-08 14:02:52 +01:00
|
|
|
class HeaderWindow:
|
2018-01-22 10:07:18 +01:00
|
|
|
def __init__(self, stdscr, height, width, y, x):
|
|
|
|
self.window = stdscr.subwin(height, width, y, x)
|
2019-04-23 13:53:06 +02:00
|
|
|
self.window.bkgdset(' ', Color.WHITE_ON_BLUE)
|
2018-01-08 14:02:52 +01:00
|
|
|
self.height = height
|
|
|
|
self.width = width
|
|
|
|
|
2019-04-23 13:53:06 +02:00
|
|
|
def draw(self, user):
|
|
|
|
username = "{}@{}".format(user.username, user.instance)
|
|
|
|
|
2018-01-08 14:02:52 +01:00
|
|
|
self.window.erase()
|
2019-04-23 13:53:06 +02:00
|
|
|
self.window.addstr(" toot", curses.A_BOLD)
|
|
|
|
self.window.addstr(" | ")
|
|
|
|
self.window.addstr(username)
|
|
|
|
self.window.addstr(" | ")
|
2018-01-08 14:02:52 +01:00
|
|
|
self.window.refresh()
|
|
|
|
|
|
|
|
|
|
|
|
class FooterWindow:
|
2018-01-22 10:07:18 +01:00
|
|
|
def __init__(self, stdscr, height, width, y, x):
|
|
|
|
self.window = stdscr.subwin(height, width, y, x)
|
2018-01-08 14:02:52 +01:00
|
|
|
self.height = height
|
|
|
|
self.width = width
|
|
|
|
|
|
|
|
def draw_status(self, selected, count):
|
|
|
|
text = "Showing toot {} of {}".format(selected + 1, count)
|
2019-02-14 15:47:40 +01:00
|
|
|
text = fit_text(text, self.width)
|
2018-01-08 14:02:52 +01:00
|
|
|
self.window.addstr(0, 0, text, Color.WHITE_ON_BLUE | curses.A_BOLD)
|
|
|
|
self.window.refresh()
|
|
|
|
|
|
|
|
def draw_message(self, text, color):
|
2019-02-14 15:47:40 +01:00
|
|
|
text = fit_text(text, self.width - 1)
|
2018-01-08 14:02:52 +01:00
|
|
|
self.window.addstr(1, 0, text, color)
|
|
|
|
self.window.refresh()
|
|
|
|
|
|
|
|
def clear_message(self):
|
2018-01-08 15:19:02 +01:00
|
|
|
self.window.addstr(1, 0, "".ljust(self.width - 1))
|
2018-01-08 14:02:52 +01:00
|
|
|
self.window.refresh()
|
|
|
|
|
|
|
|
|
2018-01-08 15:19:02 +01:00
|
|
|
class StatusListWindow:
|
|
|
|
"""Window which shows the scrollable list of statuses (left side)."""
|
2018-01-22 10:07:18 +01:00
|
|
|
def __init__(self, stdscr, height, width, top, left):
|
2018-01-08 15:19:02 +01:00
|
|
|
# Dimensions and position of region in stdscr which will contain the pad
|
|
|
|
self.region_height = height
|
|
|
|
self.region_width = width
|
|
|
|
self.region_top = top
|
|
|
|
self.region_left = left
|
|
|
|
|
|
|
|
# How many statuses fit on one page (excluding border, at 3 lines per status)
|
|
|
|
self.page_size = (height - 2) // 3
|
|
|
|
|
|
|
|
# Initially, size the pad to the dimensions of the region, will be
|
|
|
|
# increased later to accomodate statuses
|
|
|
|
self.pad = curses.newpad(10, width)
|
|
|
|
self.pad.box()
|
|
|
|
|
2019-04-24 14:05:28 +02:00
|
|
|
# Make curses interpret escape sequences for getch (why is this off by default?)
|
|
|
|
self.pad.keypad(True)
|
|
|
|
|
2018-01-08 15:19:02 +01:00
|
|
|
self.scroll_pos = 0
|
|
|
|
|
|
|
|
def draw_statuses(self, statuses, selected, starting=0):
|
|
|
|
# Resize window to accomodate statuses if required
|
|
|
|
height, width = self.pad.getmaxyx()
|
|
|
|
|
|
|
|
new_height = len(statuses) * 3 + 1
|
|
|
|
if new_height > height:
|
|
|
|
self.pad.resize(new_height, width)
|
|
|
|
self.pad.box()
|
|
|
|
|
|
|
|
last_idx = len(statuses) - 1
|
|
|
|
|
|
|
|
for index, status in enumerate(statuses):
|
|
|
|
if index >= starting:
|
|
|
|
highlight = selected == index
|
|
|
|
draw_divider = index < last_idx
|
|
|
|
self.draw_status_row(status, index, highlight, draw_divider)
|
|
|
|
|
|
|
|
def draw_status_row(self, status, index, highlight=False, draw_divider=True):
|
|
|
|
offset = 3 * index
|
|
|
|
|
|
|
|
height, width = self.pad.getmaxyx()
|
|
|
|
color = Color.GREEN if highlight else Color.WHITE
|
|
|
|
|
|
|
|
trunc_width = width - 15
|
2019-02-14 15:47:40 +01:00
|
|
|
acct = fit_text("@" + status['account']['acct'], trunc_width)
|
|
|
|
display_name = fit_text(status['account']['display_name'], trunc_width)
|
2018-01-08 15:19:02 +01:00
|
|
|
|
2018-01-13 12:50:57 +01:00
|
|
|
if status['account']['display_name']:
|
|
|
|
self.pad.addstr(offset + 1, 14, display_name, color)
|
|
|
|
self.pad.addstr(offset + 2, 14, acct, color)
|
|
|
|
else:
|
|
|
|
self.pad.addstr(offset + 1, 14, acct, color)
|
2019-01-07 10:12:05 +01:00
|
|
|
if status['in_reply_to_id'] is not None:
|
|
|
|
self.pad.addstr(offset + 1, width - 3, '⤶', Color.CYAN)
|
2018-01-08 15:19:02 +01:00
|
|
|
|
2018-01-22 10:18:44 +01:00
|
|
|
date, time = status['created_at']
|
|
|
|
self.pad.addstr(offset + 1, 1, " " + date.ljust(12), color)
|
|
|
|
self.pad.addstr(offset + 2, 1, " " + time.ljust(12), color)
|
|
|
|
|
2019-02-14 20:39:58 +01:00
|
|
|
if status['favourited']:
|
|
|
|
self.pad.addstr(offset + 2, width - 3, '⭐', Color.YELLOW)
|
|
|
|
|
2018-01-08 15:19:02 +01:00
|
|
|
if draw_divider:
|
|
|
|
draw_horizontal_divider(self.pad, offset + 3)
|
|
|
|
|
|
|
|
self.refresh()
|
|
|
|
|
|
|
|
def refresh(self):
|
|
|
|
self.pad.refresh(
|
|
|
|
self.scroll_pos * 3, # top
|
|
|
|
0, # left
|
|
|
|
self.region_top,
|
|
|
|
self.region_left,
|
2018-01-13 12:32:49 +01:00
|
|
|
self.region_height + 1, # +1 required to refresh full height, not sure why
|
2018-01-08 15:19:02 +01:00
|
|
|
self.region_width,
|
|
|
|
)
|
|
|
|
|
|
|
|
def scroll_to(self, index):
|
|
|
|
self.scroll_pos = index
|
|
|
|
self.refresh()
|
|
|
|
|
|
|
|
def scroll_up(self):
|
|
|
|
if self.scroll_pos > 0:
|
|
|
|
self.scroll_to(self.scroll_pos - 1)
|
|
|
|
|
|
|
|
def scroll_down(self):
|
|
|
|
self.scroll_to(self.scroll_pos + 1)
|
|
|
|
|
|
|
|
def scroll_if_required(self, new_index):
|
|
|
|
if new_index < self.scroll_pos:
|
|
|
|
self.scroll_up()
|
|
|
|
elif new_index >= self.scroll_pos + self.page_size:
|
|
|
|
self.scroll_down()
|
|
|
|
else:
|
|
|
|
self.refresh()
|
|
|
|
|
|
|
|
|
2018-01-06 17:32:27 +01:00
|
|
|
class StatusDetailWindow:
|
|
|
|
"""Window which shows details of a status (right side)"""
|
2018-01-22 10:07:18 +01:00
|
|
|
def __init__(self, stdscr, height, width, y, x):
|
|
|
|
self.window = stdscr.subwin(height, width, y, x)
|
2018-01-06 17:32:27 +01:00
|
|
|
self.height = height
|
|
|
|
self.width = width
|
|
|
|
|
|
|
|
def content_lines(self, status):
|
|
|
|
acct = status['account']['acct']
|
|
|
|
name = status['account']['display_name']
|
|
|
|
|
2018-01-13 12:50:57 +01:00
|
|
|
if name:
|
|
|
|
yield name, Color.YELLOW
|
2018-01-06 17:32:27 +01:00
|
|
|
yield "@" + acct, Color.GREEN
|
|
|
|
yield
|
|
|
|
|
|
|
|
text_width = self.width - 4
|
|
|
|
|
2018-01-20 12:44:22 +01:00
|
|
|
if status['sensitive']:
|
|
|
|
for line in status['spoiler_text']:
|
2018-01-20 13:43:21 +01:00
|
|
|
yield line
|
2018-01-20 12:44:22 +01:00
|
|
|
yield
|
|
|
|
|
|
|
|
if status['sensitive'] and not status['show_sensitive']:
|
|
|
|
yield "Marked as sensitive, press s to view".ljust(text_width), Color.WHITE_ON_RED
|
|
|
|
return
|
|
|
|
|
|
|
|
for line in status['content']:
|
2018-01-20 13:43:21 +01:00
|
|
|
yield line
|
2018-01-06 17:32:27 +01:00
|
|
|
|
|
|
|
if status['media_attachments']:
|
|
|
|
yield
|
|
|
|
yield "Media:"
|
|
|
|
for attachment in status['media_attachments']:
|
2018-01-20 13:43:21 +01:00
|
|
|
yield attachment['text_url'] or attachment['url']
|
2018-01-06 17:32:27 +01:00
|
|
|
|
|
|
|
def footer_lines(self, status):
|
|
|
|
if status['url'] is not None:
|
2018-01-20 13:43:21 +01:00
|
|
|
yield status['url']
|
2017-04-21 20:23:48 +02:00
|
|
|
|
2018-01-06 17:32:27 +01:00
|
|
|
if status['boosted_by']:
|
|
|
|
acct = status['boosted_by']['acct']
|
2018-01-20 13:43:21 +01:00
|
|
|
yield "Boosted by @{}".format(acct), Color.GREEN
|
2018-01-06 17:32:27 +01:00
|
|
|
|
2019-01-24 10:15:15 +01:00
|
|
|
if status['reblogged']:
|
2019-02-14 18:02:37 +01:00
|
|
|
yield "↷ Boosted", Color.CYAN
|
2019-01-24 10:15:15 +01:00
|
|
|
|
2019-01-07 10:13:29 +01:00
|
|
|
yield (
|
|
|
|
"{replies_count} replies, "
|
|
|
|
"{reblogs_count} reblogs, "
|
|
|
|
"{favourites_count} favourites"
|
|
|
|
).format(**status), Color.CYAN
|
|
|
|
|
2018-01-06 17:32:27 +01:00
|
|
|
def draw(self, status):
|
|
|
|
self.window.erase()
|
|
|
|
self.window.box()
|
|
|
|
|
2018-01-08 15:19:02 +01:00
|
|
|
if not status:
|
|
|
|
return
|
|
|
|
|
2018-01-06 17:32:27 +01:00
|
|
|
content = self.content_lines(status)
|
|
|
|
footer = self.footer_lines(status)
|
|
|
|
|
2018-01-20 13:43:21 +01:00
|
|
|
y = draw_lines(self.window, content, 1, 2, Color.WHITE)
|
2018-01-06 17:32:27 +01:00
|
|
|
draw_horizontal_divider(self.window, y)
|
2018-01-20 13:43:21 +01:00
|
|
|
draw_lines(self.window, footer, y + 1, 2, Color.WHITE)
|
2018-01-06 17:32:27 +01:00
|
|
|
|
|
|
|
self.window.refresh()
|
2018-01-04 13:13:29 +01:00
|
|
|
|
2017-04-21 20:23:48 +02:00
|
|
|
|
2018-01-22 10:07:18 +01:00
|
|
|
class Modal:
|
2019-04-25 01:42:28 +02:00
|
|
|
def __init__(self, stdscr, resize_callback=None):
|
|
|
|
self.stdscr = stdscr
|
|
|
|
self.resize_callback = resize_callback
|
2018-01-22 10:07:18 +01:00
|
|
|
|
2019-04-25 01:42:28 +02:00
|
|
|
self.setup_windows()
|
|
|
|
self.full_redraw()
|
2018-01-22 10:07:18 +01:00
|
|
|
self.panel = curses.panel.new_panel(self.window)
|
2019-04-25 06:43:38 +02:00
|
|
|
self.hide()
|
2018-01-22 10:07:18 +01:00
|
|
|
|
|
|
|
def get_content(self):
|
|
|
|
raise NotImplementedError()
|
|
|
|
|
|
|
|
def get_size_pos(self, stdscr):
|
|
|
|
screen_height, screen_width = stdscr.getmaxyx()
|
|
|
|
|
|
|
|
content = self.get_content()
|
|
|
|
height = len(content) + 2
|
|
|
|
width = max(len(l) for l in content) + 4
|
|
|
|
|
|
|
|
y = (screen_height - height) // 2
|
|
|
|
x = (screen_width - width) // 2
|
|
|
|
|
|
|
|
return height, width, y, x
|
|
|
|
|
2019-04-25 01:42:28 +02:00
|
|
|
def setup_windows(self):
|
|
|
|
height, width, y, x = self.get_size_pos(self.stdscr)
|
|
|
|
self.window = curses.newwin(height, width, y, x)
|
|
|
|
|
|
|
|
def full_redraw(self):
|
|
|
|
self.setup_windows()
|
2018-01-22 10:07:18 +01:00
|
|
|
self.window.box()
|
|
|
|
draw_lines(self.window, self.get_content(), 1, 2, Color.WHITE)
|
|
|
|
|
|
|
|
def show(self):
|
|
|
|
self.panel.top()
|
|
|
|
self.panel.show()
|
|
|
|
self.window.refresh()
|
|
|
|
curses.panel.update_panels()
|
|
|
|
|
|
|
|
def hide(self):
|
|
|
|
self.panel.hide()
|
|
|
|
curses.panel.update_panels()
|
|
|
|
|
|
|
|
def loop(self):
|
|
|
|
self.show()
|
|
|
|
|
2019-04-25 01:42:28 +02:00
|
|
|
while True:
|
|
|
|
ch = self.window.getch()
|
|
|
|
key = chr(ch).lower() if curses.ascii.isprint(ch) else None
|
|
|
|
|
|
|
|
if key == 'q':
|
|
|
|
break
|
|
|
|
elif ch == curses.KEY_RESIZE:
|
|
|
|
if self.resize_callback:
|
|
|
|
self.resize_callback()
|
|
|
|
self.full_redraw()
|
2018-01-22 10:07:18 +01:00
|
|
|
|
|
|
|
self.hide()
|
|
|
|
|
|
|
|
|
|
|
|
class HelpModal(Modal):
|
|
|
|
def get_content(self):
|
|
|
|
return [
|
|
|
|
("toot v{}".format(__version__), Color.GREEN | curses.A_BOLD),
|
|
|
|
"",
|
|
|
|
"Key bindings:",
|
|
|
|
"",
|
|
|
|
" h - show help",
|
|
|
|
" j or ↓ - move down",
|
|
|
|
" k or ↑ - move up",
|
|
|
|
" v - view current toot in browser",
|
2019-02-14 18:02:37 +01:00
|
|
|
" b - toggle boost status",
|
2019-02-13 21:05:23 +01:00
|
|
|
" f - toggle favourite status",
|
2019-02-27 03:18:34 +01:00
|
|
|
" c - post a new status",
|
|
|
|
" r - reply to status",
|
2018-01-22 10:07:18 +01:00
|
|
|
" q - quit application",
|
2018-12-30 22:34:15 +01:00
|
|
|
" s - show sensitive content"
|
2018-01-22 10:07:18 +01:00
|
|
|
"",
|
2018-08-11 10:58:13 +02:00
|
|
|
"Press q to exit help.",
|
2018-01-22 10:07:18 +01:00
|
|
|
"",
|
|
|
|
("https://github.com/ihabunek/toot", Color.YELLOW),
|
|
|
|
]
|
|
|
|
|
|
|
|
|
2019-03-01 22:17:58 +01:00
|
|
|
class EntryModal(Modal):
|
2019-04-25 01:42:28 +02:00
|
|
|
def __init__(self, stdscr, title, footer=None, size=(None, None), default=None, resize_callback=None):
|
|
|
|
self.stdscr = stdscr
|
|
|
|
self.resize_callback = resize_callback
|
2019-03-01 22:35:11 +01:00
|
|
|
self.content = [] if default is None else default.split()
|
2019-02-28 02:10:27 +01:00
|
|
|
self.cursor_pos = 0
|
|
|
|
self.pad_y, self.pad_x = 2, 2
|
|
|
|
|
2019-02-27 03:09:59 +01:00
|
|
|
self.title = title
|
|
|
|
self.footer = footer
|
2019-02-28 02:10:27 +01:00
|
|
|
self.size = size
|
2019-02-27 03:09:59 +01:00
|
|
|
if self.footer:
|
|
|
|
self.pad_y += 1
|
2019-02-27 01:29:34 +01:00
|
|
|
|
2019-04-25 01:42:28 +02:00
|
|
|
self.setup_windows()
|
|
|
|
self.full_redraw()
|
2019-02-27 01:29:34 +01:00
|
|
|
self.panel = curses.panel.new_panel(self.window)
|
2019-04-25 06:43:38 +02:00
|
|
|
self.hide()
|
2019-02-27 01:29:34 +01:00
|
|
|
|
2019-02-28 02:10:27 +01:00
|
|
|
def get_size_pos(self, stdscr):
|
2019-02-27 01:29:34 +01:00
|
|
|
screen_height, screen_width = stdscr.getmaxyx()
|
2019-02-28 02:10:27 +01:00
|
|
|
if self.size[0]:
|
|
|
|
height = self.size[0] + (self.pad_y * 2) + 1
|
2019-02-27 03:09:59 +01:00
|
|
|
else:
|
|
|
|
height = int(screen_height / 1.33)
|
2019-02-28 02:10:27 +01:00
|
|
|
if self.size[1]:
|
2019-03-01 22:17:58 +01:00
|
|
|
width = self.size[1] + (self.pad_x * 2) + 1
|
2019-02-27 03:09:59 +01:00
|
|
|
else:
|
|
|
|
width = int(screen_width / 1.25)
|
2019-02-27 01:29:34 +01:00
|
|
|
|
|
|
|
y = (screen_height - height) // 2
|
|
|
|
x = (screen_width - width) // 2
|
|
|
|
|
|
|
|
return height, width, y, x
|
|
|
|
|
2019-04-25 01:42:28 +02:00
|
|
|
def setup_windows(self):
|
|
|
|
height, width, y, x = self.get_size_pos(self.stdscr)
|
|
|
|
|
|
|
|
self.window = curses.newwin(height, width, y, x)
|
|
|
|
self.text_window = self.window.derwin(height - (self.pad_y * 2), width - (self.pad_x * 2), self.pad_y, self.pad_x)
|
|
|
|
self.text_window.keypad(True)
|
|
|
|
|
|
|
|
def full_redraw(self):
|
2019-02-27 03:09:59 +01:00
|
|
|
self.window.erase()
|
|
|
|
self.window.box()
|
2019-02-28 02:10:27 +01:00
|
|
|
|
2019-03-29 23:47:56 +01:00
|
|
|
draw_lines(self.window, ["{} (^D to confirm):".format(self.title)], 1, 2, Color.WHITE)
|
2019-02-28 02:10:27 +01:00
|
|
|
if self.footer:
|
|
|
|
window_height, window_width = self.window.getmaxyx()
|
|
|
|
draw_lines(self.window, [self.footer], window_height - self.pad_y + 1, 2, Color.WHITE)
|
|
|
|
|
2019-04-25 01:42:28 +02:00
|
|
|
self.window.refresh()
|
|
|
|
self.refresh_text()
|
2019-03-02 02:06:55 +01:00
|
|
|
|
2019-04-25 01:42:28 +02:00
|
|
|
def refresh_text(self):
|
2019-03-02 02:06:55 +01:00
|
|
|
text = self.get_content()
|
2019-02-28 02:10:27 +01:00
|
|
|
lines = text.split('\n')
|
|
|
|
draw_lines(self.text_window, lines, 0, 0, Color.WHITE)
|
|
|
|
|
|
|
|
text_window_height, text_window_width = self.text_window.getmaxyx()
|
|
|
|
text_on_screen = (''.join(self.content)[:self.cursor_pos] + '_').split('\n')
|
|
|
|
y, x = size_as_drawn(text_on_screen, text_window_width)
|
|
|
|
self.text_window.move(y, x)
|
2019-02-27 03:09:59 +01:00
|
|
|
|
2019-03-02 02:06:55 +01:00
|
|
|
def show(self):
|
|
|
|
super().show()
|
2019-04-25 06:32:28 +02:00
|
|
|
self.refresh_text()
|
2019-03-02 02:06:55 +01:00
|
|
|
|
|
|
|
def clear(self):
|
|
|
|
self.content = []
|
|
|
|
self.cursor_pos = 0
|
|
|
|
|
2019-04-25 01:42:28 +02:00
|
|
|
def on_resize(self):
|
|
|
|
if self.resize_callback:
|
|
|
|
self.resize_callback()
|
|
|
|
self.setup_windows()
|
|
|
|
self.full_redraw()
|
|
|
|
|
2019-02-27 03:09:59 +01:00
|
|
|
def do_command(self, ch):
|
2019-02-28 02:10:27 +01:00
|
|
|
if curses.ascii.isprint(ch) or ch == curses.ascii.LF:
|
|
|
|
text_window_height, text_window_width = self.text_window.getmaxyx()
|
2019-03-02 02:06:55 +01:00
|
|
|
y, x = size_as_drawn((self.get_content() + chr(ch)).split('\n'), text_window_width)
|
2019-02-28 02:10:27 +01:00
|
|
|
if y < text_window_height - 1 and x < text_window_width:
|
|
|
|
self.content.insert(self.cursor_pos, chr(ch))
|
|
|
|
self.cursor_pos += 1
|
2019-03-02 02:06:55 +01:00
|
|
|
else:
|
|
|
|
curses.beep()
|
2019-02-28 02:10:27 +01:00
|
|
|
|
|
|
|
elif ch == curses.KEY_BACKSPACE:
|
|
|
|
if self.cursor_pos > 0:
|
|
|
|
del self.content[self.cursor_pos - 1]
|
|
|
|
self.cursor_pos -= 1
|
2019-03-02 02:06:55 +01:00
|
|
|
else:
|
|
|
|
curses.beep()
|
2019-02-28 02:10:27 +01:00
|
|
|
|
|
|
|
elif ch == curses.KEY_DC:
|
|
|
|
if self.cursor_pos >= 0 and self.cursor_pos < len(self.content):
|
|
|
|
del self.content[self.cursor_pos]
|
2019-03-02 02:06:55 +01:00
|
|
|
else:
|
|
|
|
curses.beep()
|
2019-02-28 02:10:27 +01:00
|
|
|
|
|
|
|
elif ch == curses.KEY_LEFT:
|
|
|
|
if self.cursor_pos > 0:
|
|
|
|
self.cursor_pos -= 1
|
2019-03-02 02:06:55 +01:00
|
|
|
else:
|
|
|
|
curses.beep()
|
2019-02-28 02:10:27 +01:00
|
|
|
|
|
|
|
elif ch == curses.KEY_RIGHT:
|
|
|
|
if self.cursor_pos + 1 <= len(self.content):
|
|
|
|
self.cursor_pos += 1
|
2019-03-02 02:06:55 +01:00
|
|
|
else:
|
|
|
|
curses.beep()
|
2019-02-28 02:10:27 +01:00
|
|
|
|
2019-03-29 23:47:56 +01:00
|
|
|
elif ch in (curses.ascii.EOT, curses.ascii.RS): # ^D or (for some terminals) Ctrl+Enter
|
2019-03-02 02:06:55 +01:00
|
|
|
return False, False
|
2019-02-28 02:10:27 +01:00
|
|
|
|
2019-03-02 02:04:45 +01:00
|
|
|
elif ch == curses.ascii.ESC:
|
2019-03-02 02:06:55 +01:00
|
|
|
self.clear()
|
|
|
|
return False, True
|
2019-02-28 02:10:27 +01:00
|
|
|
|
2019-04-25 01:42:28 +02:00
|
|
|
elif ch == curses.KEY_RESIZE:
|
|
|
|
self.on_resize()
|
|
|
|
return True, False
|
|
|
|
|
|
|
|
self.refresh_text()
|
2019-03-02 02:06:55 +01:00
|
|
|
return True, False
|
|
|
|
|
|
|
|
def get_content(self):
|
|
|
|
return ''.join(self.content)
|
2019-02-27 03:09:59 +01:00
|
|
|
|
2019-02-27 01:29:34 +01:00
|
|
|
def loop(self):
|
|
|
|
self.show()
|
2019-02-27 03:09:59 +01:00
|
|
|
while True:
|
2019-02-28 02:10:27 +01:00
|
|
|
ch = self.text_window.getch()
|
2019-02-27 03:09:59 +01:00
|
|
|
if not ch:
|
|
|
|
continue
|
2019-03-02 02:06:55 +01:00
|
|
|
should_continue, abort_flag = self.do_command(ch)
|
|
|
|
if not should_continue:
|
2019-02-27 03:09:59 +01:00
|
|
|
break
|
2019-02-27 01:29:34 +01:00
|
|
|
self.hide()
|
2019-03-02 02:06:55 +01:00
|
|
|
if abort_flag:
|
|
|
|
return None
|
|
|
|
else:
|
|
|
|
return self.get_content()
|
2019-02-27 03:09:59 +01:00
|
|
|
|
|
|
|
|
2019-03-01 22:17:58 +01:00
|
|
|
class ComposeModal(EntryModal):
|
2019-04-25 01:42:28 +02:00
|
|
|
def __init__(self, stdscr, default_cw=None, **kwargs):
|
|
|
|
super().__init__(stdscr, title="Compose a toot", footer="^D to submit, ESC to quit, ^W to mark sensitive (cw)", **kwargs)
|
2019-03-01 22:35:11 +01:00
|
|
|
self.cw = default_cw
|
2019-04-25 01:42:28 +02:00
|
|
|
self.cwmodal = EntryModal(stdscr, title="Content warning", size=(1, 60), default=self.cw, resize_callback=self.on_resize)
|
2019-02-27 03:09:59 +01:00
|
|
|
|
|
|
|
def do_command(self, ch):
|
2019-04-16 14:37:34 +02:00
|
|
|
if ch == curses.ascii.ctrl(ord('w')):
|
2019-04-25 01:42:28 +02:00
|
|
|
self.cwmodal.on_resize()
|
2019-03-01 22:17:58 +01:00
|
|
|
self.cw = self.cwmodal.loop() or None
|
2019-04-25 01:42:28 +02:00
|
|
|
self.full_redraw()
|
2019-03-02 02:06:55 +01:00
|
|
|
return True, False
|
2019-02-27 03:09:59 +01:00
|
|
|
else:
|
|
|
|
return super().do_command(ch)
|
|
|
|
|
|
|
|
def loop(self):
|
|
|
|
content = super().loop()
|
|
|
|
return content, self.cw
|
2019-02-27 01:29:34 +01:00
|
|
|
|
|
|
|
|
2017-04-21 20:23:48 +02:00
|
|
|
class TimelineApp:
|
2019-01-03 16:52:31 +01:00
|
|
|
def __init__(self, app, user, status_generator):
|
|
|
|
self.app = app
|
|
|
|
self.user = user
|
2017-04-21 20:23:48 +02:00
|
|
|
self.status_generator = status_generator
|
|
|
|
self.statuses = []
|
2018-01-03 12:15:56 +01:00
|
|
|
self.stdscr = None
|
2017-04-21 20:23:48 +02:00
|
|
|
|
|
|
|
def run(self):
|
2019-03-02 02:04:45 +01:00
|
|
|
os.environ.setdefault('ESCDELAY', '25')
|
2017-04-21 20:23:48 +02:00
|
|
|
curses.wrapper(self._wrapped_run)
|
|
|
|
|
|
|
|
def _wrapped_run(self, stdscr):
|
2018-01-03 12:15:56 +01:00
|
|
|
self.stdscr = stdscr
|
2017-04-21 20:23:48 +02:00
|
|
|
|
|
|
|
Color.setup_palette()
|
2018-01-08 15:19:02 +01:00
|
|
|
self.setup_windows()
|
2017-04-21 20:23:48 +02:00
|
|
|
|
|
|
|
# Load some data and redraw
|
|
|
|
self.fetch_next()
|
|
|
|
self.selected = 0
|
|
|
|
self.full_redraw()
|
|
|
|
|
|
|
|
self.loop()
|
|
|
|
|
2018-01-03 12:15:56 +01:00
|
|
|
def setup_windows(self):
|
|
|
|
screen_height, screen_width = self.stdscr.getmaxyx()
|
|
|
|
|
2018-01-04 12:36:14 +01:00
|
|
|
if screen_width < 60:
|
|
|
|
raise ConsoleError("Terminal screen is too narrow, toot curses requires at least 60 columns to display properly.")
|
|
|
|
|
2019-04-23 13:53:06 +02:00
|
|
|
header_height = 1
|
2019-04-23 13:31:17 +02:00
|
|
|
footer_height = 2
|
|
|
|
footer_top = screen_height - footer_height
|
|
|
|
|
2018-01-08 15:19:02 +01:00
|
|
|
left_width = max(min(screen_width // 3, 60), 30)
|
2019-04-23 13:31:17 +02:00
|
|
|
main_height = screen_height - header_height - footer_height
|
|
|
|
main_width = screen_width - left_width
|
2018-01-03 12:15:56 +01:00
|
|
|
|
2019-04-23 13:31:17 +02:00
|
|
|
self.header = HeaderWindow(self.stdscr, header_height, screen_width, 0, 0)
|
|
|
|
self.footer = FooterWindow(self.stdscr, footer_height, screen_width, footer_top, 0)
|
|
|
|
self.left = StatusListWindow(self.stdscr, main_height, left_width, header_height, 0)
|
|
|
|
self.right = StatusDetailWindow(self.stdscr, main_height, main_width, header_height, left_width)
|
2018-01-22 10:07:18 +01:00
|
|
|
|
2019-04-25 01:42:28 +02:00
|
|
|
self.help_modal = HelpModal(self.stdscr, resize_callback=self.on_resize)
|
2018-01-03 12:15:56 +01:00
|
|
|
|
2017-04-21 20:23:48 +02:00
|
|
|
def loop(self):
|
|
|
|
while True:
|
2019-04-24 14:05:28 +02:00
|
|
|
ch = self.left.pad.getch()
|
|
|
|
key = chr(ch).lower() if curses.ascii.isprint(ch) else None
|
2017-04-21 20:23:48 +02:00
|
|
|
|
2019-04-24 14:05:28 +02:00
|
|
|
if key == 'q':
|
2017-04-21 20:23:48 +02:00
|
|
|
return
|
|
|
|
|
2019-04-24 14:05:28 +02:00
|
|
|
elif key == 'h':
|
2018-01-22 10:07:18 +01:00
|
|
|
self.help_modal.loop()
|
|
|
|
self.full_redraw()
|
|
|
|
|
2019-04-24 14:05:28 +02:00
|
|
|
elif key == 'v':
|
2017-04-21 20:23:48 +02:00
|
|
|
status = self.get_selected_status()
|
|
|
|
if status:
|
|
|
|
webbrowser.open(status['url'])
|
|
|
|
|
2019-04-24 14:05:28 +02:00
|
|
|
elif key == 'j' or ch == curses.KEY_DOWN:
|
2017-04-21 20:23:48 +02:00
|
|
|
self.select_next()
|
|
|
|
|
2019-04-24 14:05:28 +02:00
|
|
|
elif key == 'k' or ch == curses.KEY_UP:
|
2017-04-21 20:23:48 +02:00
|
|
|
self.select_previous()
|
|
|
|
|
2019-04-24 14:05:28 +02:00
|
|
|
elif key == 's':
|
2018-01-20 12:44:22 +01:00
|
|
|
self.show_sensitive()
|
|
|
|
|
2019-04-24 14:05:28 +02:00
|
|
|
elif key == 'b':
|
2019-01-03 17:08:02 +01:00
|
|
|
self.toggle_reblog()
|
|
|
|
|
2019-04-24 14:05:28 +02:00
|
|
|
elif key == 'f':
|
2019-02-13 21:05:23 +01:00
|
|
|
self.toggle_favourite()
|
|
|
|
|
2019-04-24 14:05:28 +02:00
|
|
|
elif key == 'c':
|
2019-02-27 01:29:34 +01:00
|
|
|
self.compose()
|
|
|
|
|
2019-04-24 14:05:28 +02:00
|
|
|
elif key == 'r':
|
2019-02-27 03:17:52 +01:00
|
|
|
self.reply()
|
|
|
|
|
2019-04-24 14:05:28 +02:00
|
|
|
elif ch == curses.KEY_RESIZE:
|
2019-04-25 01:42:28 +02:00
|
|
|
self.on_resize()
|
2018-01-03 12:15:56 +01:00
|
|
|
|
2018-01-20 12:44:22 +01:00
|
|
|
def show_sensitive(self):
|
|
|
|
status = self.get_selected_status()
|
|
|
|
if status['sensitive'] and not status['show_sensitive']:
|
|
|
|
status['show_sensitive'] = True
|
|
|
|
self.right.draw(status)
|
|
|
|
|
2019-02-27 01:29:34 +01:00
|
|
|
def compose(self):
|
|
|
|
"""Compose and submit a new status"""
|
|
|
|
app, user = self.app, self.user
|
|
|
|
if not app or not user:
|
|
|
|
self.footer.draw_message("You must be logged in to post", Color.RED)
|
|
|
|
return
|
|
|
|
|
2019-04-25 01:42:28 +02:00
|
|
|
compose_modal = ComposeModal(self.stdscr, resize_callback=self.on_resize)
|
2019-02-27 03:09:59 +01:00
|
|
|
content, cw = compose_modal.loop()
|
2019-02-27 01:29:34 +01:00
|
|
|
self.full_redraw()
|
2019-02-27 03:09:59 +01:00
|
|
|
if content is None:
|
|
|
|
return
|
|
|
|
elif len(content) == 0:
|
2019-02-27 01:29:34 +01:00
|
|
|
self.footer.draw_message("Status must contain content", Color.RED)
|
|
|
|
return
|
|
|
|
|
|
|
|
self.footer.draw_message("Submitting status...", Color.YELLOW)
|
2019-03-01 22:35:11 +01:00
|
|
|
response = api.post_status(app, user, content, spoiler_text=cw, sensitive=cw is not None)
|
2019-03-29 23:49:24 +01:00
|
|
|
status = parse_status(response)
|
|
|
|
self.statuses.insert(0, status)
|
|
|
|
self.selected += 1
|
|
|
|
self.left.draw_statuses(self.statuses, self.selected)
|
2019-02-27 01:29:34 +01:00
|
|
|
self.footer.draw_message("✓ Status posted", Color.GREEN)
|
|
|
|
|
2019-02-27 03:17:52 +01:00
|
|
|
def reply(self):
|
|
|
|
"""Reply to the selected status"""
|
|
|
|
status = self.get_selected_status()
|
|
|
|
app, user = self.app, self.user
|
|
|
|
if not app or not user:
|
|
|
|
self.footer.draw_message("You must be logged in to reply", Color.RED)
|
|
|
|
return
|
|
|
|
|
2019-04-25 01:42:28 +02:00
|
|
|
compose_modal = ComposeModal(self.stdscr, default_cw='\n'.join(status['spoiler_text']) or None, resize_callback=self.on_resize)
|
2019-02-27 03:17:52 +01:00
|
|
|
content, cw = compose_modal.loop()
|
|
|
|
self.full_redraw()
|
|
|
|
if content is None:
|
|
|
|
return
|
|
|
|
elif len(content) == 0:
|
|
|
|
self.footer.draw_message("Status must contain content", Color.RED)
|
|
|
|
return
|
|
|
|
|
|
|
|
self.footer.draw_message("Submitting reply...", Color.YELLOW)
|
2019-03-01 22:35:11 +01:00
|
|
|
response = api.post_status(app, user, content, spoiler_text=cw, sensitive=cw is not None, in_reply_to_id=status['id'])
|
2019-03-29 23:49:24 +01:00
|
|
|
status = parse_status(response)
|
|
|
|
self.statuses.insert(0, status)
|
|
|
|
self.selected += 1
|
|
|
|
self.left.draw_statuses(self.statuses, self.selected)
|
2019-02-27 03:17:52 +01:00
|
|
|
self.footer.draw_message("✓ Reply posted", Color.GREEN)
|
|
|
|
|
2019-01-03 17:08:02 +01:00
|
|
|
def toggle_reblog(self):
|
|
|
|
"""Reblog or unreblog selected status."""
|
|
|
|
status = self.get_selected_status()
|
|
|
|
assert status
|
|
|
|
app, user = self.app, self.user
|
|
|
|
if not app or not user:
|
|
|
|
self.footer.draw_message("You must be logged in to reblog", Color.RED)
|
|
|
|
return
|
|
|
|
status_id = status['id']
|
|
|
|
if status['reblogged']:
|
2019-02-14 18:02:37 +01:00
|
|
|
status['reblogged'] = False
|
|
|
|
self.footer.draw_message("Unboosting status...", Color.YELLOW)
|
2019-01-03 17:08:02 +01:00
|
|
|
api.unreblog(app, user, status_id)
|
2019-02-14 18:02:37 +01:00
|
|
|
self.footer.draw_message("✓ Status unboosted", Color.GREEN)
|
2019-01-03 17:08:02 +01:00
|
|
|
else:
|
2019-02-14 18:02:37 +01:00
|
|
|
status['reblogged'] = True
|
|
|
|
self.footer.draw_message("Boosting status...", Color.YELLOW)
|
2019-01-03 17:08:02 +01:00
|
|
|
api.reblog(app, user, status_id)
|
2019-02-14 18:02:37 +01:00
|
|
|
self.footer.draw_message("✓ Status boosted", Color.GREEN)
|
|
|
|
|
|
|
|
self.right.draw(status)
|
2019-01-03 17:08:02 +01:00
|
|
|
|
2019-02-13 21:05:23 +01:00
|
|
|
def toggle_favourite(self):
|
|
|
|
"""Favourite or unfavourite selected status."""
|
|
|
|
status = self.get_selected_status()
|
|
|
|
assert status
|
|
|
|
app, user = self.app, self.user
|
|
|
|
if not app or not user:
|
|
|
|
self.footer.draw_message("You must be logged in to favourite", Color.RED)
|
|
|
|
return
|
|
|
|
status_id = status['id']
|
|
|
|
if status['favourited']:
|
|
|
|
self.footer.draw_message("Undoing favourite status...", Color.YELLOW)
|
|
|
|
api.unfavourite(app, user, status_id)
|
|
|
|
self.footer.draw_message("✓ Status unfavourited", Color.GREEN)
|
|
|
|
else:
|
|
|
|
self.footer.draw_message("Favourite status...", Color.YELLOW)
|
|
|
|
api.favourite(app, user, status_id)
|
|
|
|
self.footer.draw_message("✓ Status favourited", Color.GREEN)
|
|
|
|
status['favourited'] = not status['favourited']
|
|
|
|
|
|
|
|
self.right.draw(status)
|
|
|
|
|
2017-04-21 20:23:48 +02:00
|
|
|
def select_previous(self):
|
|
|
|
"""Move to the previous status in the timeline."""
|
2018-01-08 14:02:52 +01:00
|
|
|
self.footer.clear_message()
|
2018-01-04 13:13:29 +01:00
|
|
|
|
2017-04-21 20:23:48 +02:00
|
|
|
if self.selected == 0:
|
2018-01-08 14:02:52 +01:00
|
|
|
self.footer.draw_message("Cannot move beyond first toot.", Color.GREEN)
|
2017-04-21 20:23:48 +02:00
|
|
|
return
|
|
|
|
|
|
|
|
old_index = self.selected
|
|
|
|
new_index = self.selected - 1
|
|
|
|
|
|
|
|
self.selected = new_index
|
|
|
|
self.redraw_after_selection_change(old_index, new_index)
|
|
|
|
|
|
|
|
def select_next(self):
|
|
|
|
"""Move to the next status in the timeline."""
|
2018-01-08 14:02:52 +01:00
|
|
|
self.footer.clear_message()
|
2018-01-04 13:13:29 +01:00
|
|
|
|
2018-01-08 15:19:02 +01:00
|
|
|
old_index = self.selected
|
|
|
|
new_index = self.selected + 1
|
|
|
|
|
2018-01-05 17:33:06 +01:00
|
|
|
# Load more statuses if no more are available
|
2017-04-21 20:23:48 +02:00
|
|
|
if self.selected + 1 >= len(self.statuses):
|
2018-01-04 14:16:52 +01:00
|
|
|
self.fetch_next()
|
2018-01-08 15:19:02 +01:00
|
|
|
self.left.draw_statuses(self.statuses, self.selected, new_index - 1)
|
2018-01-08 14:02:52 +01:00
|
|
|
self.draw_footer_status()
|
2017-04-21 20:23:48 +02:00
|
|
|
|
|
|
|
self.selected = new_index
|
|
|
|
self.redraw_after_selection_change(old_index, new_index)
|
|
|
|
|
|
|
|
def fetch_next(self):
|
|
|
|
try:
|
2018-01-08 14:02:52 +01:00
|
|
|
self.footer.draw_message("Loading toots...", Color.BLUE)
|
2017-04-26 11:25:12 +02:00
|
|
|
statuses = next(self.status_generator)
|
2017-04-21 20:23:48 +02:00
|
|
|
except StopIteration:
|
|
|
|
return None
|
|
|
|
|
|
|
|
for status in statuses:
|
|
|
|
self.statuses.append(parse_status(status))
|
|
|
|
|
2018-01-08 14:02:52 +01:00
|
|
|
self.footer.draw_message("Loaded {} toots".format(len(statuses)), Color.GREEN)
|
2018-01-04 14:16:52 +01:00
|
|
|
|
2017-04-21 20:23:48 +02:00
|
|
|
return len(statuses)
|
|
|
|
|
2019-04-25 01:42:28 +02:00
|
|
|
def on_resize(self):
|
|
|
|
self.setup_windows()
|
|
|
|
self.full_redraw()
|
|
|
|
|
2017-04-21 20:23:48 +02:00
|
|
|
def full_redraw(self):
|
|
|
|
"""Perform a full redraw of the UI."""
|
2019-04-25 01:49:28 +02:00
|
|
|
self.left.draw_statuses(self.statuses, self.selected)
|
|
|
|
self.right.draw(self.get_selected_status())
|
|
|
|
|
2019-04-23 13:53:06 +02:00
|
|
|
self.header.draw(self.user)
|
2018-01-08 14:02:52 +01:00
|
|
|
self.draw_footer_status()
|
2017-04-21 20:23:48 +02:00
|
|
|
|
|
|
|
|
2018-01-08 15:19:02 +01:00
|
|
|
def redraw_after_selection_change(self, old_index, new_index):
|
|
|
|
old_status = self.statuses[old_index]
|
|
|
|
new_status = self.statuses[new_index]
|
2017-04-21 20:23:48 +02:00
|
|
|
|
2018-01-08 15:19:02 +01:00
|
|
|
# Perform a partial redraw
|
|
|
|
self.left.draw_status_row(old_status, old_index, highlight=False, draw_divider=False)
|
|
|
|
self.left.draw_status_row(new_status, new_index, highlight=True, draw_divider=False)
|
|
|
|
self.left.scroll_if_required(new_index)
|
2017-04-21 20:23:48 +02:00
|
|
|
|
2018-01-08 15:19:02 +01:00
|
|
|
self.right.draw(new_status)
|
|
|
|
self.draw_footer_status()
|
2018-01-04 14:16:52 +01:00
|
|
|
|
2018-01-08 15:19:02 +01:00
|
|
|
def get_selected_status(self):
|
|
|
|
if len(self.statuses) > self.selected:
|
|
|
|
return self.statuses[self.selected]
|
2017-04-21 20:23:48 +02:00
|
|
|
|
2018-01-08 14:02:52 +01:00
|
|
|
def draw_footer_status(self):
|
|
|
|
self.footer.draw_status(self.selected, len(self.statuses))
|