ped_dialog.replace_dialog module
module to implement the search and replace dialog for ped
# Copyright 2009 James P Goodwin ped tiny python editor """ module to implement the search and replace dialog for ped """ import curses import curses.ascii import sys import os from ped_dialog import dialog from ped_dialog import confirm_dialog class ReplaceDialog(dialog.Dialog): """ Dialog subclass that prompts for pattern, replacement string, sizes itself to the containing window """ def __init__(self,scr): """ takes parent curses window or screen to pop up over """ (max_y,max_x) = scr.getmaxyx() dialog.Dialog.__init__(self, scr, "ReplaceDialog", 5, max_x-4, [ dialog.Frame("Search and Replace"), dialog.Prompt("pattern",1,2,1,"Pattern: ",-1), dialog.Prompt("replace",2,2,2,"Replace: ",-1), dialog.Button("ok",3,2,3,"OK",dialog.Component.CMP_KEY_OK), dialog.Button("cancel",4,29,3,"CANCEL",dialog.Component.CMP_KEY_CANCEL) ]) class ConfirmReplaceDialog(dialog.Dialog): """ Dialog subclass to confirm replacement, prompts for yes, no or all """ def __init__(self,scr): """ takes parent curses window or screen to pop up over """ dialog.Dialog.__init__(self, scr, "ConfirmReplaceDialog", 5, 40, [ dialog.Frame("Do replace?"), dialog.Button("yes",1,2,3,"YES",dialog.Component.CMP_KEY_OK), dialog.Button("no",2,10,3,"NO",dialog.Component.CMP_KEY_OK), dialog.Button("all",3,15,3,"ALL",dialog.Component.CMP_KEY_OK), dialog.Button("cancel",4,21,3,"CANCEL",dialog.Component.CMP_KEY_OK) ]) def replace( scr ): """ wrapper function for replace dialog, launches dialog over the passed curses window and returns a tuple of (pattern, replace) or (None,None) if canceled """ d = ReplaceDialog(scr) value = d.main() if not "pattern" in value: return (None,None) else: return (value["pattern"],value["replace"]) def confirm_replace( scr ): """ wrapper function for the confirm replace dialog, returns 1 == yes, 2 == no, 3 == all, 4 == cancel returns 4 == cancel if canceled """ d = ConfirmReplaceDialog(scr) value = d.main() if "yes" in value: if value["yes"]: return 1 elif value["no"]: return 2 elif value["all"]: return 3 elif value["cancel"]: return 4 else: return 4 def main(stdscr): return prompt(stdscr,"Test Replace","Enter a number: ", 8 ) if __name__ == '__main__': print(int(curses.wrapper(main)))
Functions
def confirm_replace(
scr)
wrapper function for the confirm replace dialog, returns 1 == yes, 2 == no, 3 == all, 4 == cancel returns 4 == cancel if canceled
def confirm_replace( scr ): """ wrapper function for the confirm replace dialog, returns 1 == yes, 2 == no, 3 == all, 4 == cancel returns 4 == cancel if canceled """ d = ConfirmReplaceDialog(scr) value = d.main() if "yes" in value: if value["yes"]: return 1 elif value["no"]: return 2 elif value["all"]: return 3 elif value["cancel"]: return 4 else: return 4
def main(
stdscr)
def main(stdscr): return prompt(stdscr,"Test Replace","Enter a number: ", 8 )
def replace(
scr)
wrapper function for replace dialog, launches dialog over the passed curses window and returns a tuple of (pattern, replace) or (None,None) if canceled
def replace( scr ): """ wrapper function for replace dialog, launches dialog over the passed curses window and returns a tuple of (pattern, replace) or (None,None) if canceled """ d = ReplaceDialog(scr) value = d.main() if not "pattern" in value: return (None,None) else: return (value["pattern"],value["replace"])
Classes
class ConfirmReplaceDialog
Dialog subclass to confirm replacement, prompts for yes, no or all
class ConfirmReplaceDialog(dialog.Dialog): """ Dialog subclass to confirm replacement, prompts for yes, no or all """ def __init__(self,scr): """ takes parent curses window or screen to pop up over """ dialog.Dialog.__init__(self, scr, "ConfirmReplaceDialog", 5, 40, [ dialog.Frame("Do replace?"), dialog.Button("yes",1,2,3,"YES",dialog.Component.CMP_KEY_OK), dialog.Button("no",2,10,3,"NO",dialog.Component.CMP_KEY_OK), dialog.Button("all",3,15,3,"ALL",dialog.Component.CMP_KEY_OK), dialog.Button("cancel",4,21,3,"CANCEL",dialog.Component.CMP_KEY_OK) ])
Ancestors (in MRO)
- ConfirmReplaceDialog
- ped_dialog.dialog.Dialog
- ped_dialog.dialog.Component
- builtins.object
Class variables
var CMP_KEY_CANCEL
var CMP_KEY_NOP
var CMP_KEY_OK
var history
Static methods
def __init__(
self, scr)
takes parent curses window or screen to pop up over
def __init__(self,scr): """ takes parent curses window or screen to pop up over """ dialog.Dialog.__init__(self, scr, "ConfirmReplaceDialog", 5, 40, [ dialog.Frame("Do replace?"), dialog.Button("yes",1,2,3,"YES",dialog.Component.CMP_KEY_OK), dialog.Button("no",2,10,3,"NO",dialog.Component.CMP_KEY_OK), dialog.Button("all",3,15,3,"ALL",dialog.Component.CMP_KEY_OK), dialog.Button("cancel",4,21,3,"CANCEL",dialog.Component.CMP_KEY_OK) ])
def btab(
self)
def btab(self): if self.focus_list: self.push_history(self.focus_list[self.current][1]) self.current -= 1 if self.current < 0: self.current = len(self.focus_list)-1
def focus(
self)
called when component is the focus
def focus(self): self.focus_list = [] for c in self.children: self.pop_history(c) if c.getorder() > 0: self.focus_list.append((c.getorder(),c)) if not self.focus_list: self.current = 0 return self.focus_list.sort(key=lambda x: x[0]) self.current = 0
def getname(
self)
get the name of this component
def getname(self): """ get the name of this component """ return self.name
def getorder(
self)
get this component's tab order number
def getorder(self): """ get this component's tab order number """ return self.order
def getparent(
self)
get this component's curses target window
def getparent(self): """ get this component's curses target window """ return self.parent
def getvalue(
self)
return the current value of the component
def getvalue(self): value = {} for c in self.children: if c.getname(): value[c.getname()] = c.getvalue() return value
def goto(
self, component)
move focus to this component
def goto(self, component): """ move focus to this component """ for i in range(0,len(self.focus_list)): if self.focus_list[i][1] == component: self.current = i return True return False
def handle(
self, ch)
handle keystrokes for this component
def handle(self, ch): if self.focus_list and ch != keytab.KEYTAB_MOUSE: ch = self.focus_list[self.current][1].handle(ch) cmd_id,ch = keymap.mapseq( keymap.keymap_dialog, ch) if extension_manager.is_extension(cmd_id): if not extension_manager.invoke_extension( cmd_id, self, ch ): return ch if cmd_id == cmd_names.CMD_DLGNEXT: self.tab() elif cmd_id == cmd_names.CMD_DLGPREV: self.btab() elif cmd_id == cmd_names.CMD_DLGUP: if self.focus_list: self.pop_history(self.focus_list[self.current][1]) ch = Component.CMP_KEY_NOP elif cmd_id == cmd_names.CMD_DLGMOUSE: ret = self.handle_mouse() if ret >= 0: ch = ret elif cmd_id == cmd_names.CMD_DLGRESIZE: self.resize() return ch
def handle_mouse(
self)
def handle_mouse(self): if self.focus_list and self.win: try: mid, mx, my, mz, mtype = curses.getmouse() by,bx = self.win.getbegyx() oy = my - by ox = mx - bx for i in range(0,len(self.focus_list)): c = self.focus_list[i][1] ret = c.mouse_event(ox,oy,mtype) if ret >= 0: self.current = i return ret else: return -1 except: return -1
def isempty(
self)
test if the component entry is empty
def isempty(self): """ test if the component entry is empty """ return False
def main(
self, blocking=True, force=False, ch_in=None)
def main(self,blocking = True,force=False,ch_in = None): curses.mousemask( curses.BUTTON1_PRESSED| curses.BUTTON1_RELEASED| curses.BUTTON1_CLICKED) self.win.nodelay(1) self.win.notimeout(0) self.win.timeout(0) old_cursor = curses.curs_set(1) while (1): if (not keymap.keypending(self.win)) or force: self.render() if not ch_in: ch = self.handle(keymap.get_keyseq(self.win,keymap.getch(self.win))) else: ch = self.handle(ch_in) if blocking: if ch == Component.CMP_KEY_CANCEL: curses.curs_set(old_cursor) return {} elif ch == Component.CMP_KEY_OK: curses.curs_set(old_cursor) return self.getvalue() else: if ch == Component.CMP_KEY_CANCEL: curses.curs_set(old_cursor) return (ch, {}) else: curses.curs_set(old_cursor) return (ch, self.getvalue())
def mouse_event(
self, ox, oy, mtype)
handle mouse events return key value or -1 for not handled
def mouse_event(self, ox, oy, mtype): """ handle mouse events return key value or -1 for not handled """ return -1
def pop_history(
self, child)
def pop_history( self, child ): if not self.enable_history: return if not issubclass(child.__class__, Prompt): return if self.getname() and child.getname() and child.getorder() >= 0: key = (self.getname(), child.getname()) if key in Dialog.history: hist = Dialog.history[key] self.hist_idx += 1 if self.hist_idx > len(hist): self.hist_idx = 0 child.setvalue(hist[-self.hist_idx])
def push_history(
self, child)
def push_history( self, child ): if not self.enable_history: return if not issubclass(child.__class__, Prompt): return if self.getname() and child.getname() and child.getorder() >= 0 and not child.isempty(): key = (self.getname(), child.getname()) if key in Dialog.history: if Dialog.history[key][-1] != child.getvalue(): Dialog.history[key].append(child.getvalue()) else: Dialog.history[key] = [child.getvalue()]
def render(
self)
render the component use parent as target
def render(self): self.win.leaveok(1) for c in self.children: if not self.focus_list or self.focus_list[self.current][1] != c: c.render() self.win.leaveok(0) if self.focus_list: self.focus_list[self.current][1].focus() self.focus_list[self.current][1].render() self.win.refresh()
def resize(
self)
def resize(self): pass
def set_history(
self, state)
def set_history( self, state ): self.enable_history = state
def setparent(
self, parent)
set the parent curses target window
def setparent(self,parent): """ set the parent curses target window """ self.parent = parent
def setpos(
self, x, y)
set the position of this component
def setpos(self, x, y ): """ set the position of this component """ self.x = x self.y = y
def setsize(
self, height, width)
set the width of this component
def setsize(self, height, width ): """ set the width of this component """ self.height = height self.width = width
def setvalue(
self, value)
set the components value, may be tuple or other data structure
def setvalue(self, value): for c in self.children: if c.getname() in value: c.setvalue(value[c.getname()]) self.push_history(c)
def tab(
self)
def tab(self): if self.focus_list: self.push_history(self.focus_list[self.current][1]) self.current += 1 if self.current >= len(self.focus_list): self.current = 0
class ReplaceDialog
Dialog subclass that prompts for pattern, replacement string, sizes itself to the containing window
class ReplaceDialog(dialog.Dialog): """ Dialog subclass that prompts for pattern, replacement string, sizes itself to the containing window """ def __init__(self,scr): """ takes parent curses window or screen to pop up over """ (max_y,max_x) = scr.getmaxyx() dialog.Dialog.__init__(self, scr, "ReplaceDialog", 5, max_x-4, [ dialog.Frame("Search and Replace"), dialog.Prompt("pattern",1,2,1,"Pattern: ",-1), dialog.Prompt("replace",2,2,2,"Replace: ",-1), dialog.Button("ok",3,2,3,"OK",dialog.Component.CMP_KEY_OK), dialog.Button("cancel",4,29,3,"CANCEL",dialog.Component.CMP_KEY_CANCEL) ])
Ancestors (in MRO)
- ReplaceDialog
- ped_dialog.dialog.Dialog
- ped_dialog.dialog.Component
- builtins.object
Class variables
var CMP_KEY_CANCEL
var CMP_KEY_NOP
var CMP_KEY_OK
var history
Static methods
def __init__(
self, scr)
takes parent curses window or screen to pop up over
def __init__(self,scr): """ takes parent curses window or screen to pop up over """ (max_y,max_x) = scr.getmaxyx() dialog.Dialog.__init__(self, scr, "ReplaceDialog", 5, max_x-4, [ dialog.Frame("Search and Replace"), dialog.Prompt("pattern",1,2,1,"Pattern: ",-1), dialog.Prompt("replace",2,2,2,"Replace: ",-1), dialog.Button("ok",3,2,3,"OK",dialog.Component.CMP_KEY_OK), dialog.Button("cancel",4,29,3,"CANCEL",dialog.Component.CMP_KEY_CANCEL) ])
def btab(
self)
def btab(self): if self.focus_list: self.push_history(self.focus_list[self.current][1]) self.current -= 1 if self.current < 0: self.current = len(self.focus_list)-1
def focus(
self)
called when component is the focus
def focus(self): self.focus_list = [] for c in self.children: self.pop_history(c) if c.getorder() > 0: self.focus_list.append((c.getorder(),c)) if not self.focus_list: self.current = 0 return self.focus_list.sort(key=lambda x: x[0]) self.current = 0
def getname(
self)
get the name of this component
def getname(self): """ get the name of this component """ return self.name
def getorder(
self)
get this component's tab order number
def getorder(self): """ get this component's tab order number """ return self.order
def getparent(
self)
get this component's curses target window
def getparent(self): """ get this component's curses target window """ return self.parent
def getvalue(
self)
return the current value of the component
def getvalue(self): value = {} for c in self.children: if c.getname(): value[c.getname()] = c.getvalue() return value
def goto(
self, component)
move focus to this component
def goto(self, component): """ move focus to this component """ for i in range(0,len(self.focus_list)): if self.focus_list[i][1] == component: self.current = i return True return False
def handle(
self, ch)
handle keystrokes for this component
def handle(self, ch): if self.focus_list and ch != keytab.KEYTAB_MOUSE: ch = self.focus_list[self.current][1].handle(ch) cmd_id,ch = keymap.mapseq( keymap.keymap_dialog, ch) if extension_manager.is_extension(cmd_id): if not extension_manager.invoke_extension( cmd_id, self, ch ): return ch if cmd_id == cmd_names.CMD_DLGNEXT: self.tab() elif cmd_id == cmd_names.CMD_DLGPREV: self.btab() elif cmd_id == cmd_names.CMD_DLGUP: if self.focus_list: self.pop_history(self.focus_list[self.current][1]) ch = Component.CMP_KEY_NOP elif cmd_id == cmd_names.CMD_DLGMOUSE: ret = self.handle_mouse() if ret >= 0: ch = ret elif cmd_id == cmd_names.CMD_DLGRESIZE: self.resize() return ch
def handle_mouse(
self)
def handle_mouse(self): if self.focus_list and self.win: try: mid, mx, my, mz, mtype = curses.getmouse() by,bx = self.win.getbegyx() oy = my - by ox = mx - bx for i in range(0,len(self.focus_list)): c = self.focus_list[i][1] ret = c.mouse_event(ox,oy,mtype) if ret >= 0: self.current = i return ret else: return -1 except: return -1
def isempty(
self)
test if the component entry is empty
def isempty(self): """ test if the component entry is empty """ return False
def main(
self, blocking=True, force=False, ch_in=None)
def main(self,blocking = True,force=False,ch_in = None): curses.mousemask( curses.BUTTON1_PRESSED| curses.BUTTON1_RELEASED| curses.BUTTON1_CLICKED) self.win.nodelay(1) self.win.notimeout(0) self.win.timeout(0) old_cursor = curses.curs_set(1) while (1): if (not keymap.keypending(self.win)) or force: self.render() if not ch_in: ch = self.handle(keymap.get_keyseq(self.win,keymap.getch(self.win))) else: ch = self.handle(ch_in) if blocking: if ch == Component.CMP_KEY_CANCEL: curses.curs_set(old_cursor) return {} elif ch == Component.CMP_KEY_OK: curses.curs_set(old_cursor) return self.getvalue() else: if ch == Component.CMP_KEY_CANCEL: curses.curs_set(old_cursor) return (ch, {}) else: curses.curs_set(old_cursor) return (ch, self.getvalue())
def mouse_event(
self, ox, oy, mtype)
handle mouse events return key value or -1 for not handled
def mouse_event(self, ox, oy, mtype): """ handle mouse events return key value or -1 for not handled """ return -1
def pop_history(
self, child)
def pop_history( self, child ): if not self.enable_history: return if not issubclass(child.__class__, Prompt): return if self.getname() and child.getname() and child.getorder() >= 0: key = (self.getname(), child.getname()) if key in Dialog.history: hist = Dialog.history[key] self.hist_idx += 1 if self.hist_idx > len(hist): self.hist_idx = 0 child.setvalue(hist[-self.hist_idx])
def push_history(
self, child)
def push_history( self, child ): if not self.enable_history: return if not issubclass(child.__class__, Prompt): return if self.getname() and child.getname() and child.getorder() >= 0 and not child.isempty(): key = (self.getname(), child.getname()) if key in Dialog.history: if Dialog.history[key][-1] != child.getvalue(): Dialog.history[key].append(child.getvalue()) else: Dialog.history[key] = [child.getvalue()]
def render(
self)
render the component use parent as target
def render(self): self.win.leaveok(1) for c in self.children: if not self.focus_list or self.focus_list[self.current][1] != c: c.render() self.win.leaveok(0) if self.focus_list: self.focus_list[self.current][1].focus() self.focus_list[self.current][1].render() self.win.refresh()
def resize(
self)
def resize(self): pass
def set_history(
self, state)
def set_history( self, state ): self.enable_history = state
def setparent(
self, parent)
set the parent curses target window
def setparent(self,parent): """ set the parent curses target window """ self.parent = parent
def setpos(
self, x, y)
set the position of this component
def setpos(self, x, y ): """ set the position of this component """ self.x = x self.y = y
def setsize(
self, height, width)
set the width of this component
def setsize(self, height, width ): """ set the width of this component """ self.height = height self.width = width
def setvalue(
self, value)
set the components value, may be tuple or other data structure
def setvalue(self, value): for c in self.children: if c.getname() in value: c.setvalue(value[c.getname()]) self.push_history(c)
def tab(
self)
def tab(self): if self.focus_list: self.push_history(self.focus_list[self.current][1]) self.current += 1 if self.current >= len(self.focus_list): self.current = 0