Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
31911d0
git: ingore meson files
csboo Mar 18, 2025
8e43db2
fix: enum -> enum class, with explicit base type
csboo Mar 18, 2025
0e45918
misc: format
csboo Mar 18, 2025
427fc42
fix: an unsigned must be >= 0, assertion not needed
csboo Mar 18, 2025
5353a7d
fix: enum classes, most value now strictly static cast
csboo Mar 18, 2025
0d17058
fix: removed unused variable
csboo Mar 18, 2025
7ebbfef
fix: properly ordered struct member initialization
csboo Mar 18, 2025
1268e7a
fix: marked function static
csboo Mar 18, 2025
286f020
fix: enum -> enum class : uint8_t
csboo Mar 18, 2025
b88bfb6
fix: initialize draw type as a ref, with vec.at(), strict type case
csboo Mar 18, 2025
29a6a6b
fix: unused var
csboo Mar 18, 2025
c7e5814
misc: use more parentheses for math expressions
csboo Mar 18, 2025
4eb064d
fix: [nodiscard] is a c++17 extension
csboo Mar 18, 2025
81b0cac
misc: use more parentheses for math expressions
csboo Mar 18, 2025
dbfcd51
fix: marked structs static
csboo Mar 18, 2025
962ae57
misc: use more parentheses for math expressions
csboo Mar 18, 2025
e00f9de
fix: marked functions static
csboo Mar 18, 2025
a80aa83
fix: marked some function static
csboo Mar 18, 2025
9dbef13
fix: comment out unused var
csboo Mar 18, 2025
3e50201
fix: marked functions and objects static
csboo Mar 18, 2025
3317a82
fix: static_cast most values
csboo Mar 18, 2025
ce8be9e
fix: enum -> enum class : uint8_t
csboo Mar 18, 2025
a1a8a5f
fix: enum classes, most value now strictly static cast
csboo Mar 18, 2025
f0db907
fix: removed unused variable
csboo Mar 18, 2025
e135b0c
fix: marked function static
csboo Mar 18, 2025
af6dac4
fix: unused var
csboo Mar 18, 2025
da60c63
refactor: satisfy `clangd` with many `static`s
jarjk Mar 19, 2025
79084c2
misc: down with `anonymous-namespace` and `boost` suggestions
jarjk Mar 19, 2025
b1e3ff3
refactor(tui)!: don't directly include `input.hpp`
jarjk Mar 19, 2025
f918610
misc(clangd): won't suggest adding non-sense (afaik) statics
jarjk May 6, 2025
3e0daa9
refactor: remove non-sense `static`s
jarjk May 6, 2025
07f75dd
fix: use native raw-mode management instead of `stty`; conventional e…
jarjk May 6, 2025
6cb343f
refactor: perf < code readability
jarjk May 6, 2025
73082cb
fix(input): don't make << ugly
jarjk May 6, 2025
211c4d2
docs(input): a comment of what were ignoring
jarjk May 6, 2025
b772597
fix(input): unnecessary cast ruins stuff
jarjk May 6, 2025
a1cdb62
fix(unix-raw-mode): use `stty` as it's somehow more universal
jarjk May 6, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .clang-tidy
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ Checks:
abseil-*,
altera-*,
android-*,
boost-*,
bugprone-*,
cert-*,
clang-*analyzer-*,
Expand All @@ -23,6 +22,7 @@ Checks:
portability-*,
readability-*,
zircon-*,
-boost-*,
-llvmlibc-*,
-altera-unroll-loops,
-hicpp-vararg,
Expand All @@ -47,6 +47,8 @@ Checks:
-readability-magic-numbers,
-altera-struct-pack-align,
-misc-non-private-member-variables-in-classes,
-misc-use-anonymous-namespace,
-misc-use-internal-linkage,
-concurrency-mt-unsafe,
-cert-env33-c,
-cert-err58-cpp,
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
/build
.cache
compile_commands.json
meson.build
22 changes: 10 additions & 12 deletions examples/boxes.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "../coords.hpp"
#include "../input.hpp"
#include "../tui.hpp"
#include <algorithm>
#include <cassert>
Expand All @@ -7,8 +8,6 @@
#include <thread>
#include <vector>

using namespace tui::input;

using Box = std::pair<Coord, Coord>;

struct AppState {
Expand All @@ -19,9 +18,8 @@ struct AppState {
} state;

// make `x` be good for `counter_box`
[[nodiscard]]
std::string count(const uint64_t& x) {
unsigned r = 0;
// unsigned r = 0;
std::string print;
if (x % 100 == 0) {
print = std::to_string(x / 100);
Expand Down Expand Up @@ -61,11 +59,11 @@ void counter_box(Coord start, Coord end) {
}
}

enum Kind {
Empty,
Basic,
Bold,
Rounded,
enum class Kind : std::uint8_t {
Empty = 0,
Basic = 1,
Bold = 2,
Rounded = 3,
};

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

auto draw = KINDS[with];
const auto& draw = KINDS.at(static_cast<size_t>(with));

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

std::vector<Box> boxes = {
Expand Down
7 changes: 3 additions & 4 deletions examples/hello_world.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#include "../input.hpp"
#include "../tui.hpp"
#include <utility> // for std::pair

using namespace tui::input;

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

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

std::cout.flush();
Expand Down
12 changes: 5 additions & 7 deletions examples/read_event.cpp
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
#include "../input.hpp"
#include "../tui.hpp"
#include <chrono>
#include <condition_variable>
#include <iostream>
#include <mutex>
#include <thread>

using tui::input::Input;
using tui::input::SpecKey;

std::mutex mtx;
std::condition_variable cv;
Input shared_input;
bool input_available = false;
static std::mutex mtx;
static std::condition_variable cv;
static Input shared_input;
static bool input_available = false;

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

Expand Down
3 changes: 1 addition & 2 deletions examples/read_input.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include "../tui.hpp"

using namespace tui::input;
#include "../input.hpp"

// get's called on terminall resize
void clear(int /*sig*/) {
Expand Down
7 changes: 3 additions & 4 deletions examples/screen-filler.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
#include "../coords.hpp"
#include "../input.hpp"
#include "../tui.hpp"
#include <thread>

using namespace tui::input;

struct State {
Input input;
bool quit = false;
Expand Down Expand Up @@ -47,8 +46,8 @@ void run() {
auto mod_mid_ver = screen_size.row % 2;
auto even_mid_ver = mod_mid_ver == 0;

auto mid_hor = screen_size.col / 2 + mod_mid_hor;
auto mid_ver = screen_size.row / 2 + mod_mid_ver;
auto mid_hor = (screen_size.col / 2) + mod_mid_hor;
auto mid_ver = (screen_size.row / 2) + mod_mid_ver;
auto mid_mid = Coord(mid_ver, mid_hor);

TOP_LEFT.print(CH.on_black());
Expand Down
23 changes: 12 additions & 11 deletions examples/snake.cpp
Original file line number Diff line number Diff line change
@@ -1,29 +1,30 @@
#include "../coords.hpp"
#include "../input.hpp"
#include "../tui.hpp"
#include <algorithm>
#include <chrono>
#include <cstddef>
#include <cstdint>
#include <iostream>
#include <limits>
// #include <limits> // needed if MAX_MS is used
#include <random>
#include <string>
#include <thread>
#include <vector>

using namespace tui::input;

// this is how the apple/food will be displayed
const tui::string APPLE_TEXT = tui::string('@').red().bold();
// where the score count will be printed
const Coord SCORE_COUNT = Coord{2, 4};
// this is the default duration a frame lives for in ms, it's 23.8 fps
const std::chrono::milliseconds SLEEP_MS = std::chrono::milliseconds(42);
const std::chrono::milliseconds ADD_MS = std::chrono::milliseconds(1);
const std::chrono::milliseconds MAX_MS = std::chrono::milliseconds(std::numeric_limits<unsigned>::infinity());
// const std::chrono::milliseconds MAX_MS = std::chrono::milliseconds(std::numeric_limits<unsigned>::infinity());
// initial size/lenght of the snake: at the game start
const unsigned INIT_LEN = 5;

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

if (row_diff == 1 || teleport.row == -row_diff) {
if (row_diff == 1 || static_cast<int>(teleport.row) == -row_diff) {
return Dir::Up;
}
if (row_diff == -1 || teleport.row == row_diff) {
if (row_diff == -1 || static_cast<int>(teleport.row) == row_diff) {
return Dir::Down;
}
if (col_diff == 1 || teleport.col == -col_diff) {
if (col_diff == 1 || static_cast<int>(teleport.col) == -col_diff) {
return Dir::Left;
}
if (col_diff == -1 || teleport.col == col_diff) {
if (col_diff == -1 || static_cast<int>(teleport.col) == col_diff) {
return Dir::Right;
}
return Dir::None;
Expand Down Expand Up @@ -157,7 +158,7 @@ struct App {
static Snake default_snake() {
auto mid = Coord::screen_size() / 2;
Snake snake;
for (auto i = 0; i < INIT_LEN; ++i) {
for (auto i = 0; i < static_cast<int>(INIT_LEN); ++i) {
snake.push_back(mid.with_col(mid.col - i));
}
return snake;
Expand Down Expand Up @@ -259,7 +260,7 @@ struct App {
// delete the last one off the screen by overwriting it with a space
this->snake.back().print(' ');
auto old_snake = this->snake;
for (auto i = 1; i < this->snake.size(); ++i) {
for (size_t i = 1; i < this->snake.size(); ++i) {
this->snake.at(i) = old_snake.at(i - 1);
}

Expand Down
11 changes: 5 additions & 6 deletions input.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,9 @@ struct Input {
SpecKey special = SpecKey::None;

Input() = default;
Input(const Arrow& arrow) : arrow(arrow), is_arrow(true) {}
Input(const char& ch) : ch(ch), is_ch(true) {}
Input(const SpecKey& special) : special(special), is_special(true) {}
Input(const Arrow& arrow) : is_arrow(true), arrow(arrow) {}
Input(const char& ch) : is_ch(true), ch(ch) {}
Input(const SpecKey& special) : is_special(true), special(special) {}

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

char ignore_byte = 0;
auto input = Input(SpecKey::None);
#ifdef _WIN32
if (byte == 0 || byte == 224 || byte == -32) {
Expand Down Expand Up @@ -178,7 +177,7 @@ struct Input {
}
#else
if (byte < 0) {
ignore_byte = get_char();
get_char(); // ignore
}
#endif
if (byte >= 32 && byte <= 126) { // <char>
Expand Down Expand Up @@ -218,7 +217,7 @@ struct Input {
case SpecKey::Delete:
case SpecKey::PageUp:
case SpecKey::PageDown:
ignore_byte = get_char(); // ~
get_char(); // ignore '~'
input = Input(static_cast<SpecKey>(special));
break;
default:
Expand Down
Loading