[Ace] Avdanced RGSS3 Console

Advanced RGSS3 Console
Version: 0.3
By: Berka

Introduction

I think the addition of the console has been the greatest feature of RMVX Ace: super handy for debugging scripts.
And that's why I could not resist the urge to customize this wonderful tool.

Features

  • Syntax highlighting,
  • Gets method,
  • Line numbers,
  • Simple color changer

Screenshots

Image

Script

script Show
  1. #============================================================
  2. #                     Advanced RGSS3 Console
  3. #   Berka          http://www.rpgmakervx-fr.com
  4. #                   v0.5 VX Ace only. 01-21-12  
  5. #
  6. # Thanks to Azuma-01 for symbols and regexp matching
  7. # Free of use. Please ask me before publishing anywhere.
  8. #
  9. # Add a basic syntax haighlighter to the default RGSS Console
  10. # May cause a slight slowdown in the console display.
  11. #------------------------------------------------------------
  12. # Use Kernel#puts to activate the syntax highlighter.
  13. # Kernel#print stays unchanged.$
  14. # Console#gets extracts the input of the console
  15. #   use: eval(Console.gets) to script while testing.
  16. #------------------------------------------------------------
  17. # Change the color of the console:
  18. #   Console.color(H_RED)       # Set the color of the console
  19. #   print("this text is red")  # Text displayed in red
  20. #   Console.color = nil        # Remove the coloration
  21. #============================================================
  22.  
  23. module Berka
  24.   module Console
  25.     LINE_NUMBERS = true  # Display line numbers ON/OFF
  26.     BACKGROUND   = true  # Display a white background ON/OFF
  27.     ENABLEPROMPT = true  # Enable the gets command ON/OFF
  28.     PARSE_P      = false # Enable parsing for Kernel#p function /!\
  29.   end
  30. end
  31.  
  32. module Win32
  33.   GSH ||= Win32API.new('kernel32','GetStdHandle','l','l')
  34.   SCTA||= Win32API.new('kernel32','SetConsoleTextAttribute','ll','l')
  35. end
  36.  
  37. module Highlighter  
  38.   H_BLACK     = 0x0000 # black
  39.   H_DBLUE     = 0x0001 # dark blue
  40.   H_DGREEN    = 0x0002 # dark green
  41.   H_DCYAN     = 0x0003 # dark cyan
  42.   H_DRED      = 0x0004 # dark red
  43.   H_DPURPLE   = 0x0005 # dark purple
  44.   H_DYELLOW   = 0x0006 # dark yellow
  45.   H_GREY      = 0x0007 # grey
  46.   H_DGREY     = 0x0008 # dark grey
  47.   H_BLUE      = 0x0009 # blue
  48.   H_GREEN     = 0x000a # green
  49.   H_CYAN      = 0x000b # cyan
  50.   H_RED       = 0x000c # red
  51.   H_PURPLE    = 0x000d # purple
  52.   H_YELLOW    = 0x000e # yellow
  53.   H_WHITE     = 0x000f # white
  54.   H_INTENSITY = 0x0080 # background intensity
  55.  
  56.   # Ruby's special words
  57.   H_KWORDS=["alias","begin","BEGIN","break","case","defined","do","else","elsif",
  58.             "end","END","ensure","for","if","in","include","loop","next","raise",
  59.             "redo","rescue","retry","return","super","then","undef","unless",
  60.             "until","when","while","yield","false","nil","self","true","__FILE__",
  61.             "__LINE__","and","not","or","def","class","module","catch","fail",
  62.             "load","throw"]
  63.            
  64.   # Ruby's operators
  65.   H_OPERATORS=["=","+","-","/","*","%",'(',')','[',']','{','}','<','>','&','|',
  66.                ',','!',"?",":",";",'.']
  67.  
  68.   def self.parse(*args)
  69.     args.flatten.each{|l|
  70.       Console.color=nil
  71.       print("#{sprintf("%03d",$consolelines+=1)}:\s")if Berka::Console::LINE_NUMBERS
  72.       l.split(/ /).each{|e|
  73.         case e
  74.         when *H_KWORDS then Console.color=H_BLUE
  75.         when /^\d*[.]?\d+$/ then Console.color=H_DRED
  76.         when /\/.*\/(?:[imx]{,3})/ then Console.color=H_DPURPLE # by Azuma-01
  77.         when /^:.*$/ then Console.color=H_DYELLOW # by Azuma-01
  78.         when /^#.*$/ then Console.color=H_DGREEN # by Azuma-01
  79.         else
  80.           e.split(//).each{|f|
  81.             @s=!@s if r=(f=='"'||f=="'")
  82.             if(@s)||r;Console.color=H_DPURPLE
  83.             elsif f=~/^\d*[.]?\d+$/;Console.color=H_DRED
  84.             else
  85.               Console.color=(H_OPERATORS.include?(f)? H_DCYAN : H_BLACK)
  86.             end
  87.             print(f)
  88.           }
  89.           print(" ")
  90.           next
  91.         end
  92.         print("#{e} ")
  93.       }
  94.     }
  95.     print("\n")
  96.   end
  97. end
  98.  
  99. include Highlighter
  100.  
  101. module Console
  102.   $consolelines=0 # Console lines counter
  103.   def self.init
  104.     @outhwnd=Win32::GSH.call(-11)
  105.   end
  106.   def self.color=(c=H_BLACK)
  107.     Win32::SCTA.call(@outhwnd,(c||=H_BLACK)|(Berka::Console::BACKGROUND ? 0x00f0 : 0))
  108.   end
  109.   def self.gets
  110.     $stdin.gets
  111.   end
  112. end
  113. Console.init
  114.  
  115. # Kernel#puts redefinition
  116. def puts(*a)
  117.   a.each{|n|Highlighter.parse(*n)}
  118. end
  119. #Kernel#p redefinition
  120. def p(*a)
  121.   Berka::Console::PARSE_P ? (a.each{|b|puts b.inspect}) : Kernel.p(*a)
  122. end
  123. # Kernel#gets
  124. def gets
  125.   Console.gets
  126. end
Expand to see the code.


Instructions

Paste this script above Vocab. Only the method Kernel#puts is modified. It enables the syntax highlighter:
  1. puts("a = ['this is a test', 1.0]")
Expand to see the code.

Kernel#print is not modified and it allows to color each line separately.
  1. Console.color = H_RED
  2. print("Ruby is amazing")
Expand to see the code.

The list of colors is mentionned at the beginning of the script.

Compatibility

No issues known yet.

Credits and Thanks

All thanks go to the Msdn reference.

Terms and Conditions

This script is free to use. Credits are not mandatory, but it would be very nice of you to quote me. Please ASK me before posting this script anywhere. Because of that I can help you to use this script.

Best regards,

Berka
I liked it! Great job! :rock:
New version:
- Regexp coloration
- Symbols coloration
Thanks to Azuma-01 for the regexp.

Regards,

Berka
That is really awesome berka :D
I didn't know Zeriab was still around! YAY!

Original thread: https://www.hbgames.org/forums/viewtopic.php?t=76040