[tor-commits] [arm/master] Moving config value editing to the config panel
atagar at torproject.org
atagar at torproject.org
Tue May 10 05:10:22 UTC 2011
commit 52998db3600cdc42e4290b9b3fa5547390a2893a
Author: Damian Johnson <atagar at torproject.org>
Date: Mon May 9 22:02:03 2011 -0700
Moving config value editing to the config panel
---
src/cli/configPanel.py | 46 ++++++++++++++++++++++++++++++++++++++
src/cli/controller.py | 57 ------------------------------------------------
src/cli/popups.py | 14 ++++++-----
3 files changed, 54 insertions(+), 63 deletions(-)
diff --git a/src/cli/configPanel.py b/src/cli/configPanel.py
index 31a78ab..37b7ddb 100644
--- a/src/cli/configPanel.py
+++ b/src/cli/configPanel.py
@@ -11,6 +11,7 @@ import popups
from util import conf, enum, panel, torTools, torConfig, uiTools
DEFAULT_CONFIG = {"features.config.selectionDetails.height": 6,
+ "features.config.prepopulateEditValues": True,
"features.config.state.showPrivateOptions": False,
"features.config.state.showVirtualOptions": False,
"features.config.state.colWidth.option": 25,
@@ -135,6 +136,14 @@ class ConfigEntry():
return self.labelCache
+ def isUnset(self):
+ """
+ True if we have no value, false otherwise.
+ """
+
+ confValue = torTools.getConn().getOption(self.get(Field.OPTION), [], True)
+ return not bool(confValue)
+
def _getValue(self):
"""
Provides the current value of the configuration entry, taking advantage of
@@ -256,6 +265,43 @@ class ConfigPanel(panel.Panel):
isChanged = self.scroller.handleKey(key, self._getConfigOptions(), pageHeight)
if isChanged: self.redraw(True)
+ elif uiTools.isSelectionKey(key):
+ # Prompts the user to edit the selected configuration value. The
+ # interface is locked to prevent updates between setting the value
+ # and showing any errors.
+
+ panel.CURSES_LOCK.acquire()
+ try:
+ selection = self.getSelection()
+ configOption = selection.get(Field.OPTION)
+ if selection.isUnset(): initialValue = ""
+ else: initialValue = selection.get(Field.VALUE)
+
+ promptMsg = "%s Value (esc to cancel): " % configOption
+ isPrepopulated = self._config["features.config.prepopulateEditValues"]
+ newValue = popups.inputPrompt(promptMsg, initialValue if isPrepopulated else "")
+
+ if newValue != None and newValue != initialValue:
+ try:
+ if selection.get(Field.TYPE) == "Boolean":
+ # if the value's a boolean then allow for 'true' and 'false' inputs
+ if newValue.lower() == "true": newValue = "1"
+ elif newValue.lower() == "false": newValue = "0"
+ elif selection.get(Field.TYPE) == "LineList":
+ # setOption accepts list inputs when there's multiple values
+ newValue = newValue.split(",")
+
+ torTools.getConn().setOption(configOption, newValue)
+
+ # resets the isDefault flag
+ customOptions = torConfig.getCustomOptions()
+ selection.fields[Field.IS_DEFAULT] = not configOption in customOptions
+
+ self.redraw(True)
+ except Exception, exc:
+ popups.showMsg("%s (press any key)" % exc)
+ finally:
+ panel.CURSES_LOCK.release()
elif key == ord('a') or key == ord('A'):
self.showAll = not self.showAll
self.redraw(True)
diff --git a/src/cli/controller.py b/src/cli/controller.py
index 3d40631..63b3b86 100644
--- a/src/cli/controller.py
+++ b/src/cli/controller.py
@@ -98,7 +98,6 @@ PAGES = [
CONFIG = {"log.torrc.readFailed": log.WARN,
"features.graph.type": 1,
- "features.config.prepopulateEditValues": True,
"queries.refreshRate.rate": 5,
"log.torEventTypeUnrecognized": log.NOTICE,
"features.graph.bw.prepopulate": True,
@@ -1101,62 +1100,6 @@ def drawTorMonitor(stdscr, startTime, loggedEvents, isBlindMode):
panel.CURSES_LOCK.release()
panels["config"].redraw(True)
- elif page == 2 and uiTools.isSelectionKey(key):
- # let the user edit the configuration value, unchanged if left blank
- panel.CURSES_LOCK.acquire()
- try:
- setPauseState(panels, isPaused, page, True)
-
- # provides prompt
- selection = panels["config"].getSelection()
- configOption = selection.get(configPanel.Field.OPTION)
- titleMsg = "%s Value (esc to cancel): " % configOption
- panels["control"].setMsg(titleMsg)
- panels["control"].redraw(True)
-
- displayWidth = panels["control"].getPreferredSize()[1]
- initialValue = selection.get(configPanel.Field.VALUE)
-
- # initial input for the text field
- initialText = ""
- if CONFIG["features.config.prepopulateEditValues"] and initialValue != "<none>":
- initialText = initialValue
-
- newConfigValue = panels["control"].getstr(0, len(titleMsg), initialText)
-
- # it would be nice to quit on esc, but looks like this might not be possible...
- if newConfigValue != None and newConfigValue != initialValue:
- conn = torTools.getConn()
-
- # if the value's a boolean then allow for 'true' and 'false' inputs
- if selection.get(configPanel.Field.TYPE) == "Boolean":
- if newConfigValue.lower() == "true": newConfigValue = "1"
- elif newConfigValue.lower() == "false": newConfigValue = "0"
-
- try:
- if selection.get(configPanel.Field.TYPE) == "LineList":
- newConfigValue = newConfigValue.split(",")
-
- conn.setOption(configOption, newConfigValue)
-
- # resets the isDefault flag
- customOptions = torConfig.getCustomOptions()
- selection.fields[configPanel.Field.IS_DEFAULT] = not configOption in customOptions
-
- panels["config"].redraw(True)
- except Exception, exc:
- errorMsg = "%s (press any key)" % exc
- panels["control"].setMsg(uiTools.cropStr(errorMsg, displayWidth), curses.A_STANDOUT)
- panels["control"].redraw(True)
-
- curses.cbreak() # wait indefinitely for key presses (no timeout)
- stdscr.getch()
- curses.halfdelay(REFRESH_RATE * 10)
-
- panels["control"].setMsg(CTL_PAUSED if isPaused else CTL_HELP)
- setPauseState(panels, isPaused, page)
- finally:
- panel.CURSES_LOCK.release()
else:
for pagePanel in getPanels(page + 1):
isKeystrokeConsumed = pagePanel.handleKey(key)
diff --git a/src/cli/popups.py b/src/cli/popups.py
index 81b4589..2b02685 100644
--- a/src/cli/popups.py
+++ b/src/cli/popups.py
@@ -44,32 +44,33 @@ def finalize():
controller.refresh()
panel.CURSES_LOCK.release()
-def inputPrompt(msg):
+def inputPrompt(msg, initialValue = ""):
"""
Prompts the user to enter a string on the control line (which usually
displays the page number and basic controls).
Arguments:
- msg - message to prompt the user for input with
+ msg - message to prompt the user for input with
+ initialValue - initial value of the field
"""
panel.CURSES_LOCK.acquire()
controlPanel = controller.getPanel("control")
controlPanel.setMsg(msg)
controlPanel.redraw(True)
- userInput = controlPanel.getstr(0, len(msg))
+ userInput = controlPanel.getstr(0, len(msg), initialValue)
controlPanel.revertMsg()
panel.CURSES_LOCK.release()
return userInput
-def showMsg(msg, maxWait, attr = curses.A_STANDOUT):
+def showMsg(msg, maxWait = -1, attr = curses.A_STANDOUT):
"""
Displays a single line message on the control line for a set time. Pressing
any key will end the message.
Arguments:
msg - message to be displayed to the user
- maxWait - time to show the message
+ maxWait - time to show the message, indefinite if -1
attr - attributes with which to draw the message
"""
@@ -78,7 +79,8 @@ def showMsg(msg, maxWait, attr = curses.A_STANDOUT):
controlPanel.setMsg(msg, attr)
controlPanel.redraw(True)
- curses.halfdelay(maxWait * 10)
+ if maxWait == -1: curses.cbreak()
+ else: curses.halfdelay(maxWait * 10)
controller.getScreen().getch()
controlPanel.revertMsg()
curses.halfdelay(controller.REFRESH_RATE * 10)
More information about the tor-commits
mailing list