Back to index

python3.2  3.2.2
Public Member Functions | Public Attributes | Private Member Functions
curses.textpad.Textbox Class Reference

List of all members.

Public Member Functions

def __init__
def do_command
def gather
def edit

Public Attributes

 win
 insert_mode
 maxy
 maxx
 stripspaces
 lastcmd

Private Member Functions

def _end_of_line
def _insert_printable_char

Detailed Description

Editing widget using the interior of a window object.
 Supports the following Emacs-like key bindings:

Ctrl-A      Go to left edge of window.
Ctrl-B      Cursor left, wrapping to previous line if appropriate.
Ctrl-D      Delete character under cursor.
Ctrl-E      Go to right edge (stripspaces off) or end of line (stripspaces on).
Ctrl-F      Cursor right, wrapping to next line when appropriate.
Ctrl-G      Terminate, returning the window contents.
Ctrl-H      Delete character backward.
Ctrl-J      Terminate if the window is 1 line, otherwise insert newline.
Ctrl-K      If line is blank, delete it, otherwise clear to end of line.
Ctrl-L      Refresh screen.
Ctrl-N      Cursor down; move down one line.
Ctrl-O      Insert a blank line at cursor location.
Ctrl-P      Cursor up; move up one line.

Move operations do nothing if the cursor is at an edge where the movement
is not possible.  The following synonyms are supported where possible:

KEY_LEFT = Ctrl-B, KEY_RIGHT = Ctrl-F, KEY_UP = Ctrl-P, KEY_DOWN = Ctrl-N
KEY_BACKSPACE = Ctrl-h

Definition at line 19 of file textpad.py.


Constructor & Destructor Documentation

def curses.textpad.Textbox.__init__ (   self,
  win,
  insert_mode = False 
)

Definition at line 43 of file textpad.py.

00043 
00044     def __init__(self, win, insert_mode=False):
00045         self.win = win
00046         self.insert_mode = insert_mode
00047         (self.maxy, self.maxx) = win.getmaxyx()
00048         self.maxy = self.maxy - 1
00049         self.maxx = self.maxx - 1
00050         self.stripspaces = 1
00051         self.lastcmd = None
00052         win.keypad(1)

Here is the caller graph for this function:


Member Function Documentation

def curses.textpad.Textbox._end_of_line (   self,
  y 
) [private]
Go to the location of the first blank on the given line,
returning the index of the last non-blank character.

Definition at line 53 of file textpad.py.

00053 
00054     def _end_of_line(self, y):
00055         """Go to the location of the first blank on the given line,
00056         returning the index of the last non-blank character."""
00057         last = self.maxx
00058         while True:
00059             if curses.ascii.ascii(self.win.inch(y, last)) != curses.ascii.SP:
00060                 last = min(self.maxx, last+1)
00061                 break
00062             elif last == 0:
00063                 break
00064             last = last - 1
00065         return last

Here is the call graph for this function:

Here is the caller graph for this function:

def curses.textpad.Textbox._insert_printable_char (   self,
  ch 
) [private]

Definition at line 66 of file textpad.py.

00066 
00067     def _insert_printable_char(self, ch):
00068         (y, x) = self.win.getyx()
00069         if y < self.maxy or x < self.maxx:
00070             if self.insert_mode:
00071                 oldch = self.win.inch()
00072             # The try-catch ignores the error we trigger from some curses
00073             # versions by trying to write into the lowest-rightmost spot
00074             # in the window.
00075             try:
00076                 self.win.addch(ch)
00077             except curses.error:
00078                 pass
00079             if self.insert_mode:
00080                 (backy, backx) = self.win.getyx()
00081                 if curses.ascii.isprint(oldch):
00082                     self._insert_printable_char(oldch)
00083                     self.win.move(backy, backx)

Here is the call graph for this function:

Here is the caller graph for this function:

def curses.textpad.Textbox.do_command (   self,
  ch 
)

Definition at line 84 of file textpad.py.

00084 
00085     def do_command(self, ch):
00086         "Process a single editing command."
00087         (y, x) = self.win.getyx()
00088         self.lastcmd = ch
00089         if curses.ascii.isprint(ch):
00090             if y < self.maxy or x < self.maxx:
00091                 self._insert_printable_char(ch)
00092         elif ch == curses.ascii.SOH:                           # ^a
00093             self.win.move(y, 0)
00094         elif ch in (curses.ascii.STX,curses.KEY_LEFT, curses.ascii.BS,curses.KEY_BACKSPACE):
00095             if x > 0:
00096                 self.win.move(y, x-1)
00097             elif y == 0:
00098                 pass
00099             elif self.stripspaces:
00100                 self.win.move(y-1, self._end_of_line(y-1))
00101             else:
00102                 self.win.move(y-1, self.maxx)
00103             if ch in (curses.ascii.BS, curses.KEY_BACKSPACE):
00104                 self.win.delch()
00105         elif ch == curses.ascii.EOT:                           # ^d
00106             self.win.delch()
00107         elif ch == curses.ascii.ENQ:                           # ^e
00108             if self.stripspaces:
00109                 self.win.move(y, self._end_of_line(y))
00110             else:
00111                 self.win.move(y, self.maxx)
00112         elif ch in (curses.ascii.ACK, curses.KEY_RIGHT):       # ^f
00113             if x < self.maxx:
00114                 self.win.move(y, x+1)
00115             elif y == self.maxy:
00116                 pass
00117             else:
00118                 self.win.move(y+1, 0)
00119         elif ch == curses.ascii.BEL:                           # ^g
00120             return 0
00121         elif ch == curses.ascii.NL:                            # ^j
00122             if self.maxy == 0:
00123                 return 0
00124             elif y < self.maxy:
00125                 self.win.move(y+1, 0)
00126         elif ch == curses.ascii.VT:                            # ^k
00127             if x == 0 and self._end_of_line(y) == 0:
00128                 self.win.deleteln()
00129             else:
00130                 # first undo the effect of self._end_of_line
00131                 self.win.move(y, x)
00132                 self.win.clrtoeol()
00133         elif ch == curses.ascii.FF:                            # ^l
00134             self.win.refresh()
00135         elif ch in (curses.ascii.SO, curses.KEY_DOWN):         # ^n
00136             if y < self.maxy:
00137                 self.win.move(y+1, x)
00138                 if x > self._end_of_line(y+1):
00139                     self.win.move(y+1, self._end_of_line(y+1))
00140         elif ch == curses.ascii.SI:                            # ^o
00141             self.win.insertln()
00142         elif ch in (curses.ascii.DLE, curses.KEY_UP):          # ^p
00143             if y > 0:
00144                 self.win.move(y-1, x)
00145                 if x > self._end_of_line(y-1):
00146                     self.win.move(y-1, self._end_of_line(y-1))
00147         return 1

Here is the call graph for this function:

Here is the caller graph for this function:

def curses.textpad.Textbox.edit (   self,
  validate = None 
)

Definition at line 164 of file textpad.py.

00164 
00165     def edit(self, validate=None):
00166         "Edit in the widget window and collect the results."
00167         while 1:
00168             ch = self.win.getch()
00169             if validate:
00170                 ch = validate(ch)
00171             if not ch:
00172                 continue
00173             if not self.do_command(ch):
00174                 break
00175             self.win.refresh()
00176         return self.gather()

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 148 of file textpad.py.

00148 
00149     def gather(self):
00150         "Collect and return the contents of the window."
00151         result = ""
00152         for y in range(self.maxy+1):
00153             self.win.move(y, 0)
00154             stop = self._end_of_line(y)
00155             if stop == 0 and self.stripspaces:
00156                 continue
00157             for x in range(self.maxx+1):
00158                 if self.stripspaces and x > stop:
00159                     break
00160                 result = result + chr(curses.ascii.ascii(self.win.inch(y, x)))
00161             if self.maxy > 0:
00162                 result = result + "\n"
00163         return result

Here is the call graph for this function:

Here is the caller graph for this function:


Member Data Documentation

Definition at line 45 of file textpad.py.

Definition at line 50 of file textpad.py.

Definition at line 48 of file textpad.py.

Definition at line 47 of file textpad.py.

Definition at line 49 of file textpad.py.

Definition at line 44 of file textpad.py.


The documentation for this class was generated from the following file: