-
Notifications
You must be signed in to change notification settings - Fork 21
/
disp.hpp
295 lines (253 loc) · 7.2 KB
/
disp.hpp
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
#ifndef DISP_HPP
#define DISP_HPP
/**
* @brief JUDAH - Jacob is equipped with a text-based user interface
*
* @file disp.hpp
* @author Norbert Bátfai <[email protected]>
* @version 0.0.1
*
* @section LICENSE
*
* Copyright (C) 2015 Norbert Bátfai, [email protected]
*
* 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 3 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. If not, see <http://www.gnu.org/licenses/>.
*
* @section DESCRIPTION
*
* JACOB, https://github.com/nbatfai/jacob
*
* "The son of Isaac is Jacob." The project called Jacob is an experiment
* to replace Isaac's (GUI based) visual imagination with a character console.
*
* ISAAC, https://github.com/nbatfai/isaac
*
* "The son of Samu is Isaac." The project called Isaac is a case study
* of using deep Q learning with neural networks for predicting the next
* sentence of a conversation.
*
* SAMU, https://github.com/nbatfai/samu
*
* The main purpose of this project is to allow the evaluation and
* verification of the results of the paper entitled "A disembodied
* developmental robotic agent called Samu Bátfai". It is our hope
* that Samu will be the ancestor of developmental robotics chatter
* bots that will be able to chat in natural language like humans do.
*
*/
#include <cstring>
#include <sstream>
#include <ncurses.h>
#include <mutex>
class Disp
{
public:
Disp()
{
initscr();
cbreak();
noecho();
timeout ( 0 );
curs_set ( FALSE );
clear();
int max_x, max_y;
getmaxyx ( stdscr, max_y, max_x );
mx = 0;
my = 0;
vi_w = newwin ( 10+2, max_x, 0, 0 );
log_w = newwin ( max_y- ( 10+2 ) - 3, max_x, 10+2, 0 );
log_iw = newwin ( max_y- ( 10+2 ) - 3 -2, max_x-2, 10+2+1, 1 );
shell_w = newwin ( 3, max_x, 10+2+max_y- ( 10+2 ) - 3, 0 );
start_color();
/*
init_pair ( 1,COLOR_WHITE, COLOR_BLUE );
init_pair ( 2, COLOR_WHITE, COLOR_YELLOW );
init_pair ( 3, COLOR_YELLOW, COLOR_BLUE );
*/
/*
init_pair ( 1, COLOR_BLACK, COLOR_WHITE );
init_pair ( 2, COLOR_WHITE, COLOR_YELLOW );
init_pair ( 3, COLOR_WHITE, COLOR_RED );
*/
init_pair ( 1, COLOR_BLACK, COLOR_WHITE );
init_pair ( 2, COLOR_WHITE, COLOR_MAGENTA );
init_pair ( 3, COLOR_WHITE, COLOR_RED );
init_pair ( 4, COLOR_BLACK, COLOR_CYAN );
init_pair ( 5, COLOR_BLACK, COLOR_GREEN );
init_pair ( 6, COLOR_BLUE, COLOR_YELLOW );
init_pair ( 7, COLOR_BLACK, COLOR_MAGENTA );
init_pair ( 8, COLOR_CYAN, COLOR_RED );
init_pair ( 9, COLOR_WHITE, COLOR_BLACK );
init_pair ( 10, COLOR_MAGENTA, COLOR_BLACK );
init_pair ( 11, COLOR_GREEN, COLOR_MAGENTA );
wbkgd ( vi_w, COLOR_PAIR ( 1 ) );
wbkgd ( log_w, COLOR_PAIR ( 2 ) );
wbkgd ( log_iw, COLOR_PAIR ( 2 ) );
wbkgd ( shell_w, COLOR_PAIR ( 1 ) );
nodelay ( shell_w, TRUE );
keypad ( shell_w, TRUE );
scrollok ( log_iw, TRUE );
ui( );
}
~Disp()
{
delwin ( vi_w );
delwin ( log_w );
delwin ( log_iw );
delwin ( shell_w );
endwin();
}
void shell ( std::string msg )
{
ncurses_mutex.lock();
ui();
werase ( shell_w );
box ( shell_w, 0, 0 );
mvwprintw ( shell_w, 0, 1, " Caregiver shell " );
mvwprintw ( shell_w, 1, 1, "Norbi> " );
waddstr ( shell_w, msg.c_str() );
wrefresh ( shell_w );
ncurses_mutex.unlock();
}
void vi ( std::string msg )
{
if ( ncurses_mutex.try_lock() )
{
ui();
werase ( vi_w );
wmove ( vi_w, 1, 0 );
waddstr ( vi_w, msg.c_str() );
box ( vi_w, 0, 0 );
mvwprintw ( vi_w, 0, 1, " Samu's visual imagery " );
wrefresh ( vi_w );
ncurses_mutex.unlock();
}
}
void vi ( char* vi_console )
{
if ( ncurses_mutex.try_lock() )
{
ui();
werase ( vi_w );
for ( int i {0}; i<10; ++i )
{
wmove ( vi_w, i+1, 1 );
for ( int j {0}; j<80; ++j )
{
char c = vi_console[i*80+j];
if ( isprint ( c ) )
{
if ( isdigit ( c ) )
{
wattron ( vi_w,COLOR_PAIR ( c-'0'+2 ) );
waddch ( vi_w, c );
wattroff ( vi_w,COLOR_PAIR ( c-'0'+2 ) );
}
else
waddch ( vi_w, c );
}
}
}
box ( vi_w, 0, 0 );
mvwprintw ( vi_w, 0, 1, " Samu's visual imagery " );
wrefresh ( vi_w );
ncurses_mutex.unlock();
}
}
void log ( std::string msg )
{
ncurses_mutex.lock();
ui();
msg = msg + "\n";
waddstr ( log_iw, msg.c_str() );
box ( log_w, 0, 0 );
mvwprintw ( log_w, 0, 1, " Samu's answers " );
wrefresh ( log_iw );
ncurses_mutex.unlock();
}
void cg_read()
{
int ch;
if ( ( ch = wgetch ( shell_w ) ) != ERR )
{
if ( ch == '\n' )
{
std::string ret ( buf );
buf.clear();
shell ( buf );
throw ret;
}
else if ( ch == KEY_BACKSPACE )
{
if ( buf.size() >= 1 )
{
buf.pop_back();
shell ( buf );
}
}
else
{
if ( isalnum ( ch ) || isspace ( ch ) )
{
if ( buf.length() < 78 )
{
buf += ch;
shell ( buf );
}
}
}
}
}
private:
void ui ( void )
{
int max_x, max_y;
getmaxyx ( stdscr, max_y, max_x );
if ( mx != max_x || my != max_y )
{
mx = max_x;
my = max_y;
wresize ( vi_w, 10+2, mx );
mvwin ( vi_w, 0, 0 );
werase ( vi_w );
wresize ( log_w, my- ( 10+2 ) - 3, mx );
mvwin ( log_w, 10+2, 0 );
werase ( log_w );
wresize ( log_iw, my- ( 10+2 ) - 3-2, mx-2 );
mvwin ( log_iw, 10+2+1, 1 );
werase ( log_iw );
wresize ( shell_w, 3, mx );
mvwin ( shell_w, 10+2+my- ( 10+2 ) - 3, 0 );
werase ( shell_w );
box ( vi_w, 0, 0 );
mvwprintw ( vi_w, 0, 1, " Samu's visual imagery " );
box ( log_w, 0, 0 );
mvwprintw ( log_w, 0, 1, " Samu's answers " );
box ( shell_w, 0, 0 );
mvwprintw ( shell_w, 0, 1, " Caregiver shell " );
mvwprintw ( shell_w, 1, 1, "Norbi> Type your sentence and press [ENTER]" );
wrefresh ( vi_w );
wrefresh ( log_w );
wrefresh ( log_iw );
wrefresh ( shell_w );
}
}
std::mutex ncurses_mutex;
std::string buf;
WINDOW *vi_w;
WINDOW *log_w, *log_iw;
WINDOW *shell_w;
int mx {0}, my {0};
};
#endif