From c392f8c2368f8fee01f98cad88193e662e8baa1c Mon Sep 17 00:00:00 2001 From: Maruno17 Date: Tue, 2 Nov 2021 23:27:42 +0000 Subject: [PATCH] Added Luka's console message colouring code --- .../001_Debugging/002_DebugConsole.rb | 169 +++++++++++++++--- .../001_Debugging/005_Deprecation.rb | 9 +- .../001_Technical/002_RubyUtilities.rb | 30 ++++ .../001_Technical/005_PluginManager.rb | 19 +- .../002_Save data/003_SaveData_Conversion.rb | 10 +- .../003_Debug menus/002_Debug_MenuCommands.rb | 2 +- .../003_Debug_MenuExtraCode.rb | 11 +- Data/Scripts/021_Compiler/001_Compiler.rb | 24 ++- .../021_Compiler/002_Compiler_CompilePBS.rb | 2 +- .../021_Compiler/003_Compiler_WritePBS.rb | 7 +- .../004_Compiler_MapsAndEvents.rb | 14 +- 11 files changed, 223 insertions(+), 74 deletions(-) diff --git a/Data/Scripts/001_Technical/001_Debugging/002_DebugConsole.rb b/Data/Scripts/001_Technical/001_Debugging/002_DebugConsole.rb index 7eb4bd803..0f4b4d588 100644 --- a/Data/Scripts/001_Technical/001_Debugging/002_DebugConsole.rb +++ b/Data/Scripts/001_Technical/001_Debugging/002_DebugConsole.rb @@ -44,27 +44,154 @@ module Kernel echo string echo "\r\n" end - - def echoln_good(string) - echo "\e[32m" # Green text - echo string - echo "\e[0m" # Back to default text color - echo "\r\n" - end - - def echoln_bad(string) - echo "\e[31m" # Red text - echo string - echo "\e[0m" # Back to default text color - echo "\r\n" - end - - def echoln_warn(string) - echo "\e[33m" # Brown/yellow text - echo string - echo "\e[0m" # Back to default text color - echo "\r\n" - end end Console.setup_console + +#=============================================================================== +# Console message formatting +#=============================================================================== +module ConsoleRGB + # string colors + CMD_COLORS = { + default: '38', black: '30', red: '31', green: '32', brown: '33', blue: '34', + purple: '35', cyan: '36', gray: '37', + dark_gray: '1;30', light_red: '1;31', light_green: '1;32', yellow: '1;33', + light_blue: '1;34', light_purple: '1;35', light_cyan: '1;36', white: '1;37' + } + # background colors + CMD_BG_COLORS = { + default: '0', black: '40', red: '41', green: '42', brown: '43', blue: '44', + purple: '45', cyan: '46', gray: '47', + dark_gray: '100', light_red: '101', light_green: '102', yellow: '103', + light_blue: '104', light_purple: '105', light_cyan: '106', white: '107' + } + # font options + CMD_FONT_OPTIONS = { + bold: '1', dim: '2', italic: '3', underline: '4', reverse: '7', hidden: '8' + } + # syntax highlighting based on markup + CMD_SYNTAX_COLOR = { + '`' => :cyan, '"' => :purple, "'" => :purple, '$' => :green, '~' => :red + } + # syntax options based on markup + CMD_SYNTAX_OPTIONS = { + '*' => :bold, '|' => :italic, '__' => :underline + } + + #----------------------------------------------------------------------------- + # apply console coloring + #----------------------------------------------------------------------------- + def self.colors(string, text: :default, bg: :default, **options) + # get colors + code_text = CMD_COLORS[text] + code_bg = CMD_BG_COLORS[bg] + # get options + option_pool = options.select { |key, val| CMD_FONT_OPTIONS.key?(key) && val } + font_options = option_pool.keys.map { |opt| CMD_FONT_OPTIONS[opt] }.join(';').squeeze + # return formatted string + return "\e[#{code_bg};#{font_options};#{code_text}m#{string}\e[0m".squeeze(';') + end + + #----------------------------------------------------------------------------- + # character markup to color mapping for console + #----------------------------------------------------------------------------- + # component level + def self.markup_component(string, options = {}) + # syntax markup format options + [CMD_SYNTAX_COLOR, CMD_SYNTAX_OPTIONS].each_with_index do |hash, i| + hash.each do |key, value| + l = key.length + # ensure escape + key = key.chars.map { |c| "\\#{c}" }.join + # define regex + regex = "#{key}.*?#{key}" + # match markup + string.scan(/#{regex}/).each do |cpt| + options[i == 0 ? :text : value] = i == 0 ? value : true + options, string = self.markup_component(cpt[l...-l], options) + end + end + end + return options, string + end + + # full string + def self.markup(string) + final_options = {} + (CMD_SYNTAX_COLOR.merge(CMD_SYNTAX_OPTIONS)).each_key do |key| + # ensure escape + key = key.chars.map { |c| "\\#{c}" }.join + # define regex + regex = "#{key}.*?#{key}" + string.scan(/#{regex}/).each do |cpt| + options, clean = self.markup_component(cpt) + if final_options[clean] + final_options[clean].deep_merge!(options) + else + final_options[clean] = options.clone + end + string.gsub!(cpt, clean) + end + end + # iterate through final options and apply them + final_options.each do |key, opt| + string.gsub!(key, self.colors(key, **opt)) + end + return string + end + + #----------------------------------------------------------------------------- + # echo string into console (example short hand for common options) + #----------------------------------------------------------------------------- + # heading 1 + def self.echo_h1(msg) + echoln ConsoleRGB.colors("*** #{msg} ***\r\n", text: :brown) + end + + # heading 2 + def self.echo_h2(msg, **options) + echoln ConsoleRGB.colors("#{msg}\r\n", **options) + end + + # heading 3 + def self.echo_h3(msg) + echoln ConsoleRGB.markup("#{msg}\r\n") + end + + # list item + def self.echo_li(msg) + echo ConsoleRGB.colors(" -> ", text: :brown) + echo ConsoleRGB.markup(msg) + end + + # list item (ends the line) + def self.echoln_li(msg) + self.echo_li(msg + "\r\n") + end + + # paragraph with markup + def self.echo_p(msg) + echoln ConsoleRGB.markup(msg) + end + + # warning message + def self.echo_warn(msg) + echoln ConsoleRGB.colors("WARNING: " + msg, text: :yellow) + end + + # error message + def self.echo_error(msg) + echoln ConsoleRGB.colors("ERROR: " + msg, text: :light_red) + end + + # status output + def self.echo_status(status) + echoln status ? ConsoleRGB.colors('OK', text: :green) : ConsoleRGB.colors('FAIL', text: :red) + end + + # completion output + def self.echo_complete(status) + echoln status ? ConsoleRGB.colors('done', text: :green) : ConsoleRGB.colors('error', text: :red) + end +end diff --git a/Data/Scripts/001_Technical/001_Debugging/005_Deprecation.rb b/Data/Scripts/001_Technical/001_Debugging/005_Deprecation.rb index 64660171a..46331a5f5 100644 --- a/Data/Scripts/001_Technical/001_Debugging/005_Deprecation.rb +++ b/Data/Scripts/001_Technical/001_Debugging/005_Deprecation.rb @@ -8,15 +8,14 @@ module Deprecation # @param removal_version [String] version the method is removed in # @param alternative [String] preferred alternative method def warn_method(method_name, removal_version = nil, alternative = nil) - text = _INTL('WARN: usage of deprecated method "{1}" or its alias.', method_name) + text = _INTL('Usage of deprecated method "{1}" or its alias.', method_name) unless removal_version.nil? - text += _INTL("\nThe method is slated to be"\ - " removed in Essentials {1}.", removal_version) + text += "\r\n" + _INTL("The method is slated to be removed in Essentials {1}.", removal_version) end unless alternative.nil? - text += _INTL("\nUse \"{1}\" instead.", alternative) + text += "\r\n" + _INTL("Use \"{1}\" instead.", alternative) end - echoln text + ConsoleRGB.echo_warn text end end diff --git a/Data/Scripts/001_Technical/002_RubyUtilities.rb b/Data/Scripts/001_Technical/002_RubyUtilities.rb index 77519b63b..c61f97377 100644 --- a/Data/Scripts/001_Technical/002_RubyUtilities.rb +++ b/Data/Scripts/001_Technical/002_RubyUtilities.rb @@ -100,6 +100,36 @@ class Array end end +#=============================================================================== +# class Hash +#=============================================================================== +class Hash + def deep_merge(hash) + h = self.clone + # failsafe + return h if !hash.is_a?(Hash) + for key in hash.keys + if self[key].is_a?(Hash) + h.deep_merge!(hash[key]) + else + h = hash[key] + end + end + return h + end + + def deep_merge!(hash) + return if !hash.is_a?(Hash) + for key in hash.keys + if self[key].is_a?(Hash) + self[key].deep_merge!(hash[key]) + else + self[key] = hash[key] + end + end + end +end + #=============================================================================== # module Enumerable #=============================================================================== diff --git a/Data/Scripts/001_Technical/005_PluginManager.rb b/Data/Scripts/001_Technical/005_PluginManager.rb index 492132bf7..48b4d3d8b 100644 --- a/Data/Scripts/001_Technical/005_PluginManager.rb +++ b/Data/Scripts/001_Technical/005_PluginManager.rb @@ -352,7 +352,7 @@ module PluginManager def self.error(msg) Graphics.update t = Thread.new do - echoln "Plugin Error:\r\n#{msg}" + ConsoleRGB.echo_error "Plugin Error:\r\n#{msg}" p "Plugin Error: #{msg}" Thread.exit end @@ -659,7 +659,7 @@ module PluginManager # Check if plugins need compiling #----------------------------------------------------------------------------- def self.compilePlugins(order, plugins) - echo " -> Compiling plugin scripts..." + ConsoleRGB.echo_li "Compiling plugin scripts..." scripts = [] # go through the entire order one by one for o in order @@ -681,14 +681,13 @@ module PluginManager File.open("Data/PluginScripts.rxdata", 'wb') { |f| Marshal.dump(scripts, f) } # collect garbage GC.start - echoln_good "done" + ConsoleRGB.echo_complete(true) end #----------------------------------------------------------------------------- # Check if plugins need compiling #----------------------------------------------------------------------------- def self.runPlugins - echoln_warn "*** Checking plugins ***" - echoln "" + ConsoleRGB.echo_h1 "Checking plugins" # get the order of plugins to interpret order, plugins = self.getPluginOrder # compile if necessary @@ -700,7 +699,7 @@ module PluginManager # get the required data name, meta, script = plugin if !meta[:essentials] || !meta[:essentials].include?(Essentials::VERSION) - echoln_warn "WARNING: Plugin '#{name}' may not be compatible with Essentials v#{Essentials::VERSION}. Trying to load anyway." + ConsoleRGB.echo_warn "Plugin '#{name}' may not be compatible with Essentials v#{Essentials::VERSION}. Trying to load anyway." end # register plugin self.register(meta) @@ -716,7 +715,7 @@ module PluginManager # try to run the code begin eval(code, TOPLEVEL_BINDING, fname) - echoln " -> Loaded plugin: \e[35m#{name}\e[0m" if !echoed_plugins.include?(name) + ConsoleRGB.echoln_li "Loaded plugin: '#{name}'" if !echoed_plugins.include?(name) echoed_plugins.push(name) rescue Exception # format error message to display self.pluginErrorMsg(name, sname) @@ -726,11 +725,9 @@ module PluginManager end if scripts.length > 0 echoln "" - echoln_good "Successfully loaded #{scripts.length} plugin(s)" - echoln "" + ConsoleRGB.echo_h2("Successfully loaded #{scripts.length} plugin(s)", text: :green) else - echoln_good "No plugins found" - echoln "" + ConsoleRGB.echo_h2("No plugins found", text: :green) end end #----------------------------------------------------------------------------- diff --git a/Data/Scripts/002_Save data/003_SaveData_Conversion.rb b/Data/Scripts/002_Save data/003_SaveData_Conversion.rb index 04dc82b2c..77af3a135 100644 --- a/Data/Scripts/002_Save data/003_SaveData_Conversion.rb +++ b/Data/Scripts/002_Save data/003_SaveData_Conversion.rb @@ -195,16 +195,14 @@ module SaveData conversions_to_run = self.get_conversions(save_data) return false if conversions_to_run.none? File.open(SaveData::FILE_PATH + '.bak', 'wb') { |f| Marshal.dump(save_data, f) } - echoln_warn "*** Running #{conversions_to_run.length} save file conversions ***" - echoln "" + ConsoleRGB.echo_h1 "Running #{conversions_to_run.length} save file conversions" conversions_to_run.each do |conversion| - echo " -> #{conversion.title}..." + ConsoleRGB.echo_li "#{conversion.title}..." conversion.run(save_data) - echoln_good "done" + ConsoleRGB.echo_complete(true) end echoln "" if conversions_to_run.length > 0 - echoln_good "All save file conversions applied successfully" - echoln "" + ConsoleRGB.echo_h2("All save file conversions applied successfully", text: :green) save_data[:essentials_version] = Essentials::VERSION save_data[:game_version] = Settings::GAME_VERSION return true diff --git a/Data/Scripts/020_Debug/003_Debug menus/002_Debug_MenuCommands.rb b/Data/Scripts/020_Debug/003_Debug menus/002_Debug_MenuCommands.rb index 02cebff63..7ebba86cc 100644 --- a/Data/Scripts/020_Debug/003_Debug menus/002_Debug_MenuCommands.rb +++ b/Data/Scripts/020_Debug/003_Debug menus/002_Debug_MenuCommands.rb @@ -1110,7 +1110,7 @@ DebugMenuCommands.register("compiledata", { "always_show" => true, "effect" => proc { msgwindow = pbCreateMessageWindow - Compiler.compile_all(true) { |msg| echoln msg } + Compiler.compile_all(true) pbMessageDisplay(msgwindow, _INTL("All game data was compiled.")) pbDisposeMessageWindow(msgwindow) } diff --git a/Data/Scripts/020_Debug/003_Debug menus/003_Debug_MenuExtraCode.rb b/Data/Scripts/020_Debug/003_Debug menus/003_Debug_MenuExtraCode.rb index 7522f73fb..c6eb51a9d 100644 --- a/Data/Scripts/020_Debug/003_Debug menus/003_Debug_MenuExtraCode.rb +++ b/Data/Scripts/020_Debug/003_Debug menus/003_Debug_MenuExtraCode.rb @@ -677,7 +677,7 @@ def pbDebugFixInvalidTiles t = Time.now.to_i Graphics.update total_maps = mapData.mapinfos.keys.length - echoln _INTL("Checking {1} maps for invalid tiles...", total_maps) + ConsoleRGB.echo_h1 _INTL("Checking {1} maps for invalid tiles", total_maps) for id in mapData.mapinfos.keys.sort if Time.now.to_i - t >= 5 Graphics.update @@ -710,16 +710,19 @@ def pbDebugFixInvalidTiles end next if map_errors == 0 # Map was changed; save it - echoln _INTL("{1} error tile(s) found on map {2}: {3}.", map_errors, id, mapData.mapinfos[id].name) + ConsoleRGB.echoln_li _INTL("{1} error tile(s) found on map {2}: {3}.", map_errors, id, mapData.mapinfos[id].name) total_errors += map_errors num_error_maps += 1 mapData.saveMap(id) end if num_error_maps == 0 - echoln _INTL("Done. No errors found.") + ConsoleRGB.echo_h2(_INTL("Done. No errors found."), text: :green) pbMessage(_INTL("No invalid tiles were found.")) else - echoln _INTL("Done. {1} errors found and fixed. Close RPG Maker XP to ensure fixes are applied.", total_errors) + echoln "" + ConsoleRGB.echo_h2(_INTL("Done. {1} errors found and fixed.", total_errors), text: :green) + ConsoleRGB.echo_warn _INTL("RMXP data was altered. Close RMXP now to ensure changes are applied.") + echoln "" pbMessage(_INTL("{1} error(s) were found across {2} map(s) and fixed.", total_errors, num_error_maps)) pbMessage(_INTL("Close RPG Maker XP to ensure the changes are applied properly.")) end diff --git a/Data/Scripts/021_Compiler/001_Compiler.rb b/Data/Scripts/021_Compiler/001_Compiler.rb index 22fe251a2..b01f7acea 100644 --- a/Data/Scripts/021_Compiler/001_Compiler.rb +++ b/Data/Scripts/021_Compiler/001_Compiler.rb @@ -700,25 +700,24 @@ module Compiler # Compile all data #============================================================================= def compile_pbs_file_message_start(filename) - # Filename is in magenta - echo _INTL(" -> Compiling PBS file {1}\"{2}\"{3}...", "\e[36m", filename.split("/").last, "\e[0m") + # The `` around the file's name turns it cyan + ConsoleRGB.echo_li _INTL("Compiling PBS file `{1}`...", filename.split("/").last) end def write_pbs_file_message_start(filename) - # Filename is in magenta - echo _INTL(" -> Writing PBS file {1}\"{2}\"{3}...", "\e[36m", filename.split("/").last, "\e[0m") + # The `` around the file's name turns it cyan + ConsoleRGB.echo_li _INTL("Writing PBS file `{1}`...", filename.split("/").last) end def process_pbs_file_message_end - echoln_good _INTL("done") + ConsoleRGB.echo_complete(true) Graphics.update end def compile_all(mustCompile) return if !mustCompile FileLineData.clear - echoln_warn _INTL("*** Starting full compile ***") - echoln "" + ConsoleRGB.echo_h1 _INTL("Starting full compile") compile_town_map # No dependencies compile_connections # No dependencies compile_phone # No dependencies @@ -741,17 +740,16 @@ module Compiler compile_map_metadata # No dependencies compile_animations compile_trainer_events(mustCompile) - echo _INTL(" -> Saving messages...") + ConsoleRGB.echo_li _INTL("Saving messages...") pbSetTextMessages MessageTypes.saveMessages MessageTypes.loadMessageFile("Data/messages.dat") if safeExists?("Data/messages.dat") - echoln_good _INTL("done") - echo _INTL(" -> Reloading cache...") + ConsoleRGB.echo_complete(true) + ConsoleRGB.echo_li _INTL("Reloading cache...") System.reload_cache - echoln_good _INTL("done") - echoln "" - echoln_good _INTL("Successfully fully compiled") + ConsoleRGB.echo_complete(true) echoln "" + ConsoleRGB.echo_h2("Successfully fully compiled", text: :green) end def main diff --git a/Data/Scripts/021_Compiler/002_Compiler_CompilePBS.rb b/Data/Scripts/021_Compiler/002_Compiler_CompilePBS.rb index 1f9248370..a56d6242b 100644 --- a/Data/Scripts/021_Compiler/002_Compiler_CompilePBS.rb +++ b/Data/Scripts/021_Compiler/002_Compiler_CompilePBS.rb @@ -1692,7 +1692,7 @@ module Compiler # Compile battle animations #============================================================================= def compile_animations - echo _INTL(" -> Compiling animations...") + ConsoleRGB.echo_li _INTL("Compiling animations...") begin pbanims = load_data("Data/PkmnAnimations.rxdata") rescue diff --git a/Data/Scripts/021_Compiler/003_Compiler_WritePBS.rb b/Data/Scripts/021_Compiler/003_Compiler_WritePBS.rb index 5f939f643..1d77df332 100644 --- a/Data/Scripts/021_Compiler/003_Compiler_WritePBS.rb +++ b/Data/Scripts/021_Compiler/003_Compiler_WritePBS.rb @@ -889,9 +889,7 @@ module Compiler # Save all data to PBS files #============================================================================= def write_all - echoln "" - echoln_warn _INTL("*** Writing all PBS files ***") - echoln "" + ConsoleRGB.echo_h1 _INTL("Writing all PBS files") write_town_map write_connections write_phone @@ -913,7 +911,6 @@ module Compiler write_metadata write_map_metadata echoln "" - echoln_good _INTL("Successfully rewrote all PBS files") - echoln "" + ConsoleRGB.echo_h2("Successfully rewrote all PBS files", text: :green) end end diff --git a/Data/Scripts/021_Compiler/004_Compiler_MapsAndEvents.rb b/Data/Scripts/021_Compiler/004_Compiler_MapsAndEvents.rb index 4c31f121a..112f18718 100644 --- a/Data/Scripts/021_Compiler/004_Compiler_MapsAndEvents.rb +++ b/Data/Scripts/021_Compiler/004_Compiler_MapsAndEvents.rb @@ -1445,7 +1445,7 @@ module Compiler Graphics.update trainerChecker = TrainerChecker.new change_record = [] - echo _INTL(" -> Processing {1} maps...", mapData.mapinfos.keys.length) + ConsoleRGB.echo_li _INTL("Processing {1} maps...", mapData.mapinfos.keys.length) idx = 0 for id in mapData.mapinfos.keys.sort echo "." if idx % 20 == 0 @@ -1485,15 +1485,15 @@ module Compiler if changed mapData.saveMap(id) mapData.saveTilesets - change_record.push(_INTL(" Map {1}: '{2}' was modified and saved.", id, mapData.mapinfos[id].name)) + change_record.push(_INTL("Map {1}: '{2}' was modified and saved.", id, mapData.mapinfos[id].name)) end end - echoln_good "done" - change_record.each { |msg| echoln_warn msg } + ConsoleRGB.echo_complete(true) + change_record.each { |msg| ConsoleRGB.echo_warn msg } changed = false Graphics.update commonEvents = load_data("Data/CommonEvents.rxdata") - echo _INTL(" -> Processing common events...") + ConsoleRGB.echo_li _INTL("Processing common events...") for key in 0...commonEvents.length newevent = fix_event_use(commonEvents[key],0,mapData) if newevent @@ -1502,9 +1502,9 @@ module Compiler end end save_data(commonEvents,"Data/CommonEvents.rxdata") if changed - echoln_good "done" + ConsoleRGB.echo_complete(true) if change_record.length > 0 || changed - echoln_bad _INTL("!!! RMXP data was altered. Close RMXP now to ensure changes are applied. !!!") + ConsoleRGB.echo_warn _INTL("RMXP data was altered. Close RMXP now to ensure changes are applied.") end end end