Anonymous avatar Anonymous committed 066346b

Far cleaner implementation of diag thanks to AKFoerster.

Comments (0)

Files changed (1)

rpg_lib/anyrpg.py

 
 __version__   = '0.1' 
 
+import sys
 from textrpg_1d6 import Char as ews_char
 from time import sleep # for autoscrolling
 
 	# Limitation: This looks really dumb, if the text is longer than one line. 
         else: 
 	    if text: 
-		print '\033[' + str(position_in_line) + 'G' + text[0]
+		sys.stdout.write('\033[' + str(position_in_line) + 'G')
+                sys.stdout.flush()
 	    else: 
 		self.diag("...press enter to read on...") # press enter to read on at each empty line, so readers can rest. 
                 # clear the line after the user pressed enter.
-		print "\033[1A", 
+                sys.stdout.write('\033[2A\033[' + str(position_in_line) + 'G')
 		for i in range(len(_("...press enter to read on..."))): 
-		    print " ",
-		print text
+		    sys.stdout.write(" ")
+                sys.stdout.flush()
 		sleep(line_wait)
             # First sleep 1/10th second. This ensures that the text gets shown before sleeping.
             sleep(char_wait)
 	    # If we have text, wait one 10th of a second per char. 
-	    for i in range(len(text)): 
+	    for i in text: 
 		# load the current cursor position
-		print '\033[1A\033[' + str(position_in_line) + 'G' + text[:i+1] # !!! this depends on the shell scape sequences to work !!!
+		sys.stdout.write(i)
+                sys.stdout.flush()
 		sleep(char_wait)
 	
-	# If we don't want a linebreak at the end, jump back up to the last line. 
-	if not end_with_linebreak: 
+	# If we want a linebreak at the end, add it. 
+	if end_with_linebreak: 
 	    # Move back after the last char on the last line. 
-	    print '\033[1A\033[' + str(len(text)) + 'G',
+	    sys.stdout.write("\n")
+            sys.stdout.flush()
 
 class Dialogs(object): 
     """Basic dialogs. 
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.