Skip to content

Commit d73549c

Browse files
csboojarjk
andauthored
Code optimization (#49)
* git: ingore meson files * fix: enum -> enum class, with explicit base type * misc: format * fix: an unsigned must be >= 0, assertion not needed * fix: enum classes, most value now strictly static cast * fix: removed unused variable * fix: properly ordered struct member initialization * fix: marked function static * fix: enum -> enum class : uint8_t * fix: initialize draw type as a ref, with vec.at(), strict type case * fix: unused var * misc: use more parentheses for math expressions * fix: [nodiscard] is a c++17 extension * misc: use more parentheses for math expressions * fix: marked structs static * misc: use more parentheses for math expressions * fix: marked functions static * fix: marked some function static * fix: comment out unused var * fix: marked functions and objects static * fix: static_cast most values * fix: enum -> enum class : uint8_t * fix: enum classes, most value now strictly static cast * fix: removed unused variable * fix: marked function static * fix: unused var * refactor: satisfy `clangd` with many `static`s * misc: down with `anonymous-namespace` and `boost` suggestions * refactor(tui)!: don't directly include `input.hpp` * misc(clangd): won't suggest adding non-sense (afaik) statics * refactor: remove non-sense `static`s * fix: use native raw-mode management instead of `stty`; conventional error msgs; use `err()` where applicable * refactor: perf < code readability * fix(input): don't make << ugly * docs(input): a comment of what were ignoring * fix(input): unnecessary cast ruins stuff * fix(unix-raw-mode): use `stty` as it's somehow more universal --------- Co-authored-by: Jeromos Kovács <[email protected]>
1 parent f8db64e commit d73549c

File tree

10 files changed

+87
-94
lines changed

10 files changed

+87
-94
lines changed

.clang-tidy

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ Checks:
22
abseil-*,
33
altera-*,
44
android-*,
5-
boost-*,
65
bugprone-*,
76
cert-*,
87
clang-*analyzer-*,
@@ -23,6 +22,7 @@ Checks:
2322
portability-*,
2423
readability-*,
2524
zircon-*,
25+
-boost-*,
2626
-llvmlibc-*,
2727
-altera-unroll-loops,
2828
-hicpp-vararg,
@@ -47,6 +47,8 @@ Checks:
4747
-readability-magic-numbers,
4848
-altera-struct-pack-align,
4949
-misc-non-private-member-variables-in-classes,
50+
-misc-use-anonymous-namespace,
51+
-misc-use-internal-linkage,
5052
-concurrency-mt-unsafe,
5153
-cert-env33-c,
5254
-cert-err58-cpp,

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
11
/build
2+
.cache
3+
compile_commands.json
4+
meson.build

examples/boxes.cpp

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "../coords.hpp"
2+
#include "../input.hpp"
23
#include "../tui.hpp"
34
#include <algorithm>
45
#include <cassert>
@@ -7,8 +8,6 @@
78
#include <thread>
89
#include <vector>
910

10-
using namespace tui::input;
11-
1211
using Box = std::pair<Coord, Coord>;
1312

1413
struct AppState {
@@ -19,9 +18,8 @@ struct AppState {
1918
} state;
2019

2120
// make `x` be good for `counter_box`
22-
[[nodiscard]]
2321
std::string count(const uint64_t& x) {
24-
unsigned r = 0;
22+
// unsigned r = 0;
2523
std::string print;
2624
if (x % 100 == 0) {
2725
print = std::to_string(x / 100);
@@ -61,11 +59,11 @@ void counter_box(Coord start, Coord end) {
6159
}
6260
}
6361

64-
enum Kind {
65-
Empty,
66-
Basic,
67-
Bold,
68-
Rounded,
62+
enum class Kind : std::uint8_t {
63+
Empty = 0,
64+
Basic = 1,
65+
Bold = 2,
66+
Rounded = 3,
6967
};
7068

7169
const std::vector<std::vector<std::string>> KINDS = {{{" ", " ", " ", " ", " ", " "}},
@@ -87,7 +85,7 @@ void draw_box(Box box, Kind with) {
8785
auto end = box.second;
8886
assert(start.row <= end.row && start.col <= end.col);
8987

90-
auto draw = KINDS[with];
88+
const auto& draw = KINDS.at(static_cast<size_t>(with));
9189

9290
// do rows
9391
for (auto row = start.row + 1; row < end.row; ++row) {
@@ -162,8 +160,8 @@ void handle_keys(std::vector<Box>& boxes, unsigned& cnt_box_ix) {
162160
void run() {
163161
const auto msg = tui::string("Szia Csongi!");
164162
auto msg_coord = [msg](bool left) {
165-
return Coord{state.size.row / 2 + 1,
166-
static_cast<unsigned int>((state.size.col / 2) + (left ? -msg.size() : +msg.size()) / 2)};
163+
return Coord{(state.size.row / 2) + 1,
164+
static_cast<unsigned int>((state.size.col / 2) + ((left ? -msg.size() : +msg.size()) / 2))};
167165
};
168166

169167
std::vector<Box> boxes = {

examples/hello_world.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1+
#include "../input.hpp"
12
#include "../tui.hpp"
23
#include <utility> // for std::pair
34

4-
using namespace tui::input;
5-
65
int main() {
76
tui::init(false); // alternate buffer, raw mode, no cursor
87
auto screen_size = tui::screen::size(); // initially set
@@ -14,11 +13,11 @@ int main() {
1413
while (input != SpecKey::CtrlC && input != 'q') { // main loop
1514
// NOTE: use `.size()` before styling, as it'd be completely crazy after applying styles
1615
// `msg` will be in the middle of the screen
17-
tui::cursor::set_position(screen_size.first / 2 - 1, screen_size.second / 2 - (msg.size() / 2));
16+
tui::cursor::set_position((screen_size.first / 2) - 1, (screen_size.second / 2) - (msg.size() / 2));
1817
std::cout << msg.blue().link("https://github.com/csboo/cpptui").bold().underline();
1918

2019
// `msg` will be in the middle of the screen
21-
tui::cursor::set_position(screen_size.first / 2 + 1, screen_size.second / 2 - (note.size() / 2));
20+
tui::cursor::set_position((screen_size.first / 2) + 1, (screen_size.second / 2) - (note.size() / 2));
2221
std::cout << note.on_rgb(106, 150, 137).rgb(148, 105, 117).italic().dim();
2322

2423
std::cout.flush();

examples/read_event.cpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
1+
#include "../input.hpp"
12
#include "../tui.hpp"
23
#include <chrono>
34
#include <condition_variable>
45
#include <iostream>
56
#include <mutex>
67
#include <thread>
78

8-
using tui::input::Input;
9-
using tui::input::SpecKey;
10-
11-
std::mutex mtx;
12-
std::condition_variable cv;
13-
Input shared_input;
14-
bool input_available = false;
9+
static std::mutex mtx;
10+
static std::condition_variable cv;
11+
static Input shared_input;
12+
static bool input_available = false;
1513

1614
bool should_quit(const Input& input) { return input == SpecKey::CtrlC || input == 'q'; }
1715

examples/read_input.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#include "../tui.hpp"
2-
3-
using namespace tui::input;
2+
#include "../input.hpp"
43

54
// get's called on terminall resize
65
void clear(int /*sig*/) {

examples/screen-filler.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
#include "../coords.hpp"
2+
#include "../input.hpp"
23
#include "../tui.hpp"
34
#include <thread>
45

5-
using namespace tui::input;
6-
76
struct State {
87
Input input;
98
bool quit = false;
@@ -47,8 +46,8 @@ void run() {
4746
auto mod_mid_ver = screen_size.row % 2;
4847
auto even_mid_ver = mod_mid_ver == 0;
4948

50-
auto mid_hor = screen_size.col / 2 + mod_mid_hor;
51-
auto mid_ver = screen_size.row / 2 + mod_mid_ver;
49+
auto mid_hor = (screen_size.col / 2) + mod_mid_hor;
50+
auto mid_ver = (screen_size.row / 2) + mod_mid_ver;
5251
auto mid_mid = Coord(mid_ver, mid_hor);
5352

5453
TOP_LEFT.print(CH.on_black());

examples/snake.cpp

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,30 @@
11
#include "../coords.hpp"
2+
#include "../input.hpp"
23
#include "../tui.hpp"
34
#include <algorithm>
45
#include <chrono>
6+
#include <cstddef>
7+
#include <cstdint>
58
#include <iostream>
6-
#include <limits>
9+
// #include <limits> // needed if MAX_MS is used
710
#include <random>
811
#include <string>
912
#include <thread>
1013
#include <vector>
1114

12-
using namespace tui::input;
13-
1415
// this is how the apple/food will be displayed
1516
const tui::string APPLE_TEXT = tui::string('@').red().bold();
1617
// where the score count will be printed
1718
const Coord SCORE_COUNT = Coord{2, 4};
1819
// this is the default duration a frame lives for in ms, it's 23.8 fps
1920
const std::chrono::milliseconds SLEEP_MS = std::chrono::milliseconds(42);
2021
const std::chrono::milliseconds ADD_MS = std::chrono::milliseconds(1);
21-
const std::chrono::milliseconds MAX_MS = std::chrono::milliseconds(std::numeric_limits<unsigned>::infinity());
22+
// const std::chrono::milliseconds MAX_MS = std::chrono::milliseconds(std::numeric_limits<unsigned>::infinity());
2223
// initial size/lenght of the snake: at the game start
2324
const unsigned INIT_LEN = 5;
2425

2526
// direction
26-
enum Dir {
27+
enum class Dir : std::uint8_t {
2728
Up = 0,
2829
Down,
2930
Left,
@@ -100,16 +101,16 @@ Dir meets_at(const Coord& lhs, const Coord& rhs, const Coord& screen_size) {
100101
// we set both row and col to x-1 as it's needed :D
101102
auto teleport = Coord{screen_size.row - 1, screen_size.col - 1};
102103

103-
if (row_diff == 1 || teleport.row == -row_diff) {
104+
if (row_diff == 1 || static_cast<int>(teleport.row) == -row_diff) {
104105
return Dir::Up;
105106
}
106-
if (row_diff == -1 || teleport.row == row_diff) {
107+
if (row_diff == -1 || static_cast<int>(teleport.row) == row_diff) {
107108
return Dir::Down;
108109
}
109-
if (col_diff == 1 || teleport.col == -col_diff) {
110+
if (col_diff == 1 || static_cast<int>(teleport.col) == -col_diff) {
110111
return Dir::Left;
111112
}
112-
if (col_diff == -1 || teleport.col == col_diff) {
113+
if (col_diff == -1 || static_cast<int>(teleport.col) == col_diff) {
113114
return Dir::Right;
114115
}
115116
return Dir::None;
@@ -157,7 +158,7 @@ struct App {
157158
static Snake default_snake() {
158159
auto mid = Coord::screen_size() / 2;
159160
Snake snake;
160-
for (auto i = 0; i < INIT_LEN; ++i) {
161+
for (auto i = 0; i < static_cast<int>(INIT_LEN); ++i) {
161162
snake.push_back(mid.with_col(mid.col - i));
162163
}
163164
return snake;
@@ -259,7 +260,7 @@ struct App {
259260
// delete the last one off the screen by overwriting it with a space
260261
this->snake.back().print(' ');
261262
auto old_snake = this->snake;
262-
for (auto i = 1; i < this->snake.size(); ++i) {
263+
for (size_t i = 1; i < this->snake.size(); ++i) {
263264
this->snake.at(i) = old_snake.at(i - 1);
264265
}
265266

input.hpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,9 @@ struct Input {
105105
SpecKey special = SpecKey::None;
106106

107107
Input() = default;
108-
Input(const Arrow& arrow) : arrow(arrow), is_arrow(true) {}
109-
Input(const char& ch) : ch(ch), is_ch(true) {}
110-
Input(const SpecKey& special) : special(special), is_special(true) {}
108+
Input(const Arrow& arrow) : is_arrow(true), arrow(arrow) {}
109+
Input(const char& ch) : is_ch(true), ch(ch) {}
110+
Input(const SpecKey& special) : is_special(true), special(special) {}
111111

112112
bool operator==(const Input& other) const {
113113
return (this->ch == other.ch && this->is_ch == other.is_ch && this->arrow == other.arrow &&
@@ -138,7 +138,6 @@ struct Input {
138138
static Input read_helper(reader_fn get_char) {
139139
char byte = get_char();
140140

141-
char ignore_byte = 0;
142141
auto input = Input(SpecKey::None);
143142
#ifdef _WIN32
144143
if (byte == 0 || byte == 224 || byte == -32) {
@@ -178,7 +177,7 @@ struct Input {
178177
}
179178
#else
180179
if (byte < 0) {
181-
ignore_byte = get_char();
180+
get_char(); // ignore
182181
}
183182
#endif
184183
if (byte >= 32 && byte <= 126) { // <char>
@@ -218,7 +217,7 @@ struct Input {
218217
case SpecKey::Delete:
219218
case SpecKey::PageUp:
220219
case SpecKey::PageDown:
221-
ignore_byte = get_char(); // ~
220+
get_char(); // ignore '~'
222221
input = Input(static_cast<SpecKey>(special));
223222
break;
224223
default:

0 commit comments

Comments
 (0)