hovercard

package
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 15, 2026 License: MIT Imports: 8 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package hovercard is a card of details shown next to the thing it describes while the pointer is over that thing: a preview of a link, a profile behind a name. It is an overlay: Render draws it on top of a finished frame, under its target, or above when there is no room below.

It differs from tooltip in what it holds and how long it stays. It has a title and a body the caller has drawn, and it stays open while the pointer is on the card itself, so the pointer can move from the target onto it.

The pointer opens it only when the program reports motion with no button held, tui.WithMouse(tui.MouseAllMotion). Without a mouse, Show it from a key of the app's own and let Esc close it.

Stability: experimental. Its API may change in any minor release.

Example

The pointer moving onto the name opens its card; Esc closes it.

package main

import (
	"fmt"
	"strings"

	"github.com/ows4444/tui"
	"github.com/ows4444/tui/ansi"
	"github.com/ows4444/tui/hittest"
	"github.com/ows4444/tui/hovercard"
)

func main() {
	view := "by @ada" + strings.Repeat("\n"+strings.Repeat(" ", 24), 6)
	card := hovercard.New("Ada Lovelace", "Wrote the first\nprogram", hittest.Rect{X: 3, Y: 0, W: 4, H: 1})
	card.Mouse, card.Bounds = true, hittest.Rect{W: 24, H: 7}
	card, _ = card.Update(tui.MouseEvent{X: 4, Y: 0, Action: tui.MouseActionMotion})
	// Rows are trimmed of the view's blank cells for the comparison.
	for _, row := range strings.Split(ansi.StripANSI(card.Render(view)), "\n") {
		fmt.Println(strings.TrimRight(row, " "))
	}
	card, _ = card.Update(tui.Key{Type: tui.KeyEsc})
	fmt.Println(card.Open())
}
Output:
by @ada
   ┌─────────────────┐
   │ Ada Lovelace    │
   │                 │
   │ Wrote the first │
   │ program         │
   └─────────────────┘
false

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ClosedMsg

type ClosedMsg struct {
	// ID is the card's ID.
	ID string
}

ClosedMsg is delivered (via the Cmd Update returns) when the card closes by the pointer or by Esc. Hide delivers nothing.

type KeyMap

type KeyMap struct {
	Close keymap.Binding // close the card and deliver ClosedMsg
}

KeyMap names the keys of each action of a Model.

func DefaultKeyMap

func DefaultKeyMap() KeyMap

DefaultKeyMap returns Esc.

type Model

type Model struct {
	// ID is carried by ClosedMsg, to tell one card's closing from another's.
	ID string
	// Title heads the card, in bold. Empty leaves the heading out.
	Title string
	// Content is the body: text the caller has already drawn, of any number
	// of lines. Its colours and attributes are kept and every other escape
	// sequence is dropped.
	Content string
	// Width, when above 0, wraps Content to that many cells. Zero or less
	// keeps each line as it is.
	Width int
	// Target is the screen rectangle of the thing the card describes. The
	// card is drawn under it and lined up with its left edge.
	Target hittest.Rect
	Theme  theme.Theme

	// KeyMap holds the keys that close the card. New fills it with
	// DefaultKeyMap; a Model built as a struct literal with a zero KeyMap
	// behaves as if it held DefaultKeyMap.
	KeyMap KeyMap

	// Mouse, when true, makes Update handle tui.MouseEvent: the pointer
	// moving onto Target opens the card; moving off both Target and the
	// card, or a press outside both, closes it. Off (the default) ignores
	// the mouse.
	Mouse bool
	// Bounds is the screen rectangle of the base view the card is rendered
	// over.
	Bounds hittest.Rect
	// contains filtered or unexported fields
}

Model is one hover card. The zero value is closed and empty; build one with New.

func New

func New(title, content string, target hittest.Rect) Model

New returns a closed card with the given title and content for the thing drawn at target.

func (Model) Bindings

func (m Model) Bindings() []keymap.Binding

Bindings returns the actions the card honours now, with descriptions, for help text. A closed card honours none.

func (*Model) Hide

func (m *Model) Hide()

Hide closes the card. No ClosedMsg is delivered.

func (Model) LayoutNode

func (m Model) LayoutNode() layout.Node

LayoutNode returns the card as an ordinary layout.Node, for an app that places it itself. While the card is closed it measures as nothing. The Model is not changed.

func (Model) Linearize

func (m Model) Linearize() string

Linearize renders the card as plain text for accessible output (see tui.Linearizer): its title and its content without styling, or "" while it is closed.

func (Model) Open

func (m Model) Open() bool

Open reports whether the card is shown.

func (Model) Rect

func (m Model) Rect() hittest.Rect

Rect is the screen rectangle the card takes when rendered over a base view filling Bounds. It is the zero Rect while the card is closed.

func (Model) Render

func (m Model) Render(base string) string

Render draws the card over base: a bordered box, its top-left corner under Target's bottom-left, so the pointer can move from one onto the other without crossing a gap. A card that would pass base's right edge is moved left, and one that would pass its last row is drawn above Target. A closed card returns base unchanged.

func (Model) SetTheme

func (m Model) SetTheme(t theme.Theme) Model

SetTheme returns m with t applied. It makes Model a tui.ThemeSetter, so a root model can forward the Program's theme (see tui.WithTheme).

func (*Model) Show

func (m *Model) Show()

Show opens the card.

func (Model) Tokens

func (m Model) Tokens() theme.Tokens

Tokens returns the colour tokens the widget renders with: its theme's roles, overridden by any theme.WithTokens(theme.ComponentHoverCard, ...) and then by WithTokens.

func (Model) Update

func (m Model) Update(msg tui.Msg) (Model, tui.Cmd)

Update opens the card when the pointer moves onto Target and closes it when the pointer moves off both Target and the card, or a button is pressed outside both, when Mouse is on. Esc closes an open card. It does not keep a message from the widgets behind it: forward the same message to them.

func (Model) WithTokens

func (m Model) WithTokens(tok theme.Tokens) Model

WithTokens returns m with tok as its per-instance colour override. Nil fields inherit from the theme, so only the roles tok names change; the theme and every other widget are untouched. A second call replaces the first.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL