def open_link(self, url):
        """
        Open a media link using the definitions from the user's mailcap file.

        Most urls are parsed using their file extension, but special cases
        exist for websites that are prevalent on reddit such as Imgur and
        Gfycat. If there are no valid mailcap definitions, RTV will fall back
        to using the default webbrowser.

        RTV checks for certain mailcap fields to determine how to open a link:
            - If ``copiousoutput`` is specified, the curses application will
              be paused and stdout will be piped to the system pager.
            - If `needsterminal`` is specified, the curses application will
              yield terminal control to the subprocess until it has exited.
            - Otherwise, we assume that the subprocess is meant to open a new
              x-window, and we swallow all stdout output.

        Examples:
            Stream youtube videos with VLC
            Browse images and imgur albums with feh
            Watch .webm videos through your terminal with mplayer
            View images directly in your terminal with fbi or w3m
            Play .mp3 files with sox player
            Send HTML pages your pager using to html2text
            ...anything is possible!
        """

        if not self.config['enable_media']:
            self.open_browser(url)
            return

        try:
            with self.loader('Checking link', catch_exception=False):
                command, entry = self.get_mailcap_entry(url)
        except exceptions.MailcapEntryNotFound:
            self.open_browser(url)
            return

        _logger.info('Executing command: %s', command)
        needs_terminal = 'needsterminal' in entry
        copious_output = 'copiousoutput' in entry

        if needs_terminal or copious_output:
            # Blocking, pause rtv until the process returns
            with self.suspend():
                os.system('clear')
                p = subprocess.Popen(
                    [command], stderr=subprocess.PIPE,
                    universal_newlines=True, shell=True)
                _, stderr = p.communicate()
                if copious_output:
                    six.moves.input('Press any key to continue')
            code = p.poll()
            if code != 0:
                _logger.warning(stderr)
                self.show_notification(
                    'Program exited with status={0}\n{1}'.format(
                        code, stderr.strip()), style='Error')

        else:
            # Non-blocking, open a background process
            with self.loader('Opening page', delay=0):
                p = subprocess.Popen(
                    [command], shell=True, universal_newlines=True,
                    stdout=subprocess.PIPE, stderr=subprocess.PIPE)
                # Wait a little while to make sure that the command doesn't
                # exit with an error. This isn't perfect, but it should be good
                # enough to catch invalid commands.
                time.sleep(1.0)
                code = p.poll()
                if code is not None and code != 0:
                    _, stderr = p.communicate()
                    raise exceptions.BrowserError(
                        'Program exited with status={0}\n{1}'.format(
                            code, stderr.strip()))

                # Spin off a thread with p.communicate() to avoid subprocess
                # hang when the stodout/stderr PIPE gets filled up. This
                # behavior was discovered when opening long gifs with mpv
                # because mpv sends a progress bar to stderr.
                # https://thraxil.org/users/anders/posts/2008/03/13/
                threading.Thread(target=p.communicate).start()

        Open a media link using the definitions from the user's mailcap file.

        Most urls are parsed using their file extension, but special cases
        exist for websites that are prevalent on reddit such as Imgur and
        Gfycat. If there are no valid mailcap definitions, RTV will fall back
        to using the default webbrowser.

        RTV checks for certain mailcap fields to determine how to open a link:
            - If ``copiousoutput`` is specified, the curses application will
              be paused and stdout will be piped to the system pager.
            - If `needsterminal`` is specified, the curses application will
              yield terminal control to the subprocess until it has exited.
            - Otherwise, we assume that the subprocess is meant to open a new
              x-window, and we swallow all stdout output.

        Examples:
            Stream youtube videos with VLC
            Browse images and imgur albums with feh
            Watch .webm videos through your terminal with mplayer
            View images directly in your terminal with fbi or w3m
            Play .mp3 files with sox player
            Send HTML pages your pager using to html2text
            ...anything is possible!
        
rtv/terminal.py