help

package
v0.4.2 Latest Latest
Warning

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

Go to latest
Published: Nov 29, 2026 License: Apache-2.0 Imports: 17 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 help shows a book of Markdown pages in a window, as the F1 help of Midnight Commander does.

A Book is read from an fs.FS: every file "<id>.md" at its root is a page, and its first line is the "# Title" heading. Pages link to each other with relative links such as [Row filter](row-filter.md), which also work when the files are read on a forge. The page index.md (Index) is the table of contents, and help.md (Help) explains the help itself. A program usually embeds its pages:

//go:embed pages/*.md
var files embed.FS

sub, _ := fs.Sub(files, "pages")
book, err := help.NewBook(sub)

Book.Search finds the pages that hold a text, the pages whose title holds it first, and Book.SearchPage turns the result into a page with a link to each match. Book.Check reports broken links; a program runs it in a test of its pages.

A Viewer is a tea.Model that shows the book. Its text wraps at MaxTextWidth or the width it gets, whichever is less, so a window of WindowWidth columns is wide enough. Tab and Shift-Tab select links in mc's colors (black on cyan, the selected one white on blue), Enter or a click follows one, F3 returns to the page before, F2 opens the index, F1 the page about the help, F7 types a text to search for on the last row, and F10 calls the function set with SetClose. The program draws the viewer inside a frame, passes it the keys and mouse events of the window, draws FooterHints as the key bar, and lets HandleEscape end a search before Esc closes the window.

Example

A program reads its pages into a book, searches it, and shows it in a viewer. Here the pages come from a map; a program usually embeds them.

package main

import (
	"fmt"
	"strings"
	"testing/fstest"

	tea "charm.land/bubbletea/v2"
	"github.com/charmbracelet/x/ansi"

	"github.com/stokaro/kc/tui/help"
)

func main() {
	book, err := help.NewBook(fstest.MapFS{
		"index.md": {Data: []byte("# Index\n\n- [Panels](panels.md)\n- [Using the help](help.md)\n")},
		"help.md":  {Data: []byte("# Using the help\n\nTab selects a link, Enter follows it.\n")},
		"panels.md": {Data: []byte("# Panels\n\nTwo panels side by side. " +
			"Tab moves to the other panel. Back to the [index](index.md).\n")},
	})
	if err != nil {
		fmt.Println(err)
		return
	}
	for _, m := range book.Search("tab") {
		fmt.Printf("%s: %d match\n", m.Page.Title, m.Count)
	}
	fmt.Println("check:", book.Check(40))

	v := help.NewViewer(book, help.Index)
	v.SetDimensions(30, 6)
	v.Update(tea.KeyPressMsg(tea.Key{Code: tea.KeyEnter})) // follow the first link
	fmt.Println("page:", v.Page())
	for line := range strings.SplitSeq(ansi.Strip(v.View().Content), "\n") {
		fmt.Printf("|%s|\n", line)
	}
}
Output:
Panels: 1 match
Using the help: 1 match
check: <nil>
page: panels
|  Panels                      |
|                              |
| Two panels side by side. Tab |
| moves to the other panel.    |
| Back to the index.           |
|                              |

Index

Examples

Constants

View Source
const (
	// MaxTextWidth is the widest the text gets.
	MaxTextWidth = 76

	// WindowWidth is the widest window the viewer needs: the text, its
	// padding and a frame of one column on each side.
	WindowWidth = MaxTextWidth + 2*padX + 2
)

Like mc's help window, the text wraps at a fixed width however wide the terminal is, and the program centers the window around it.

Variables

This section is empty.

Functions

func ReplaceLinks(md string, repl func(text, dest string) string) string

ReplaceLinks rewrites every inline link of md with what repl returns for its text and destination. Images stay as they are.

Types

type Book

type Book struct {
	// contains filtered or unexported fields
}

Book is a set of help pages. It does not change after NewBook, so its methods are safe for concurrent use.

func NewBook

func NewBook(fsys fs.FS) (*Book, error)

NewBook reads every file at the root of fsys whose name ends in ".md" as a page; other files and directories are left out. A page's first line is its "# Title" heading. On a read error it returns the pages it could read along with an error that names each file it could not.

func (*Book) Check

func (b *Book) Check(width int) error

Check renders every page of the book at the text width width, as the viewer does, and reports each link to a page the book lacks, each link the rendered page does not show where it can be selected, and each page with no "# Title" heading. A program calls it from a test of its pages.

func (*Book) Lookup

func (b *Book) Lookup(id ID) (Page, bool)

Lookup returns the page id names. A nil book has no pages.

func (*Book) Pages

func (b *Book) Pages() []Page

Pages returns every page, sorted by ID.

func (*Book) Search

func (b *Book) Search(query string) []Match

Search returns the pages whose text holds query, ignoring case: the pages whose title holds it first, then those with the most matches, then by title. Link targets do not count, only the link text.

func (*Book) SearchPage

func (b *Book) SearchPage(query string) string

SearchPage returns the Markdown of a page of the matches of query: a link to each page with the number of matches and the first line that holds it. With no match it links the index.

type FooterHint

type FooterHint struct {
	Key     string
	Label   string
	Enabled bool
}

FooterHint is a function key of the viewer's key bar and whether it does something now.

type ID

type ID string

ID names a page: the base name of its file without ".md".

const (
	// Index is the book's table of contents; F2 opens it, and the viewer
	// falls back to it for a page the book lacks.
	Index ID = "index"
	// Help is the page about the help itself; F1 in the viewer opens it.
	Help ID = "help"
	// SearchID names the page SearchPage builds. Book.Lookup does not find
	// it unless the book has a file of that name.
	SearchID ID = "search"
)

Pages the viewer opens by name.

func TargetOf

func TargetOf(dest string) (ID, bool)

TargetOf returns the page a link destination names: "row-filter.md" and "./row-filter.md" name row-filter. Other destinations (URLs, anchors, files elsewhere) name no page.

type Link struct {
	Text   string
	Target ID
}

Link is a link from one page to another.

func Links(md string) []Link

Links returns the links to other pages in md, in order. Links inside code are included; pages keep links out of code.

type Match

type Match struct {
	Page  Page
	Count int    // how often the text occurs, link targets not counted
	Line  string // the first line that holds it, without markup
}

Match is a page that holds the searched text.

type Page

type Page struct {
	ID       ID
	Title    string // the text of the first "# " heading
	Markdown string // the whole page, heading included
}

Page is one help page.

type Viewer

type Viewer struct {
	// contains filtered or unexported fields
}

Viewer shows the pages of a book in a window, as mc's help does. Tab moves between links, Enter follows one, F3 goes back, F2 opens the index, F1 the page about the help itself, F7 searches every page and F10 closes the window. It is a tea.Model; the program draws its View inside a frame and passes it the keys and mouse events while the window is open.

func NewViewer

func NewViewer(book *Book, page ID) *Viewer

NewViewer returns a viewer of book showing page, or the index when book has no such page.

func (*Viewer) Back

func (v *Viewer) Back() bool

Back returns to the previous page; it reports false when there is none.

func (*Viewer) FooterHints

func (v *Viewer) FooterHints() []FooterHint

FooterHints is the viewer's own key bar, as in mc's help: F1, F2, F3, F7 and F10, each enabled when it does something on the page shown. The program draws it under the window.

func (*Viewer) HandleEscape

func (v *Viewer) HandleEscape(tea.KeyMsg) (bool, tea.Cmd)

HandleEscape ends a search and reports true. Without a search it reports false, and the window around the viewer handles Esc, usually by closing.

func (*Viewer) Init

func (v *Viewer) Init() tea.Cmd

Init implements tea.Model.

func (*Viewer) Open

func (v *Viewer) Open(page ID)

Open shows page; F3 returns to the page shown before.

func (*Viewer) Page

func (v *Viewer) Page() ID

Page returns the page shown.

func (*Viewer) Reset

func (v *Viewer) Reset(page ID)

Reset shows page with an empty history, as on a fresh F1.

func (*Viewer) SetClose

func (v *Viewer) SetClose(f func() tea.Cmd)

SetClose sets what F10 calls to close the window.

func (*Viewer) SetDimensions

func (v *Viewer) SetDimensions(w, h int)

SetDimensions applies the content area inside the frame.

func (*Viewer) TextWidth

func (v *Viewer) TextWidth() int

TextWidth returns the width the text wraps at.

func (*Viewer) Update

func (v *Viewer) Update(msg tea.Msg) (tea.Model, tea.Cmd)

Update handles the keys and the mouse.

func (*Viewer) View

func (v *Viewer) View() tea.View

View renders the visible lines with the links in their colors.

Jump to

Keyboard shortcuts

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