From f3fa3ee47ed23d671f21079c0908f7def81eb65d Mon Sep 17 00:00:00 2001 From: Jeffrey Morlan Date: Sun, 27 Jul 2008 14:03:44 +0000 Subject: [PATCH] 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. svn path=/trunk/; revision=34847 --- reactos/base/shell/cmd/console.c | 42 ++++++++++---------------------- 1 file changed, 13 insertions(+), 29 deletions(-) diff --git a/reactos/base/shell/cmd/console.c b/reactos/base/shell/cmd/console.c index 94bebe8be32..eac2a8eb949 100644 --- a/reactos/base/shell/cmd/console.c +++ b/reactos/base/shell/cmd/console.c @@ -236,10 +236,10 @@ VOID ConPrintf(LPTSTR szFormat, va_list arg_ptr, DWORD nStdHandle) 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 @@ INT ConPrintfPaging(BOOL NewPage, LPTSTR szFormat, va_list arg_ptr, DWORD nStdHa /* 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 CharSL; - int i = 0; + int from = 0, i = 0; if(NewPage == TRUE) LineCount = 0; @@ -264,7 +261,7 @@ INT ConPrintfPaging(BOOL NewPage, LPTSTR szFormat, va_list arg_ptr, DWORD nStdHa //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 @@ INT ConPrintfPaging(BOOL NewPage, LPTSTR szFormat, va_list arg_ptr, DWORD nStdHa } //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 @@ INT ConPrintfPaging(BOOL NewPage, LPTSTR szFormat, va_list arg_ptr, DWORD nStdHa } 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++) + while (i < len) { - // search 'end of string' '\n' or 'end of screen line' - for(; (i < len) && (pBuf[i] != _T('\n') && (CharSL= 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; }