1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
|
/** \file terminal.cc */
// Copyright (C) 2010 Daniel Burrows
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License as
// published by the Free Software Foundation; either version 2 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; see the file COPYING. If not, write to
// the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
// Boston, MA 02111-1307, USA.
// Local includes:
#include "terminal.h"
#include <aptitude.h>
// System includes:
#include <boost/make_shared.hpp>
#include <boost/shared_ptr.hpp>
#include <cwidget/generic/util/transcode.h>
#include <iostream>
#include <sys/ioctl.h>
using boost::make_shared;
using boost::shared_ptr;
using cwidget::util::transcode;
std::string StdinEOFException::errmsg() const
{
return _("Unexpected end-of-file on standard input");
}
namespace aptitude
{
namespace cmdline
{
namespace
{
// \todo Consider using a signal handler to catch window resizes
// -- watch out for what happens if the curses UI starts up,
// we'll have to tear down the handler, and be sure to handle
// memory barriers.
class terminal_impl : public terminal_io
{
public:
bool output_is_a_terminal();
void write_text(const std::wstring &msg);
void move_to_beginning_of_line();
void flush();
std::wstring prompt_for_input(const std::wstring &msg);
unsigned int get_screen_width();
int wcwidth(wchar_t ch);
};
bool terminal_impl::output_is_a_terminal()
{
return isatty(1);
}
void terminal_impl::write_text(const std::wstring &msg)
{
std::cout << transcode(msg);
}
void terminal_impl::move_to_beginning_of_line()
{
std::cout << '\r';
}
void terminal_impl::flush()
{
std::cout << std::flush;
}
std::wstring terminal_impl::prompt_for_input(const std::wstring &msg)
{
std::cout << transcode(msg) << std::flush;
std::string rval;
char buf[1024];
std::cin.getline(buf, 1023);
rval += buf;
while(!std::cin && !std::cin.eof())
{
std::cin.getline(buf, 1023);
rval += buf;
}
if(!std::cin)
throw StdinEOFException();
return transcode(rval);
}
unsigned int terminal_impl::get_screen_width()
{
// Ripped from apt-get, which ripped it from GNU ls
winsize ws;
if (ioctl(1, TIOCGWINSZ, &ws) != -1 && ws.ws_col >= 5)
return ws.ws_col;
else
// \todo Should we distinguish between "can't read a
// terminal size" and "read a tiny terminal size"?
return 80;
}
int terminal_impl::wcwidth(wchar_t ch)
{
return ::wcwidth(ch);
}
}
terminal_input::~terminal_input()
{
}
terminal_locale::~terminal_locale()
{
}
terminal_metrics::~terminal_metrics()
{
}
terminal_output::~terminal_output()
{
}
shared_ptr<terminal_io> create_terminal()
{
return make_shared<terminal_impl>();
}
}
}
|