Repository: nvaccess/nvda

Issue:
nvda logs a lot of '\r\n\r\n' if UIA support for Windows console is enabled

### Steps to reproduce:
1. In NVDA's Advanced Settings panel, make the following settings:
  - Windows Console support: combo box: UIA when available
2. Open cmd or git bash and press NVDA+Shift+M(Laptop);
### Actual behavior:
There are a lot of '\r\n' in the log.

### Expected behavior:
There won't be so many '\r\n' in the log
### NVDA logs, crash dumps and other attachments:
[log.txt](https://github.com/nvaccess/nvda/files/10866869/log.txt)

### System configuration
#### NVDA installed/portable/running from source:
Installed
#### NVDA version:
2023.1Beta2
#### Windows version:
Windows 10 22H2 (AMD64) build 19045.2604
#### Name and version of other software in use when reproducing the issue:
None
#### Other information about your system:
None
### Other questions
#### Does the issue still occur after restarting your computer?
Yes
#### Have you tried any other versions of NVDA? If so, please report their behaviors.
2022.4 is the same
#### If NVDA add-ons are disabled, is your problem still occurring?
Yes
#### Does the issue still occur after you run the COM Registration Fixing Tool in NVDA's tools menu?
Yes


Discussion:
According to your log, the spoken string is 18095 character long. And there are 17994 characters after the last '>', i.e. 8997 '\r\n'.

@codeofdusk: any idea why so many new lines are captured in the command window when it becomes the foreground?


*
This is a known bug in API level 0 consoles (see microsoft/terminal#6986). In consoles with less than API level 2, no support is provided as indicated in the user guide.

Mitigation strategies:
* Ideally, upgrade to Windows 11 Sun Valley 2.
* If possible, use Windows Terminal (`wt.exe`), which is continuously updated.
* If all else fails, upgrade `conhost.exe` to the version included with Windows Terminal. I've provided guidance in other issues, etc. on how to do that.

@seanbudd I recommend closing as duplicate of microsoft/terminal#6986.

CC @carlos-zamora, @DHowett.
@codeofdusk 
If I can't upgrade to windows11 now, can you provide other solutions?

This problem bothers me because of: https://github.com/ABuffEr/numberProcessing/issues/4

But due to another issue, not forcing UIA support for Windows Console under Chinese operating systems will cause every Chinese character displayed on the screen to be reported twice by NVDA.

If you can't upgrade, either use Windows Terminal (`wt.exe`) or replace your conhost.
Hi,
just for documentation, the freeze seems to born in speechDictHandler.processText function. Adding something like:
```
	try:
		if config.conf["UIA"]["winConsoleImplementation"] == "UIA":
			stripText = text.rstrip()
			if len(text)-len(stripText) > 10000:
				text = stripText
	except KeyError:
		pass
```
before the dictionaries loop, no freeze happens. But I doubt it would be considered an acceptable solution for a PR...
@codeofdusk 
You wrote:
> If all else fails, upgrade conhost.exe to the version included with Windows Terminal. I've provided guidance in other issues, etc. on how to do that.
---
Can you provide a link on how to do this?
I've updated my [console test package](https://n6.io/ConsoleTestPackage_2023-03-02.zip) to the latest released build of OpenConsole. Hopefully this can provide a stopgap solution until you upgrade to SV2 or switch to `wt`.
@ABuffEr a fix similar to what you suggested is appropriate
Ok, I'll provide a PR in next hours.

Resolving patch:
diff --git a/source/speechDictHandler/__init__.py b/source/speechDictHandler/__init__.py
--- a/source/speechDictHandler/__init__.py
+++ b/source/speechDictHandler/__init__.py
@@ -8,8 +8,8 @@
 from logHandler import log
 import os
 import codecs
+import review
 import api
-import config
 from . import dictFormatUpgrade
 from .speechDictVars import speechDictsPath
 
@@ -115,9 +115,26 @@ def sub(self, text):
 				del self[index]
 		return text
 
+
 def processText(text):
 	if not globalVars.speechDictionaryProcessing:
 		return text
+	# #14689: older (IMPROVED and END_INCLUSIVE) UIA consoles have many blank lines,
+	# which slows processing to a halt
+	focus = api.getFocusObject()
+	try:
+		# get TextInfo implementation for object review mode
+		textInfo, obj = review.getObjectPosition(focus)
+	except AttributeError:  # no makeTextInfo
+		textInfo = None
+	# late import to prevent circular dependency
+	# ConsoleUIATextInfo is used by IMPROVED and END_INCLUSIVE consoles
+	from NVDAObjects.UIA.winConsoleUIA import ConsoleUIATextInfo
+	if isinstance(textInfo, ConsoleUIATextInfo):
+		stripText = text.rstrip()
+		IGNORE_TRAILING_WHITESPACE_LENGTH = 100
+		if len(text) - len(stripText) > IGNORE_TRAILING_WHITESPACE_LENGTH:
+			text = stripText
 	for type in dictTypes:
 		text=dictionaries[type].sub(text)
 	return text


Test patch: