Advertisement
spacechase0

EWO ConsoleWrapper

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