SemlerPDX icon

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

SemlerPDX | PRO | 05/11/23 05:47:27 PM UTC (Edited) | 0 ⭐ | 1916 👁️ | Never ⏰ | [C#, csharp, VoiceAttack, inline function]
C# |

2.52 KB

|

Software

|

0 👍

/

0 👎

// Write to Event Log (Long)
// for VoiceAttack profile developers
//
// by SemlerPDX Apr2023
// VETERANS-GAMING.COM/AVCS
 
using System;
 
public class VAInline
{
  public void main()
  {
    string message = VA.ParseTokens("{CLIP}"); // example input - copy a paragraph of text to clipboard and call this function
    WriteToLog_Long(VA, message, "grey");
  }
 
  /// <summary>
  /// A method to write very long messages to the VoiceAttack Event Log,
  /// wrapping text which exceeds the minimum width of the window to a new
  /// event log entry on a new line.
  /// </summary>
  /// <param name="VA">The dynamic object which provides VoiceAttack attributes and functionality for this method.</param>
  /// <param name="longString">The text string containing the message to write to the Event Log without truncation.</param>
  /// <param name="colorString">The color of the square to the left of each Event Log entry.</param>
  public static void WriteToLog_Long(dynamic VA, string longString, string colorString)
  {
    try
    {
      int maxWidth = 91;
      string[] lines = longString.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
 
      foreach (string line in lines)
      {
        if (line.Length <= maxWidth)
        {
          VA.WriteToLog(line, colorString);
          continue;
        }
 
        int index = 0;
        while (index < line.Length)
        {
          try
          {
            int length = Math.Min(maxWidth, line.Length - index);
 
            if (length == maxWidth)
            {
              // Check if the line should wrap at a whitespace
              int lastSpaceIndex = line.LastIndexOf(' ', index + length, length);
              if (lastSpaceIndex != -1 && lastSpaceIndex != index + length)
              {
                length = lastSpaceIndex - index;
              }
            }
 
            OpenAIplugin.VA_Proxy.WriteToLog(line.Substring(index, length), colorString);
            index += length;
 
            // Ignore whitespace at the beginning of a new line
            while (index < line.Length && char.IsWhiteSpace(line[index]))
            {
              index++;
            }
          }
          catch (ArgumentOutOfRangeException)
          {
            // Catch ArgumentOutOfRangeException and continue the loop
            continue;
          }
        }
      }
    }
    catch
    {
      VA.WriteToLog("Inline Function Error: Failure ignored at WriteToLog_Long() method", "red");
    }
  }
}

Comments