Skip to content

Commit

Permalink
moved to std versions of size_t, uint8_t, uint16_t
Browse files Browse the repository at this point in the history
  • Loading branch information
MCWertGaming committed Sep 16, 2022
1 parent 53251ac commit b21c778
Show file tree
Hide file tree
Showing 11 changed files with 200 additions and 181 deletions.
52 changes: 26 additions & 26 deletions cpp-terminal/base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,15 @@ Term::RGB Term::bit4_to_rgb(const Term::Color4 color) {
return {};
}

Term::RGB Term::bit24_to_rgb(uint8_t r, uint8_t g, uint8_t b) {
Term::RGB Term::bit24_to_rgb(std::uint8_t r, std::uint8_t g, std::uint8_t b) {
return {r, g, b, false};
}
Term::RGB Term::rgb_empty() {
return {0, 0, 0, true};
}

uint16_t Term::rgb_compare(RGB color_first, RGB color_second) {
uint16_t diff = 0;
std::uint16_t diff = 0;
// red
if (color_first.r > color_second.r) {
diff += color_first.r - color_second.r;
Expand Down Expand Up @@ -86,7 +86,7 @@ Term::Color4 Term::rgb_to_bit4(Term::RGB color) {

// set initial start value
Color4 color_result = Color4::BLACK;
uint16_t diff = rgb_compare(color, Term::Bit4_reference::BLACK);
std::uint16_t diff = rgb_compare(color, Term::Bit4_reference::BLACK);

// compare all colors
if (diff > rgb_compare(color, Bit4_reference::RED)) {
Expand Down Expand Up @@ -151,7 +151,7 @@ Term::Color4 Term::rgb_to_bit4(Term::RGB color) {
return color_result;
}

uint8_t Term::rgb_to_bit8(RGB color) {
std::uint8_t Term::rgb_to_bit8(RGB color) {
if (color.empty) {
return 0; // there is not really a way to handle empty color here
}
Expand Down Expand Up @@ -226,13 +226,13 @@ std::string Term::rgb_to_bit24_auto_bg(RGB color) {
/* FOREGROUND COLORS */

std::string Term::color_fg(Term::Color4 color) {
return "\033[" + std::to_string((uint8_t)color + 30) + 'm';
return "\033[" + std::to_string((std::uint8_t)color + 30) + 'm';
}

std::string Term::color_fg(uint8_t color) {
std::string Term::color_fg(std::uint8_t color) {
return "\033[38;5;" + std::to_string(color) + 'm';
}
std::string Term::color_fg(uint8_t r, uint8_t g, uint8_t b) {
std::string Term::color_fg(std::uint8_t r, std::uint8_t g, std::uint8_t b) {
return "\033[38;2;" + std::to_string(r) + ';' + std::to_string(g) + ';' +
std::to_string(b) + 'm';
}
Expand Down Expand Up @@ -271,10 +271,10 @@ std::string Term::color_fg(Term::RGBF rgbf, Mode mode) {
std::string Term::color_bg(Term::Color4 color) {
return "\033[" + std::to_string((uint8_t)color + 40) + 'm';
}
std::string Term::color_bg(uint8_t color) {
std::string Term::color_bg(std::uint8_t color) {
return "\033[48;5;" + std::to_string(color) + 'm';
}
std::string Term::color_bg(uint8_t r, uint8_t g, uint8_t b) {
std::string Term::color_bg(std::uint8_t r, std::uint8_t g, std::uint8_t b) {
return "\033[48;2;" + std::to_string(r) + ';' + std::to_string(g) + ';' +
std::to_string(b) + 'm';
}
Expand Down Expand Up @@ -306,7 +306,7 @@ std::string Term::color_bg(Term::RGBF rgbf, Mode mode) {
return {};
}
std::string Term::style(Term::Style style) {
return "\033[" + std::to_string((uint8_t)style) + 'm';
return "\033[" + std::to_string((std::uint8_t)style) + 'm';
}

/* RGBF FUNCTIONS */
Expand All @@ -322,7 +322,7 @@ Term::RGBF Term::rgbf_fg(Term::RGB rgb) {
return {rgb, Mode::BIT24, rgb_empty(), Mode::NONE};
}

Term::RGBF Term::rgbf_fg(uint8_t r, uint8_t g, uint8_t b) {
Term::RGBF Term::rgbf_fg(std::uint8_t r, std::uint8_t g, std::uint8_t b) {
return {{r, g, b}, Mode::BIT24, rgb_empty(), Mode::NONE};
}

Expand All @@ -332,7 +332,7 @@ Term::RGBF Term::rgbf_bg(Term::Color4 color) {
Term::RGBF Term::rgbf_bg(Term::RGB rgb) {
return {rgb_empty(), Mode::NONE, rgb, Mode::BIT24};
}
Term::RGBF Term::rgbf_bg(uint8_t r, uint8_t g, uint8_t b) {
Term::RGBF Term::rgbf_bg(std::uint8_t r, std::uint8_t g, std::uint8_t b) {
return {rgb_empty(), Mode::NONE, {r, g, b}, Mode::BIT24};
}

Expand All @@ -343,12 +343,12 @@ Term::RGBF Term::rgbf_fb(Term::Color4 fg, Term::Color4 bg) {
Term::RGBF Term::rgbf_fb(Term::RGB rgb_fg, Term::RGB rgb_bg) {
return {rgb_fg, Mode::BIT24, rgb_bg, Mode::BIT24};
}
Term::RGBF Term::rgbf_fb(uint8_t r_fg,
uint8_t g_fg,
uint8_t b_fg,
uint8_t r_bg,
uint8_t g_bg,
uint8_t b_bg) {
Term::RGBF Term::rgbf_fb(std::uint8_t r_fg,
std::uint8_t g_fg,
std::uint8_t b_fg,
std::uint8_t r_bg,
std::uint8_t g_bg,
std::uint8_t b_bg) {
return {{r_fg, g_fg, b_fg}, Mode::BIT24, {r_bg, g_bg, b_bg}, Mode::BIT24};
}

Expand All @@ -372,7 +372,7 @@ std::string Term::color_auto(Term::RGBF rgbf) {
return "";
}

std::tuple<size_t, size_t> Term::get_size() {
std::tuple<std::size_t, std::size_t> Term::get_size() {
return Private::get_term_size(); // function uses platform dependent code
}

Expand All @@ -399,27 +399,27 @@ std::string Term::clear_buffer() {
return "\033[3J";
}

std::string Term::cursor_move(size_t row, size_t column) {
std::string Term::cursor_move(std::size_t row, std::size_t column) {
return "\033[" + std::to_string(row) + ';' + std::to_string(column) + 'H';
}

std::string Term::cursor_up(size_t rows) {
std::string Term::cursor_up(std::size_t rows) {
return "\033[" + std::to_string(rows) + 'A';
}

std::string Term::cursor_down(size_t rows) {
std::string Term::cursor_down(std::size_t rows) {
return "\033[" + std::to_string(rows) + 'B';
}

std::string Term::cursor_right(size_t columns) {
std::string Term::cursor_right(std::size_t columns) {
return "\033[" + std::to_string(columns) + 'C';
}

std::string Term::cursor_left(size_t columns) {
std::string Term::cursor_left(std::size_t columns) {
return "\033[" + std::to_string(columns) + 'D';
}

std::tuple<size_t, size_t> Term::cursor_position() {
std::tuple<std::size_t, std::size_t> Term::cursor_position() {
char buf[32];
// write cursor position report
std::cout << cursor_position_report() << std::flush;
Expand All @@ -439,7 +439,7 @@ std::tuple<size_t, size_t> Term::cursor_position() {
}
// Find the result in the response, drop the rest:
for (unsigned int i = 0; i < sizeof(buf) - 6; i++) {
size_t rows, columns;
std::size_t rows, columns;
if (buf[i] == '\x1b' && buf[i + 1] == '[') {
if (Private::unified_sscanf(&buf[i + 2], "%d;%d", &rows,
&columns) != 2) {
Expand Down
53 changes: 26 additions & 27 deletions cpp-terminal/base.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#pragma once

#include <cstdint>
#include <string>
#include "cpp-terminal/private/platform.hpp"
namespace Term {
Expand All @@ -9,7 +8,7 @@ namespace Term {
* get the foreground color: Color4 + 30, Background color: Color4 + 40
* See https://en.wikipedia.org/wiki/ANSI_escape_code#3-bit_and_4-bit
*/
enum class Color4 : uint8_t {
enum class Color4 : std::uint8_t {
// FG: 30, BG: 40
BLACK = 0,
// FG: 31, BG: 41
Expand Down Expand Up @@ -48,7 +47,7 @@ enum class Color4 : uint8_t {
/*
* Styles for text in the terminal
*/
enum class Style : uint8_t {
enum class Style : std::uint8_t {
// resets all attributes (styles and colors)
RESET = 0,
// Thick text font
Expand All @@ -71,7 +70,7 @@ enum class Style : uint8_t {

// Represents a RGB (24bit) color
struct RGB {
uint8_t r{}, g{}, b{};
std::uint8_t r{}, g{}, b{};
bool empty = false;
};
// indicates the color mode (basically the original color resolution)
Expand Down Expand Up @@ -121,14 +120,14 @@ struct Bit4_reference {
// Converts a 4bit color to Term::RGB
RGB bit4_to_rgb(Color4 color);
// Converts a 8bit color to Term::RGB
RGB bit8_to_rgb(uint8_t color);
RGB bit8_to_rgb(std::uint8_t color);
// converts rgb values into Term::RGB
RGB bit24_to_rgb(uint8_t r, uint8_t g, uint8_t b);
RGB bit24_to_rgb(std::uint8_t r, std::uint8_t g, std::uint8_t b);
// creates an empty rgb color
RGB rgb_empty();

// compares two Term::RGB colors and returns how much they are different
uint16_t rgb_compare(RGB rgb_first, RGB rgb_second);
std::uint16_t rgb_compare(RGB rgb_first, RGB rgb_second);

// Converts an RGB color to a 4bit color
Color4 rgb_to_bit4(RGB rgb);
Expand All @@ -145,9 +144,9 @@ std::string rgb_to_bit24_auto_bg(RGB color);
// Set the given 4bit color from Term::Color4
std::string color_fg(Color4 color);
// Set the given 4bit / 8bit color
std::string color_fg(uint8_t color);
std::string color_fg(std::uint8_t color);
// Set the given 24bit color
std::string color_fg(uint8_t r, uint8_t g, uint8_t b);
std::string color_fg(std::uint8_t r, std::uint8_t g, std::uint8_t b);
// Set the given RGB (24bit) color
std::string color_fg(RGB rgb);
// Set the given foreground color from the RGBF struct
Expand All @@ -158,9 +157,9 @@ std::string color_fg(RGBF rgbf, Mode mode);
// Set the given 4bit color from Term::Color4
std::string color_bg(Color4 color);
// Set the given 4bit / 8bit color
std::string color_bg(uint8_t color);
std::string color_bg(std::uint8_t color);
// Set the given 24bit color
std::string color_bg(uint8_t r, uint8_t g, uint8_t b);
std::string color_bg(std::uint8_t r, std::uint8_t g, std::uint8_t b);
// Set the given RGB (24bit) color
std::string color_bg(RGB rgb);
// Set the given background color from the RGBF struct
Expand All @@ -176,26 +175,26 @@ std::string colorf(RGBF rgbf);
// Create a Term::RGBF color using a 4bit foreground color
RGBF rgbf_fg(Color4 color);
// Create a Term::RGBF color using a 24bit (RGB) foreground color
RGBF rgbf_fg(uint8_t r, uint8_t g, uint8_t b);
RGBF rgbf_fg(std::uint8_t r, std::uint8_t g, std::uint8_t b);
// Create a Term::RGBF color using a Term::RGB foreground color
RGBF rgbf_fg(RGB rgb);

// Create a Term::RGBF color using a 4bit background color
RGBF rgbf_bg(Color4 color);
// Create a Term::RGBF color using a 24bit (RGB) background color
RGBF rgbf_bg(uint8_t r, uint8_t g, uint8_t b);
RGBF rgbf_bg(std::uint8_t r, std::uint8_t g, std::uint8_t b);
// Create a Term::RGBF color using a Term::RGB background color
RGBF rgbf_bg(RGB rgb);

// Create a Term::RGBF color using a 4bit foreground and background color
RGBF rgbf_fb(Color4 fg, Color4 bg);
// Create a Term::RGBF color using a 24bit (RGB) fore- and background color
RGBF rgbf_fb(uint8_t r_fg,
uint8_t g_fg,
uint8_t b_fg,
uint8_t r_bg,
uint8_t g_bg,
uint8_t b_bg);
RGBF rgbf_fb(std::uint8_t r_fg,
std::uint8_t g_fg,
std::uint8_t b_fg,
std::uint8_t r_bg,
std::uint8_t g_bg,
std::uint8_t b_bg);
// Create a Term::RGBF color using a Term::RGB fore- and background color
RGBF rgbf_fb(RGB rgb_fg, RGB rgb_bg);

Expand All @@ -208,7 +207,7 @@ std::string color_auto(RGBF rgbf);
std::string color_auto(RGBF rgbf, Mode mode);

// get the terminal size (row, column) / (Y, X)
std::tuple<size_t, size_t> get_size();
std::tuple<std::size_t, std::size_t> get_size();
// check if stdin is connected to a TTY
bool stdin_connected();
// check if stdout is connected to a TTY
Expand All @@ -222,17 +221,17 @@ std::string clear_screen();
// clear the screen and the scroll-back buffer
std::string clear_buffer();
// move the cursor to the given (row, column) / (Y, X)
std::string cursor_move(size_t row, size_t column);
std::string cursor_move(std::size_t row, std::size_t column);
// move the cursor the given rows up
std::string cursor_up(size_t rows);
std::string cursor_up(std::size_t rows);
// move the cursor the given rows down
std::string cursor_down(size_t rows);
std::string cursor_down(std::size_t rows);
// move the cursor the given columns left
std::string cursor_left(size_t columns);
std::string cursor_left(std::size_t columns);
// move the cursor the given columns right
std::string cursor_right(size_t columns);
std::string cursor_right(std::size_t columns);
// returns the current cursor position (row, column) (Y, X)
std::tuple<size_t, size_t> cursor_position();
std::tuple<std::size_t, std::size_t> cursor_position();
// the ANSI code to generate a cursor position report
std::string cursor_position_report();
// clears the screen from the current cursor position to the end of the screen
Expand Down Expand Up @@ -260,4 +259,4 @@ class Terminal : public Private::BaseTerminal {

~Terminal() override;
};
} // namespace Term
} // namespace Term
30 changes: 16 additions & 14 deletions cpp-terminal/private/conversion.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#pragma once

#include <cstdint>
#include <stdexcept>
#include <string>
#include <vector>
Expand All @@ -11,28 +10,30 @@
#include <sys/ioctl.h>
#endif

static constexpr uint8_t UTF8_ACCEPT = 0;
static constexpr uint8_t UTF8_REJECT = 0xf;
static constexpr std::uint8_t UTF8_ACCEPT = 0;
static constexpr std::uint8_t UTF8_REJECT = 0xf;

namespace Term::Private {

inline uint8_t utf8_decode_step(uint8_t state, uint8_t octet, uint32_t* cpp) {
static const uint32_t utf8ClassTab[0x10] = {
inline std::uint8_t utf8_decode_step(std::uint8_t state,
std::uint8_t octet,
std::uint32_t* cpp) {
static const std::uint32_t utf8ClassTab[0x10] = {
0x88888888UL, 0x88888888UL, 0x99999999UL, 0x99999999UL,
0xaaaaaaaaUL, 0xaaaaaaaaUL, 0xaaaaaaaaUL, 0xaaaaaaaaUL,
0x222222ffUL, 0x22222222UL, 0x22222222UL, 0x22222222UL,
0x3333333bUL, 0x33433333UL, 0xfff5666cUL, 0xffffffffUL,
};

static const uint32_t utf8StateTab[0x10] = {
static const std::uint32_t utf8StateTab[0x10] = {
0xfffffff0UL, 0xffffffffUL, 0xfffffff1UL, 0xfffffff3UL,
0xfffffff4UL, 0xfffffff7UL, 0xfffffff6UL, 0xffffffffUL,
0x33f11f0fUL, 0xf3311f0fUL, 0xf33f110fUL, 0xfffffff2UL,
0xfffffff5UL, 0xffffffffUL, 0xffffffffUL, 0xffffffffUL,
};

const uint8_t reject = (state >> 3), nonAscii = (octet >> 7);
const uint8_t class_ =
const std::uint8_t reject = (state >> 3), nonAscii = (octet >> 7);
const std::uint8_t class_ =
(!nonAscii
? 0
: (0xf & (utf8ClassTab[(octet >> 3) & 0xf] >> (4 * (octet & 7)))));
Expand Down Expand Up @@ -71,8 +72,8 @@ inline void codepoint_to_utf8(std::string& s, char32_t c) {
}

inline std::u32string utf8_to_utf32(const std::string& s) {
uint32_t codepoint{};
uint8_t state = UTF8_ACCEPT;
std::uint32_t codepoint{};
std::uint8_t state = UTF8_ACCEPT;
std::u32string r{};
for (char i : s) {
state = utf8_decode_step(state, i, &codepoint);
Expand All @@ -99,8 +100,8 @@ inline std::string utf32_to_utf8(const std::u32string& s) {
// coverts a string into an integer
inline int unified_sscanf(const char* string,
const char* format,
size_t* rows,
size_t* cols) {
std::size_t* rows,
std::size_t* cols) {
#ifdef _WIN32
// on windows it's recommended to use their own sscanf_s function
return sscanf_s(string, format, rows, cols);
Expand All @@ -110,8 +111,9 @@ inline int unified_sscanf(const char* string,
#endif
}

inline std::tuple<size_t, size_t> convert_string_to_size_t(const char* string,
const char* format) {
inline std::tuple<std::size_t, std::size_t> convert_string_to_size_t(
const char* string,
const char* format) {
size_t rows{}, cols{};
#ifdef _WIN32
// Windows provides its own alternative to sscanf()
Expand Down
Loading

0 comments on commit b21c778

Please sign in to comment.