use new wrap_string function in MessagePopup

This commit is contained in:
Nick Lanham 2011-03-01 19:12:05 +01:00
commit 8d541ad419

View file

@ -40,11 +40,12 @@ try:
except ImportError: except ImportError:
pass pass
import format_utils
import logging import logging
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
class Popup: class Popup:
def __init__(self,parent_mode,title,width_req=-1,height_req=-1,close_cb=None): def __init__(self,parent_mode,title,width_req=-1,height_req=-1,close_cb=None,init_lines=None):
""" """
Init a new popup. The default constructor will handle sizing and borders and the like. Init a new popup. The default constructor will handle sizing and borders and the like.
@ -81,6 +82,9 @@ class Popup:
self.height,self.width = self.screen.getmaxyx() self.height,self.width = self.screen.getmaxyx()
self._divider = None self._divider = None
self._lineoff = 0 self._lineoff = 0
if init_lines:
self._lines = init_lines
else:
self._lines = [] self._lines = []
def _refresh_lines(self): def _refresh_lines(self):
@ -252,42 +256,14 @@ class MessagePopup(Popup):
""" """
Popup that just displays a message Popup that just displays a message
""" """
import re
_strip_re = re.compile("\{!.*?!\}")
_min_height = 3
def __init__(self, parent_mode, title, message): def __init__(self, parent_mode, title, message):
self.message = message self.message = message
self.width= int(parent_mode.cols/2) self.width= int(parent_mode.cols/2)
lns = self._split_message() lns = format_utils.wrap_string(self.message,self.width-2,3,True)
Popup.__init__(self,parent_mode,title,height_req=(len(lns)+2)) hr = min(len(lns)+2,int(parent_mode.rows/2))
Popup.__init__(self,parent_mode,title,height_req=hr)
self._lines = lns self._lines = lns
def _split_message(self):
ret = []
wl = (self.width-2)
s1 = self.message.split("\n")
for s in s1:
while len(self._strip_re.sub('',s)) > wl:
sidx = s.rfind(" ",0,wl-1)
sidx += 1
if sidx > 0:
ret.append(s[0:sidx])
s = s[sidx:]
else:
# can't find a reasonable split, just split at width
ret.append(s[0:wl])
s = s[wl:]
if s:
ret.append(s)
for i in range(len(ret),self._min_height):
ret.append(" ")
return ret
def handle_resize(self): def handle_resize(self):
Popup.handle_resize(self) Popup.handle_resize(self)
self.clear() self.clear()