Skip to content

Commit

Permalink
Initial work on basic word count
Browse files Browse the repository at this point in the history
  • Loading branch information
TheJoeFin committed Nov 8, 2024
1 parent b5146fa commit 024ce3f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
16 changes: 16 additions & 0 deletions Text-Grab/Utilities/StringMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -767,4 +767,20 @@ public static bool EndsWithNewline(this string s)
{
return Regex.IsMatch(s, @"\n$");
}

public static string RemoveNonWordChars(this string strIn)
{
// Replace invalid characters with empty strings.
try
{
return Regex.Replace(strIn, @"[^\w\s]", "",
RegexOptions.None, TimeSpan.FromSeconds(5));
}
// If we timeout when replacing invalid characters,
// we should return Empty.
catch (RegexMatchTimeoutException)
{
return String.Empty;
}
}
}
6 changes: 5 additions & 1 deletion Text-Grab/Views/EditTextWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2046,17 +2046,21 @@ private void UnstackGroupExecuted(object? sender = null, ExecutedRoutedEventArgs

private void UpdateLineAndColumnText()
{
char[] delimiters = [' ', '\r', '\n'];

if (PassedTextControl.SelectionLength < 1)
{
int lineNumber = PassedTextControl.GetLineIndexFromCharacterIndex(PassedTextControl.CaretIndex);
int columnNumber = PassedTextControl.CaretIndex - PassedTextControl.GetCharacterIndexFromLineIndex(lineNumber);
int words = PassedTextControl.Text.RemoveNonWordChars().Split(delimiters, StringSplitOptions.RemoveEmptyEntries).Length;

BottomBarText.Text = $"Ln {lineNumber + 1}, Col {columnNumber}";
BottomBarText.Text = $"Wrds {words}, Ln {lineNumber + 1}, Col {columnNumber}";
}
else
{
int selectionStartIndex = PassedTextControl.SelectionStart;
int selectionStopIndex = PassedTextControl.SelectionStart + PassedTextControl.SelectionLength;
int words = PassedTextControl.Text.Split(delimiters, StringSplitOptions.RemoveEmptyEntries).Length;

int selStartLine = PassedTextControl.GetLineIndexFromCharacterIndex(selectionStartIndex);

Expand Down

0 comments on commit 024ce3f

Please sign in to comment.