2024-04-30 13:49:29 +02:00
|
|
|
const std = @import("std");
|
2024-01-30 23:26:35 +01:00
|
|
|
const Image = @import("Image.zig");
|
|
|
|
|
2024-02-11 19:59:33 +01:00
|
|
|
char: Character = .{},
|
|
|
|
style: Style = .{},
|
|
|
|
link: Hyperlink = .{},
|
|
|
|
image: ?Image.Placement = null,
|
2024-04-30 13:49:29 +02:00
|
|
|
default: bool = false,
|
2024-01-19 04:02:17 +01:00
|
|
|
|
2024-02-01 02:13:56 +01:00
|
|
|
/// Segment is a contiguous run of text that has a constant style
|
|
|
|
pub const Segment = struct {
|
|
|
|
text: []const u8,
|
|
|
|
style: Style = .{},
|
|
|
|
link: Hyperlink = .{},
|
|
|
|
};
|
|
|
|
|
2024-01-19 04:02:17 +01:00
|
|
|
pub const Character = struct {
|
2024-01-19 18:02:32 +01:00
|
|
|
grapheme: []const u8 = " ",
|
2024-01-29 05:42:51 +01:00
|
|
|
/// width should only be provided when the application is sure the terminal
|
2024-02-11 19:59:33 +01:00
|
|
|
/// will measure the same width. This can be ensure by using the gwidth method
|
2024-01-29 05:42:51 +01:00
|
|
|
/// included in libvaxis. If width is 0, libvaxis will measure the glyph at
|
|
|
|
/// render time
|
2024-01-19 18:02:32 +01:00
|
|
|
width: usize = 1,
|
2024-01-19 04:02:17 +01:00
|
|
|
};
|
|
|
|
|
2024-04-15 14:14:31 +02:00
|
|
|
pub const CursorShape = enum {
|
|
|
|
default,
|
|
|
|
block_blink,
|
|
|
|
block,
|
|
|
|
underline_blink,
|
|
|
|
underline,
|
|
|
|
beam_blink,
|
|
|
|
beam,
|
|
|
|
};
|
|
|
|
|
2024-01-25 02:04:12 +01:00
|
|
|
pub const Hyperlink = struct {
|
|
|
|
uri: []const u8 = "",
|
|
|
|
/// ie "id=app-1234"
|
|
|
|
params: []const u8 = "",
|
|
|
|
};
|
|
|
|
|
2024-01-19 04:02:17 +01:00
|
|
|
pub const Style = struct {
|
2024-01-19 06:17:57 +01:00
|
|
|
pub const Underline = enum {
|
|
|
|
off,
|
|
|
|
single,
|
|
|
|
double,
|
|
|
|
curly,
|
|
|
|
dotted,
|
|
|
|
dashed,
|
|
|
|
};
|
|
|
|
|
2024-01-19 04:02:17 +01:00
|
|
|
fg: Color = .default,
|
|
|
|
bg: Color = .default,
|
|
|
|
ul: Color = .default,
|
2024-01-19 06:17:57 +01:00
|
|
|
ul_style: Underline = .off,
|
2024-01-20 02:43:35 +01:00
|
|
|
|
|
|
|
bold: bool = false,
|
|
|
|
dim: bool = false,
|
|
|
|
italic: bool = false,
|
|
|
|
blink: bool = false,
|
|
|
|
reverse: bool = false,
|
|
|
|
invisible: bool = false,
|
|
|
|
strikethrough: bool = false,
|
2024-04-30 13:49:29 +02:00
|
|
|
|
|
|
|
pub fn eql(a: Style, b: Style) bool {
|
|
|
|
const SGRBits = packed struct {
|
|
|
|
bold: bool,
|
|
|
|
dim: bool,
|
|
|
|
italic: bool,
|
|
|
|
blink: bool,
|
|
|
|
reverse: bool,
|
|
|
|
invisible: bool,
|
|
|
|
strikethrough: bool,
|
|
|
|
};
|
|
|
|
const a_sgr: SGRBits = .{
|
|
|
|
.bold = a.bold,
|
|
|
|
.dim = a.dim,
|
|
|
|
.italic = a.italic,
|
|
|
|
.blink = a.blink,
|
|
|
|
.reverse = a.reverse,
|
|
|
|
.invisible = a.invisible,
|
|
|
|
.strikethrough = a.strikethrough,
|
|
|
|
};
|
|
|
|
const b_sgr: SGRBits = .{
|
|
|
|
.bold = b.bold,
|
|
|
|
.dim = b.dim,
|
|
|
|
.italic = b.italic,
|
|
|
|
.blink = b.blink,
|
|
|
|
.reverse = b.reverse,
|
|
|
|
.invisible = b.invisible,
|
|
|
|
.strikethrough = b.strikethrough,
|
|
|
|
};
|
|
|
|
const a_cast: u7 = @bitCast(a_sgr);
|
|
|
|
const b_cast: u7 = @bitCast(b_sgr);
|
|
|
|
return a_cast == b_cast and
|
|
|
|
Color.eql(a.fg, b.fg) and
|
|
|
|
Color.eql(a.bg, b.bg) and
|
|
|
|
Color.eql(a.ul, b.ul) and
|
|
|
|
a.ul_style == b.ul_style;
|
|
|
|
}
|
2024-01-19 04:02:17 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
pub const Color = union(enum) {
|
|
|
|
default,
|
|
|
|
index: u8,
|
|
|
|
rgb: [3]u8,
|
2024-04-15 14:15:00 +02:00
|
|
|
|
2024-05-24 17:59:20 +02:00
|
|
|
pub const Kind = union(enum) {
|
|
|
|
fg,
|
|
|
|
bg,
|
|
|
|
cursor,
|
|
|
|
index: u8,
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Returned when querying a color from the terminal
|
|
|
|
pub const Report = struct {
|
|
|
|
kind: Kind,
|
|
|
|
value: [3]u8,
|
|
|
|
};
|
|
|
|
|
2024-05-26 13:52:06 +02:00
|
|
|
pub const Scheme = enum {
|
|
|
|
dark,
|
|
|
|
light,
|
|
|
|
};
|
|
|
|
|
2024-04-30 13:49:29 +02:00
|
|
|
pub fn eql(a: Color, b: Color) bool {
|
2024-04-30 20:38:32 +02:00
|
|
|
switch (a) {
|
|
|
|
.default => return b == .default,
|
|
|
|
.index => |a_idx| {
|
|
|
|
switch (b) {
|
|
|
|
.index => |b_idx| return a_idx == b_idx,
|
|
|
|
else => return false,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
.rgb => |a_rgb| {
|
|
|
|
switch (b) {
|
|
|
|
.rgb => |b_rgb| return a_rgb[0] == b_rgb[0] and
|
|
|
|
a_rgb[1] == b_rgb[1] and
|
|
|
|
a_rgb[2] == b_rgb[2],
|
|
|
|
else => return false,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
2024-04-30 13:49:29 +02:00
|
|
|
}
|
|
|
|
|
2024-04-15 14:15:00 +02:00
|
|
|
pub fn rgbFromUint(val: u24) Color {
|
|
|
|
const r_bits = val & 0b11111111_00000000_00000000;
|
|
|
|
const g_bits = val & 0b00000000_11111111_00000000;
|
|
|
|
const b_bits = val & 0b00000000_00000000_11111111;
|
|
|
|
const rgb = [_]u8{
|
|
|
|
@truncate(r_bits >> 16),
|
|
|
|
@truncate(g_bits >> 8),
|
|
|
|
@truncate(b_bits),
|
|
|
|
};
|
|
|
|
return .{ .rgb = rgb };
|
|
|
|
}
|
2024-05-24 17:59:20 +02:00
|
|
|
|
|
|
|
/// parse an XParseColor-style rgb specification into an rgb Color. The spec
|
|
|
|
/// is of the form: rgb:rrrr/gggg/bbbb. Generally, the high two bits will always
|
|
|
|
/// be the same as the low two bits.
|
|
|
|
pub fn rgbFromSpec(spec: []const u8) !Color {
|
|
|
|
var iter = std.mem.splitScalar(u8, spec, ':');
|
|
|
|
const prefix = iter.next() orelse return error.InvalidColorSpec;
|
|
|
|
if (!std.mem.eql(u8, "rgb", prefix)) return error.InvalidColorSpec;
|
|
|
|
|
|
|
|
const spec_str = iter.next() orelse return error.InvalidColorSpec;
|
|
|
|
|
|
|
|
var spec_iter = std.mem.splitScalar(u8, spec_str, '/');
|
|
|
|
|
|
|
|
const r_raw = spec_iter.next() orelse return error.InvalidColorSpec;
|
|
|
|
if (r_raw.len != 4) return error.InvalidColorSpec;
|
|
|
|
|
|
|
|
const g_raw = spec_iter.next() orelse return error.InvalidColorSpec;
|
|
|
|
if (g_raw.len != 4) return error.InvalidColorSpec;
|
|
|
|
|
|
|
|
const b_raw = spec_iter.next() orelse return error.InvalidColorSpec;
|
|
|
|
if (b_raw.len != 4) return error.InvalidColorSpec;
|
|
|
|
|
|
|
|
const r = try std.fmt.parseUnsigned(u8, r_raw[2..], 16);
|
|
|
|
const g = try std.fmt.parseUnsigned(u8, g_raw[2..], 16);
|
|
|
|
const b = try std.fmt.parseUnsigned(u8, b_raw[2..], 16);
|
|
|
|
|
|
|
|
return .{
|
|
|
|
.rgb = [_]u8{ r, g, b },
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
test "rgbFromSpec" {
|
|
|
|
const spec = "rgb:aaaa/bbbb/cccc";
|
|
|
|
const actual = try rgbFromSpec(spec);
|
|
|
|
switch (actual) {
|
|
|
|
.rgb => |rgb| {
|
|
|
|
try std.testing.expectEqual(0xAA, rgb[0]);
|
|
|
|
try std.testing.expectEqual(0xBB, rgb[1]);
|
|
|
|
try std.testing.expectEqual(0xCC, rgb[2]);
|
|
|
|
},
|
|
|
|
else => try std.testing.expect(false),
|
|
|
|
}
|
|
|
|
}
|
2024-01-19 04:02:17 +01:00
|
|
|
};
|