# Buggy code
def keypress(self, size, key):
    key = common.shortcuts(key)
    if key == "A":
        for f in self.master.view:
            if f.intercepted:
                f.resume(self.master)
        signals.flowlist_change.send(self)
    elif key == "z":
        self.master.view.clear()
    elif key == "e":
        self.master.toggle_eventlog()
    elif key == "g":
        self.master.view.focus.index = 0
    elif key == "G":
        self.master.view.focus.index = len(self.master.view) - 1
    elif key == "f":
        signals.status_prompt.send(
            prompt="Filter View",
            text=self.master.options.filter,
            callback=self.master.options.setter("filter"),
        )
    elif key == "L":
        signals.status_prompt_path.send(
            self, prompt="Load flows", callback=self.master.load_flows_callback
        )
    elif key == "n":
        signals.status_prompt_onekey.send(
            prompt="Method", keys=common.METHOD_OPTIONS, callback=self.get_method
        )
    elif key == "o":
        orders = [(i[1], i[0]) for i in view.orders]
        lookup = dict([(i[0], i[1]) for i in view.orders])

        def change_order(k):
            self.master.options.order = lookup[k]

        signals.status_prompt_onekey.send(
            prompt="Order", keys=orders, callback=change_order
        )
    elif key == "F":
        o = self.master.options
        o.focus_follow = not o.focus_follow
    elif key == "v":
        val = not self.master.options.order_reversed
        self.master.options.order_reversed = val
    elif key == "W":
        if self.master.options.streamfile:
            self.master.options.streamfile = None
        else:
            signals.status_prompt_path.send(
                self,
                prompt="Stream flows to",
                callback=lambda path: self.master.options.update(streamfile=path),
            )
    else:
        return urwid.ListBox.keypress(self, size, key)


# Traceback (ValueError)
Traceback (most recent call last):
File "/mnt/c/Users/user/git/mitmproxy/mitmproxy/tools/console/master.py", line 290, in run
self.loop.run()
File "/usr/local/lib/python3.5/dist-packages/urwid/main_loop.py", line 278, in run
self._run()
File "/usr/local/lib/python3.5/dist-packages/urwid/main_loop.py", line 376, in _run
self.event_loop.run()
File "/usr/local/lib/python3.5/dist-packages/urwid/main_loop.py", line 682, in run
self._loop()
File "/usr/local/lib/python3.5/dist-packages/urwid/main_loop.py", line 719, in _loop
self._watch_files[fd]()
File "/usr/local/lib/python3.5/dist-packages/urwid/raw_display.py", line 393, in <lambda>
event_loop, callback, self.get_available_raw_input())
File "/usr/local/lib/python3.5/dist-packages/urwid/raw_display.py", line 493, in parse_input
callback(processed, processed_codes)
File "/usr/local/lib/python3.5/dist-packages/urwid/main_loop.py", line 403, in _update
self.process_input(keys)
File "/usr/local/lib/python3.5/dist-packages/urwid/main_loop.py", line 503, in process_input
k = self._topmost_widget.keypress(self.screen_size, k)
File "/mnt/c/Users/user/git/mitmproxy/mitmproxy/tools/console/window.py", line 84, in keypress
k = super().keypress(size, k)
File "/usr/local/lib/python3.5/dist-packages/urwid/container.py", line 1128, in keypress
return self.body.keypress( (maxcol, remaining), key )
File "/mnt/c/Users/user/git/mitmproxy/mitmproxy/tools/console/flowlist.py", line 360, in keypress
self.master.view.focus.index = len(self.master.view) - 1
File "/mnt/c/Users/user/git/mitmproxy/mitmproxy/addons/view.py", line 364, in index
raise ValueError("Index out of view bounds")
ValueError: Index out of view bounds

# Fixed code
def keypress(self, size, key):
    key = common.shortcuts(key)
    if key == "A":
        for f in self.master.view:
            if f.intercepted:
                f.resume(self.master)
        signals.flowlist_change.send(self)
    elif key == "z":
        self.master.view.clear()
    elif key == "e":
        self.master.toggle_eventlog()
    elif key == "g":
        if len(self.master.view):
            self.master.view.focus.index = 0
    elif key == "G":
        if len(self.master.view):
            self.master.view.focus.index = len(self.master.view) - 1
    elif key == "f":
        signals.status_prompt.send(
            prompt="Filter View",
            text=self.master.options.filter,
            callback=self.master.options.setter("filter"),
        )
    elif key == "L":
        signals.status_prompt_path.send(
            self, prompt="Load flows", callback=self.master.load_flows_callback
        )
    elif key == "n":
        signals.status_prompt_onekey.send(
            prompt="Method", keys=common.METHOD_OPTIONS, callback=self.get_method
        )
    elif key == "o":
        orders = [(i[1], i[0]) for i in view.orders]
        lookup = dict([(i[0], i[1]) for i in view.orders])

        def change_order(k):
            self.master.options.order = lookup[k]

        signals.status_prompt_onekey.send(
            prompt="Order", keys=orders, callback=change_order
        )
    elif key == "F":
        o = self.master.options
        o.focus_follow = not o.focus_follow
    elif key == "v":
        val = not self.master.options.order_reversed
        self.master.options.order_reversed = val
    elif key == "W":
        if self.master.options.streamfile:
            self.master.options.streamfile = None
        else:
            signals.status_prompt_path.send(
                self,
                prompt="Stream flows to",
                callback=lambda path: self.master.options.update(streamfile=path),
            )
    else:
        return urwid.ListBox.keypress(self, size, key)