[ros-diffs] [jmorlan] 34847: Improvements to cmd ConPrintfPaging: - Even if there is a console, don't do paging when output is redirected to a file; it would just confuse users with prompts they can't see. - Instead of WriteFile, use WriteConsole (which has a convenient Unicode version). - Rework the paging algorithm: don't treat end of string the same way as end-of-line (was causing nul bytes to be output after strings that didn't end with \n), and output multiple lines at once when possible. - Use the screen buffer width, not the window width, to determine when wrapping will happen. - Take the initial cursor position into account.

jmorlan at svn.reactos.org jmorlan at svn.reactos.org
Sun Jul 27 16:03:44 CEST 2008


Author: jmorlan
Date: Sun Jul 27 09:03:44 2008
New Revision: 34847

URL: http://svn.reactos.org/svn/reactos?rev=34847&view=rev
Log:
Improvements to cmd ConPrintfPaging:
- Even if there is a console, don't do paging when output is redirected to a file; it would just confuse users with prompts they can't see.
- Instead of WriteFile, use WriteConsole (which has a convenient Unicode version).
- Rework the paging algorithm: don't treat end of string the same way as end-of-line (was causing nul bytes to be output after strings that didn't end with \n), and output multiple lines at once when possible.
- Use the screen buffer width, not the window width, to determine when wrapping will happen.
- Take the initial cursor position into account.

Modified:
    trunk/reactos/base/shell/cmd/console.c

Modified: trunk/reactos/base/shell/cmd/console.c
URL: http://svn.reactos.org/svn/reactos/trunk/reactos/base/shell/cmd/console.c?rev=34847&r1=34846&r2=34847&view=diff
==============================================================================
--- trunk/reactos/base/shell/cmd/console.c [iso-8859-1] (original)
+++ trunk/reactos/base/shell/cmd/console.c [iso-8859-1] Sun Jul 27 09:03:44 2008
@@ -236,10 +236,10 @@
 INT ConPrintfPaging(BOOL NewPage, LPTSTR szFormat, va_list arg_ptr, DWORD nStdHandle)
 {
 	INT len;
-	PCHAR pBuf;
 	CONSOLE_SCREEN_BUFFER_INFO csbi;
 	TCHAR szOut[OUTPUT_BUFFER_SIZE];
 	DWORD dwWritten;
+	HANDLE hOutput = GetStdHandle(nStdHandle);
 
 	/* used to count number of lines since last pause */
 	static int LineCount = 0;
@@ -247,13 +247,10 @@
 	/* used to see how big the screen is */
 	int ScreenLines = 0;
 
-	/* the number of chars in a roow */
-	int ScreenCol = 0;
-
 	/* chars since start of line */
-	int CharSL = 0;
-
-	int i = 0;
+	int CharSL;
+
+	int from = 0, i = 0;
 
 	if(NewPage == TRUE)
 		LineCount = 0;
@@ -264,7 +261,7 @@
 
 
 	//get the size of the visual screen that can be printed too
-	if (!GetConsoleScreenBufferInfo(hConsole, &csbi))
+	if (!GetConsoleScreenBufferInfo(hOutput, &csbi))
 	{
 		// we assuming its a file handle
 		ConPrintf(szFormat, arg_ptr, nStdHandle);
@@ -272,7 +269,7 @@
 	}
 	//subtract 2 to account for "press any key..." and for the blank line at the end of PagePrompt()
 	ScreenLines = (csbi.srWindow.Bottom  - csbi.srWindow.Top) - 4;
-	ScreenCol = (csbi.srWindow.Right - csbi.srWindow.Left) + 1;
+	CharSL = csbi.dwCursorPosition.X;
 
 	//make sure they didnt make the screen to small
 	if(ScreenLines<4)
@@ -282,45 +279,32 @@
 	}
 
 	len = _vstprintf (szOut, szFormat, arg_ptr);
-#ifdef _UNICODE
-	pBuf = cmd_alloc(len + 1);
-	len = WideCharToMultiByte( OutputCodePage, 0, szOut, len + 1, pBuf, len + 1, NULL, NULL) - 1;
-#else
-	pBuf = szOut;
-#endif
-
-	for(i = 0; i < len; i++)
-	{
-		// search 'end of string' '\n' or 'end of screen line'
-		for(; (i < len) && (pBuf[i] != _T('\n') && (CharSL<ScreenCol)) ; i++)
-			CharSL++;
-
-		WriteFile (GetStdHandle (nStdHandle),&pBuf[i-CharSL],sizeof(CHAR)*(CharSL+1),&dwWritten,NULL);
+
+	while (i < len)
+	{
+		// Search until the end of a line is reached
+		if (szOut[i++] != _T('\n') && ++CharSL < csbi.dwSize.X)
+			continue;
+
 		LineCount++;
 		CharSL=0;
 
 		if(LineCount >= ScreenLines)
 		{
-			if(_strnicmp(&pBuf[i], "\n", 2)!=0)
-				WriteFile (GetStdHandle (nStdHandle),_T("\n"),sizeof(CHAR),&dwWritten,NULL);
+			WriteConsole(hOutput, &szOut[from], i-from, &dwWritten, NULL);
+			from = i;
 
 			if(PagePrompt() != PROMPT_YES)
 			{
-#ifdef _UNICODE
-				cmd_free(pBuf);
-#endif
 				return 1;
 			}
 			//reset the number of lines being printed
 			LineCount = 0;
-			CharSL=0;
 		}
-
-	}
-
-#ifdef _UNICODE
-	cmd_free(pBuf);
-#endif
+	}
+
+	WriteConsole(hOutput, &szOut[from], i-from, &dwWritten, NULL);
+
 	return 0;
 }
 



More information about the Ros-diffs mailing list