very broken but I dont wanna lose this code
This commit is contained in:
parent
1d59e48a4f
commit
f9b1de720a
2 changed files with 161 additions and 16 deletions
176
src/main.zig
176
src/main.zig
|
@ -13,23 +13,45 @@ pub fn main() !void {
|
||||||
|
|
||||||
var term = try Terminal.init(allocator);
|
var term = try Terminal.init(allocator);
|
||||||
defer term.deinit();
|
defer term.deinit();
|
||||||
|
|
||||||
try term.clearScreen();
|
try term.clearScreen();
|
||||||
try term.print("fooboo", .{});
|
|
||||||
try term.cursorLeft();
|
var box = Terminal.Box.init(allocator);
|
||||||
try term.cursorLeft();
|
defer box.deinit();
|
||||||
try term.print("ar", .{});
|
box.content = "hi";
|
||||||
// try term.cursorSet(12, 3);
|
box.border = true;
|
||||||
try term.blinkOn();
|
|
||||||
try term.boldOn();
|
try term.box.addChild(&box);
|
||||||
try term.underlineOn();
|
term.box.border = true;
|
||||||
try term.italicsOn();
|
|
||||||
try term.print("ABCDEFGHIJKLMNOPQRSTUVWXYZ", .{});
|
try term.draw();
|
||||||
try term.blinkOff();
|
|
||||||
try term.boldOff();
|
// var box = Terminal.Box{
|
||||||
try term.underlineOff();
|
// .x = 0,
|
||||||
try term.italicsOff();
|
// .y = 0,
|
||||||
try term.print("eeheeveehee\n", .{});
|
// .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 {
|
pub const Terminal = struct {
|
||||||
|
@ -38,12 +60,15 @@ pub const Terminal = struct {
|
||||||
info: Info,
|
info: Info,
|
||||||
allocator: mem.Allocator,
|
allocator: mem.Allocator,
|
||||||
|
|
||||||
|
box: Box,
|
||||||
|
|
||||||
pub fn init(allocator: mem.Allocator) !Terminal {
|
pub fn init(allocator: mem.Allocator) !Terminal {
|
||||||
var term = Terminal{
|
var term = Terminal{
|
||||||
.tty = try fs.openFileAbsolute("/dev/tty", .{ .mode = .read_write }),
|
.tty = try fs.openFileAbsolute("/dev/tty", .{ .mode = .read_write }),
|
||||||
.original_termios = undefined,
|
.original_termios = undefined,
|
||||||
.info = try Info.init(allocator),
|
.info = try Info.init(allocator),
|
||||||
.allocator = allocator,
|
.allocator = allocator,
|
||||||
|
.box = Box.init(allocator),
|
||||||
};
|
};
|
||||||
errdefer term.tty.close();
|
errdefer term.tty.close();
|
||||||
errdefer term.info.deinit();
|
errdefer term.info.deinit();
|
||||||
|
@ -52,6 +77,7 @@ pub const Terminal = struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn deinit(self: *Terminal) void {
|
pub fn deinit(self: *Terminal) void {
|
||||||
|
self.box.deinit();
|
||||||
self.cook();
|
self.cook();
|
||||||
self.tty.close();
|
self.tty.close();
|
||||||
self.info.deinit();
|
self.info.deinit();
|
||||||
|
@ -86,6 +112,10 @@ pub const Terminal = struct {
|
||||||
_ = os.linux.tcsetattr(self.tty.handle, .FLUSH, &self.original_termios);
|
_ = 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 {
|
pub fn print(self: *Terminal, comptime format: []const u8, args: anytype) !void {
|
||||||
const formatted = try fmt.allocPrint(self.allocator, format, args);
|
const formatted = try fmt.allocPrint(self.allocator, format, args);
|
||||||
defer self.allocator.free(formatted);
|
defer self.allocator.free(formatted);
|
||||||
|
@ -162,4 +192,118 @@ pub const Terminal = struct {
|
||||||
arrow_up,
|
arrow_up,
|
||||||
arrow_down,
|
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);
|
||||||
|
}
|
||||||
|
};
|
||||||
};
|
};
|
||||||
|
|
|
@ -742,6 +742,7 @@ pub const String = enum {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn default(self: String) ?[]const u8 {
|
pub fn default(self: String) ?[]const u8 {
|
||||||
|
// TODO: add more from https://gist.github.com/fnky/458719343aabd01cfb17a3a4f7296797
|
||||||
return switch (self) {
|
return switch (self) {
|
||||||
.enter_bold_mode => "\x1b[1m",
|
.enter_bold_mode => "\x1b[1m",
|
||||||
.enter_dim_mode => "\x1b[2m",
|
.enter_dim_mode => "\x1b[2m",
|
||||||
|
|
Loading…
Add table
Reference in a new issue