installer/readtext.py
f4d17450
 #
 #
 #    Author: Mahmoud Bassiouny <mbassiouny@vmware.com>
 
 import curses
4d53ba03
 import sys
f4d17450
 from actionresult import ActionResult
 from action import Action
1ec3a643
 from confirmwindow import ConfirmWindow
f4d17450
 
 class ReadText(Action):
c533b308
     def __init__(self, maxy, maxx, textwin, y, install_config, field,
                  confirmation_error_msg, echo_char, accepted_chars, validation_fn,
                  conversion_fn, default_string=None, tab_enabled=True):
f4d17450
         self.textwin = textwin
1ec3a643
         self.maxy = maxy
         self.maxx = maxx
f4d17450
         self.y = y
         self.install_config = install_config
4d53ba03
         self.field = field
3c4cf0da
         self.confirmation_error_msg = confirmation_error_msg
         self.echo_char = echo_char
         self.accepted_chars = accepted_chars
         self.validation_fn = validation_fn
         self.conversion_fn = conversion_fn
4d53ba03
         self.default_string = default_string
61dabd25
         self.textwin_width = self.textwin.getmaxyx()[1] - 1
         self.visible_text_width = self.textwin_width - 1
c533b308
         self.tab_enabled = tab_enabled
61dabd25
 
         self.init_text()
4d53ba03
         self.maxlength = 255
f4d17450
 
72832a72
         if not tab_enabled:
             self.textwin.keypad(1)
 
f4d17450
         #initialize the accepted characters
3c4cf0da
         if accepted_chars:
             self.accepted_chars = accepted_chars
4d53ba03
         else:
5aac5a9d
             self.accepted_chars = range(32, 127)
f4d17450
 
     def hide(self):
         return
c533b308
 
1ec3a643
     def init_text(self):
c533b308
         self.x = 0
1ec3a643
         #initialize the ----
61dabd25
         dashes = '_' * self.textwin_width
1ec3a643
         self.textwin.addstr(self.y, 0, dashes)
         self.str = ''
 
         #remove the error messages
61dabd25
         spaces = ' ' * self.textwin_width
1ec3a643
         self.textwin.addstr(self.y + 2, 0, spaces)
 
72832a72
     def do_action(self, returned=False, go_back=False):
         if returned:
             if len(self.str) > self.visible_text_width:
                 text = self.str[-self.visible_text_width:]
             else:
                 text = self.str
             if self.echo_char:
                 text = self.echo_char * len(text)
             # Add the dashes
             text = text + '_' * (self.visible_text_width - len(self.str))
             self.textwin.addstr(self.y, 0, text)
         if not returned:
             curses.curs_set(1)
             self.init_text()
             if self.default_string != None:
                 self.textwin.addstr(self.y, 0, self.default_string)
                 self.str = self.default_string
4d53ba03
 
f4d17450
         while True:
61dabd25
             if len(self.str) > self.visible_text_width:
                 curs_loc = self.visible_text_width
             else:
                 curs_loc = len(self.str)
             ch = self.textwin.getch(self.y, curs_loc)
f4d17450
 
ac39a0c4
             update_text = False
f4d17450
             if ch in [curses.KEY_ENTER, ord('\n')]:
72832a72
                 curses.curs_set(0)
                 if go_back:
                     return ActionResult(False, {'goBack': True})
3c4cf0da
                 if self.confirmation_error_msg:
                     if self.str != self.install_config[self.field]:
1ec3a643
                         conf_message_height = 8
f093864e
                         conf_message_width = 48
a672bd24
                         conf_message_button_y = (self.maxy - conf_message_height) // 2 + 5
c533b308
                         confrim_window = ConfirmWindow(conf_message_height, conf_message_width,
                                                        self.maxy,
                                                        self.maxx, conf_message_button_y,
                                                        self.confirmation_error_msg, True)
1ec3a643
                         confrim_window.do_action()
                         return ActionResult(False, {'goBack': True})
3c4cf0da
                     self.set_field()
4d53ba03
                 else:
3c4cf0da
                     if not self.validate_input():
                         continue
                     self.set_field()
f4d17450
                 curses.curs_set(0)
                 return ActionResult(True, None)
c533b308
             elif ch == curses.KEY_LEFT and not self.tab_enabled:
72832a72
                 return ActionResult(False, {'direction': -1})
c533b308
             elif ch == curses.KEY_RIGHT and not self.tab_enabled:
72832a72
                 return ActionResult(False, {'direction': 1})
f4d17450
             elif ch in [ord('\t')]:
                 curses.curs_set(0)
                 return ActionResult(False, None)
72832a72
             elif ch == curses.KEY_BACKSPACE: #originally ==127
f4d17450
                 # Handle the backspace case
                 self.str = self.str[:len(self.str) - 1]
61dabd25
 
                 update_text = True
 
             elif len(self.str) < self.maxlength and ch in self.accepted_chars:
f4d17450
                 self.str += chr(ch)
61dabd25
                 update_text = True
 
             if update_text:
                 if len(self.str) > self.visible_text_width:
                     text = self.str[-self.visible_text_width:]
                 else:
                     text = self.str
3c4cf0da
                 if self.echo_char:
                     text = self.echo_char * len(text)
61dabd25
                 # Add the dashes
                 text = text + '_' * (self.visible_text_width - len(self.str))
                 self.textwin.addstr(self.y, 0, text)
f4d17450
 
3c4cf0da
     def set_field(self):
         if self.conversion_fn:
             self.install_config[self.field] = self.conversion_fn(self.str)
         else:
             self.install_config[self.field] = self.str
 
     def validate_input(self):
         if self.validation_fn:
             success, err = self.validation_fn(self.str)
             if not success:
1d241d9e
                 spaces = ' ' * self.textwin_width
                 self.textwin.addstr(self.y + 2, 0, spaces)
3c4cf0da
                 self.textwin.addstr(self.y + 2, 0, err, curses.color_pair(4))
             return success
         else:
             return True