Skip to content
This repository has been archived by the owner on Feb 20, 2023. It is now read-only.

prompt for post specific actions #604

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions rtv/docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,3 +184,23 @@
[5] Past year
[6] All time
"""

SUBREDDIT_ACTION_MENU = """
Available actions:
[o] open permalink in browser
[s] (un)subscribe from {subreddit}
[f] (un)friend {author}
[v] view {author} user page
[V] view {subreddit}
"""

SUBSCRIPTION_ACTION_MENU = """
Available actions:
[d] delete {name}
"""

SUBMISSION_ACTION_MENU = """
Available actions:
[f] (un)friend {author}
[v] view {author} user page
"""
36 changes: 36 additions & 0 deletions rtv/submission_page.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@
import re
import time

import six

from . import docs
from .content import SubmissionContent, SubredditContent
from .page import Page, PageController, logged_in
from .packages import praw
from .objects import Navigator, Command
from .exceptions import TemporaryFileError

Expand Down Expand Up @@ -285,6 +288,39 @@ def move_sibling_next(self):

self.clear_input_queue()

@SubmissionController.register(Command('PROMPT_ACTION'))
def prompt_action(self):
choices = {
'f': 'friend',
'v': 'view_user',
}
data = self.get_selected_item()
message = docs.SUBMISSION_ACTION_MENU.format(**data).strip().splitlines()
ch = self.term.show_notification(message)
ch = six.unichr(ch)
action = choices.get(ch)
if action == 'friend':
obj = getattr(data['object'], 'author')
if isinstance(obj, praw.objects.Redditor):
obj.friend(_unfriend=obj.refresh().is_friend)
msg = '{1} is {0}your friend'.format(
'no longer ' if obj.is_friend else '', obj.name)
# logic inverted to avoid addtional refresh
self.term.show_notification(msg)
self.reload_page()
elif action == 'view_user':
name = data.get('author')
if name and name != '[deleted]':
name = '/u/' + str(name)
with self.term.loader('Loading page'):
content = SubredditContent.from_name(
self.reddit, name, self.term.loader)
if not self.term.loader.exception:
self.selected_subreddit = content
self.active = False
else:
self.term.show_notification('Invalid option')

def _draw_item(self, win, data, inverted):

if data['type'] == 'MoreComments':
Expand Down
45 changes: 45 additions & 0 deletions rtv/subreddit_page.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@
import re
import time

import six

from . import docs
from .content import SubredditContent
from .packages import praw
from .page import Page, PageController, logged_in
from .objects import Navigator, Command
from .submission_page import SubmissionPage
Expand Down Expand Up @@ -315,6 +318,48 @@ def hide(self):
data['object'].hide()
data['hidden'] = True

@SubredditController.register(Command('PROMPT_ACTION'))
def prompt_action(self):
choices = {
'o': 'open_permalink',
's': 'subscribe',
'f': 'friend',
'v': 'view_user',
'V': 'view_subreddit',
}
data = self.get_selected_item()
message = docs.SUBREDDIT_ACTION_MENU.format(**data).strip().splitlines()
ch = self.term.show_notification(message)
ch = six.unichr(ch)
action = choices.get(ch)
if action == 'subscribe':
obj = data['object'].subreddit
self.reddit.subscribe(obj, obj.refresh().user_is_subscriber)
msg = 'You are {}subscribed to {}'.format(
'no longer ' if obj.user_is_subscriber else '', obj.display_name)
# logic inverted to avoid addtional refresh
self.term.show_notification(msg)
self.reload_page()
elif action == 'friend':
obj = getattr(data['object'], 'author')
if isinstance(obj, praw.objects.Redditor):
obj.friend(_unfriend=obj.refresh().is_friend)
msg = '{1} is {0}your friend'.format(
'no longer ' if obj.is_friend else '', obj.name)
# logic inverted to avoid addtional refresh
self.term.show_notification(msg)
self.reload_page()
elif action == 'view_user':
name = data.get('author')
if name and name != '[deleted]':
self.refresh_content(name='/u/' + str(name))
elif action == 'view_subreddit':
self.refresh_content(name='/r/' + str(data['object'].subreddit))
elif action == 'open_permalink':
self.term.open_link(data['permalink'])
else:
self.term.show_notification('Invalid option')

def _draw_item(self, win, data, inverted):

n_rows, n_cols = win.getmaxyx()
Expand Down
35 changes: 35 additions & 0 deletions rtv/subscription_page.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

import six

from . import docs
from .packages import praw
from .page import Page, PageController
from .content import SubscriptionContent, SubredditContent
from .objects import Navigator, Command
Expand Down Expand Up @@ -79,6 +82,38 @@ def close_subscriptions(self):

self.active = False

@SubscriptionController.register(Command('PROMPT_ACTION'))
def prompt_action(self):
choices = {
'd': 'subscribe',
}
data = self.get_selected_item()
message = docs.SUBSCRIPTION_ACTION_MENU.format(**data).strip().splitlines()
ch = self.term.show_notification(message)
ch = six.unichr(ch)
action = choices.get(ch)
if action == 'subscribe':
obj = data['object']
if isinstance(obj, praw.objects.Submission):
obj = data['object'].subreddit
if isinstance(obj, praw.objects.Subreddit):
self.reddit.subscribe(obj, obj.refresh().user_is_subscriber)
msg = 'You are {}subscribed to {}'.format(
'no longer ' if obj.user_is_subscriber else '', obj.display_name)
# logic inverted to avoid addtional refresh
self.term.show_notification(msg)
self.reload_page()
elif isinstance(obj, praw.objects.Multireddit) and obj.can_edit:
msg = 'Do you really want to delete {}? (y/n): '.format(obj.display_name)
if self.term.prompt_y_or_n(msg):
obj.delete()
self.reload_page()
else:
self.term.show_notification('Invalid option')

def prompt_action(self):
self._prompt_action(['subscribe'], docs.SUBSCRIPTION_ACTION_MENU)

def _draw_banner(self):
# Subscriptions can't be sorted, so disable showing the order menu
pass
Expand Down
1 change: 1 addition & 0 deletions rtv/templates/rtv.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ PROMPT = /
SAVE = w
COPY_PERMALINK = y
COPY_URL = Y
PROMPT_ACTION = v

; Submission page
SUBMISSION_TOGGLE_COMMENT = 0x20
Expand Down