[Urwid] detecting when an edit box has changed

Derek Peschel dpeschel at eskimo.com
Sat Jul 8 00:17:20 EDT 2006


On Fri, Jul 07, 2006 at 11:36:09PM -0400, Ian Ward wrote:
> Eric S. Johansson wrote:
> 
> > is there any way to detect when an edit box has changed short of 
> > comparing against some "original" data source?
> 
> You can hook its keypress() function and check if it returned None 
> (which means that the key was handled by the edit widget. To do that you 
> can either subclass the Edit widget or the WidgetWrap class to define 
> your own keypress function.

I swore I'd let urwid wait for a day or two, but people keep addressing
topics I've also been thinking about.

All keypresses don't create changed text; some just move the cursor.  Even
the keystrokes that you expect to change text (like backspace) don't always,
as when you backspace at the beginning of the text.  I've been trying out
a few ideas:

	- For a single edit widget, the keypres method that sets a
	  "changed" flag -- called "dirty" in the software I'm familiar
	  with.  But sometimes parent widgets handle keystrokes that a
	  child widget rejects, so more than one class may need to be
	  aware of the flag.

	- For a list, it would be stupid to keep track of changes in every
	  single edit widget.  You don't need that, you just need changes for
	  the list as a whole.  It would be too time-consuming to set and
	  reset the flags in every line in the list.  And fib.py has an
	  _unbounded_ number of lines, which I don't even want to think
	  about handling.

	  So for something like edit.py (which Eric was probably not asking
	  about) my best plan is to modify the list-to-edit-widget interface,
	  so that keypres() returns a tuple instead of a single element.
	  The first element of the tuple is the key or None, as before.
	  The second element is a Boolean indicating whether the text has
	  changed from this keypress.  The first change to any line sets a
	  global flag for the set of lines.

	  Normally the user can reset the flag with a key that runs a
	  command.  Some commands may need to set the flag, i.e., the list
	  widget may need public make_clean and make_dirty methods.;

	  I'm still writing code so details above may be wrong.

-- Derek




More information about the Urwid mailing list