refactor: zigify imports and correct minor mistakes
This commit is contained in:
@@ -7,11 +7,13 @@
|
||||
|
||||
// taken from https://github.com/rockorager/libvaxis/blob/main/src/Cell.zig (MIT-License)
|
||||
// with slight modifications
|
||||
const std = @import("std");
|
||||
|
||||
const Color = @import("color.zig").Color;
|
||||
|
||||
pub const Style = @This();
|
||||
fg: Color = .default,
|
||||
bg: Color = .default,
|
||||
ul: Color = .default,
|
||||
cursor: bool = false,
|
||||
ul_style: Underline = .off,
|
||||
emphasis: []const Emphasis,
|
||||
|
||||
pub const Underline = enum {
|
||||
off,
|
||||
@@ -34,42 +36,43 @@ pub const Emphasis = enum(u8) {
|
||||
strikethrough,
|
||||
};
|
||||
|
||||
fg: Color = .default,
|
||||
bg: Color = .default,
|
||||
ul: Color = .default,
|
||||
cursor: bool = false,
|
||||
ul_style: Underline = .off,
|
||||
emphasis: []const Emphasis,
|
||||
|
||||
pub fn eql(this: Style, other: Style) bool {
|
||||
return std.meta.eql(this, other);
|
||||
return meta.eql(this, other);
|
||||
}
|
||||
|
||||
// TODO might be useful to use the std.ascii stuff!
|
||||
|
||||
pub fn value(this: Style, writer: anytype, cp: u21) !void {
|
||||
var buffer: [4]u8 = undefined;
|
||||
const bytes = try std.unicode.utf8Encode(cp, &buffer);
|
||||
std.debug.assert(bytes > 0);
|
||||
const bytes = try unicode.utf8Encode(cp, &buffer);
|
||||
assert(bytes > 0);
|
||||
// build ansi sequence for 256 colors ...
|
||||
// foreground
|
||||
try std.fmt.format(writer, "\x1b[", .{});
|
||||
try format(writer, "\x1b[", .{});
|
||||
try this.fg.write(writer, .fg);
|
||||
// background
|
||||
try std.fmt.format(writer, ";", .{});
|
||||
try format(writer, ";", .{});
|
||||
try this.bg.write(writer, .bg);
|
||||
// underline
|
||||
// FIX assert that if the underline property is set that the ul style and the attribute for underlining is available
|
||||
try std.fmt.format(writer, ";", .{});
|
||||
try format(writer, ";", .{});
|
||||
try this.ul.write(writer, .ul);
|
||||
// append styles (aka attributes like bold, italic, strikethrough, etc.)
|
||||
for (this.emphasis) |attribute| try std.fmt.format(writer, ";{d}", .{@intFromEnum(attribute)});
|
||||
try std.fmt.format(writer, "m", .{});
|
||||
for (this.emphasis) |attribute| try format(writer, ";{d}", .{@intFromEnum(attribute)});
|
||||
try format(writer, "m", .{});
|
||||
// content
|
||||
try std.fmt.format(writer, "{s}", .{buffer[0..bytes]});
|
||||
try std.fmt.format(writer, "\x1b[0m", .{});
|
||||
try format(writer, "{s}", .{buffer[0..bytes]});
|
||||
try format(writer, "\x1b[0m", .{});
|
||||
}
|
||||
|
||||
// TODO implement helper functions for terminal capabilities:
|
||||
// - links / url display (osc 8)
|
||||
// - show / hide cursor?
|
||||
|
||||
const std = @import("std");
|
||||
const unicode = std.unicode;
|
||||
const meta = std.meta;
|
||||
const assert = std.debug.assert;
|
||||
const format = std.fmt.format;
|
||||
const Color = @import("color.zig").Color;
|
||||
const Style = @This();
|
||||
|
||||
Reference in New Issue
Block a user