Skip to content

Commit 49f3f61

Browse files
authored
Merge pull request jupyter-xeus#171 from MCWertGaming/fixes
Fixed a few smaller things
2 parents c6ba295 + 76c1293 commit 49f3f61

File tree

13 files changed

+59
-63
lines changed

13 files changed

+59
-63
lines changed

cpp-terminal/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# configure version information
22
configure_file(version.h.in version.h)
33

4-
# create and confirgure library target
4+
# create and configure library target
55
add_library(cpp-terminal base.cpp prompt.cpp window.cpp input.cpp private/platform.cpp)
66

77
target_include_directories(cpp-terminal BEFORE PUBLIC

cpp-terminal/base.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ Term::Terminal::Terminal(bool _clear_screen,
129129
hide_cursor{_hide_cursor} {
130130
if (clear_screen) {
131131
write(save_screen() +
132-
// fixes consoles that ignore save_screen()
132+
// Fix consoles that ignore save_screen()
133133
clear_screen_buffer());
134134
}
135135
if (hide_cursor)
@@ -139,13 +139,13 @@ Term::Terminal::Terminal(bool _clear_screen)
139139
: BaseTerminal(false, true), clear_screen{_clear_screen} {
140140
if (clear_screen) {
141141
write(save_screen()
142-
// fixes consoles that ignore save_screen()
142+
// Fix consoles that ignore save_screen()
143143
+ clear_screen_buffer());
144144
}
145145
}
146146
Term::Terminal::~Terminal() {
147147
if (clear_screen) {
148-
// fixes consoles that ignore save_screen()
148+
// Fix consoles that ignore save_screen()
149149
write(color(Term::style::reset) + clear_screen_buffer() +
150150
move_cursor(1, 1) +
151151
// restores the screen, might be ignored by some terminals

cpp-terminal/base.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,11 +115,11 @@ class Terminal : public Private::BaseTerminal {
115115
Terminal(bool _clear_screen,
116116
bool enable_keyboard,
117117
bool disable_ctrl_c,
118-
bool);
118+
bool _hide_cursor);
119119
// providing no parameters will disable the keyboard and ctrl+c
120-
Terminal(bool _clear_screen);
120+
explicit Terminal(bool _clear_screen);
121121

122-
virtual ~Terminal() override;
122+
~Terminal() override;
123123
};
124124

125125
} // namespace Term

cpp-terminal/private/conversion.hpp

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,9 @@
66
#include <vector>
77

88
#ifdef _WIN32
9-
#include <stdio.h>
9+
#include <cstdio>
1010
#else
11-
1211
#include <sys/ioctl.h>
13-
1412
#endif
1513

1614
static constexpr uint8_t UTF8_ACCEPT = 0;
@@ -19,31 +17,30 @@ static constexpr uint8_t UTF8_REJECT = 0xf;
1917
namespace Term::Private {
2018

2119
inline uint8_t utf8_decode_step(uint8_t state, uint8_t octet, uint32_t* cpp) {
22-
static const uint32_t utf8_classtab[0x10] = {
20+
static const uint32_t utf8ClassTab[0x10] = {
2321
0x88888888UL, 0x88888888UL, 0x99999999UL, 0x99999999UL,
2422
0xaaaaaaaaUL, 0xaaaaaaaaUL, 0xaaaaaaaaUL, 0xaaaaaaaaUL,
2523
0x222222ffUL, 0x22222222UL, 0x22222222UL, 0x22222222UL,
2624
0x3333333bUL, 0x33433333UL, 0xfff5666cUL, 0xffffffffUL,
2725
};
2826

29-
static const uint32_t utf8_statetab[0x10] = {
27+
static const uint32_t utf8StateTab[0x10] = {
3028
0xfffffff0UL, 0xffffffffUL, 0xfffffff1UL, 0xfffffff3UL,
3129
0xfffffff4UL, 0xfffffff7UL, 0xfffffff6UL, 0xffffffffUL,
3230
0x33f11f0fUL, 0xf3311f0fUL, 0xf33f110fUL, 0xfffffff2UL,
3331
0xfffffff5UL, 0xffffffffUL, 0xffffffffUL, 0xffffffffUL,
3432
};
3533

36-
const uint8_t reject = (state >> 3), nonascii = (octet >> 7);
34+
const uint8_t reject = (state >> 3), nonAscii = (octet >> 7);
3735
const uint8_t class_ =
38-
(!nonascii ? 0
39-
: (0xf & (utf8_classtab[(octet >> 3) & 0xf] >>
40-
(4 * (octet & 7)))));
36+
(!nonAscii
37+
? 0
38+
: (0xf & (utf8ClassTab[(octet >> 3) & 0xf] >> (4 * (octet & 7)))));
4139

4240
*cpp = (state == UTF8_ACCEPT ? (octet & (0xffU >> class_))
4341
: ((octet & 0x3fU) | (*cpp << 6)));
4442

45-
return (reject ? 0xf
46-
: (0xf & (utf8_statetab[class_] >> (4 * (state & 7)))));
43+
return (reject ? 0xf : (0xf & (utf8StateTab[class_] >> (4 * (state & 7)))));
4744
}
4845

4946
inline void codepoint_to_utf8(std::string& s, char32_t c) {
@@ -105,7 +102,7 @@ inline int convert_string_to_int(const char* string,
105102
int* rows,
106103
int* cols) {
107104
#ifdef _WIN32
108-
// windows provides it's own alternative to sscanf()
105+
// Windows provides its own alternative to sscanf()
109106
return sscanf_s(string, format, rows, cols);
110107
#else
111108
// TODO move to a better way

cpp-terminal/private/inputOutputModeFlags.hpp

Lines changed: 0 additions & 13 deletions
This file was deleted.

cpp-terminal/private/platform.cpp

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,17 @@
1313

1414
#include <stdexcept>
1515

16-
#include "inputOutputModeFlags.hpp"
16+
#ifndef ENABLE_VIRTUAL_TERMINAL_PROCESSING
17+
#define ENABLE_VIRTUAL_TERMINAL_PROCESSING 0x0004
18+
#endif
19+
20+
#ifndef DISABLE_NEWLINE_AUTO_RETURN
21+
#define DISABLE_NEWLINE_AUTO_RETURN 0x0008
22+
#endif
23+
24+
#ifndef ENABLE_VIRTUAL_TERMINAL_INPUT
25+
#define ENABLE_VIRTUAL_TERMINAL_INPUT 0x0200
26+
#endif
1727

1828
bool Term::Private::is_stdin_a_tty() {
1929
#ifdef _WIN32
@@ -60,7 +70,7 @@ bool Term::Private::get_term_size(int& rows, int& cols) {
6070
}
6171

6272
bool Term::Private::read_raw(char* s) {
63-
// TODO: What if the keyboard is not initialzed?
73+
// TODO: What if the keyboard is not initialized?
6474
if (false) {
6575
int c = getchar();
6676
if (c >= 0) {
@@ -175,7 +185,7 @@ Term::Private::BaseTerminal::BaseTerminal(bool enable_keyboard,
175185
#else
176186
Term::Private::BaseTerminal::BaseTerminal(bool enable_keyboard,
177187
bool disable_ctrl_c)
178-
: orig_termios{std::unique_ptr<termios>(new termios)},
188+
: orig_termios{std::make_unique<termios>()},
179189
keyboard_enabled{enable_keyboard} {
180190
// Uncomment this to silently disable raw mode for non-tty
181191
// if (keyboard_enabled) keyboard_enabled = is_stdin_a_tty();
@@ -186,7 +196,7 @@ Term::Private::BaseTerminal::BaseTerminal(bool enable_keyboard,
186196

187197
// Put terminal in raw mode
188198

189-
struct termios raw = termios(*orig_termios.get());
199+
auto raw = termios(*orig_termios);
190200

191201
raw.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON);
192202

cpp-terminal/prompt.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ enum class Result_simple {
3434

3535
// A simple yes/no prompt, requires the user to press the ENTER key to continue
3636
// The arguments are used like this: 1 [2/3]4 <user Input>
37-
// the immediate switch indicates toggles wether pressing enter for
37+
// the immediate switch indicates toggles whether pressing enter for
3838
// confirming the input is required or not
3939
Result prompt(const std::string& message,
4040
const std::string& first_option,

cpp-terminal/window.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ Term::style Term::Window_24bit::get_style(size_t x, size_t y) {
2424
return m_style[(y - 1) * w + (x - 1)];
2525
}
2626

27-
size_t Term::Window_24bit::get_w() {
27+
size_t Term::Window_24bit::get_w() const {
2828
return w;
2929
}
3030

31-
size_t Term::Window_24bit::get_h() {
31+
size_t Term::Window_24bit::get_h() const {
3232
return h;
3333
}
3434

@@ -260,14 +260,14 @@ std::string Term::Window_24bit::render(int x0, int y0, bool term) {
260260
}
261261
}
262262

263-
if (current_fg_reset == false) {
263+
if (!current_fg_reset) {
264264
if (!rgb_equal(current_fg, get_fg(i, j))) {
265265
current_fg = get_fg(i, j);
266266
update_fg = true;
267267
}
268268
}
269269

270-
if (current_fg_reset == false) {
270+
if (!current_fg_reset) {
271271
if (!rgb_equal(current_bg, get_bg(i, j))) {
272272
current_bg = get_bg(i, j);
273273
update_bg = true;
@@ -277,12 +277,12 @@ std::string Term::Window_24bit::render(int x0, int y0, bool term) {
277277
current_style = get_style(i, j);
278278
update_style = true;
279279
if (current_style == style::reset) {
280-
// style::reset resets fg and bg colors too, we have to
280+
// style::reset: reset fg and bg colors too, we have to
281281
// set them again if they are non-default, but if fg or
282282
// bg colors are reset, we do not update them, as
283283
// style::reset already did that.
284-
update_fg = (current_fg_reset == false);
285-
update_bg = (current_bg_reset == false);
284+
update_fg = !current_fg_reset;
285+
update_bg = !current_bg_reset;
286286
}
287287
}
288288
// Set style first, as style::reset will reset colors too
@@ -334,11 +334,11 @@ Term::style Term::Window::get_style(size_t x, size_t y) {
334334
return m_style[(y - 1) * w + (x - 1)];
335335
}
336336

337-
size_t Term::Window::get_w() {
337+
size_t Term::Window::get_w() const {
338338
return w;
339339
}
340340

341-
size_t Term::Window::get_h() {
341+
size_t Term::Window::get_h() const {
342342
return h;
343343
}
344344

@@ -520,7 +520,7 @@ std::string Term::Window::render(int x0, int y0, bool term) {
520520
current_style = get_style(i, j);
521521
update_style = true;
522522
if (current_style == style::reset) {
523-
// style::reset resets fg and bg colors too, we have to
523+
// style::reset: resets fg and bg colors too, we have to
524524
// set them again if they are non-default, but if fg or
525525
// bg colors are reset, we do not update them, as
526526
// style::reset already did that.

cpp-terminal/window.hpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@ class Window_24bit {
5050
m_bg_reset(w * h, true),
5151
m_style(w * h, style::reset){};
5252

53-
size_t get_w();
53+
[[nodiscard]] size_t get_w() const;
5454

55-
size_t get_h();
55+
[[nodiscard]] size_t get_h() const;
5656

5757
void set_char(size_t, size_t, char32_t);
5858

@@ -84,7 +84,7 @@ class Window_24bit {
8484

8585
void clear();
8686

87-
bool rgb_equal(rgb&, rgb);
87+
static bool rgb_equal(rgb&, rgb);
8888

8989
// TODO: add Window/Screen parameter here, to be used like this:
9090
// old_scr = scr;
@@ -130,9 +130,9 @@ class Window {
130130
m_bg(w * h, bg::reset),
131131
m_style(w * h, style::reset){};
132132

133-
size_t get_w();
133+
[[nodiscard]] size_t get_w() const;
134134

135-
size_t get_h();
135+
[[nodiscard]] size_t get_h() const;
136136

137137
void set_char(size_t, size_t, char32_t);
138138

examples/keys.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@ using Term::Terminal;
1010

1111
int main() {
1212
try {
13-
Terminal term(true, true, false, false);
13+
Terminal term(true, true, true, false);
1414
int rows{}, cols{};
1515
Term::get_term_size(rows, cols);
16+
std::cout << Term::move_cursor(1, 1);
1617
std::cout << "Dimension:" << cols << " " << rows << std::endl;
1718
std::cout << "Press any key ('q' to quit):" << std::endl;
1819
bool on = true;
@@ -24,17 +25,16 @@ int main() {
2425
if (key == 'q')
2526
on = false;
2627
} else if (key >= 'A' && key <= 'Z') {
27-
s = (char)key; // Already in upper case
28-
s = "Shift+" + s;
28+
s = (std::string) "Shift+" +
29+
(char)key; // Already in upper case;
2930
} else if (key >= Key::CTRL + 'a' && key <= Key::CTRL + 'z') {
30-
s = (char)((-Key::CTRL + key) - 32); // Convert to upper case
31-
s = "Ctrl+" + s;
31+
s = (std::string) "CTRL+" + (char)((-Key::CTRL + key) - 32);
3232
} else if (key >= Key::ALT + 'a' && key <= Key::ALT + 'z') {
33-
s = (char)(key + 'A' -
33+
s = (std::string) "Alt+" +
34+
(char)(key + 'A' -
3435
(Key::ALT + 'a')); // Convert to upper case
35-
s = "Alt+" + s;
3636
} else if (key > 0 && !iscntrl(key) && key < 128) {
37-
s = std::to_string(key);
37+
s = (char)key;
3838
} else {
3939
switch (key) {
4040
case Key::BACKSPACE:

0 commit comments

Comments
 (0)