Something went wrong. Try again.
Charm-like tui library
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516const std = @import("std");const Color = @import("Color.zig");
pub const Underline = struct { style: Style = .none, /// null means "follow the style's foreground" (lipgloss `UnderlineColor`). color: ?Color = .follows, /// Whether spaces receive the underline (lipgloss `UnderlineSpaces`). underline_spaces: bool = false,
pub const none: Underline = .{}; pub const single: Underline = .{ .style = .single, .underline_spaces = true }; pub const double: Underline = .{ .style = .double, .underline_spaces = true }; pub const curly: Underline = .{ .style = .curly, .underline_spaces = true }; pub const dotted: Underline = .{ .style = .dotted, .underline_spaces = true }; pub const dashed: Underline = .{ .style = .dashed, .underline_spaces = true };
pub fn set(style: Style, color: ?Color) Underline { return .{ .style = style, .color = color, .underline_spaces = false, }; }
pub const Style = enum { none, single, double, curly, dotted, dashed };};
pub const Strikethrough = struct { enabled: bool = false, strikethrough_spaces: bool = false,
pub const off: Strikethrough = .{ .enabled = false, .strikethrough_spaces = false }; pub const on: Strikethrough = .{ .enabled = true, .strikethrough_spaces = true }; pub const skip_spaces: Strikethrough = .{ .enabled = true, .strikethrough_spaces = false };};
pub const Border = struct { style: Style = .{}, foreground_colors: Colors = .{}, background_colors: Colors = .{},
/// Which of the four outer edges are drawn (lipgloss `BorderTop`/etc.). /// [none] disables all four; the named borders enable all four. edges: Edges = .{},
pub const none: Border = .{ .style = .none, .edges = .off }; pub const hidden: Border = .{ .style = .hidden, .edges = .off }; pub const normal: Border = .{ .style = .normal, .edges = .all }; pub const rounded: Border = .{ .style = .rounded, .edges = .all }; pub const thick: Border = .{ .style = .thick, .edges = .all }; pub const double: Border = .{ .style = .double, .edges = .all }; pub const ascii: Border = .{ .style = .ascii, .edges = .all }; pub const markdown: Border = .{ .style = .markdown, .edges = .all }; pub const block: Border = .{ .style = .block, .edges = .all }; pub const inner_half_block: Border = .{ .style = .inner_half_block, .edges = .all }; pub const outer_half_block: Border = .{ .style = .outer_half_block, .edges = .all };
pub fn set(style: Style, options: Options) Border { return .{ .style = style, .foreground_colors = options.foreground, .background_colors = options.background, .edges = options.edges, }; }
const Colors = struct { top: ?Color = null, bottom: ?Color = null, left: ?Color = null, right: ?Color = null, top_left: ?Color = null, top_right: ?Color = null, bottom_left: ?Color = null, bottom_right: ?Color = null, middle_left: ?Color = null, middle_right: ?Color = null, middle: ?Color = null, middle_top: ?Color = null, middle_bottom: ?Color = null,
pub fn all(color: ?Color) Colors { return .{ .top = color, .bottom = color, .left = color, .right = color, .top_left = color, .top_right = color, .bottom_left = color, .bottom_right = color, .middle_left = color, .middle_right = color, .middle = color, .middle_top = color, .middle_bottom = color, }; }
pub fn groups(g: []const Group) Colors { var colors: Colors = .{}; for (g) |group| { for (group.sections) |section| { switch (section) { inline else => |s| @field(colors, @tagName(s)) = group.color, } } } return colors; }
const Group = struct { sections: []const Section, color: ?Color,
pub fn group(sections: []const Section, color: ?Color) Group { return .{ .sections = sections, .color = color }; } }; };
pub const Style = struct { top: []const u8 = " ", bottom: []const u8 = " ", left: []const u8 = " ", right: []const u8 = " ", top_left: []const u8 = " ", top_right: []const u8 = " ", bottom_left: []const u8 = " ", bottom_right: []const u8 = " ", middle_left: []const u8 = " ", middle_right: []const u8 = " ", middle: []const u8 = " ", middle_top: []const u8 = " ", middle_bottom: []const u8 = " ",
pub const none: Style = .{};
/// Standard light border with 90° corners (lipgloss `NormalBorder`). pub const normal: Style = .{ .top = "─", .bottom = "─", .left = "│", .right = "│", .top_left = "┌", .top_right = "┐", .bottom_left = "└", .bottom_right = "┘", .middle_left = "├", .middle_right = "┤", .middle = "┼", .middle_top = "┬", .middle_bottom = "┴", };
/// Light border with rounded corners (lipgloss `RoundedBorder`). pub const rounded: Style = .{ .top_left = "╭", .top_right = "╮", .bottom_left = "╰", .bottom_right = "╯", .top = "─", .bottom = "─", .left = "│", .right = "│", .middle_left = "├", .middle_right = "┤", .middle = "┼", .middle_top = "┬", .middle_bottom = "┴", };
/// Heavy-weight border (lipgloss `ThickBorder`). pub const thick: Style = .{ .top = "━", .bottom = "━", .left = "┃", .right = "┃", .top_left = "┏", .top_right = "┓", .bottom_left = "┗", .bottom_right = "┛", .middle_left = "┣", .middle_right = "┫", .middle = "╋", .middle_top = "┳", .middle_bottom = "┻", };
/// Double-stroke border (lipgloss `DoubleBorder`). pub const double: Style = .{ .top = "═", .bottom = "═", .left = "║", .right = "║", .top_left = "╔", .top_right = "╗", .bottom_left = "╚", .bottom_right = "╝", .middle_left = "╠", .middle_right = "╣", .middle = "╬", .middle_top = "╦", .middle_bottom = "╩", };
/// Solid block border occupying the whole frame (lipgloss `BlockBorder`). pub const block: Style = .{ .top = "█", .bottom = "█", .left = "█", .right = "█", .top_left = "█", .top_right = "█", .bottom_left = "█", .bottom_right = "█", .middle_left = "█", .middle_right = "█", .middle = "█", .middle_top = "█", .middle_bottom = "█", };
/// Half-block border that sits outside the frame (lipgloss /// `OuterHalfBlockBorder`). Has no middle pieces. pub const outer_half_block: Style = .{ .top = "▀", .bottom = "▄", .left = "▌", .right = "▐", .top_left = "▛", .top_right = "▜", .bottom_left = "▙", .bottom_right = "▟", .middle_left = "", .middle_right = "", .middle = "", .middle_top = "", .middle_bottom = "", };
/// Half-block border that sits inside the frame (lipgloss /// `InnerHalfBlockBorder`). Has no middle pieces. pub const inner_half_block: Style = .{ .top = "▄", .bottom = "▀", .left = "▐", .right = "▌", .top_left = "▗", .top_right = "▖", .bottom_left = "▝", .bottom_right = "▘", .middle_left = "", .middle_right = "", .middle = "", .middle_top = "", .middle_bottom = "", };
/// ASCII-only border (lipgloss `ASCIIBorder`). pub const ascii: Style = .{ .top = "-", .bottom = "-", .left = "|", .right = "|", .top_left = "+", .top_right = "+", .bottom_left = "+", .bottom_right = "+", .middle_left = "+", .middle_right = "+", .middle = "+", .middle_top = "+", .middle_bottom = "+", };
/// Markdown-table border (lipgloss `MarkdownBorder`). Disable the top and /// bottom edges for valid markdown output. pub const markdown: Style = .{ .top = "-", .bottom = "-", .left = "|", .right = "|", .top_left = "|", .top_right = "|", .bottom_left = "|", .bottom_right = "|", .middle_left = "|", .middle_right = "|", .middle = "|", .middle_top = "|", .middle_bottom = "|", };
/// Blank border of single-cell spaces — keeps layout positioning without a /// visible border (lipgloss `HiddenBorder`). pub const hidden: Style = .{ .top = " ", .bottom = " ", .left = " ", .right = " ", .top_left = " ", .top_right = " ", .bottom_left = " ", .bottom_right = " ", .middle_left = " ", .middle_right = " ", .middle = " ", .middle_top = " ", .middle_bottom = " ", }; };
const Options = struct { edges: Edges = .all, foreground: Colors = .{}, background: Colors = .{}, };
/// Every segment of a [Border], for bulk operations like [Border.colored]. const Section = enum { top, bottom, left, right, middle_left, middle_right, middle_top, middle_bottom, middle, top_left, top_right, bottom_left, bottom_right,
pub const all = [_]Section{ .top, .bottom, .left, .right, .middle, .middle_left, .middle_right, .middle_top, .middle_bottom, .top_left, .top_right, .bottom_left, .bottom_right, };
pub fn select(sections: []const Section) []const Section { return sections; } };
/// The four outer edges, for enabling or disabling them individually. const Edge = enum { top, right, bottom, left };
const Edges = struct { top: bool = true, right: bool = true, bottom: bool = true, left: bool = true,
pub const all: Edges = .{}; pub const off: Edges = .{ .top = false, .right = false, .bottom = false, .left = false, }; };
// /// Sets the foreground of one segment (lipgloss // /// `BorderTopForeground`/`BorderTopLeftForeground`/etc.). // pub fn fg(b: Border, comptime section: Section, color: ?Color) Border { // var copy = b; // @field(copy, @tagName(section)).foreground = color; // return copy; // } // // /// Sets the background of one segment (lipgloss // /// `BorderTopBackground`/etc.). // pub fn bg(b: Border, comptime section: Section, color: ?Color) Border { // var copy = b; // @field(copy, @tagName(section)).background = color; // return copy; // }
// /// Applies one foreground/background pair to every segment — the common case // /// (lipgloss `BorderForeground`/`BorderBackground`). Drop to [Border.fg] / // /// [Border.bg] to recolor a single edge or corner. // pub fn colored(b: Border, fg_color: ?Color, bg_color: ?Color) Border { // var copy = b; // inline for (Section.all) |section| { // const seg: *Segment = &@field(copy, @tagName(section)); // seg.foreground = fg_color; // seg.background = bg_color; // } // return copy; // } // // /// Enables or disables all four outer edges at once. // pub fn edges(b: Border, on: bool) Border { // var copy = b; // copy.enabled = if (on) .all else .off; // return copy; // } // // pub fn new(border: New) Border { // return .{ // .top = .set(border.top), // .bottom = .set(border.bottom), // .left = .set(border.left), // .right = .set(border.right), // .top_left = .set(border.top_left), // .top_right = .set(border.top_right), // .bottom_left = .set(border.bottom_left), // .bottom_right = .set(border.bottom_right), // .middle_left = .set(border.middle_left), // .middle_right = .set(border.middle_right), // .middle = .set(border.middle), // .middle_top = .set(border.middle_top), // .middle_bottom = .set(border.middle_bottom), // }; // } // // const New = struct { // top: []const u8 = " ", // bottom: []const u8 = " ", // left: []const u8 = " ", // right: []const u8 = " ", // top_left: []const u8 = " ", // top_right: []const u8 = " ", // bottom_left: []const u8 = " ", // bottom_right: []const u8 = " ", // middle_left: []const u8 = " ", // middle_right: []const u8 = " ", // middle: []const u8 = " ", // middle_top: []const u8 = " ", // middle_bottom: []const u8 = " ", // };};
pub const Padding = struct { spacing: Spacing = .{}, char: u21 = ' ',
pub const none: Padding = .{};};
pub const Margin = struct { spacing: Spacing = .{}, char: u21 = ' ', color: ?Color = null,
pub const none: Margin = .{}; pub const unset: Margin = .{};
pub fn colored(m: Margin, color: ?Color) Margin { var copy = m; copy.color = color; return copy; }};
/// Which edges a spacing change applies to. Only used for padding and margin;/// borders are addressed by [Border.Edge]/[Border.Section] instead, since a/// border segment is not an edge.pub const Side = enum { top, right, bottom, left, horizontal, vertical, all,};
pub const Spacing = struct { top: u16 = 0, right: u16 = 0, bottom: u16 = 0, left: u16 = 0,
pub const none: Spacing = .{};
pub fn set(s: *Spacing, side: Side, cells: u16) void { switch (side) { .top => s.top = cells, .right => s.right = cells, .bottom => s.bottom = cells, .left => s.left = cells, .horizontal => { s.left = cells; s.right = cells; }, .vertical => { s.top = cells; s.bottom = cells; }, .all => { s.top = cells; s.right = cells; s.bottom = cells; s.left = cells; }, } }};
/// Toggle argument for the boolean attributes. Stored as a plain `bool` on/// [Internal]; this enum only exists so call sites read `.set`/`.unset` and line/// up with `Strikethrough.set`/`Strikethrough.unset`.const State = enum { unset, set,
pub fn on(s: State) bool { return s == .set; }};