Anonymous avatar Anonymous committed 338f53b

Now Char.say uses split_diag and the name is the first part of any line.

Comments (0)

Files changed (1)

rpg_lib/anyrpg.py

 class UserInteraction(object): 
     """Basic user interaction: Showing and asking things."""
     def __init__(self, *args, **kwds): 
-        super(UserInteraction, self).__init__(*args, **kwds)
+	self.line_length = 80 #: The maximum length of story lines. 
 	self.line_wait = 0.4
 	self.char_wait = 0.05
+        super(UserInteraction, self).__init__(*args, **kwds)
+	
         
     def ask(self, question=None, *args, **kwds): 
         """Ask a question.
         if question is not None: 
 	    sleep(0.01)
             return raw_input(_(question) + " ")
+
+    def split_diag(self, text, line_length = None, localize=True, autoscroll=False, line_beginning = "", *args, **kwds): 
+	"""Print a dialog in single lines split at word borders. 
+	
+	@param text: The text to output. 
+	@param line_length: Maximum length of a line. 
+	
+	"""
+	# Split the text by words to output at most 80 chars per line. 
+	if line_length is None: 
+	    line_length = self.line_length
+	text_to_print = ""
+	# Preserve leading spaces. 
+	if text: 
+	    while text[0] == " ": 
+		text_to_print += " "
+		text = text[1:]
+	# Print as many words as possible without breaking the max line width. 
+	for j in text.split(): 
+	    if len(line_beginning) + len(text_to_print) + len(j) > line_length: 
+		self.diag(line_beginning + text_to_print, localize=localize, autoscroll=autoscroll, *args, **kwds) 
+		# If j > 80 -> split j and use only the remaining rest as default, printing all else directly. 
+		if len(line_beginning) + len(j) > 80: 
+		    while j[80 - len(line_beginning):]: 
+			self.diag(line_beginning + j[:80 - len(line_beginning)], localize=localize, autoscroll=autoscroll, *args, **kwds)
+			j = j[80 - len(line_beginning):]
+		text_to_print = j
+	    else: 
+		# if we already have text_to_print, add j with a space. 
+		if text_to_print: 
+		    text_to_print += " " + j
+		else: 
+		    text_to_print = j
+	# Print the last line of this dialog. 
+	self.diag(line_beginning + text_to_print, localize=localize, autoscroll=autoscroll, *args, **kwds)
     
     def diag(self, data, localize=True, autoscroll=False, line_wait = None, char_wait = None, relative_speed = 1, end_with_linebreak = True, position_in_line = 0, *args, **kwds):
 	""" Print a dialog line. 
         super(Char, self).__init__(template=template, *args, **kwds)
         self.battle_diff_treshold = 4 #: The treshold of the char below which no hit is scored. 
     
-    def say(self, data): 
+    def say(self, data, *args, **kwds): 
         """Say something -> Show that the char says it."""
         data = _(data)
         for i in data.split("\n"): 
-            self.diag(self.name + ': ' + i, localize=False)
+            self.split_diag(i, localize=False, line_beginning = self.name + ': ', *args, **kwds)
     
     def ask(self, data, localize=False, *args, **kwds): 
         """Say something -> Show that the char says it."""
             self.diag(self.name + ': ' + i, localize=localize, *args, **kwds)
         return super(Char, self).ask(self.name + ': ' + data.split("\n")[-1], *args, **kwds)
     
-    def act(self, data): 
+    def act(self, data, *args, **kwds): 
         """Do something -> Show that the char does it in the style "<name> walks away.".
         
         Usage: 
         """
         data = _(data)
         for i in data.split("\n"): 
-            self.diag(self.name + ' ' + i, localize=False)
+            self.diag(self.name + ' ' + i, localize=False, *args, **kwds)
         
     def compete_skill(self, other, skill_name, self_mods=[], other_mods=[]): 
         """Compete with the other char in the specified skill. 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.