Advertisement
SemlerPDX

VoiceAttack C# Inline Function - Write to Event Log (with wrapping for long messages)

Apr 22nd, 2023 (edited)
1,455
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.52 KB | Software | 0 0
  1. // Write to Event Log (Long)
  2. // for VoiceAttack profile developers
  3. //
  4. // by SemlerPDX Apr2023
  5. // VETERANS-GAMING.COM/AVCS
  6.  
  7. using System;
  8.  
  9. public class VAInline
  10. {
  11.   public void main()
  12.   {
  13.     string message = VA.ParseTokens("{CLIP}"); // example input - copy a paragraph of text to clipboard and call this function
  14.     WriteToLog_Long(VA, message, "grey");
  15.   }
  16.  
  17.   /// <summary>
  18.   /// A method to write very long messages to the VoiceAttack Event Log,
  19.   /// wrapping text which exceeds the minimum width of the window to a new
  20.   /// event log entry on a new line.
  21.   /// </summary>
  22.   /// <param name="VA">The dynamic object which provides VoiceAttack attributes and functionality for this method.</param>
  23.   /// <param name="longString">The text string containing the message to write to the Event Log without truncation.</param>
  24.   /// <param name="colorString">The color of the square to the left of each Event Log entry.</param>
  25.   public static void WriteToLog_Long(dynamic VA, string longString, string colorString)
  26.   {
  27.     try
  28.     {
  29.       int maxWidth = 91;
  30.       string[] lines = longString.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
  31.  
  32.       foreach (string line in lines)
  33.       {
  34.         if (line.Length <= maxWidth)
  35.         {
  36.           VA.WriteToLog(line, colorString);
  37.           continue;
  38.         }
  39.  
  40.         int index = 0;
  41.         while (index < line.Length)
  42.         {
  43.           try
  44.           {
  45.             int length = Math.Min(maxWidth, line.Length - index);
  46.  
  47.             if (length == maxWidth)
  48.             {
  49.               // Check if the line should wrap at a whitespace
  50.               int lastSpaceIndex = line.LastIndexOf(' ', index + length, length);
  51.               if (lastSpaceIndex != -1 && lastSpaceIndex != index + length)
  52.               {
  53.                 length = lastSpaceIndex - index;
  54.               }
  55.             }
  56.  
  57.             OpenAIplugin.VA_Proxy.WriteToLog(line.Substring(index, length), colorString);
  58.             index += length;
  59.  
  60.             // Ignore whitespace at the beginning of a new line
  61.             while (index < line.Length && char.IsWhiteSpace(line[index]))
  62.             {
  63.               index++;
  64.             }
  65.           }
  66.           catch (ArgumentOutOfRangeException)
  67.           {
  68.             // Catch ArgumentOutOfRangeException and continue the loop
  69.             continue;
  70.           }
  71.         }
  72.       }
  73.     }
  74.     catch
  75.     {
  76.       VA.WriteToLog("Inline Function Error: Failure ignored at WriteToLog_Long() method", "red");
  77.     }
  78.   }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement