r20432 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r20431‎ | r20432 | r20433 >
Date:02:15, 14 March 2007
Author:river
Status:old
Tags:
Comment:
paging for long command output
Modified paths:
  • /trunk/skirmish/Makefile (modified) (history)
  • /trunk/skirmish/linereader.cc (deleted) (history)
  • /trunk/skirmish/linereader.h (deleted) (history)
  • /trunk/skirmish/skirmish.cc (modified) (history)
  • /trunk/skirmish/terminal.cc (added) (history)
  • /trunk/skirmish/terminal.cc (added) (history)
  • /trunk/skirmish/terminal.h (added) (history)
  • /trunk/skirmish/terminal.h (added) (history)

Diff [purge]

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
1112 + 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
126 + Id Revision
Index: trunk/skirmish/skirmish.cc
@@ -6,7 +6,7 @@
77 #include <boost/function.hpp>
88 #include <boost/lexical_cast.hpp>
99
10 -#include "linereader.h"
 10+#include "terminal.h"
1111 #include "db.h"
1212
1313 static db::connectionptr open_connection(std::string const &);
@@ -71,10 +71,11 @@
7272 show_connection();
7373 }
7474
 75+terminal term;
 76+
7577 int
7678 main(int argc, char *argv[])
7779 {
78 - linereader rl;
7980 for (int a = 1; argv[a]; ++a)
8081 add_connection(argv[a]);
8182
@@ -83,14 +84,14 @@
8485 for (;;) {
8586 std::string cnrs;
8687 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");
8990 } 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);
9293 }
9394
94 - if (!rl.readline(input, prompt))
 95+ if (!term.readline(input, prompt))
9596 break;
9697
9798 if (input.empty())
@@ -151,28 +152,33 @@
152153 }
153154
154155 for (int row = 0; row < data.size(); ++row) {
 156+ std::stringstream output;
 157+
155158 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];
157160 if (col != (res->num_fields() - 1))
158 - std::cout << " |";
 161+ output << " |";
159162 }
160 - std::cout << '\n';
 163+ term.putline(output.str());
161164
 165+ output.clear();
 166+ output.str("");
 167+
162168 if (row == 0) {
163169 for (int col = 0; col < ncols; ++col) {
164 - std::cout << std::string(sizes[col] + 2, '-');
 170+ output << std::string(sizes[col] + 2, '-');
165171 if (col == ncols-1)
166 - std::cout << '-';
 172+ output << '-';
167173 else
168 - std::cout << '+';
 174+ output << '+';
169175 }
170 - std::cout << '\n';
 176+ term.putline(output.str());
171177 }
172178 }
173179 }
174180
175181 if (nrows)
176 - std::cout << boost::format("\nOK (%d rows).\n") % nrows;
 182+ term.putline(str(boost::format("\nOK (%d rows).\n") % nrows));
177183 }
178184 }
179185
@@ -371,22 +377,27 @@
372378
373379 int ncols = sizes.size();
374380 for (int row = 0; row < data.size(); ++row) {
 381+ std::ostringstream output;
 382+
375383 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];
377385 if (col != (sizes.size() - 1))
378 - std::cout << " |";
 386+ output << " |";
379387 }
380 - std::cout << '\n';
 388+ term.putline(output.str());
381389
382390 if (row == 0) {
 391+ output.str("");
 392+ output.clear();
 393+
383394 for (int col = 0; col < ncols; ++col) {
384 - std::cout << std::string(sizes[col] + 2, '-');
 395+ output << std::string(sizes[col] + 2, '-');
385396 if (col == ncols-1)
386 - std::cout << '-';
 397+ output << '-';
387398 else
388 - std::cout << '+';
 399+ output << '+';
389400 }
390 - std::cout << '\n';
 401+ term.putline(output.str());
391402 }
392403 }
393404 }
Index: trunk/skirmish/Makefile
@@ -2,7 +2,7 @@
33 include rules.mk
44
55 SRCS = \
6 - linereader.cc \
 6+ terminal.cc \
77 skirmish.cc \
88 db.cc \
99 $(DB_SRCS)