Make "T" pretty view over-ride persistent when switching between flows.

We do this by adding a flow settings mechanism to ConsoleState. This is pretty
rough at the moment and should become more sophisticated as needed.
This commit is contained in:
Aldo Cortesi 2012-04-02 09:30:38 +12:00
parent 35f4a1c424
commit 7fef0ecdf5
3 changed files with 47 additions and 18 deletions

View File

@ -14,7 +14,7 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import mailcap, mimetypes, tempfile, os, subprocess, glob, time, shlex
import os.path, sys
import os.path, sys, weakref
import urwid
from .. import controller, utils, flow
import flowlist, flowview, help, common, grideditor, palettes, contentview
@ -235,6 +235,15 @@ class ConsoleState(flow.State):
self.view_flow_mode = common.VIEW_FLOW_REQUEST
self.last_script = ""
self.last_saveload = ""
self.flowsettings = weakref.WeakKeyDictionary()
def add_flow_setting(self, flow, key, value):
d = self.flowsettings.setdefault(flow, {})
d[key] = value
def get_flow_setting(self, flow, key, default=None):
d = self.flowsettings.get(flow, {})
return d.get(key, default)
def add_request(self, req):
f = flow.State.add_request(self, req)

View File

@ -123,7 +123,6 @@ class FlowView(common.WWrap):
]
def __init__(self, master, state, flow):
self.master, self.state, self.flow = master, state, flow
self.view_body_pretty_type = contentview.VIEW_CONTENT_PRETTY_TYPE_AUTO
if self.state.view_flow_mode == common.VIEW_FLOW_RESPONSE:
self.view_response()
else:
@ -205,7 +204,11 @@ class FlowView(common.WWrap):
body = self._conn_text(
self.flow.request,
self.state.view_body_mode,
self.view_body_pretty_type
self.state.get_flow_setting(
self.flow,
(common.VIEW_FLOW_REQUEST, "prettyview"),
contentview.VIEW_CONTENT_PRETTY_TYPE_AUTO
)
)
self.w = self.wrap_body(common.VIEW_FLOW_REQUEST, body)
self.master.statusbar.redraw()
@ -216,7 +219,11 @@ class FlowView(common.WWrap):
body = self._conn_text(
self.flow.response,
self.state.view_body_mode,
self.view_body_pretty_type
self.state.get_flow_setting(
self.flow,
(common.VIEW_FLOW_RESPONSE, "prettyview"),
contentview.VIEW_CONTENT_PRETTY_TYPE_AUTO
)
)
else:
body = urwid.ListBox(
@ -371,18 +378,19 @@ class FlowView(common.WWrap):
return self._view_nextprev_flow("prev", flow)
def change_pretty_type(self, t):
if t == "a":
self.view_body_pretty_type = contentview.VIEW_CONTENT_PRETTY_TYPE_AUTO
elif t == "i":
self.view_body_pretty_type = contentview.VIEW_CONTENT_PRETTY_TYPE_IMAGE
elif t == "j":
self.view_body_pretty_type = contentview.VIEW_CONTENT_PRETTY_TYPE_JAVASCRIPT
elif t == "s":
self.view_body_pretty_type = contentview.VIEW_CONTENT_PRETTY_TYPE_JSON
elif t == "u":
self.view_body_pretty_type = contentview.VIEW_CONTENT_PRETTY_TYPE_URLENCODED
elif t == "x":
self.view_body_pretty_type = contentview.VIEW_CONTENT_PRETTY_TYPE_XML
d = {
"a": contentview.VIEW_CONTENT_PRETTY_TYPE_AUTO,
"i": contentview.VIEW_CONTENT_PRETTY_TYPE_IMAGE,
"j": contentview.VIEW_CONTENT_PRETTY_TYPE_JAVASCRIPT,
"s": contentview.VIEW_CONTENT_PRETTY_TYPE_JSON,
"u": contentview.VIEW_CONTENT_PRETTY_TYPE_URLENCODED,
"x": contentview.VIEW_CONTENT_PRETTY_TYPE_XML,
}
self.state.add_flow_setting(
self.flow,
(self.state.view_flow_mode, "prettyview"),
d.get(t)
)
self.master.refresh_flow(self.flow)
def keypress(self, size, key):

View File

@ -4,7 +4,7 @@ import tutils
import libpry
class uState(libpry.AutoTree):
class uConsoleState(libpry.AutoTree):
def test_flow(self):
"""
normal flow:
@ -76,6 +76,18 @@ class uState(libpry.AutoTree):
assert len(c.view) == 3
assert c.focus == 0
def test_settings(self):
c = console.ConsoleState()
f = self._add_request(c)
c.add_flow_setting(f, "foo", "bar")
assert c.get_flow_setting(f, "foo") == "bar"
assert c.get_flow_setting(f, "oink") == None
assert c.get_flow_setting(f, "oink", "foo") == "foo"
assert len(c.flowsettings) == 1
c.delete_flow(f)
del f
assert len(c.flowsettings) == 0
class uformat_keyvals(libpry.AutoTree):
def test_simple(self):
@ -139,7 +151,7 @@ class uOptions(libpry.AutoTree):
tests = [
uformat_keyvals(),
uState(),
uConsoleState(),
uPathCompleter(),
uOptions()
]