def test_textwidth(self): ''' test textwidth() ''' # without tab self.assertEqual(textwidth('A\u00c0\u3042'), 1 + 1 + 2) self.assertEqual(textwidth('A\u00c0\u3042'.encode(get_encoding())), 1 + 1 + 2) # with tab self.assertEqual(textwidth('\tA\u00c0\u3042'), 4 + 1 + 1 + 2) self.assertEqual(textwidth('\tA\u00c0\u3042'.encode(get_encoding())), 4 + 1 + 1 + 2)
def test_charwidth(self): ''' test charwidth() ''' # example of 1-column character (ASCII) self.assertEqual(charwidth('A'), 1) self.assertEqual(charwidth('A'.encode(get_encoding())), 1) # example of 1-column character (non-ASCII) self.assertEqual(charwidth('\u00c0'), 1) self.assertEqual(charwidth('\u00c0'.encode(get_encoding())), 1) # example of 2-column character self.assertEqual(charwidth('\u3042'), 2) self.assertEqual(charwidth('\u3042'.encode(get_encoding())), 2)
def show_actions(self): '''Read through the actions dictionary, displaying all the actions descriptive text along the footer (along with a prefix linked to its associated keystroke) ''' self.footer.window.clear() if InnerWindow.USE_ESC: prefix = " Esc-" else: prefix = " F" strings = [] length = 0 action_format = "%s%i_%s" for key in sorted(self.actions.keys()): key_num = key - curses.KEY_F0 action_text = self.actions[key].text action_str = action_format % (prefix, key_num, action_text) strings.append(action_str) display_str = "".join(strings) max_len = self.footer.window.getmaxyx()[1] length = textwidth(display_str) if not InnerWindow.USE_ESC: length += (len(" Esc-") - len(" F")) * len(self.actions) if length > max_len: raise ValueError("Can't display footer actions - string too long") self.footer.window.addstr(display_str.encode(get_encoding())) self.footer.window.noutrefresh()
def exit_text_installer(logname=None, errcode=0): '''Close out the logger and exit with errcode''' logging.info("**** END ****") logging.shutdown() if logname is not None: print _("Exiting Text Installer. Log is available at:\n%s") % logname if isinstance(errcode, unicode): errcode = errcode.encode(get_encoding()) sys.exit(errcode)
def add_text(self, text, start_y=0, start_x=0, max_chars=None, centered=False): '''Add a single line of text to the window 'text' must fit within the specified space, or it will be truncated ''' win_y, win_x = self.window.getmaxyx() logging.log( LOG_LEVEL_INPUT, "start_y=%d, start_x=%d, max_chars=%s, " "centered=%s, win_max_x=%s, win_max_y=%s", start_y, start_x, max_chars, centered, win_x, win_y) max_x = self.window.getmaxyx()[1] - self.border_size[1] start_x += self.border_size[1] abs_max_chars = max_x - start_x if max_chars is None: max_chars = abs_max_chars else: max_chars = min(max_chars, abs_max_chars) text = fit_text_truncate(text, max_chars) if centered: start_x = (max_x - textwidth(text)) / 2 + start_x text = unicode(text) text = text.encode(get_encoding()) logging.log( LOG_LEVEL_INPUT, "calling addstr with params start_y=%s," "start_x=%s, text=%s", start_y, start_x, text) self.window.addstr(start_y, start_x, text) self.no_ut_refresh()