Skip to content

Utils.Class.Layout

sravioli edited this page Oct 27, 2024 · 2 revisions

Overview

The Utils.Class.Layout module is a Lua class designed to simplify the creation and management of formatted text layouts in Wezterm. It abstracts the complexity of handling Wezterm's FormatItem objects by providing a clear and concise API.

Usage

-- Layout used when formatting the tab title.
-- Some code has been removed.

local wt = require "wezterm"

local Utils = require "utils"
local str = Utils.fn.str
local Icon = Utils.class.icon
local tabicons = Icon.Sep.tb

wt.on("format-tab-title", function(tab, _, _, config, hover, max_width)
  -- [...]

  local Title = Utils.class.layout:new "TabTitle"

  -- [...]

  local title = str.format_tab_title(tab, config, max_width)

  Title:push(bg, fg, tab_idx == 0 and tabicons.leftmost or tabicons.left, attributes)
  Title:push(fg, bg, (unseen_output and Icon.Notification or Icon.Nums[tab_idx + 1] or "") .. " ", attributes)
  Title:push(fg, bg, title, attributes)
  Title:push(bg, fg, Icon.Sep.block .. tabicons.right, attributes)

  return Title
end)

Features

  • Simplified Layout Management: Provides a straightforward way to build and manage text layouts with various attributes.
  • Attribute Support: Supports all attributes defined by Wezterm's FormatItem, including underline styles, text intensity, and color settings.
  • Color Handling: Allows defining colors using ANSI colors, named colors, RGB values, or Wezterm's color objects.
  • Logging: Includes logging capabilities to help debug layout creation and formatting processes.

Class Structure

The Utils.Class.Layout class is designed to encapsulate the complexity of Wezterm's formatting options, making it easier to manage and debug text layouts.

Attributes

The class supports the following attributes for text styling:

  • "None": Reset attributes.
  • "NoUnderline": No underline.
  • "Single": Single underline.
  • "Double": Double underline.
  • "Curly": Curly underline.
  • "Dotted": Dotted underline.
  • "Dashed": Dashed underline.
  • "Normal": Normal intensity.
  • "Bold": Bold intensity.
  • "Half": Half intensity.
  • "Italic": Italic text.
  • "NoItalic": No italic text.

Methods

:new(name?)

Creates a new Utils.Class.Layout instance.

Parameters

  • name [string] (optional) - (default: "Layout") name of the layout; used when logging.

Returns

  • instance [Utils.Class.Layout] - new class instance.

Usage

local layout = Layout:new("LeftStatus")

:push(background, foreground, text, attributes?)

Adds a text element to the layout with specified background and foreground colors and optional attributes.

Parameters

  • background [string] - background color of the element
  • foreground [string] - foreground color of the element
  • text [string] - text to add.
  • attributes [table] (optional) - list of attributes to style text.

Returns

  • self [Utils.Class.Layout] - updated class instance.

Usage

layout:push("Black", "White", "Hello World", { "Bold", "Italic" })

:clear()

Clears all elements from the layout.

Returns

  • self [Utils.Class.Layout] - cleared class instance.

Usage

layout:clear()

:format()

Formats the layout using Wezterm's wezterm.format().

Returns

  • format [string] - formatted layout.

Usage

local formatted = layout:format()

:debug()

Logs the layout to Wezterm's debug console.

Parameters

  • formatted [Boolean] (optional) - whether to log layout formatted or not.

Usage

layout:debug(true)