Commit patch to not break on spaces.
[bowtie.git] / color.cpp
1 /*
2  * color.cpp
3  *
4  *  Created on: Oct 18, 2009
5  *      Author: Ben Langmead
6  */
7
8 #include <iostream>
9 #include <sstream>
10 #include <string>
11 #include "color.h"
12
13 using namespace std;
14
15 /**
16  * Set the console color.
17  */
18 void setConsoleColor(int color) {
19         cout << (char)0x1B << "[" << 0 << ";" << color + 30 << ";" << 0 + 40 << "m";
20 }
21
22 /**
23  * Set the console color.
24  */
25 void appendConsoleColor(string& s, int color) {
26         s.push_back((char)0x1B);
27         s.append("[0;");
28         ostringstream ss;
29         ss << (color + 30);
30         s.append(ss.str());
31         s.append(";40m");
32 }
33
34 /**
35  * Print color character in the appropriate color to the console.
36  * Console must support color.
37  */
38 void printColor(char color) {
39         char ch = ' ';
40         switch(color) {
41                 case 'A':
42                 case '0':
43                 case 0:
44                         setConsoleColor(COLOR_BLUE);
45                         ch = '0'; break;
46                 case 'C':
47                 case '1':
48                 case 1:
49                         setConsoleColor(COLOR_GREEN);
50                         ch = '1'; break;
51                 case 'G':
52                 case '2':
53                 case 2:
54                         setConsoleColor(COLOR_YELLOW);
55                         ch = '2'; break;
56                 case 'T':
57                 case '3':
58                 case 3:
59                         setConsoleColor(COLOR_RED);
60                         ch = '3'; break;
61                 case 'N':
62                 case '4':
63                 case '.':
64                 case 4:
65                         setConsoleColor(COLOR_WHITE);
66                         ch = '.'; break;
67                 default:
68                         setConsoleColor(COLOR_WHITE);
69                         break;
70         }
71         cout << ch;
72         setConsoleColor(COLOR_WHITE);
73 }
74
75 /**
76  * Print color character in the appropriate color to the console.
77  * Console must support color.
78  */
79 void appendColor(string& s, char color) {
80         char ch = ' ';
81         switch(color) {
82                 case 'A':
83                 case '0':
84                 case 0:
85                         appendConsoleColor(s, COLOR_BLUE);
86                         ch = '0'; break;
87                 case 'C':
88                 case '1':
89                 case 1:
90                         appendConsoleColor(s, COLOR_GREEN);
91                         ch = '1'; break;
92                 case 'G':
93                 case '2':
94                 case 2:
95                         appendConsoleColor(s, COLOR_YELLOW);
96                         ch = '2'; break;
97                 case 'T':
98                 case '3':
99                 case 3:
100                         appendConsoleColor(s, COLOR_RED);
101                         ch = '3'; break;
102                 case 'N':
103                 case '4':
104                 case '.':
105                 case 4:
106                         appendConsoleColor(s, COLOR_WHITE);
107                         ch = '.'; break;
108                 default:
109                         appendConsoleColor(s, COLOR_WHITE);
110                         break;
111         }
112         s.push_back(ch);
113         appendConsoleColor(s, COLOR_WHITE);
114 }