From 2389da9759433546f7061e9d94be507cdb482480 Mon Sep 17 00:00:00 2001 From: Kittycannon Date: Thu, 9 Apr 2026 10:07:43 -0600 Subject: [PATCH] initial load --- desktoplib.code-workspace | 8 + makefile | 73 ++++++ src/desktoplib/terminal/Terminal.hpp | 292 +++++++++++++++++++++ src/desktoplib/terminal/ui/Box.hpp | 0 src/desktoplib/terminal/ui/Element.hpp | 0 src/desktoplib/terminal/ui/InnerScroll.hpp | 2 + src/desktoplib/terminal/ui/MenuMap.hpp | 0 src/desktoplib/terminal/ui/TitledBox.hpp | 3 + 8 files changed, 378 insertions(+) create mode 100644 desktoplib.code-workspace create mode 100644 makefile create mode 100644 src/desktoplib/terminal/Terminal.hpp create mode 100644 src/desktoplib/terminal/ui/Box.hpp create mode 100644 src/desktoplib/terminal/ui/Element.hpp create mode 100644 src/desktoplib/terminal/ui/InnerScroll.hpp create mode 100644 src/desktoplib/terminal/ui/MenuMap.hpp create mode 100644 src/desktoplib/terminal/ui/TitledBox.hpp diff --git a/desktoplib.code-workspace b/desktoplib.code-workspace new file mode 100644 index 0000000..876a149 --- /dev/null +++ b/desktoplib.code-workspace @@ -0,0 +1,8 @@ +{ + "folders": [ + { + "path": "." + } + ], + "settings": {} +} \ No newline at end of file diff --git a/makefile b/makefile new file mode 100644 index 0000000..590408f --- /dev/null +++ b/makefile @@ -0,0 +1,73 @@ +#Compiler and Linker +CC := g++ + +#The Target Binary Program +TARGET := out.exe + +#The Directories, Source, Includes, Objects, Binary and Resources +SRCDIR := src +BUILDDIR := bin +TARGETDIR := out +SRCEXT := cpp +DEPEXT := d +OBJEXT := o + +#Flags, Libraries and Includes +ROOT := ./ +CFLAGS := -std=c++20 -O2 \ + -Wall -Werror -Wextra \ + -Wshadow -Wnon-virtual-dtor -Wold-style-cast -Wcast-align \ + -Wunused -Woverloaded-virtual -Wconversion \ + -Wsign-conversion -Wnull-dereference -Wdouble-promotion \ + -Wformat=2 -Wimplicit-fallthrough -Wsuggest-override \ + -Wextra-semi -Wduplicated-cond -Wduplicated-branches \ + -Wlogical-op -Wuseless-cast +LFLAGS := -std=c++20 -static-libstdc++ -static-libgcc \ + -Wl,--fatal-warnings -Wl,--warn-common +LIB := +INC := -I./src/ + +#--------------------------------------------------------------------------------- +#DO NOT EDIT BELOW THIS LINE +#--------------------------------------------------------------------------------- +SOURCES := $(shell find $(SRCDIR) -type f -name *.$(SRCEXT)) +OBJECTS := $(patsubst $(SRCDIR)/%,$(BUILDDIR)/%,$(SOURCES:.$(SRCEXT)=.$(OBJEXT))) + +#Defauilt Make +all: directories $(TARGET) + +#Remake +remake: cleaner all + +#Make the Directories +directories: + @mkdir -p $(TARGETDIR) + @mkdir -p $(BUILDDIR) + +#Clean only Objecst +clean: + @$(RM) -rf $(BUILDDIR) + +#Full Clean, Objects and Binaries +cleaner: clean + @$(RM) -rf $(TARGETDIR) + +#Pull in dependency info for *existing* .o files +-include $(OBJECTS:.$(OBJEXT)=.$(DEPEXT)) + +#Link +$(TARGET): $(OBJECTS) + $(CC) $(LFLAGS) -o $(TARGETDIR)/$(TARGET) $^ $(LIB) + +#Compile +$(BUILDDIR)/%.$(OBJEXT): $(SRCDIR)/%.$(SRCEXT) + @mkdir -p $(dir $@) + $(CC) $(CFLAGS) $(INC) -c -o $@ $< + @$(CC) $(CFLAGS) -MM $(SRCDIR)/$*.$(SRCEXT) > $(BUILDDIR)/$*.$(DEPEXT) + @cp -f $(BUILDDIR)/$*.$(DEPEXT) $(BUILDDIR)/$*.$(DEPEXT).tmp + @sed -e 's|.*:|$(BUILDDIR)/$*.$(OBJEXT):|' < $(BUILDDIR)/$*.$(DEPEXT).tmp > $(BUILDDIR)/$*.$(DEPEXT) + @sed -e 's/.*://' -e 's/\\$$//' < $(BUILDDIR)/$*.$(DEPEXT).tmp | fmt -1 | sed -e 's/^ *//' -e 's/$$/:/' >> $(BUILDDIR)/$*.$(DEPEXT) + @rm -f $(BUILDDIR)/$*.$(DEPEXT).tmp + +#Non-File Targets +.PHONY: all remake clean cleaner resources \ No newline at end of file diff --git a/src/desktoplib/terminal/Terminal.hpp b/src/desktoplib/terminal/Terminal.hpp new file mode 100644 index 0000000..0acb6c6 --- /dev/null +++ b/src/desktoplib/terminal/Terminal.hpp @@ -0,0 +1,292 @@ +#pragma once + +#include +#include +#include +#include + +namespace ckitty { + + namespace terminal { + + /** + * Positions in the terminal begin at + * the origin (1,1). Usually, x are columns + * and y are rows. Also, they are expressed + * in (y, x) aka (row, column). Here, they + * are expressed in (x, y) order for + * ease with people familiar with cartesian + * coordinates. + */ + struct pos { + public: + int x, y; + pos(int x, int y); + }; + + /** + * Represents a relative movement in the + * cursor. + */ + struct move { + public: + int x, y; + move(int x, int y); + }; + + /** + * Represents an escape sequence which + * changes the color of the background. + */ + struct backg { + friend class Terminal; + public: + + static const backg BLACK; static const backg B_BLACK; + static const backg RED; static const backg B_RED; + static const backg GREEN; static const backg B_GREEN; + static const backg YELLOW; static const backg B_YELLOW; + static const backg BLUE; static const backg B_BLUE; + static const backg MAGENTA; static const backg B_MAGENTA; + static const backg CYAN; static const backg B_CYAN; + static const backg WHITE; static const backg B_WHITE; + + private: + + std::string_view code; + backg(const std::string_view code); + + public: + static backg rgb(int r, int g, int b); + static backg hsl(float hue, float sat, float lum); + }; + + /** + * Represents an escape sequence which + * changes the color of the foreground. + */ + struct color { + friend class Terminal; + public: + + static const color BLACK; static const color B_BLACK; + static const color RED; static const color B_RED; + static const color GREEN; static const color B_GREEN; + static const color YELLOW; static const color B_YELLOW; + static const color BLUE; static const color B_BLUE; + static const color MAGENTA; static const color B_MAGENTA; + static const color CYAN; static const color B_CYAN; + static const color WHITE; static const color B_WHITE; + + private: + std::string_view code; + color(const std::string_view code); + public: + static color rgb(int r, int g, int b); + static color hsl(float hue, float sat, float lum); + }; + + /** + * Represents an escape code which will alter + * the style of the printed text + */ + struct style { + friend class Terminal; + public: + static const style RESET; + static const style BOLD; + static const style ITALIC; + static const style FAINT; + static const style STRIKE; + private: + std::string_view code; + style(const std::string_view code); + }; + + /** + * Represents a keyboard key input. + */ + struct key { + friend class Terminal; + public: + // Key Definitions (ASCII OK) // + static constexpr const char UP = 0x80; + static constexpr const char DOWN = 0x81; + static constexpr const char LEFT = 0x82; + static constexpr const char RIGHT = 0x83; + static constexpr const char ENTER = 0x84; + static constexpr const char ESC = 0x85; + static constexpr const char BACKSPACE = 0x86; + static constexpr const char UNKNOWN = 0xFF; + private: + }; + + /** + * Represents a mouse input. + */ + struct mouse {}; + + /** + * Represents a terminal, which allows for displaying + * text based aplications. Actual constrains are placed by + * the trifecta of OS supported. + */ + class Terminal { + private: + std::ostream* _os; + std::istream* _is; + bool _own; + + public: + + Terminal(); + + ~Terminal(); + + public: + + /** + * Fills the screen with a specific background color. + * @param color The background color constant (e.g., Terminal::BG_BLUE) + */ + Terminal& fill(const std::string_view color); + + /** + * Clears the screen + */ + Terminal& cls(); + + Terminal& altbuff(bool enable); + + Terminal& createScrollRange(int start, int end); + + Terminal& undoScrollRange(); + + /** + * Clears a specific row. + * @param row The 1-based index of the row to clear. + */ + Terminal& clearRow(int row); + + /** + * Clears a range of rows (inclusive). + * @param start The first row. + * @param end The last row. + */ + Terminal& clearRows(int start, int end); + + /** + * Clears a specific column. + * @param col The 1-based index of the column to clear. + */ + Terminal& clearColumn(int col); + + /** + * Clears a range of columns (inclusive). + * @param start The first column. + * @param end The last column. + */ + Terminal& clearColumns(int start, int end); + + /** + * Shows / Hides the cursor. + */ + Terminal& cursor(bool show); + + public: + + /** + * Flushes the output buffer + */ + Terminal& flush(); + + /** + * Clears the input buffer. + * Useful for some specific input cases + */ + Terminal& sink(); + + public: + + Terminal& wait(); + + int getKey(); + + /** + * Get key non blocking + */ + int getKeyNb(); + + std::optional getChar(); + + std::optional getCharNb(); + + pos getSize(); + + public: + + template + Terminal& operator<<(const T& msg) { + (*_os) << msg; + return *this; + } + + Terminal& operator<<(const std::string_view& msg); + + Terminal& operator<<(const pos c); + + Terminal& operator<<(const move c); + + public: + + /** + * Represents an operation which will print up + * to a specific amount of characters. + */ + template + Terminal& subprint(int i0, int i1, const T& msg) {} + + /** + * Prints a centered version of the final text, + * adding padding to center it as best as it can. + */ + template + Terminal& center(int width, const T& msg) { + // to string + std::ostringstream oss; + oss << msg; + std::string s = oss.str(); + + // then print + if (s.length() >= isize(width)) { + std::cout << s; + } + else { + isize total_padding = isize(width) - s.length(); + isize left_padding = total_padding / 2; + std::cout << std::string(left_padding, ' '); + std::cout << s; + std::cout << std::string(total_padding - left_padding, ' '); + } + return *this; + } + + /** + * Centered version of the subprint version. + */ + template + Terminal& subprint_center(int i0, int i1, const T& msg) {} + + private: + + template + Terminal& read(T& var) { + std::cin >> var; + return *this; + } + + }; + + } + +} diff --git a/src/desktoplib/terminal/ui/Box.hpp b/src/desktoplib/terminal/ui/Box.hpp new file mode 100644 index 0000000..e69de29 diff --git a/src/desktoplib/terminal/ui/Element.hpp b/src/desktoplib/terminal/ui/Element.hpp new file mode 100644 index 0000000..e69de29 diff --git a/src/desktoplib/terminal/ui/InnerScroll.hpp b/src/desktoplib/terminal/ui/InnerScroll.hpp new file mode 100644 index 0000000..139597f --- /dev/null +++ b/src/desktoplib/terminal/ui/InnerScroll.hpp @@ -0,0 +1,2 @@ + + diff --git a/src/desktoplib/terminal/ui/MenuMap.hpp b/src/desktoplib/terminal/ui/MenuMap.hpp new file mode 100644 index 0000000..e69de29 diff --git a/src/desktoplib/terminal/ui/TitledBox.hpp b/src/desktoplib/terminal/ui/TitledBox.hpp new file mode 100644 index 0000000..45dcbb0 --- /dev/null +++ b/src/desktoplib/terminal/ui/TitledBox.hpp @@ -0,0 +1,3 @@ +#pragma once + +