blipgloss

repository·main·Indexed 41 days ago

https://github.com/wobsoriano/blipgloss

A library for creating expressive, declarative terminal layouts using a CSS-like API. Powered by lipgloss and bun:ffi, it supports true color, adaptive themes, and complex layout utilities including padding, margins, borders, and alignment. It provides a chainable API via NewStyle() and layout functions like JoinHorizontal, JoinVertical, and Place to assemble terminal UIs.

Tokens
4.4K
Snippets
20
Records
20
Agent score
53%

What's inside blipgloss

  1. Basic usage of blipgloss

    main

    Blipgloss uses a declarative, chainable API to define terminal styles. You create a style using NewStyle(), apply various properties, and then call .Render(string) to get the styled output.

    import { NewStyle } from 'blipgloss'
    
    const style = NewStyle()
      .Bold(true)
      .Foreground("#FAFAFA")
      .Background("#7D56F4")
      .PaddingTop(2)
      .PaddingLeft(4)
      .Width(22)
    
    console.log(style.Render("Hello, bun."))
  2. Use BlipglossColor for flexible coloring

    main

    The BlipglossColor type allows you to specify colors in multiple formats, providing compatibility across different terminal capabilities:

    • String: A simple color string (e.g., hex code or name).
    • AdaptiveColor: Provides different colors for light and dark terminal themes.
      • { Light: string; Dark: string; }
    • CompleteColor: Provides specific colors for different terminal levels.
      • { True: string; ANSI256: string; ANSI: string; }
    • CompleteAdaptiveColor: Combines adaptive themes with multiple color levels.
      • { Light: CompleteColor; Dark: CompleteColor; }
    import { NewStyle, type BlipglossColor } from 'blipgloss';
    
    const adaptive: BlipglossColor = {
      Light: { True: '#000000', ANSI256: '0', ANSI: '0' },
      Dark: { True: '#ffffff', ANSI256: '255', ANSI: '15' }
    };
    
    const style = NewStyle().Foreground(adaptive);
  3. Configure colors in blipgloss

    main

    Blipgloss supports multiple color profiles. The terminal's color profile is automatically detected, and colors outside the gamut are coerced to the closest available value.

    Color Profiles

    • ANSI 16 (4-bit): Use string IDs like "5", "9", or "12".
    • ANSI 256 (8-bit): Use string IDs like "86" or "201".
    • True Color (24-bit): Use hex codes like "#0000FF".
    • 1-bit Ascii: Black and white only.

    Adaptive and Complete Colors

    You can specify colors that adapt to light or dark terminal backgrounds, or provide fallback values for different color profiles to prevent degradation.

    • Adaptive Colors: Pass an object with Light and Dark keys.
    • Complete Colors: Pass an object with True, ANSI256, and ANSI keys.
    • Complete Adaptive Colors: Combine both for full control across themes.
    // Adaptive Colors
    Background({
      Light: '236',
      Dark: '248'
    })
    
    // Complete Colors
    Background({
      True: "#0000FF",
      ANSI256: "86",
      ANSI: "5"
    })
    
    // Complete Adaptive Colors
    Background({
      Light: {TrueColor: "#d7ffae", ANSI256: "193", ANSI: "11"},
      Dark: {TrueColor: "#d75fee", ANSI256: "163", ANSI: "5"}
    })
  4. Apply inline text formatting

    main

    Use the following methods on a style instance to apply standard ANSI text formatting:

    • .Bold(boolean)
    • .Italic(boolean)
    • `.Faint(boolean)
    • `.Blink(boolean)
    • `.Strikethrough(boolean)
    • `.Underline(boolean)
    • `.Reverse(boolean)
    const style = NewStyle()
      .Bold(true)
      .Italic(true)
      .Faint(true)
      .Blink(true)
      .Strikethrough(true)
      .Underline(true)
      .Reverse(true)
  5. Configure borders in blipgloss

    main

    You can add borders to styles using .BorderStyle().

    Border Styles

    • Border.Normal: A standard rectangular border.
    • Border.Rounded: A rounded border.
    • Custom Border: Pass an object defining characters for Top, Bottom, Left, Right, TopLeft, TopRight, BottomLeft, and BottomRight.

    Border Properties

    • .BorderForeground(color)
    • .BorderBackground(color)
    • .BorderTop(boolean), .BorderLeft(boolean), etc., to enable specific sides.
    import { NewStyle, Border } from 'blipgloss'
    
    // Standard border
    const style = NewStyle()
      .BorderStyle(Border.Normal)
      .BorderForeground("63")
    
    // Custom border
    const custom = NewStyle()
      .BorderStyle({
        Top: "._.:*:",
        Bottom: "._.:*:",
        Left: "|*",
        Right: "|*",
        TopLeft: "*",
        TopRight: "*",
        BottomLeft: "*",
        BottomRight: "*",
      })
  6. Use layout utilities (Join, Measure, Place)

    main

    Blipgloss provides utilities to assemble complex terminal layouts.

    Joining Paragraphs

    • JoinHorizontal(alignment, ...paragraphs): Joins paragraphs horizontally. alignment can be a Position enum or a float (e.g., 0.2 for 20% alignment).
    • JoinVertical(alignment, ...paragraphs): Joins paragraphs vertically.

    Measuring Dimensions

    Use Width(block) and Height(block) to get the physical dimensions of a rendered text block.

    Placing Text in Whitespace

    • PlaceHorizontal(width, alignment, paragraph): Centers/aligns a paragraph within a specific width.
    • PlaceVertical(height, alignment, paragraph): Centers/aligns a paragraph within a specific height.
    • Place(width, height, horizontalAlign, verticalAlign, paragraph): Places a paragraph in a specific 2D coordinate space.
    import { Position, JoinHorizontal, Width, Place } from 'blipgloss'
    
    // Join
    JoinHorizontal(Position.Bottom, p1, p2)
    
    // Measure
    const w = Width(renderedBlock)
    
    // Place
    const block = Place(30, 80, Position.Right, Position.Bottom, fancyParagraph)
  7. Apply block-level formatting (Padding, Margins, Alignment)

    main

    Blipgloss supports CSS-like rules for spacing and alignment.

    Padding and Margins

    You can set individual sides (e.g., .PaddingTop(n)) or use shorthand syntax:

    • .Padding(n): Applies n cells to all sides.
    • .Margin(top, right): Applies n cells to top/bottom and m cells to left/right.
    • .Padding(top, right, bottom): Applies specific values to three sides.
    • .Margin(top, right, bottom, left): Applies specific values to all four sides (clockwise).

    Alignment

    Align text within a defined width using the Position enum:

    • Position.Left
    • Position.Right
    • Position.Center
    import { Position, NewStyle } from 'blipgloss'
    
    // Alignment example
    const style = NewStyle()
      .Width(24)
      .Align(Position.Center)
  8. Manage and enforce style rules

    main

    Copying Styles

    To avoid mutating an existing style, use .Copy(). This performs a true, dereferenced copy of the underlying data structure.

    Unsetting Rules

    Every rule can be removed using an Unset method (e.g., .UnsetBold(), .UnsetBackground()).

    Enforcing Constraints

    Use these methods to ensure components respect UI boundaries:

    • .Inline(boolean): Forces rendering onto a single line, ignoring margins, padding, and borders.
    • .MaxWidth(n): Limits the width to n cells.
    • .MaxHeight(n): Limits the height to n cells.
    // Copying
    const wildStyle = style.Copy().Blink(true)
    
    // Enforcing
    someStyle.Inline(true).MaxWidth(5).Render("text")
  9. Modify style properties with SetIntValue

    main

    Use SetIntValue to update style properties that require an integer, such as Padding or Margin.

    Parameters:

    • fieldPtr: The unique ID of the style.
    • keyPtr: The name of the method to call (e.g., "Padding").
    • value: The integer value.

    Note: This operation updates the style in-place within the internal styleMap.

    // Set padding to 2
    SetIntValue(styleId, "Padding", 2)
  10. Check for dark terminal background

    main

    Use HasDarkBackground() to detect if the user's terminal is currently using a dark theme. This is useful for selecting appropriate AdaptiveColor values.

    import { HasDarkBackground } from 'blipgloss';
    
    if (HasDarkBackground()) {
      console.log("User is in dark mode");
    }
  11. Create a new style with NewStyle

    main

    Use NewStyle to create a new lipgloss.Style instance. It returns a unique string ID (a *C.char) that you must use as a key for all subsequent operations on that style (e.g., setting colors, borders, or rendering text).

    // Returns a unique ID for a new style
    styleId = NewStyle()