2424import app .text_buffer
2525import app .vi_editor
2626import bisect
27+ import threading
2728import os
2829import sys
2930import curses
@@ -1193,26 +1194,30 @@ def __init__(self, host):
11931194 self .host = host
11941195 self .controller = app .cu_editor .PopupController (self )
11951196 self .setTextBuffer (app .text_buffer .TextBuffer ())
1196- self .longestLineLength = 0
11971197 self .__message = []
11981198 self .showOptions = True
11991199 # This will be displayed and should contain the keys that respond to user
12001200 # input. This should be updated if you change the controller's command set.
1201- self .options = []
1201+ self .__displayOptions = []
1202+ # Prevent sync issues from occuring when setting options.
1203+ self .__optionsLock = threading .Lock ()
12021204
12031205 def render (self ):
12041206 """
12051207 Display a box of text in the center of the window.
12061208 """
12071209 maxRows , maxCols = self .host .rows , self .host .cols
1208- cols = min (self .longestLineLength + 6 , maxCols )
1210+ longestLineLength = 0
1211+ if len (self .__message ):
1212+ longestLineLength = len (max (self .__message , key = len ))
1213+ cols = min (longestLineLength + 6 , maxCols )
12091214 rows = min (len (self .__message ) + 4 , maxRows )
12101215 self .resizeTo (rows , cols )
12111216 self .moveTo (maxRows / 2 - rows / 2 , maxCols / 2 - cols / 2 )
12121217 color = app .color .get ('popup_window' )
12131218 for row in range (rows ):
12141219 if row == rows - 2 and self .showOptions :
1215- message = '/' .join (self .options )
1220+ message = '/' .join (self .__displayOptions )
12161221 elif row == 0 or row >= rows - 3 :
12171222 self .addStr (row , 0 , ' ' * cols , color )
12181223 continue
@@ -1223,27 +1228,73 @@ def render(self):
12231228 spacing2 = cols - lineLength - spacing1
12241229 self .addStr (row , 0 , ' ' * spacing1 + message + ' ' * spacing2 , color )
12251230
1226- def setMessage (self , message ):
1231+ def __setMessage (self , message ):
12271232 """
12281233 Sets the Popup window's message to the given message.
1229- message (str): A string that you want to display.
1234+ This function should only be called by setUpWindow.
1235+
1236+ Args:
1237+ message (str): A string that you want to display.
12301238
12311239 Returns:
12321240 None.
12331241 """
12341242 self .__message = message .split ("\n " )
1235- self .longestLineLength = max ([len (line ) for line in self .__message ])
12361243
1237- def setOptionsToDisplay (self , options ):
1244+ def __setDisplayOptions (self , displayOptions ):
12381245 """
12391246 This function is used to change the options that are displayed in the
12401247 popup window. They will be separated by a '/' character when displayed.
1248+ This function should only be called by setUpWindow.
1249+
1250+ Args:
1251+ displayOptions (list): A list of possible keys which the user can press
1252+ and should be responded to by the controller.
1253+ """
1254+ self .__displayOptions = displayOptions
1255+
1256+ def __setControllerOptions (self , controllerOptions ):
1257+ """
1258+ This function is used to change the options that are displayed in the
1259+ popup window as well as their functions. This function should only be
1260+ called by setUpWindow.
1261+
1262+ Args:
1263+ controllerOptions (dict): A dictionary mapping keys (ints) to its
1264+ corresponding action.
1265+
1266+ Returns;
1267+ None.
1268+ """
1269+ self .controller .commandSet = controllerOptions
1270+
1271+ def setUpWindow (self , message = None , displayOptions = None ,
1272+ controllerOptions = None ):
1273+ """
1274+ Sets up the popup window. You should pass in the following arguments in
1275+ case of any sync issues, even if the values seem to already be set. By
1276+ default, the values will be set to None, meaning that the values will
1277+ not be changed. However, if you wish to set each value to be empty,
1278+ message should be an empty string, displayOptions should be an empty list,
1279+ and controllerOptions should be an empty dictionary.
12411280
12421281 Args:
1243- options (list): A list of possible keys which the user can press and
1244- should be responded to by the controller.
1282+ message (str): The string that you want the popup window to display.
1283+ displayOptions (list): The list of strings representing the options
1284+ that will be displayed on the popup window.
1285+ controllerOptions (dict): The mapping of user keypresses to functions.
1286+
1287+ Returns:
1288+ None.
12451289 """
1246- self .options = options
1290+ self .__optionsLock .acquire ()
1291+ if message is not None :
1292+ self .__setMessage (message )
1293+ if displayOptions is not None :
1294+ self .__setDisplayOptions (displayOptions )
1295+ if controllerOptions is not None :
1296+ self .__setControllerOptions (controllerOptions )
1297+ self .__optionsLock .release ()
12471298
12481299 def setTextBuffer (self , textBuffer ):
12491300 Window .setTextBuffer (self , textBuffer )
0 commit comments