Exemple #1
0
    def __init__(self):
        # Handles page downloads and cookies
        self.handler = RedditHandler()
        self.listings = []
        self.__subreddit = None
        self.nav = None
        self.__load_stories()

        # Prep header and footer ui widgets
        self.__update_header()
        self.footer_content = urwid.Text(('footer', ""), wrap='clip')
        self.footer = urwid.Padding(self.footer_content, left=1, right=1)

        self.frame = urwid.Frame(self.__get_widget(),
                                 header=self.header,
                                 footer=self.footer)
Exemple #2
0
 def __init__(self):
     # Handles page downloads and cookies                            
     self.handler = RedditHandler()
     self.listings = []
     self.__subreddit = None
     self.nav = None
     self.__load_stories()
     
     # Prep header and footer ui widgets 
     self.__update_header()
     self.footer_content = urwid.Text(('footer', ""), wrap='clip') 
     self.footer = urwid.Padding(self.footer_content, left=1, right=1)
     
     self.frame = urwid.Frame(   self.__get_widget(),
                                 header=self.header, 
                                 footer=self.footer )
Exemple #3
0
class MainWindow(object):
    """manages main window elements"""
    def __init__(self):
        # Handles page downloads and cookies
        self.handler = RedditHandler()
        self.listings = []
        self.__subreddit = None
        self.nav = None
        self.__load_stories()

        # Prep header and footer ui widgets
        self.__update_header()
        self.footer_content = urwid.Text(('footer', ""), wrap='clip')
        self.footer = urwid.Padding(self.footer_content, left=1, right=1)

        self.frame = urwid.Frame(self.__get_widget(),
                                 header=self.header,
                                 footer=self.footer)

    def login(self, username, password):
        """attempt to login"""
        login_result = self.handler.login(username, password)
        if login_result:
            self.__update_header()
        return login_result

    def set_subreddit(self, subreddit):
        """switch subreddits"""
        self.nav = None
        old_subreddit = self.__subreddit
        if subreddit == "fp":
            self.__subreddit = None
        else:
            self.set_status("Loading subreddit: /r/{0}".format(subreddit))
            self.__subreddit = subreddit
        try:
            self.__load_stories()
        except BadSubredditError:
            self.set_status(
                "Error loading subreddit /r/{0}!".format(subreddit))
            self.__subreddit = old_subreddit
            self.__load_stories()
        main_widget = self.__get_widget()
        self.frame.set_body(main_widget)
        self.set_status()

    def __update_header(self):
        """set the titlebar to the currently logged in user"""
        if self.handler.user:
            header_text = "[{0}] - reddit-cli - github.com/cev/reddit-cli".format(
                self.handler.user)
        else:
            header_text = "reddit-cli - github.com/cev/reddit-cli"
        self.header = urwid.Text(('header', header_text), align='center')
        if hasattr(self, 'frame'):
            self.frame.set_header(self.header)

    def __load_stories(self, direction=None):
        """load stories from (sub)reddit and store Listings"""
        self.listings = []
        data = self.handler.download_stories(self.__subreddit, self.nav,
                                             direction)

        self.nav = data[1]
        for s in data[0]:
            current = Listing(s)
            self.listings.append(urwid.Padding(current, left=1, right=1))

    def __get_widget(self):
        """return TextBox widget containing all Listings"""
        listings_formatted = self.listings[:]

        # Separate stories with blank line & highlight on focus
        for (i, l) in enumerate(listings_formatted):
            filled = urwid.Filler(urwid.AttrMap(l, None, 'focus'))
            listings_formatted[i] = urwid.BoxAdapter(filled, 3)
        listings_formatted.append(urwid.Divider("*"))

        self.listings_active = urwid.ListBox(
            urwid.SimpleListWalker(listings_formatted))
        return self.listings_active

    def __format_status(self):
        """format status text for use in footer"""
        if self.__subreddit is None:
            subreddit_text = "/r/front_page"
        else:
            subreddit_text = "/r/" + self.__subreddit
        status = "[{0}] ({1}) ?: help n/m:pagination".format(
            self.nav.count / 25 + 1, subreddit_text)
        return status

    def switch_page(self, direction):
        """load stories from the previous or next page"""
        if direction == "prev":
            self.set_status("(<) Loading...")
            self.__load_stories(direction=direction)
        elif direction == "next":
            self.set_status("(>) Loading...")
            self.__load_stories(direction=direction)
        else:
            raise Exception, "Direction must be 'prev' or 'next'"
        main_widget = self.__get_widget()
        self.frame.set_body(main_widget)
        self.set_status()

    def set_status(self, message=None):
        """write message on footer or else default status string"""
        if message is None:
            status = self.__format_status()
        else:
            status = message
        self.footer_content.set_text(('footer', status))

        global main_loop
        if not main_loop is None:
            main_loop.draw_screen()

    def refresh(self):
        """reload stories in main window"""
        self.set_status("Reloading...")
        self.nav = None
        try:
            self.__load_stories()
        except BadSubredditError:
            self.set_status("Error loading subreddit!")
            return
        main_widget = self.__get_widget()
        self.frame.set_body(main_widget)
        self.set_status()
Exemple #4
0
class MainWindow(object):
    """manages main window elements"""
    def __init__(self):
        # Handles page downloads and cookies                            
        self.handler = RedditHandler()
        self.listings = []
        self.__subreddit = None
        self.nav = None
        self.__load_stories()
        
        # Prep header and footer ui widgets 
        self.__update_header()
        self.footer_content = urwid.Text(('footer', ""), wrap='clip') 
        self.footer = urwid.Padding(self.footer_content, left=1, right=1)
        
        self.frame = urwid.Frame(   self.__get_widget(),
                                    header=self.header, 
                                    footer=self.footer )

    
    def login(self, username, password):
        """attempt to login"""
        login_result = self.handler.login(username, password)
        if login_result:
            self.__update_header()
        return login_result
      
        
    def set_subreddit(self, subreddit):
        """switch subreddits"""
        self.nav = None
        old_subreddit = self.__subreddit
        if subreddit == "fp":
            self.__subreddit = None
        else:
            self.set_status("Loading subreddit: /r/{0}".format(subreddit))
            self.__subreddit = subreddit
        try:
            self.__load_stories()
        except BadSubredditError:
            self.set_status("Error loading subreddit /r/{0}!".format(subreddit))
            self.__subreddit = old_subreddit
            self.__load_stories()
        main_widget = self.__get_widget()
        self.frame.set_body(main_widget)
        self.set_status()
    
    def __update_header(self):
        """set the titlebar to the currently logged in user"""
        if self.handler.user:
            header_text = "[{0}] - reddit-cli - github.com/cev/reddit-cli".format(self.handler.user)
        else:
            header_text = "reddit-cli - github.com/cev/reddit-cli"
        self.header = urwid.Text(('header',
                                header_text),
                                align='center')
        if hasattr(self, 'frame'):
            self.frame.set_header(self.header)                                   
 
    def __load_stories(self, direction=None):
        """load stories from (sub)reddit and store Listings"""
        self.listings = []
        data = self.handler.download_stories(self.__subreddit, self.nav, direction)
        
        self.nav = data[1]
        for s in data[0]:
            current = Listing(s)
            self.listings.append(urwid.Padding(current, left=1, right=1))
        
    def __get_widget(self):
        """return TextBox widget containing all Listings"""
        listings_formatted = self.listings[:]
            
        # Separate stories with blank line & highlight on focus
        for (i, l) in enumerate(listings_formatted):
            filled = urwid.Filler(urwid.AttrMap(l, None, 'focus'))
            listings_formatted[i] = urwid.BoxAdapter(filled, 3)
        listings_formatted.append(urwid.Divider("*"))
        
        self.listings_active = urwid.ListBox(urwid.SimpleListWalker(listings_formatted))
        return self.listings_active
        
    def __format_status(self):
        """format status text for use in footer"""
        if self.__subreddit is None:
            subreddit_text = "/r/front_page"
        else:
            subreddit_text = "/r/" + self.__subreddit
        status = "[{0}] ({1}) ?: help n/m:pagination".format(self.nav.count/25+1, subreddit_text)
        return status
    
    def switch_page(self, direction):
        """load stories from the previous or next page"""
        if direction == "prev":
            self.set_status("(<) Loading...")
            self.__load_stories(direction=direction)
        elif direction == "next":
            self.set_status("(>) Loading...")
            self.__load_stories(direction=direction)
        else:
            raise Exception, "Direction must be 'prev' or 'next'"
        main_widget = self.__get_widget()
        self.frame.set_body(main_widget)
        self.set_status()
    
    def set_status(self, message=None):
        """write message on footer or else default status string"""
        if message is None:
            status = self.__format_status()
        else:
            status = message
        self.footer_content.set_text(('footer', status))
        
        global main_loop
        if not main_loop is None:
            main_loop.draw_screen()
        
    def refresh(self):
        """reload stories in main window"""
        self.set_status("Reloading...")
        self.nav = None
        try:
            self.__load_stories()
        except BadSubredditError:
            self.set_status("Error loading subreddit!")
            return
        main_widget = self.__get_widget()
        self.frame.set_body(main_widget)
        self.set_status()