Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "util/ConsoleWrapper.h"
- #include <SFML/Config.hpp>
- #include <iostream>
- #ifndef SFML_SYSTEM_LINUX
- #include <conio.h> // Okay, so not it's standard. Meh.
- #endif
- namespace util
- {
- inline void ConsoleWrapper::ClearInputArea()
- {
- // Clear input area
- std::string back;
- for ( size_t i = 0; i < inputBuffer.length() + 2; ++i )
- {
- back += "\b \b";
- }
- std::cout << back;
- }
- inline void ConsoleWrapper::PrintInputArea()
- {
- std::cout << "> ";
- #ifndef SFLM_SYSTEM_LINUX
- std::cout << inputBuffer;
- #endif
- }
- ConsoleWrapper::ConsoleWrapper( InputCallback theCallback )
- : inputThread( &ConsoleWrapper::ReadInput, this ),
- callback( theCallback )
- {
- inputThread.launch();
- }
- ConsoleWrapper::~ConsoleWrapper()
- {
- inputThread.terminate();
- }
- void ConsoleWrapper::ReadInput()
- {
- std::cout << "> ";
- while ( true )
- {
- #ifdef SFML_SYSTEM_LINUX
- std::getline( std::cin, inputBuffer );
- #else
- // Get input
- for ( char c = getch(); true; c = getch() )
- {
- if ( c == '\b' )
- {
- if ( inputBuffer.length() <= 0 )
- {
- continue;
- }
- sf::Lock lock( consoleMutex );
- std::cout << "\b \b";
- inputBuffer = inputBuffer.substr( 0, inputBuffer.length() - 1 );
- }
- else if ( c == '\r' or c == '\n' )
- {
- break;
- }
- else
- {
- sf::Lock lock( consoleMutex );
- inputBuffer += c;
- std::cout << c;
- }
- }
- #endif
- // Handle commands
- if ( callback )
- {
- std::string tmp = inputBuffer;
- {
- sf::Lock lock( consoleMutex );
- #ifndef SFML_SYSTEM_LINUX
- std::cout << std::endl;
- #endif
- inputBuffer = "";
- PrintInputArea();
- }
- callback( tmp );
- }
- }
- }
- void ConsoleWrapper::Print( const std::string& line )
- {
- PrintLine( line, false );
- }
- void ConsoleWrapper::PrintLine( const std::string& line, bool clearInput )
- {
- sf::Lock lock( consoleMutex );
- // Clear input
- ClearInputArea();
- if ( clearInput )
- {
- inputBuffer = "";
- }
- // Print output
- std::cout << line << std::endl;
- PrintInputArea();
- // Finish up
- std::cout.flush();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement