Index: trunk/skirmish/linereader.cc |
— | — | @@ -1,69 +0,0 @@ |
2 | | -#include <stdio.h> |
3 | | -#include <readline/readline.h> |
4 | | -#include <readline/history.h> |
5 | | - |
6 | | -#include <cstdlib> |
7 | | -#include <iostream> |
8 | | - |
9 | | -#include "linereader.h" |
10 | | - |
11 | | -linereader::linereader(void) |
12 | | -{ |
13 | | -} |
14 | | - |
15 | | -linereader::~linereader(void) |
16 | | -{ |
17 | | -} |
18 | | - |
19 | | -bool |
20 | | -linereader::readline(std::string &r, std::string const &prompt) |
21 | | -{ |
22 | | - char *s = ::readline(form_prompt(prompt).c_str()); |
23 | | - if (s == NULL) |
24 | | - return false; |
25 | | - r = s; |
26 | | - if (*s) |
27 | | - add_history(s); |
28 | | - std::free(s); |
29 | | - return true; |
30 | | -} |
31 | | - |
32 | | -std::string |
33 | | -linereader::form_prompt(std::string const &src) |
34 | | -{ |
35 | | - std::string::size_type i, last = 0; |
36 | | - std::string pr; |
37 | | - |
38 | | - while ((i = src.find('$', last)) != std::string::npos) { |
39 | | - pr.append(src.substr(last, i - last)); |
40 | | - if (i == src.size()-1) |
41 | | - return pr + "$"; |
42 | | - switch (src[i + 1]) { |
43 | | - case '$': |
44 | | - pr += '$'; |
45 | | - last = i + 2; |
46 | | - continue; |
47 | | - case '(': |
48 | | - break; |
49 | | - default: |
50 | | - return src; |
51 | | - } |
52 | | - if ((last = src.find(')', i)) == std::string::npos) |
53 | | - return src; |
54 | | - std::string key = src.substr(i + 2, last - i - 2); |
55 | | - std::string value; |
56 | | - std::map<std::string, std::string>::iterator it; |
57 | | - if ((it = promptvars.find(key)) != promptvars.end()) |
58 | | - value = it->second; |
59 | | - pr += value; |
60 | | - last++; |
61 | | - } |
62 | | - pr += src.substr(last); |
63 | | - return pr; |
64 | | -} |
65 | | - |
66 | | -void |
67 | | -linereader::set_prompt_variable(std::string const &var, std::string const &key) |
68 | | -{ |
69 | | - promptvars[var] = key; |
70 | | -} |
Index: trunk/skirmish/linereader.h |
— | — | @@ -1,20 +0,0 @@ |
2 | | -#ifndef LINEREADER_H |
3 | | -#define LINEREADER_H |
4 | | - |
5 | | -#include <string> |
6 | | -#include <map> |
7 | | - |
8 | | -struct linereader { |
9 | | - linereader(); |
10 | | - ~linereader(); |
11 | | - |
12 | | - bool readline(std::string &, std::string const &); |
13 | | - void set_prompt_variable(std::string const &var, std::string const &value); |
14 | | - |
15 | | -private: |
16 | | - std::string form_prompt(std::string const &); |
17 | | - |
18 | | - std::map<std::string, std::string> promptvars; |
19 | | -}; |
20 | | - |
21 | | -#endif |
Index: trunk/skirmish/terminal.cc |
— | — | @@ -0,0 +1,110 @@ |
| 2 | +#include <stdio.h> |
| 3 | +#include <readline/readline.h> |
| 4 | +#include <readline/history.h> |
| 5 | + |
| 6 | +#include <cstdlib> |
| 7 | +#include <iostream> |
| 8 | + |
| 9 | +#include <sys/types.h> |
| 10 | +#include <sys/ioctl.h> |
| 11 | +#include <termio.h> |
| 12 | + |
| 13 | +#include "terminal.h" |
| 14 | + |
| 15 | +terminal::terminal(void) |
| 16 | + : rows_output(0) |
| 17 | +{ |
| 18 | + struct winsize wz; |
| 19 | + ioctl(0, TIOCGWINSZ, &wz); |
| 20 | + rows = wz.ws_row; |
| 21 | + cols = wz.ws_col; |
| 22 | +} |
| 23 | + |
| 24 | +terminal::~terminal(void) |
| 25 | +{ |
| 26 | +} |
| 27 | + |
| 28 | +bool |
| 29 | +terminal::readline(std::string &r, std::string const &prompt) |
| 30 | +{ |
| 31 | + char *s = ::readline(form_prompt(prompt).c_str()); |
| 32 | + if (s == NULL) |
| 33 | + return false; |
| 34 | + r = s; |
| 35 | + if (*s) |
| 36 | + add_history(s); |
| 37 | + std::free(s); |
| 38 | + return true; |
| 39 | +} |
| 40 | + |
| 41 | +std::string |
| 42 | +terminal::form_prompt(std::string const &src) |
| 43 | +{ |
| 44 | + std::string::size_type i, last = 0; |
| 45 | + std::string pr; |
| 46 | + |
| 47 | + while ((i = src.find('$', last)) != std::string::npos) { |
| 48 | + pr.append(src.substr(last, i - last)); |
| 49 | + if (i == src.size()-1) |
| 50 | + return pr + "$"; |
| 51 | + switch (src[i + 1]) { |
| 52 | + case '$': |
| 53 | + pr += '$'; |
| 54 | + last = i + 2; |
| 55 | + continue; |
| 56 | + case '(': |
| 57 | + break; |
| 58 | + default: |
| 59 | + return src; |
| 60 | + } |
| 61 | + if ((last = src.find(')', i)) == std::string::npos) |
| 62 | + return src; |
| 63 | + std::string key = src.substr(i + 2, last - i - 2); |
| 64 | + std::string value; |
| 65 | + std::map<std::string, std::string>::iterator it; |
| 66 | + if ((it = promptvars.find(key)) != promptvars.end()) |
| 67 | + value = it->second; |
| 68 | + pr += value; |
| 69 | + last++; |
| 70 | + } |
| 71 | + pr += src.substr(last); |
| 72 | + return pr; |
| 73 | +} |
| 74 | + |
| 75 | +void |
| 76 | +terminal::set_prompt_variable(std::string const &var, std::string const &key) |
| 77 | +{ |
| 78 | + promptvars[var] = key; |
| 79 | +} |
| 80 | + |
| 81 | +void |
| 82 | +terminal::putline(std::string const &line) |
| 83 | +{ |
| 84 | + /* |
| 85 | + * If we don't know the size of the terminal, just print it. |
| 86 | + */ |
| 87 | + if (rows == 0 || cols == 0) { |
| 88 | + std::cout << line << '\n'; |
| 89 | + return; |
| 90 | + } |
| 91 | + |
| 92 | + std::string rest = line; |
| 93 | + while (rest.size() > cols) { |
| 94 | + really_put_line(rest.substr(0, cols)); |
| 95 | + rest = rest.substr(cols); |
| 96 | + } |
| 97 | + really_put_line(rest); |
| 98 | +} |
| 99 | + |
| 100 | +void |
| 101 | +terminal::really_put_line(std::string const &line) |
| 102 | +{ |
| 103 | + if (rows - 1 == rows_output) { |
| 104 | + rows_output = 0; |
| 105 | + std::cout << "-- More --"; |
| 106 | + std::string dummy; |
| 107 | + std::getline(std::cin, dummy); |
| 108 | + } |
| 109 | + std::cout << line << '\n'; |
| 110 | + ++rows_output; |
| 111 | +} |
Property changes on: trunk/skirmish/terminal.cc |
___________________________________________________________________ |
Added: svn:keywords |
1 | 112 | + Id Revision |
Index: trunk/skirmish/terminal.h |
— | — | @@ -0,0 +1,24 @@ |
| 2 | +#ifndef TERMINAL_H |
| 3 | +#define TERMINAL_H |
| 4 | + |
| 5 | +#include <string> |
| 6 | +#include <map> |
| 7 | + |
| 8 | +struct terminal { |
| 9 | + terminal(); |
| 10 | + ~terminal(); |
| 11 | + |
| 12 | + bool readline(std::string &, std::string const &); |
| 13 | + void set_prompt_variable(std::string const &var, std::string const &value); |
| 14 | + void putline(std::string const &line); |
| 15 | + |
| 16 | +private: |
| 17 | + std::string form_prompt(std::string const &); |
| 18 | + void really_put_line(std::string const &line); |
| 19 | + |
| 20 | + std::map<std::string, std::string> promptvars; |
| 21 | + int rows, cols; |
| 22 | + int rows_output; |
| 23 | +}; |
| 24 | + |
| 25 | +#endif |
Property changes on: trunk/skirmish/terminal.h |
___________________________________________________________________ |
Added: svn:keywords |
1 | 26 | + Id Revision |
Index: trunk/skirmish/skirmish.cc |
— | — | @@ -6,7 +6,7 @@ |
7 | 7 | #include <boost/function.hpp> |
8 | 8 | #include <boost/lexical_cast.hpp> |
9 | 9 | |
10 | | -#include "linereader.h" |
| 10 | +#include "terminal.h" |
11 | 11 | #include "db.h" |
12 | 12 | |
13 | 13 | static db::connectionptr open_connection(std::string const &); |
— | — | @@ -71,10 +71,11 @@ |
72 | 72 | show_connection(); |
73 | 73 | } |
74 | 74 | |
| 75 | +terminal term; |
| 76 | + |
75 | 77 | int |
76 | 78 | main(int argc, char *argv[]) |
77 | 79 | { |
78 | | - linereader rl; |
79 | 80 | for (int a = 1; argv[a]; ++a) |
80 | 81 | add_connection(argv[a]); |
81 | 82 | |
— | — | @@ -83,14 +84,14 @@ |
84 | 85 | for (;;) { |
85 | 86 | std::string cnrs; |
86 | 87 | if (cnr == -1) { |
87 | | - rl.set_prompt_variable("desc", "not connected"); |
88 | | - rl.set_prompt_variable("cnr", "none"); |
| 88 | + term.set_prompt_variable("desc", "not connected"); |
| 89 | + term.set_prompt_variable("cnr", "none"); |
89 | 90 | } else { |
90 | | - rl.set_prompt_variable("cnr", boost::lexical_cast<std::string>(cnr)); |
91 | | - rl.set_prompt_variable("desc", conns[cnr]->desc); |
| 91 | + term.set_prompt_variable("cnr", boost::lexical_cast<std::string>(cnr)); |
| 92 | + term.set_prompt_variable("desc", conns[cnr]->desc); |
92 | 93 | } |
93 | 94 | |
94 | | - if (!rl.readline(input, prompt)) |
| 95 | + if (!term.readline(input, prompt)) |
95 | 96 | break; |
96 | 97 | |
97 | 98 | if (input.empty()) |
— | — | @@ -151,28 +152,33 @@ |
152 | 153 | } |
153 | 154 | |
154 | 155 | for (int row = 0; row < data.size(); ++row) { |
| 156 | + std::stringstream output; |
| 157 | + |
155 | 158 | for (int col = 0; col < ncols; ++col) { |
156 | | - std::cout << ' ' << std::setw(sizes[col]) << std::left << data[row][col]; |
| 159 | + output << ' ' << std::setw(sizes[col]) << std::left << data[row][col]; |
157 | 160 | if (col != (res->num_fields() - 1)) |
158 | | - std::cout << " |"; |
| 161 | + output << " |"; |
159 | 162 | } |
160 | | - std::cout << '\n'; |
| 163 | + term.putline(output.str()); |
161 | 164 | |
| 165 | + output.clear(); |
| 166 | + output.str(""); |
| 167 | + |
162 | 168 | if (row == 0) { |
163 | 169 | for (int col = 0; col < ncols; ++col) { |
164 | | - std::cout << std::string(sizes[col] + 2, '-'); |
| 170 | + output << std::string(sizes[col] + 2, '-'); |
165 | 171 | if (col == ncols-1) |
166 | | - std::cout << '-'; |
| 172 | + output << '-'; |
167 | 173 | else |
168 | | - std::cout << '+'; |
| 174 | + output << '+'; |
169 | 175 | } |
170 | | - std::cout << '\n'; |
| 176 | + term.putline(output.str()); |
171 | 177 | } |
172 | 178 | } |
173 | 179 | } |
174 | 180 | |
175 | 181 | if (nrows) |
176 | | - std::cout << boost::format("\nOK (%d rows).\n") % nrows; |
| 182 | + term.putline(str(boost::format("\nOK (%d rows).\n") % nrows)); |
177 | 183 | } |
178 | 184 | } |
179 | 185 | |
— | — | @@ -371,22 +377,27 @@ |
372 | 378 | |
373 | 379 | int ncols = sizes.size(); |
374 | 380 | for (int row = 0; row < data.size(); ++row) { |
| 381 | + std::ostringstream output; |
| 382 | + |
375 | 383 | for (int col = 0; col < ncols; ++col) { |
376 | | - std::cout << ' ' << std::setw(sizes[col]) << std::left << data[row][col]; |
| 384 | + output << ' ' << std::setw(sizes[col]) << std::left << data[row][col]; |
377 | 385 | if (col != (sizes.size() - 1)) |
378 | | - std::cout << " |"; |
| 386 | + output << " |"; |
379 | 387 | } |
380 | | - std::cout << '\n'; |
| 388 | + term.putline(output.str()); |
381 | 389 | |
382 | 390 | if (row == 0) { |
| 391 | + output.str(""); |
| 392 | + output.clear(); |
| 393 | + |
383 | 394 | for (int col = 0; col < ncols; ++col) { |
384 | | - std::cout << std::string(sizes[col] + 2, '-'); |
| 395 | + output << std::string(sizes[col] + 2, '-'); |
385 | 396 | if (col == ncols-1) |
386 | | - std::cout << '-'; |
| 397 | + output << '-'; |
387 | 398 | else |
388 | | - std::cout << '+'; |
| 399 | + output << '+'; |
389 | 400 | } |
390 | | - std::cout << '\n'; |
| 401 | + term.putline(output.str()); |
391 | 402 | } |
392 | 403 | } |
393 | 404 | } |
Index: trunk/skirmish/Makefile |
— | — | @@ -2,7 +2,7 @@ |
3 | 3 | include rules.mk |
4 | 4 | |
5 | 5 | SRCS = \ |
6 | | - linereader.cc \ |
| 6 | + terminal.cc \ |
7 | 7 | skirmish.cc \ |
8 | 8 | db.cc \ |
9 | 9 | $(DB_SRCS) |