silkdot/src/main.zig

166 lines
4.7 KiB
Zig
Raw Normal View History

2024-04-05 15:05:14 -06:00
const std = @import("std");
const os = std.os;
const io = std.io;
const fs = std.fs;
const mem = std.mem;
const heap = std.heap;
const fmt = std.fmt;
pub fn main() !void {
var gpa = heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
const allocator = gpa.allocator();
var term = try Terminal.init(allocator);
defer term.deinit();
try term.clearScreen();
try term.print("fooboo", .{});
2024-04-05 15:05:14 -06:00
try term.cursorLeft();
try term.cursorLeft();
try term.print("ar", .{});
2024-04-05 18:03:30 -06:00
// try term.cursorSet(12, 3);
try term.blinkOn();
2024-04-05 18:03:30 -06:00
try term.boldOn();
try term.underlineOn();
try term.italicsOn();
try term.print("ABCDEFGHIJKLMNOPQRSTUVWXYZ", .{});
try term.blinkOff();
2024-04-05 18:03:30 -06:00
try term.boldOff();
try term.underlineOff();
try term.italicsOff();
try term.print("eeheeveehee\n", .{});
2024-04-05 15:05:14 -06:00
}
pub const Terminal = struct {
tty: fs.File,
original_termios: os.linux.termios,
info: Info,
allocator: mem.Allocator,
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,
};
errdefer term.tty.close();
errdefer term.info.deinit();
term.uncook();
return term;
}
pub fn deinit(self: *Terminal) void {
self.cook();
self.tty.close();
self.info.deinit();
}
// pub fn poll(self: *Terminal) void {}
fn uncook(self: *Terminal) void {
_ = os.linux.tcgetattr(self.tty.handle, &self.original_termios);
var raw = self.original_termios;
raw.lflag.ECHO = false;
raw.lflag.ICANON = false;
raw.lflag.ISIG = false;
raw.lflag.IEXTEN = false;
raw.iflag.IXON = false;
raw.iflag.ICRNL = false;
raw.iflag.BRKINT = false;
raw.iflag.INPCK = false;
raw.iflag.ISTRIP = false;
raw.oflag.OPOST = false;
raw.cc[@intFromEnum(os.linux.V.TIME)] = 0;
raw.cc[@intFromEnum(os.linux.V.MIN)] = 1;
_ = os.linux.tcsetattr(self.tty.handle, .FLUSH, &raw);
}
fn cook(self: *Terminal) void {
_ = os.linux.tcsetattr(self.tty.handle, .FLUSH, &self.original_termios);
}
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);
try self.tty.writeAll(formatted);
}
2024-04-05 18:03:30 -06:00
pub fn cursorUp(self: *Terminal) !void {
try self.info.writeString(.cursor_up, self.tty.writer(), &[_]u32{});
}
pub fn cursorDown(self: *Terminal) !void {
try self.info.writeString(.cursor_down, self.tty.writer(), &[_]u32{});
}
2024-04-05 15:05:14 -06:00
pub fn cursorLeft(self: *Terminal) !void {
2024-04-05 17:08:08 -06:00
try self.info.writeString(.cursor_left, self.tty.writer(), &[_]u32{});
2024-04-05 18:03:30 -06:00
}
pub fn cursorRight(self: *Terminal) !void {
try self.info.writeString(.cursor_right, self.tty.writer(), &[_]u32{});
}
pub fn cursorSet(self: *Terminal, x: u32, y: u32) !void {
try self.info.writeString(.cursor_address, self.tty.writer(), &[_]u32{ y, x });
}
pub fn blinkOn(self: *Terminal) !void {
try self.info.writeString(.enter_blink_mode, self.tty.writer(), &[_]u32{});
}
pub fn blinkOff(self: *Terminal) !void {
try self.info.writeString(.exit_blink_mode, self.tty.writer(), &[_]u32{});
}
pub fn boldOn(self: *Terminal) !void {
try self.info.writeString(.enter_bold_mode, self.tty.writer(), &[_]u32{});
}
pub fn boldOff(self: *Terminal) !void {
try self.info.writeString(.exit_bold_mode, self.tty.writer(), &[_]u32{});
}
pub fn italicsOn(self: *Terminal) !void {
try self.info.writeString(.enter_italics_mode, self.tty.writer(), &[_]u32{});
}
pub fn italicsOff(self: *Terminal) !void {
try self.info.writeString(.exit_italics_mode, self.tty.writer(), &[_]u32{});
}
pub fn underlineOn(self: *Terminal) !void {
try self.info.writeString(.enter_underline_mode, self.tty.writer(), &[_]u32{});
}
pub fn underlineOff(self: *Terminal) !void {
try self.info.writeString(.exit_underline_mode, self.tty.writer(), &[_]u32{});
2024-04-05 15:05:14 -06:00
}
pub fn clearScreen(self: *Terminal) !void {
try self.info.writeString(.clear_screen, self.tty.writer(), &[_]u32{});
}
2024-04-05 15:05:14 -06:00
pub const Info = @import("terminfo.zig");
pub const SpecialKey = enum(u16) {
home,
end,
page_up,
page_down,
delete,
backspace,
arrow_left,
arrow_right,
arrow_up,
arrow_down,
};
};