Style definitions for nice terminal layouts 👄

Overview

blipgloss

Style definitions for nice terminal layouts. Powered by lipgloss and bun:ffi.

Install

bun add blipgloss

Usage

Blipgloss takes an expressive, declarative approach to terminal rendering. Users familiar with CSS will feel at home with Blipgloss.

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."))

demo

Colors

Blipgloss supports the following color profiles:

ANSI 16 colors (4-bit)

Background("5")  // magenta
Background("9")  // red
Background("12") // light blue

ANSI 256 colors (8-bit)

Background("86")  // aqua
Background("201") // hot pink
Background("202") // orange

True Color (16,777,216 colors; 24-bit)

Background("#0000FF") // good ol' 100% blue
Background("#04B575") // a green
Background("#3C3C3C") // a dark gray

...as well as a 1-bit Ascii profile, which is black and white only.

The terminal's color profile will be automatically detected, and colors outside the gamut of the current palette will be automatically coerced to their closest available value.

Adaptive Colors

You can also specify color options for light and dark backgrounds:

Background({
  Light: '236',
  Dark: '248'
})

The terminal's background color will automatically be detected and the appropriate color will be chosen at runtime.

Inline Formatting

Blipgloss supports the usual ANSI text formatting options:

const style = NewStyle()
  .Bold(true)
  .Italic(true)
  .Faint(true)
  .Blink(true)
  .Strikethrough(true)
  .Underline(true)
  .Reverse(true)

Block-Level Formatting

Blipgloss also supports rules for block-level formatting:

// Padding
const style = NewStyle()
  .PaddingTop(2)
  .PaddingRight(4)
  .PaddingBottom(2)
  .PaddingLeft(4)

// Margins
const style = NewStyle()
  .MarginTop(2)
  .MarginRight(4)
  .MarginBottom(2)
  .MarginLeft(4)

There is also shorthand syntax for margins and padding, which follows the same format as CSS:

// 2 cells on all sides
NewStyle().Padding(2)

// 2 cells on the top and bottom, 4 cells on the left and right
NewStyle().Margin(2, 4)

// 1 cell on the top, 4 cells on the sides, 2 cells on the bottom
NewStyle().Padding(1, 4, 2)

// Clockwise, starting from the top: 2 cells on the top, 4 on the right, 3 on
// the bottom, and 1 on the left
NewStyle().Margin(2, 4, 3, 1)

Aligning Text

You can align paragraphs of text to the left, right, or center.

import { Position } from 'blipgloss'

const style = NewStyle()
  .Width(24)
  .Align(Position.Left)   // align it left
  .Align(Position.Right)  // no wait, align it right
  .Align(Position.Center) // just kidding, align it in the center

Width and Height

Setting a minimum width and height is simple and straightforward.

const str = NewStyle()
  .Width(24)
  .Height(32)
  .Foreground("63")
  .Render("What’s for lunch?")

Borders

Adding borders is easy:

// Add a purple, rectangular border
const style = NewStyle()
  .BorderStyle('normal')
  .BorderForeground("63")

// Set a rounded, yellow-on-purple border to the top and left
const anotherStyle = NewStyle()
  .BorderStyle('rounded')
  .BorderForeground("228")
  .BorderBackground("63")
  .BorderTop(true).
  .BorderLeft(true)

// Make your own border
const style = NewStyle()
  .BorderStyle({
    Top:         "._.:*:",
    Bottom:      "._.:*:",
    Left:        "|*",
    Right:       "|*",
    TopLeft:     "*",
    TopRight:    "*",
    BottomLeft:  "*",
    BottomRight: "*",
  })

Copying Styles

Just use Copy():

const style = NewStyle().Foreground("219")

const wildStyle = style.Copy().Blink(true)

Copy() performs a copy on the underlying data structure ensuring that you get a true, dereferenced copy of a style. Without copying it's possible to mutate styles.

Unsetting Rules

All rules can be unset:

const style = NewStyle().
    Bold(true).                        // make it bold
    UnsetBold().                       // jk don't make it bold
    Background("227"). // yellow background
    UnsetBackground()                  // never mind

Enforcing Rules

Sometimes, such as when developing a component, you want to make sure style definitions respect their intended purpose in the UI. This is where Inline and MaxWidth, and MaxHeight come in:

// Force rendering onto a single line, ignoring margins, padding, and borders.
someStyle.Inline(true).Render("yadda yadda")

// Also limit rendering to five cells
someStyle.Inline(true).MaxWidth(5).Render("yadda yadda")

// Limit rendering to a 5x5 cell block
someStyle.MaxWidth(5).MaxHeight(5).Render("yadda yadda")

Rendering

Generally, you just call the Render(string) method:

console.log(NewStyle().Bold(true).Render("Hello, bun."))

Utilities

In addition to pure styling, Lip Gloss also ships with some utilities to help assemble your layouts.

Joining Paragraphs

Horizontally and vertically joining paragraphs is a cinch.

import { Position, JoinHorizontal, JoinVertical } from 'blipgloss'

// Horizontally join three paragraphs along their bottom edges
JoinHorizontal(Position.Bottom, paragraphA, paragraphB, paragraphC)

// Vertically join two paragraphs along their center axes
JoinVertical(Position.Center, paragraphA, paragraphB)

// Horizontally join three paragraphs, with the shorter ones aligning 20%
// from the top of the tallest
JoinHorizontal(0.2, paragraphA, paragraphB, paragraphC)

Measuring Width and Height

Sometimes you’ll want to know the width and height of text blocks when building your layouts.

import { NewStyle, Width, Height } from 'blipgloss'

const block = NewStyle()
  .Width(40)
  .Padding(2)
  .Render(someLongString)

// Get the actual, physical dimensions of the text block.
const width = Width(block)
const height = Height(block)

License

MIT

You might also like...

Snippets4Dummies is an easy to use Visual Code Extension which is used for building beautiful layouts as fast as your crush rejects you!

Why Snippets4Dummies? Snippets4Dummies is an easy to use Visual Code Extension which is used for building beautiful layouts as fast as your crush reje

Oct 11, 2022

sorry Stanley, but PirateStealer is a nice project and cannot be deleted

sorry Stanley, but PirateStealer is a nice project and cannot be deleted

PirateStealer PirateStealer got deleted by his creator who I love but this project is too cool to be deleted, sorry Stanley please forgive me What is

May 15, 2022

A Typescript assembly(ish) interpreter with a nice web UI

Assembly-Online Example Fibonacci Program MOV R0, #1 MOV R1, #1 fib: MOV R2, R0 ADD R0, R0, R1 MOV R1, R2 B print CMP R0, #40 BGT exit B fib exit: \ I

May 23, 2022

JustGage is a handy JavaScript plugin for generating and animating nice & clean dashboard gauges. It is based on Raphaël library for vector drawing.

JustGage is a handy JavaScript plugin for generating and animating nice & clean dashboard gauges. It is based on Raphaël library for vector drawing.

JustGage JustGage is a handy JavaScript plugin for generating and animating nice & clean dashboard gauges. It is based on Raphaël library for vector d

Jan 3, 2023

A cross-platform desktop app with a nice interface to Stable Diffusion and others

A cross-platform desktop app with a nice interface to Stable Diffusion and others

GenerationQ GenerationQ (for "image generation queue") is a cross-platform desktop application (screens below) designed to provide a general purpose G

Dec 28, 2022

nice-dag is a lightweight javascript library, which is used to present a DAG diagram.

nice-dag is a lightweight javascript library, which is used to present a DAG diagram.

Nice-DAG Overview Nice-DAG is a lightweight javascript library, which is used to present a DAG diagram. Essentially, it uses dagre to layout nodes and

Oct 25, 2022

FortBlog adds a nice UI where you can manage a publication of any size with posts, pages, tags, and authors

FortBlog adds a nice UI where you can manage a publication of any size with posts, pages, tags, and authors

FortBlog adds a nice UI where you can manage a publication of any size with posts, pages, tags, and authors. You can add photos, code blocks, featured images, social media & SEO attributes, embedded HTML (YouTube Videos, Embedded Podcasts Episodes, Tweets, ...), and markdown! Dark & Light modes available so everyone is happy

Jan 2, 2023

Base62-token.js - Generate & Verify GitHub-style & npm-style Base62 Tokens

base62-token.js Generate & Verify GitHub-style & npm-style Secure Base62 Tokens Works in Vanilla JS (Browsers), Node.js, and Webpack. Online Demo See

Jun 11, 2022

🥰 Mini world simulator is a terminal application made in JavaScript to control the world that is being generated.

Mini-world "Simulator" Mini world simulator is a terminal application made in JavaScript to control the world that is being generated. It has no other

Mar 14, 2022
Releases(v0.1.9)
Owner
Robert Soriano
What compiles today may not compile tomorrow.
Robert Soriano
Grupprojekt för kurserna 'Javascript med Ramverk' och 'Agil Utveckling'

JavaScript-med-Ramverk-Laboration-3 Grupprojektet för kurserna Javascript med Ramverk och Agil Utveckling. Utvecklingsguide För information om hur utv

Svante Jonsson IT-Högskolan 3 May 18, 2022
Hemsida för personer i Sverige som kan och vill erbjuda boende till människor på flykt

Getting Started with Create React App This project was bootstrapped with Create React App. Available Scripts In the project directory, you can run: np

null 4 May 3, 2022
Kurs-repo för kursen Webbserver och Databaser

Webbserver och databaser This repository is meant for CME students to access exercises and codealongs that happen throughout the course. I hope you wi

null 14 Jan 3, 2023
Generate type definitions compatible with @kintone/rest-api-client

kintone-form-model-generator Generate type definitions compatible with @kintone/rest-api-client Prerequirements Node.js (>=12) Install # Install npm i

Yuuki Takahashi 5 Dec 15, 2022
TypeScript type definitions for Bun's JavaScript runtime APIs

Bun TypeScript type definitions These are the type definitions for Bun's JavaScript runtime APIs. Installation Install the bun-types npm package: # ya

Oven 73 Dec 16, 2022
A script that combines a folder of SVG files into a single sprites file and generates type definitions for safe usage.

remix-sprites-example A script that combines a folder of .svg files into a single sprites.svg file and type definitions for safe usage. Technical Over

Nicolas Kleiderer 19 Nov 9, 2022
Opinionated collection of TypeScript definitions and utilities for Deno and Deno Deploy. With complete types for Deno/NPM/TS config files, constructed from official JSON schemas.

Schemas Note: You can also import any type from the default module, ./mod.ts deno.json import { type DenoJson } from "https://deno.land/x/[email protected]

deno911 2 Oct 12, 2022
A refined tool for exploring open-source projects on GitHub with a file tree, rich Markdown and image previews, multi-pane multi-tab layouts and first-class support for Ink syntax highlighting.

Ink codebase browser, "Kin" ?? The Ink codebase browser is a tool to explore open-source code on GitHub, especially my side projects written in the In

Linus Lee 20 Oct 30, 2022
Lit + File Based Routing + Nested Layouts

Lit File Based Routing Lit router for nested layouts and file based routing. Similar to https://remix.run/ but at client side. Demo Package Archived i

Rody Davis 9 Jan 14, 2022
Freewall is a cross-browser and responsive jQuery plugin to help you create grid, image and masonry layouts for desktop, mobile, and tablet...

Freewall Freewall is a cross-browser and responsive jQuery plugin to help you create many types of grid layouts: flexible layouts, images layouts, nes

Minh Nguyen 1.9k Dec 27, 2022