Repository: mitmproxy/mitmproxy

Issue:
Pressing g/G crashes mitmproxy when flow list is empty
##### Steps to reproduce the problem:

1. `mitmproxy`
2. `g` or `G`

```
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
```


Discussion:

Resolving patch:
diff --git a/mitmproxy/addons/view.py b/mitmproxy/addons/view.py
--- a/mitmproxy/addons/view.py
+++ b/mitmproxy/addons/view.py
@@ -145,9 +145,9 @@ def store_count(self):
 
     def inbounds(self, index: int) -> bool:
         """
-            Is this index >= 0 and < len(self)
+            Is this 0 <= index < len(self)
         """
-        return index >= 0 and index < len(self)
+        return 0 <= index < len(self)
 
     def _rev(self, idx: int) -> int:
         """
@@ -359,7 +359,7 @@ def index(self) -> typing.Optional[int]:
             return self.view.index(self.flow)
 
     @index.setter
-    def index(self, idx) -> typing.Optional[int]:
+    def index(self, idx):
         if idx < 0 or idx > len(self.view) - 1:
             raise ValueError("Index out of view bounds")
         self.flow = self.view[idx]
diff --git a/mitmproxy/tools/console/flowlist.py b/mitmproxy/tools/console/flowlist.py
--- a/mitmproxy/tools/console/flowlist.py
+++ b/mitmproxy/tools/console/flowlist.py
@@ -355,9 +355,11 @@ def keypress(self, size, key):
         elif key == "e":
             self.master.toggle_eventlog()
         elif key == "g":
-            self.master.view.focus.index = 0
+            if len(self.master.view):
+                self.master.view.focus.index = 0
         elif key == "G":
-            self.master.view.focus.index = len(self.master.view) - 1
+            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",


Test patch: