From d9d6bb3a8ce7c13924217a7301a3a79b868221fc Mon Sep 17 00:00:00 2001 From: Kbz-8 Date: Sat, 20 Jan 2024 19:22:18 +0100 Subject: [PATCH] adding ansi color codes --- Makefile | 3 +- includes/ansi.hpp | 52 +++++++++++++++++++ includes/unstd/{array.h => array.hpp} | 4 +- .../bits/{ref_counter.h => ref_counter.hpp} | 4 +- .../unstd/{shared_ptr.h => shared_ptr.hpp} | 6 +-- includes/unstd/string.hpp | 29 +++++++++++ .../unstd/{unique_ptr.h => unique_ptr.hpp} | 4 +- includes/unstd/{weak_ptr.h => weak_ptr.hpp} | 10 ++-- srcs/main.cpp | 10 ++-- srcs/utils/ansi.cpp | 19 +++++++ 10 files changed, 122 insertions(+), 19 deletions(-) create mode 100644 includes/ansi.hpp rename includes/unstd/{array.h => array.hpp} (93%) rename includes/unstd/bits/{ref_counter.h => ref_counter.hpp} (88%) rename includes/unstd/{shared_ptr.h => shared_ptr.hpp} (90%) create mode 100644 includes/unstd/string.hpp rename includes/unstd/{unique_ptr.h => unique_ptr.hpp} (91%) rename includes/unstd/{weak_ptr.h => weak_ptr.hpp} (84%) create mode 100644 srcs/utils/ansi.cpp diff --git a/Makefile b/Makefile index ac776b4..6bfe1ad 100644 --- a/Makefile +++ b/Makefile @@ -6,13 +6,14 @@ # By: vavaas +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2023/08/09 15:08:49 by vavaas #+# #+# # -# Updated: 2024/01/20 10:01:26 by maldavid ### ########.fr # +# Updated: 2024/01/20 19:16:37 by maldavid ### ########.fr # # # # **************************************************************************** # NAME = ircserv SRCS = srcs/main.cpp \ + srcs/utils/ansi.cpp \ OBJ_DIR = objs OBJS = $(addprefix $(OBJ_DIR)/, $(SRCS:.cpp=.o)) diff --git a/includes/ansi.hpp b/includes/ansi.hpp new file mode 100644 index 0000000..475924b --- /dev/null +++ b/includes/ansi.hpp @@ -0,0 +1,52 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ansi.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maldavid +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/01/20 19:05:23 by maldavid #+# #+# */ +/* Updated: 2024/01/20 19:09:18 by maldavid ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef __ANSI__ +#define __ANSI__ + +#include + +namespace AnsiColor +{ + enum Codes + { + red = 31, + green = 32, + blue = 34, + def = 0, + black = 30, + yellow = 33, + magenta = 35, + cyan = 36, + white = 37, + bg_red = 41, + bg_green = 42, + bg_blue = 44, + bg_def = 0, + bg_black = 40, + bg_yellow = 43, + bg_magenta = 45, + bg_cyan = 46, + bg_white = 47, + reset = 0, + bold = 1, + underline = 4, + inverse = 7, + bold_off = 21, + underline_off = 24, + inverse_off = 27 + }; +} + +std::ostream& operator<<(std::ostream& os, const AnsiColor::Codes code); + +#endif diff --git a/includes/unstd/array.h b/includes/unstd/array.hpp similarity index 93% rename from includes/unstd/array.h rename to includes/unstd/array.hpp index e00551a..70a72f0 100644 --- a/includes/unstd/array.h +++ b/includes/unstd/array.hpp @@ -1,12 +1,12 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* array.h :+: :+: :+: */ +/* array.hpp :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/20 09:31:30 by maldavid #+# #+# */ -/* Updated: 2024/01/20 09:40:29 by maldavid ### ########.fr */ +/* Updated: 2024/01/20 19:12:36 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/includes/unstd/bits/ref_counter.h b/includes/unstd/bits/ref_counter.hpp similarity index 88% rename from includes/unstd/bits/ref_counter.h rename to includes/unstd/bits/ref_counter.hpp index de3f0ea..312c985 100644 --- a/includes/unstd/bits/ref_counter.h +++ b/includes/unstd/bits/ref_counter.hpp @@ -1,12 +1,12 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* ref_counter.h :+: :+: :+: */ +/* ref_counter.hpp :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/20 12:54:09 by maldavid #+# #+# */ -/* Updated: 2024/01/20 12:55:02 by maldavid ### ########.fr */ +/* Updated: 2024/01/20 19:12:33 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/includes/unstd/shared_ptr.h b/includes/unstd/shared_ptr.hpp similarity index 90% rename from includes/unstd/shared_ptr.h rename to includes/unstd/shared_ptr.hpp index 3622017..a80a7f5 100644 --- a/includes/unstd/shared_ptr.h +++ b/includes/unstd/shared_ptr.hpp @@ -1,19 +1,19 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* shared_ptr.h :+: :+: :+: */ +/* shared_ptr.hpp :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/20 10:09:02 by maldavid #+# #+# */ -/* Updated: 2024/01/20 13:08:04 by maldavid ### ########.fr */ +/* Updated: 2024/01/20 19:12:42 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ #ifndef __UNSTD_SHARED_PTR__ #define __UNSTD_SHARED_PTR__ -#include +#include #include #include diff --git a/includes/unstd/string.hpp b/includes/unstd/string.hpp new file mode 100644 index 0000000..2af4733 --- /dev/null +++ b/includes/unstd/string.hpp @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* string.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maldavid +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/01/20 19:13:26 by maldavid #+# #+# */ +/* Updated: 2024/01/20 19:14:55 by maldavid ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef __UNSTD_STRING__ +#define __UNSTD_STRING__ + +#include + +namespace unstd +{ + template + std::string toString(T n) + { + std::ostringstream ss; + ss << n; + return ss.str(); + } +} + +#endif diff --git a/includes/unstd/unique_ptr.h b/includes/unstd/unique_ptr.hpp similarity index 91% rename from includes/unstd/unique_ptr.h rename to includes/unstd/unique_ptr.hpp index 4d94e46..69e044b 100644 --- a/includes/unstd/unique_ptr.h +++ b/includes/unstd/unique_ptr.hpp @@ -1,12 +1,12 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* unique_ptr.h :+: :+: :+: */ +/* unique_ptr.hpp :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/20 09:40:56 by maldavid #+# #+# */ -/* Updated: 2024/01/20 10:06:54 by maldavid ### ########.fr */ +/* Updated: 2024/01/20 19:12:51 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/includes/unstd/weak_ptr.h b/includes/unstd/weak_ptr.hpp similarity index 84% rename from includes/unstd/weak_ptr.h rename to includes/unstd/weak_ptr.hpp index 44bc6a1..6e5e9b2 100644 --- a/includes/unstd/weak_ptr.h +++ b/includes/unstd/weak_ptr.hpp @@ -1,20 +1,20 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* weak_ptr.h :+: :+: :+: */ +/* weak_ptr.hpp :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/20 11:17:07 by maldavid #+# #+# */ -/* Updated: 2024/01/20 13:16:14 by maldavid ### ########.fr */ +/* Updated: 2024/01/20 19:13:13 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ #ifndef __UNSTD_WEAK_PTR__ #define __UNSTD_WEAK_PTR__ -#include -#include +#include +#include namespace unstd { @@ -46,6 +46,6 @@ namespace unstd }; } -#include +#include #endif diff --git a/srcs/main.cpp b/srcs/main.cpp index 06befe5..38891a4 100644 --- a/srcs/main.cpp +++ b/srcs/main.cpp @@ -6,17 +6,19 @@ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/20 09:27:04 by maldavid #+# #+# */ -/* Updated: 2024/01/20 13:16:21 by maldavid ### ########.fr */ +/* Updated: 2024/01/20 19:19:39 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ -#include -#include -#include +#include +#include +#include +#include int main(int ac, char** av) { (void)ac; (void)av; + std::cout << AnsiColor::red << "this" << AnsiColor::blue << " is" << AnsiColor::green << " a" << AnsiColor::yellow << " test" << AnsiColor::reset << std::endl; return 0; } diff --git a/srcs/utils/ansi.cpp b/srcs/utils/ansi.cpp new file mode 100644 index 0000000..0c58595 --- /dev/null +++ b/srcs/utils/ansi.cpp @@ -0,0 +1,19 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ansi.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maldavid +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/01/20 19:08:31 by maldavid #+# #+# */ +/* Updated: 2024/01/20 19:18:41 by maldavid ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include + +std::ostream& operator<<(std::ostream& os, const AnsiColor::Codes code) +{ + return os << "\033[1;" << unstd::toString(static_cast(code)) << "m"; +}