From f9b1de720afae59ec6c39ebde0985d97ee9049e7 Mon Sep 17 00:00:00 2001
From: Jeeves <guydoodlesdev@gmail.com>
Date: Sat, 6 Apr 2024 13:28:16 -0600
Subject: [PATCH] very broken but I dont wanna lose this code

---
 src/main.zig     | 176 ++++++++++++++++++++++++++++++++++++++++++-----
 src/terminfo.zig |   1 +
 2 files changed, 161 insertions(+), 16 deletions(-)

diff --git a/src/main.zig b/src/main.zig
index 458a986..84f8441 100644
--- a/src/main.zig
+++ b/src/main.zig
@@ -13,23 +13,45 @@ pub fn main() !void {
 
     var term = try Terminal.init(allocator);
     defer term.deinit();
-
     try term.clearScreen();
-    try term.print("fooboo", .{});
-    try term.cursorLeft();
-    try term.cursorLeft();
-    try term.print("ar", .{});
-    // try term.cursorSet(12, 3);
-    try term.blinkOn();
-    try term.boldOn();
-    try term.underlineOn();
-    try term.italicsOn();
-    try term.print("ABCDEFGHIJKLMNOPQRSTUVWXYZ", .{});
-    try term.blinkOff();
-    try term.boldOff();
-    try term.underlineOff();
-    try term.italicsOff();
-    try term.print("eeheeveehee\n", .{});
+
+    var box = Terminal.Box.init(allocator);
+    defer box.deinit();
+    box.content = "hi";
+    box.border = true;
+
+    try term.box.addChild(&box);
+    term.box.border = true;
+
+    try term.draw();
+
+    // var box = Terminal.Box{
+    //     .x = 0,
+    //     .y = 0,
+    //     .width = 13,
+    //     .height = 1,
+
+    //     .content = "hello world",
+    //     .border = true,
+    // };
+    // try box.draw(&term);
+
+    // try term.clearScreen();
+    // try term.print("fooboo", .{});
+    // try term.cursorLeft();
+    // try term.cursorLeft();
+    // try term.print("ar", .{});
+    // // try term.cursorSet(12, 3);
+    // try term.blinkOn();
+    // try term.boldOn();
+    // try term.underlineOn();
+    // try term.italicsOn();
+    // try term.print("ABCDEFGHIJKLMNOPQRSTUVWXYZ", .{});
+    // try term.blinkOff();
+    // try term.boldOff();
+    // try term.underlineOff();
+    // try term.italicsOff();
+    // try term.print("eeheeveehee\n", .{});
 }
 
 pub const Terminal = struct {
@@ -38,12 +60,15 @@ pub const Terminal = struct {
     info: Info,
     allocator: mem.Allocator,
 
+    box: Box,
+
     pub fn init(allocator: mem.Allocator) !Terminal {
         var term = Terminal{
             .tty = try fs.openFileAbsolute("/dev/tty", .{ .mode = .read_write }),
             .original_termios = undefined,
             .info = try Info.init(allocator),
             .allocator = allocator,
+            .box = Box.init(allocator),
         };
         errdefer term.tty.close();
         errdefer term.info.deinit();
@@ -52,6 +77,7 @@ pub const Terminal = struct {
     }
 
     pub fn deinit(self: *Terminal) void {
+        self.box.deinit();
         self.cook();
         self.tty.close();
         self.info.deinit();
@@ -86,6 +112,10 @@ pub const Terminal = struct {
         _ = os.linux.tcsetattr(self.tty.handle, .FLUSH, &self.original_termios);
     }
 
+    pub fn draw(self: *Terminal) !void {
+        try self.box.draw(self);
+    }
+
     pub fn print(self: *Terminal, comptime format: []const u8, args: anytype) !void {
         const formatted = try fmt.allocPrint(self.allocator, format, args);
         defer self.allocator.free(formatted);
@@ -162,4 +192,118 @@ pub const Terminal = struct {
         arrow_up,
         arrow_down,
     };
+
+    pub const Box = struct {
+        parent: ?*Box,
+        children: std.ArrayList(*Box),
+
+        // x: u32,
+        // y: u32,
+        // width: u32,
+        // height: u32,
+
+        left: u32,
+        right: u32,
+        top: u32,
+        bottom: u32,
+
+        border_type: enum { line, bg } = .line,
+        border_char: u8 = ' ',
+        border_bg: i16 = -1,
+        border_fg: i16 = -1,
+
+        content: []const u8 = "",
+        border: bool = false,
+
+        pub fn init(allocator: mem.Allocator) Box {
+            return .{
+                .parent = null,
+                .children = std.ArrayList(*Box).init(allocator),
+                .x = 0,
+                .y = 0,
+                .width = 20,
+                .height = 8,
+            };
+        }
+
+        pub fn deinit(self: *Box) void {
+            for (self.children.items) |child| child.deinit();
+            self.children.deinit();
+        }
+
+        pub fn draw(self: *Box, term: *Terminal) !void {
+            var x: u32 = 0;
+            var y: u32 = 0;
+            try term.cursorSet(self.x, self.y);
+
+            switch (self.border_type) {
+                .line => {
+                    try term.print("", .{});
+                    while (x < self.width) : (x += 1) try term.print("", .{});
+                    try term.print("", .{});
+
+                    y += 1;
+                    while (y <= self.height) : (y += 1) {
+                        try term.cursorSet(self.x, self.y + y);
+                        try term.print("", .{});
+                        try term.cursorSet(self.x + self.width + 1, self.y + y);
+                        try term.print("", .{});
+                    }
+
+                    try term.cursorSet(self.x, self.y + y);
+                },
+                .bg => {},
+            }
+
+            // if (self.border) {
+            //     try term.print("┌", .{});
+            //     // x += 1;
+            //     while (x < self.width) : (x += 1) try term.print("─", .{});
+            //     try term.print("┐", .{});
+            //     // x += 1;
+            //     y += 1;
+            //     while (y <= self.height) : (y += 1) {
+            //         try term.cursorSet(self.x, self.y + y);
+            //         try term.print("│", .{});
+            //         try term.cursorSet(self.x + self.width + 1, self.y + y);
+            //         try term.print("│", .{});
+            //     }
+            //     x = 0;
+            //     try term.cursorSet(self.x + x, self.y + y);
+            //     try term.print("└", .{});
+            //     while (x < self.width) : (x += 1) try term.print("─", .{});
+            //     try term.print("┘", .{});
+            // }
+
+            // x = self.x + 2;
+            // y = self.y + 1;
+            // try term.cursorSet(x, y);
+            // if (self.content.len < self.width) {
+            //     try term.print("{s}", .{self.content});
+            //     x += @intCast(self.content.len);
+            // } else {
+            //     const space = mem.indexOfScalar(u8, self.content, ' ');
+            //     if (space) |sp| {
+            //         try term.print("{s}", .{self.content[0..sp]});
+            //         y += 1;
+            //         try term.cursorSet(x, y);
+            //         try term.print("{s}", .{self.content[sp + 1 ..]});
+            //     }
+            // }
+
+            for (self.children.items) |child| try child.draw(term);
+
+            try term.cursorSet(self.x + self.width + 2, self.y + self.height + 1);
+        }
+
+        pub fn addChild(self: *Box, child: *Box) !void {
+            std.debug.assert(child.parent == null);
+            child.parent = self;
+            child.x = self.x + 2;
+            child.y = self.y + 2;
+            child.width = self.width - 4;
+            child.height = self.height - 4;
+            try self.children.append(child);
+        }
+    };
 };
diff --git a/src/terminfo.zig b/src/terminfo.zig
index f02c5c8..019bf61 100644
--- a/src/terminfo.zig
+++ b/src/terminfo.zig
@@ -742,6 +742,7 @@ pub const String = enum {
     }
 
     pub fn default(self: String) ?[]const u8 {
+        // TODO: add more from https://gist.github.com/fnky/458719343aabd01cfb17a3a4f7296797
         return switch (self) {
             .enter_bold_mode => "\x1b[1m",
             .enter_dim_mode => "\x1b[2m",