CSC112 Spring 2016 Examples
/*1* File: termmanip.h2* Purpose: To have fun messing with people's terminals, and making3* lots of colorful and interesting programs.4*5* This provides a nice little interface to the ANSI escape6* sequence set.7* Revision: $Revision: 1.1 $8* Change Log9* $Log: termmanip.h,v $10* Revision 1.1 2016/03/17 15:58:41 pngwen11* Initial revision12*13*/14#ifndef TERMMANIP_H15#define TERMMANIP_H16#include <iostream>17#include <string>18#include <sstream>1920/*21* Graphic Mode Modifiers22*/23inline std::ostream& normal(std::ostream &os) {24return os << "\033[0m";25}262728inline std::ostream& bold(std::ostream &os) {29return os << "\033[1m";30}313233inline std::ostream& underline(std::ostream &os) {34return os << "\033[4m";35}363738inline std::ostream& blink(std::ostream &os) {39return os << "\033[5m";40}414243inline std::ostream& reverseVideo(std::ostream &os) {44return os << "\033[7m";45}46474849/*50* Foreground Color Graphic Mode Modifiers51*/52inline std::ostream& black(std::ostream &os) {53return os << "\033[30m";54}555657inline std::ostream& red(std::ostream &os) {58return os << "\033[31m";59}606162inline std::ostream& green(std::ostream &os) {63return os << "\033[32m";64}656667inline std::ostream& yellow(std::ostream &os) {68return os << "\033[33m";69}707172inline std::ostream& blue(std::ostream &os) {73return os << "\033[34m";74}757677inline std::ostream& magenta(std::ostream &os) {78return os << "\033[35m";79}808182inline std::ostream& cyan(std::ostream &os) {83return os << "\033[36m";84}858687inline std::ostream& white(std::ostream &os) {88return os << "\033[37m";89}9091/*92* Foreground Color Graphic Mode Modifiers93*/94inline std::ostream& blackBackground(std::ostream &os) {95return os << "\033[40m";96}979899inline std::ostream& redBackground(std::ostream &os) {100return os << "\033[41m";101}102103104inline std::ostream& greenBackground(std::ostream &os) {105return os << "\033[42m";106}107108109inline std::ostream& yellowBackground(std::ostream &os) {110return os << "\033[43m";111}112113114inline std::ostream& blueBackground(std::ostream &os) {115return os << "\033[44m";116}117118119inline std::ostream& magentaBackground(std::ostream &os) {120return os << "\033[45m";121}122123124inline std::ostream& cyanBackground(std::ostream &os) {125return os << "\033[46m";126}127128129inline std::ostream& whiteBackground(std::ostream &os) {130return os << "\033[47m";131}132133134/*135* Simple terminal commands136*/137inline std::ostream& clearScreen(std::ostream &os) {138return os << "\033[2J";139}140141142inline std::ostream& clearLine(std::ostream &os) {143return os << "\033[K";144}145146147inline std::ostream& saveCursor(std::ostream &os) {148return os << "\033[s";149}150151152inline std::ostream& restoreCursor(std::ostream &os) {153return os << "\033[u";154}155156157/*158* This class is used by commands requiring arguments159*/160class ArgSequence {161public:162ArgSequence(std::string seq) {163this->seq = seq;164}165166std::ostream& operator()(std::ostream &os) const {167return os << seq;168}169170private:171std::string seq;172};173174175//overload the << operator so ostream can use the sequence!176inline std::ostream& operator<<(std::ostream& os, const ArgSequence& aSeq) {177return aSeq(os);178}179180181/*182* Commands requiring arguments183*/184inline ArgSequence cursorUp(int value) {185//compute the arg sequence186std::ostringstream os;187os << "\033[" << value << "A";188189//return the result190return ArgSequence(os.str());191}192193194inline ArgSequence cursorDown(int value) {195//compute the arg sequence196std::ostringstream os;197os << "\033[" << value << "B";198199//return the result200return ArgSequence(os.str());201}202203204inline ArgSequence cursorForward(int value) {205//compute the arg sequence206std::ostringstream os;207os << "\033[" << value << "C";208209//return the result210return ArgSequence(os.str());211}212213214inline ArgSequence cursorBackward(int value) {215//compute the arg sequence216std::ostringstream os;217os << "\033[" << value << "D";218219//return the result220return ArgSequence(os.str());221}222223224inline ArgSequence cursorPosition(int x, int y) {225//compute the arg sequence226std::ostringstream os;227os << "\033[" << y << ";" << x << "H";228229//return the result230return ArgSequence(os.str());231}232233234inline std::ostream& cursorOff(std::ostream &os) {235return os << "\033[?25l";236}237238239inline std::ostream& cursorOn(std::ostream &os) {240return os << "\033[?25h";241}242243#endif244245246