Skip to content

Commit

Permalink
Move SetConsoleWinSize func to ConsoleUtils
Browse files Browse the repository at this point in the history
  • Loading branch information
plstcharles committed Feb 18, 2016
1 parent 9f1bd28 commit e80d433
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 38 deletions.
41 changes: 40 additions & 1 deletion modules/utils/include/litiv/utils/ConsoleUtils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
// limitations under the License.

#pragma once

#include "litiv/utils/DefineUtils.hpp"
#include "litiv/utils/CxxUtils.hpp"

/**
* ConsoleUtils.h (litiv version inspired from rlutil.h)
*
Expand Down Expand Up @@ -472,7 +476,41 @@ namespace rlutil {
} //namespace rlutil

namespace litiv {
// shows a progression bar in the console

#if defined(_MSC_VER)
//! sets the console window to a certain size (with optional buffer resizing)
void SetConsoleWindowSize(int x, int y, int buffer_lines=-1) {
// derived from http://www.cplusplus.com/forum/windows/121444/
HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
if(h==INVALID_HANDLE_VALUE)
lvError("SetConsoleWindowSize: Unable to get stdout handle");
COORD largestSize = GetLargestConsoleWindowSize(h);
if(x>largestSize.X)
x = largestSize.X;
if(y>largestSize.Y)
y = largestSize.Y;
if(buffer_lines<=0)
buffer_lines = y;
CONSOLE_SCREEN_BUFFER_INFO bufferInfo;
if(!GetConsoleScreenBufferInfo(h,&bufferInfo))
lvError("SetConsoleWindowSize: Unable to retrieve screen buffer info");
SMALL_RECT& winInfo = bufferInfo.srWindow;
COORD windowSize = {winInfo.Right-winInfo.Left+1,winInfo.Bottom-winInfo.Top+1};
if(windowSize.X>x || windowSize.Y>y) {
SMALL_RECT info = {0,0,SHORT((x<windowSize.X)?(x-1):(windowSize.X-1)),SHORT((y<windowSize.Y)?(y-1):(windowSize.Y-1))};
if(!SetConsoleWindowInfo(h,TRUE,&info))
lvError("SetConsoleWindowSize: Unable to resize window before resizing buffer");
}
COORD size = {SHORT(x),SHORT(y)};
if(!SetConsoleScreenBufferSize(h,size))
lvError("SetConsoleWindowSize: Unable to resize screen buffer");
SMALL_RECT info = {0,0,SHORT(x-1),SHORT(y-1)};
if(!SetConsoleWindowInfo(h, TRUE, &info))
lvError("SetConsoleWindowSize: Unable to resize window after resizing buffer");
}
#endif //defined(_MSC_VER)

//! shows a progression bar in the console
void updateConsoleProgressBar(const std::string& sMsg, float fCompletion, size_t nBarCols=20) {
if(nBarCols==0)
return;
Expand Down Expand Up @@ -506,6 +544,7 @@ namespace litiv {
fflush(stdout);
}

//! cleans a specific row from the console (default=last)
void cleanConsoleRow(int nRowIdx=INT_MAX) {
if(nRowIdx<0)
return;
Expand Down
6 changes: 1 addition & 5 deletions modules/utils/include/litiv/utils/PlatformUtils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ namespace PlatformUtils {
}

template<typename T>
inline typename std::enable_if<std::is_floating_point<T>::value,std::vector<T>>::type L1dist(T a, T b, size_t steps, bool bIncludeInitVal=true) {
inline typename std::enable_if<std::is_floating_point<T>::value,std::vector<T>>::type linspace(T a, T b, size_t steps, bool bIncludeInitVal=true) {
if(steps==0)
return std::vector<T>();
else if(steps==1)
Expand All @@ -215,8 +215,4 @@ namespace PlatformUtils {
return vfResult;
}

#if defined(_MSC_VER)
void SetConsoleWindowSize(int x, int y, int buffer_lines=-1);
#endif //defined(_MSC_VER)

} //namespace PlatformUtils
32 changes: 0 additions & 32 deletions modules/utils/src/PlatformUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,35 +152,3 @@ bool PlatformUtils::CreateDirIfNotExist(const std::string& sDirPath) {
return (stat(sDirPath.c_str(),&st)==0 && S_ISDIR(st.st_mode));
#endif //!defined(_MSC_VER)
}

#if defined(_MSC_VER)
// SetConsoleWindowSize(...) : derived from http://www.cplusplus.com/forum/windows/121444/
void PlatformUtils::SetConsoleWindowSize(int x, int y, int buffer_lines) {
HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
if(h==INVALID_HANDLE_VALUE)
lvError("SetConsoleWindowSize: Unable to get stdout handle");
COORD largestSize = GetLargestConsoleWindowSize(h);
if(x>largestSize.X)
x = largestSize.X;
if(y>largestSize.Y)
y = largestSize.Y;
if(buffer_lines<=0)
buffer_lines = y;
CONSOLE_SCREEN_BUFFER_INFO bufferInfo;
if(!GetConsoleScreenBufferInfo(h,&bufferInfo))
lvError("SetConsoleWindowSize: Unable to retrieve screen buffer info");
SMALL_RECT& winInfo = bufferInfo.srWindow;
COORD windowSize = {winInfo.Right-winInfo.Left+1,winInfo.Bottom-winInfo.Top+1};
if(windowSize.X>x || windowSize.Y>y) {
SMALL_RECT info = {0,0,SHORT((x<windowSize.X)?(x-1):(windowSize.X-1)),SHORT((y<windowSize.Y)?(y-1):(windowSize.Y-1))};
if(!SetConsoleWindowInfo(h,TRUE,&info))
lvError("SetConsoleWindowSize: Unable to resize window before resizing buffer");
}
COORD size = {SHORT(x),SHORT(y)};
if(!SetConsoleScreenBufferSize(h,size))
lvError("SetConsoleWindowSize: Unable to resize screen buffer");
SMALL_RECT info = {0,0,SHORT(x-1),SHORT(y-1)};
if(!SetConsoleWindowInfo(h, TRUE, &info))
lvError("SetConsoleWindowSize: Unable to resize window after resizing buffer");
}
#endif //defined(_MSC_VER)

0 comments on commit e80d433

Please sign in to comment.