Advertisement
spacechase0

util::ConsoleWrapper

Oct 21st, 2011
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.63 KB | None | 0 0
  1. #ifndef UTIL_CONSOLEWRAPPER_H
  2. #define UTIL_CONSOLEWRAPPER_H
  3.  
  4. #include <functional>
  5.  
  6. namespace util
  7. {
  8.     class ConsoleWrapper
  9.     {
  10.         public:
  11.             typedef std::function< void ( std::string ) > InputCallback;
  12.            
  13.             ConsoleWrapper( InputCallback theCallback );
  14.             ~ConsoleWrapper();
  15.            
  16.             void Print( const std::string& line );
  17.            
  18.        
  19.         private:
  20.             void ReadInput();
  21.             sf::Thread inputThread;
  22.             std::string inputBuffer;
  23.             InputCallback callback;
  24.            
  25.             void PrintLine( const std::string& line, bool clearInput );
  26.             inline void ClearInputArea();
  27.             inline void PrintInputArea();
  28.            
  29.             mutable sf::Mutex consoleMutex;
  30.     };
  31. }
  32.  
  33. #endif // UTIL_CONSOLEWRAPPER_H
  34.  
  35. #include "util/ConsoleWrapper.h"
  36.  
  37. #include <iostream>
  38. #include <conio.h> // Okay, so not it's standard. Meh.
  39.  
  40. namespace util
  41. {
  42.     inline void ConsoleWrapper::ClearInputArea()
  43.     {
  44.         // Clear input area
  45.         std::string back;
  46.         for ( size_t i = 0; i < inputBuffer.length() + 2; ++i )
  47.         {
  48.             back += "\b \b";
  49.         }
  50.         std::cout << back;
  51.     }
  52.    
  53.     inline void ConsoleWrapper::PrintInputArea()
  54.     {
  55.         std::cout << "> " << inputBuffer;
  56.     }
  57.    
  58.     ConsoleWrapper::ConsoleWrapper( InputCallback theCallback )
  59.        : inputThread( &ConsoleWrapper::ReadInput, this ),
  60.          callback( theCallback )
  61.     {
  62.         inputThread.Launch();
  63.     }
  64.    
  65.     ConsoleWrapper::~ConsoleWrapper()
  66.     {
  67.         inputThread.Terminate();
  68.     }
  69.    
  70.     void ConsoleWrapper::ReadInput()
  71.     {
  72.         std::cout << "> ";
  73.         while ( true )
  74.         {
  75.             // Get input
  76.             for ( char c = getch(); true; c = getch() )
  77.             {
  78.                 if ( c == '\b' )
  79.                 {
  80.                     if ( inputBuffer.length() <= 0 )
  81.                     {
  82.                         continue;
  83.                     }
  84.                    
  85.                     sf::Lock lock( consoleMutex );
  86.                    
  87.                     std::cout << "\b \b";
  88.                     inputBuffer = inputBuffer.substr( 0, inputBuffer.length() - 1 );
  89.                 }
  90.                 else if ( c == '\r' or c == '\n' )
  91.                 {
  92.                     break;
  93.                 }
  94.                 else
  95.                 {
  96.                     sf::Lock lock( consoleMutex );
  97.                    
  98.                     inputBuffer += c;
  99.                     std::cout << c;
  100.                 }
  101.             }
  102.            
  103.             // Handle commands
  104.             if ( callback )
  105.             {
  106.                 std::string tmp = inputBuffer;
  107.                 {
  108.                     sf::Lock lock( consoleMutex );
  109.                    
  110.                     ClearInputArea();
  111.                     inputBuffer = "";
  112.                     PrintInputArea();
  113.                 }
  114.                
  115.                 callback( tmp );
  116.             }
  117.         }
  118.     }
  119.    
  120.     void ConsoleWrapper::Print( const std::string& line )
  121.     {
  122.         PrintLine( line, false );
  123.     }
  124.    
  125.     void ConsoleWrapper::PrintLine( const std::string& line, bool clearInput )
  126.     {
  127.         sf::Lock lock( consoleMutex );
  128.        
  129.         // Clear input
  130.         ClearInputArea();
  131.         if ( clearInput )
  132.         {
  133.             inputBuffer = "";
  134.         }
  135.        
  136.         // Print output
  137.         std::cout << line << std::endl;
  138.         PrintInputArea();
  139.        
  140.         // Finish up
  141.         std::cout.flush();
  142.     }
  143. }
  144.  
  145.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement