mirror of
https://github.com/Grasscutters/mitmproxy.git
synced 2024-11-27 02:24:18 +00:00
Some refactoring of the console inteface.
Reduce some state duplication, by removing currentflow variable. Fixes #141
This commit is contained in:
parent
10b744ee08
commit
10a9e3365f
@ -204,14 +204,9 @@ class StatusBar(common.WWrap):
|
|||||||
self.message("")
|
self.message("")
|
||||||
|
|
||||||
fc = self.master.state.flow_count()
|
fc = self.master.state.flow_count()
|
||||||
if self.master.currentflow:
|
offset = min(self.master.state.focus + 1, fc)
|
||||||
idx = self.master.state.view.index(self.master.currentflow) + 1
|
|
||||||
t = [
|
t = [
|
||||||
('heading', ("[%s/%s]"%(idx, fc)).ljust(9))
|
('heading', ("[%s/%s]"%(offset, fc)).ljust(9))
|
||||||
]
|
|
||||||
else:
|
|
||||||
t = [
|
|
||||||
('heading', ("[%s]"%fc).ljust(9))
|
|
||||||
]
|
]
|
||||||
|
|
||||||
if self.master.server.bound:
|
if self.master.server.bound:
|
||||||
@ -263,7 +258,10 @@ class ConsoleState(flow.State):
|
|||||||
self.focus = None
|
self.focus = None
|
||||||
self.follow_focus = None
|
self.follow_focus = None
|
||||||
self.default_body_view = contentview.get("Auto")
|
self.default_body_view = contentview.get("Auto")
|
||||||
|
|
||||||
|
self.view_mode = common.VIEW_LIST
|
||||||
self.view_flow_mode = common.VIEW_FLOW_REQUEST
|
self.view_flow_mode = common.VIEW_FLOW_REQUEST
|
||||||
|
|
||||||
self.last_script = ""
|
self.last_script = ""
|
||||||
self.last_saveload = ""
|
self.last_saveload = ""
|
||||||
self.flowsettings = weakref.WeakKeyDictionary()
|
self.flowsettings = weakref.WeakKeyDictionary()
|
||||||
@ -308,6 +306,9 @@ class ConsoleState(flow.State):
|
|||||||
idx = 0
|
idx = 0
|
||||||
self.focus = idx
|
self.focus = idx
|
||||||
|
|
||||||
|
def set_focus_flow(self, f):
|
||||||
|
self.set_focus(self.view.index(f))
|
||||||
|
|
||||||
def get_from_pos(self, pos):
|
def get_from_pos(self, pos):
|
||||||
if len(self.view) <= pos or pos < 0:
|
if len(self.view) <= pos or pos < 0:
|
||||||
return None, None
|
return None, None
|
||||||
@ -320,6 +321,8 @@ class ConsoleState(flow.State):
|
|||||||
return self.get_from_pos(pos-1)
|
return self.get_from_pos(pos-1)
|
||||||
|
|
||||||
def delete_flow(self, f):
|
def delete_flow(self, f):
|
||||||
|
if f in self.view and self.view.index(f) <= self.focus:
|
||||||
|
self.focus -= 1
|
||||||
ret = flow.State.delete_flow(self, f)
|
ret = flow.State.delete_flow(self, f)
|
||||||
self.set_focus(self.focus)
|
self.set_focus(self.focus)
|
||||||
return ret
|
return ret
|
||||||
@ -570,8 +573,6 @@ class ConsoleMaster(flow.FlowMaster):
|
|||||||
self.palette = palettes.palettes[name]
|
self.palette = palettes.palettes[name]
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
self.currentflow = None
|
|
||||||
|
|
||||||
self.ui = urwid.raw_display.Screen()
|
self.ui = urwid.raw_display.Screen()
|
||||||
self.ui.set_terminal_properties(256)
|
self.ui.set_terminal_properties(256)
|
||||||
self.ui.register_palette(self.palette)
|
self.ui.register_palette(self.palette)
|
||||||
@ -605,13 +606,6 @@ class ConsoleMaster(flow.FlowMaster):
|
|||||||
sys.stderr.flush()
|
sys.stderr.flush()
|
||||||
self.shutdown()
|
self.shutdown()
|
||||||
|
|
||||||
def focus_current(self):
|
|
||||||
if self.currentflow:
|
|
||||||
try:
|
|
||||||
self.flow_list_walker.set_focus(self.state.view.index(self.currentflow))
|
|
||||||
except (IndexError, ValueError):
|
|
||||||
pass
|
|
||||||
|
|
||||||
def make_view(self):
|
def make_view(self):
|
||||||
self.view = urwid.Frame(
|
self.view = urwid.Frame(
|
||||||
self.body,
|
self.body,
|
||||||
@ -646,8 +640,6 @@ class ConsoleMaster(flow.FlowMaster):
|
|||||||
self.ui.clear()
|
self.ui.clear()
|
||||||
if self.state.follow_focus:
|
if self.state.follow_focus:
|
||||||
self.state.set_focus(self.state.flow_count())
|
self.state.set_focus(self.state.flow_count())
|
||||||
else:
|
|
||||||
self.focus_current()
|
|
||||||
|
|
||||||
if self.eventlog:
|
if self.eventlog:
|
||||||
self.body = flowlist.BodyPile(self)
|
self.body = flowlist.BodyPile(self)
|
||||||
@ -655,7 +647,7 @@ class ConsoleMaster(flow.FlowMaster):
|
|||||||
self.body = flowlist.FlowListBox(self)
|
self.body = flowlist.FlowListBox(self)
|
||||||
self.statusbar = StatusBar(self, flowlist.footer)
|
self.statusbar = StatusBar(self, flowlist.footer)
|
||||||
self.header = None
|
self.header = None
|
||||||
self.currentflow = None
|
self.state.view_mode = common.VIEW_LIST
|
||||||
|
|
||||||
self.make_view()
|
self.make_view()
|
||||||
self.help_context = flowlist.help_context
|
self.help_context = flowlist.help_context
|
||||||
@ -664,7 +656,8 @@ class ConsoleMaster(flow.FlowMaster):
|
|||||||
self.body = flowview.FlowView(self, self.state, flow)
|
self.body = flowview.FlowView(self, self.state, flow)
|
||||||
self.header = flowview.FlowViewHeader(self, flow)
|
self.header = flowview.FlowViewHeader(self, flow)
|
||||||
self.statusbar = StatusBar(self, flowview.footer)
|
self.statusbar = StatusBar(self, flowview.footer)
|
||||||
self.currentflow = flow
|
self.state.set_focus_flow(flow)
|
||||||
|
self.state.view_mode = common.VIEW_FLOW
|
||||||
|
|
||||||
self.make_view()
|
self.make_view()
|
||||||
self.help_context = flowview.help_context
|
self.help_context = flowview.help_context
|
||||||
@ -711,7 +704,6 @@ class ConsoleMaster(flow.FlowMaster):
|
|||||||
f.close()
|
f.close()
|
||||||
if self.flow_list_walker:
|
if self.flow_list_walker:
|
||||||
self.sync_list_view()
|
self.sync_list_view()
|
||||||
self.focus_current()
|
|
||||||
return reterr
|
return reterr
|
||||||
|
|
||||||
def path_prompt(self, prompt, text, callback, *args):
|
def path_prompt(self, prompt, text, callback, *args):
|
||||||
@ -777,8 +769,7 @@ class ConsoleMaster(flow.FlowMaster):
|
|||||||
def change_default_display_mode(self, t):
|
def change_default_display_mode(self, t):
|
||||||
v = contentview.get_by_shortcut(t)
|
v = contentview.get_by_shortcut(t)
|
||||||
self.state.default_body_view = v
|
self.state.default_body_view = v
|
||||||
if self.currentflow:
|
self.refresh_focus()
|
||||||
self.refresh_flow(self.currentflow)
|
|
||||||
|
|
||||||
def set_reverse_proxy(self, txt):
|
def set_reverse_proxy(self, txt):
|
||||||
if not txt:
|
if not txt:
|
||||||
@ -796,8 +787,8 @@ class ConsoleMaster(flow.FlowMaster):
|
|||||||
return size
|
return size
|
||||||
|
|
||||||
def pop_view(self):
|
def pop_view(self):
|
||||||
if self.currentflow:
|
if self.state.view_mode == common.VIEW_FLOW:
|
||||||
self.view_flow(self.currentflow)
|
self.view_flow(self.state.view[self.state.focus])
|
||||||
else:
|
else:
|
||||||
self.view_flowlist()
|
self.view_flowlist()
|
||||||
|
|
||||||
@ -972,7 +963,7 @@ class ConsoleMaster(flow.FlowMaster):
|
|||||||
if a == "h":
|
if a == "h":
|
||||||
self.showhost = not self.showhost
|
self.showhost = not self.showhost
|
||||||
self.sync_list_view()
|
self.sync_list_view()
|
||||||
self.refresh_flow(self.currentflow)
|
self.refresh_focus()
|
||||||
elif a == "k":
|
elif a == "k":
|
||||||
self.killextra = not self.killextra
|
self.killextra = not self.killextra
|
||||||
elif a == "n":
|
elif a == "n":
|
||||||
@ -1003,6 +994,10 @@ class ConsoleMaster(flow.FlowMaster):
|
|||||||
self.state.delete_flow(f)
|
self.state.delete_flow(f)
|
||||||
self.sync_list_view()
|
self.sync_list_view()
|
||||||
|
|
||||||
|
def refresh_focus(self):
|
||||||
|
if self.state.view:
|
||||||
|
self.refresh_flow(self.state.view[self.state.focus])
|
||||||
|
|
||||||
def refresh_flow(self, c):
|
def refresh_flow(self, c):
|
||||||
if hasattr(self.header, "refresh_flow"):
|
if hasattr(self.header, "refresh_flow"):
|
||||||
self.header.refresh_flow(c)
|
self.header.refresh_flow(c)
|
||||||
|
@ -18,6 +18,9 @@ import urwid.util
|
|||||||
from .. import utils, flow
|
from .. import utils, flow
|
||||||
|
|
||||||
|
|
||||||
|
VIEW_LIST = 0
|
||||||
|
VIEW_FLOW = 1
|
||||||
|
|
||||||
|
|
||||||
VIEW_FLOW_REQUEST = 0
|
VIEW_FLOW_REQUEST = 0
|
||||||
VIEW_FLOW_RESPONSE = 1
|
VIEW_FLOW_RESPONSE = 1
|
||||||
|
@ -160,8 +160,7 @@ class ConnectionItem(common.WWrap):
|
|||||||
self.master.sync_list_view()
|
self.master.sync_list_view()
|
||||||
elif key == "D":
|
elif key == "D":
|
||||||
f = self.master.duplicate_flow(self.flow)
|
f = self.master.duplicate_flow(self.flow)
|
||||||
self.master.currentflow = f
|
self.master.view_flow(f)
|
||||||
self.master.focus_current()
|
|
||||||
elif key == "r":
|
elif key == "r":
|
||||||
self.flow.backup()
|
self.flow.backup()
|
||||||
r = self.master.replay_request(self.flow)
|
r = self.master.replay_request(self.flow)
|
||||||
|
@ -393,7 +393,7 @@ class FlowView(common.WWrap):
|
|||||||
new_flow, new_idx = self.state.get_next(idx)
|
new_flow, new_idx = self.state.get_next(idx)
|
||||||
else:
|
else:
|
||||||
new_flow, new_idx = self.state.get_prev(idx)
|
new_flow, new_idx = self.state.get_prev(idx)
|
||||||
if new_idx is None:
|
if new_flow is None:
|
||||||
self.master.statusbar.message("No more flows!")
|
self.master.statusbar.message("No more flows!")
|
||||||
return
|
return
|
||||||
self.master.view_flow(new_flow)
|
self.master.view_flow(new_flow)
|
||||||
@ -478,7 +478,6 @@ class FlowView(common.WWrap):
|
|||||||
elif key == "D":
|
elif key == "D":
|
||||||
f = self.master.duplicate_flow(self.flow)
|
f = self.master.duplicate_flow(self.flow)
|
||||||
self.master.view_flow(f)
|
self.master.view_flow(f)
|
||||||
self.master.currentflow = f
|
|
||||||
self.master.statusbar.message("Duplicated.")
|
self.master.statusbar.message("Duplicated.")
|
||||||
elif key == "e":
|
elif key == "e":
|
||||||
if self.state.view_flow_mode == common.VIEW_FLOW_REQUEST:
|
if self.state.view_flow_mode == common.VIEW_FLOW_REQUEST:
|
||||||
|
@ -591,7 +591,7 @@ class TestFlowMaster:
|
|||||||
s = flow.State()
|
s = flow.State()
|
||||||
fm = flow.FlowMaster(None, s)
|
fm = flow.FlowMaster(None, s)
|
||||||
f = tutils.tflow_full()
|
f = tutils.tflow_full()
|
||||||
fm.load_flow(f)
|
f = fm.load_flow(f)
|
||||||
assert s.flow_count() == 1
|
assert s.flow_count() == 1
|
||||||
f2 = fm.duplicate_flow(f)
|
f2 = fm.duplicate_flow(f)
|
||||||
assert f2.response
|
assert f2.response
|
||||||
|
Loading…
Reference in New Issue
Block a user