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 ¶
- Constants
- func ReplaceLinks(md string, repl func(text, dest string) string) string
- type Book
- type FooterHint
- type ID
- type Link
- type Match
- type Page
- type Viewer
- func (v *Viewer) Back() bool
- func (v *Viewer) FooterHints() []FooterHint
- func (v *Viewer) HandleEscape(tea.KeyMsg) (bool, tea.Cmd)
- func (v *Viewer) Init() tea.Cmd
- func (v *Viewer) Open(page ID)
- func (v *Viewer) Page() ID
- func (v *Viewer) Reset(page ID)
- func (v *Viewer) SetClose(f func() tea.Cmd)
- func (v *Viewer) SetDimensions(w, h int)
- func (v *Viewer) TextWidth() int
- func (v *Viewer) Update(msg tea.Msg) (tea.Model, tea.Cmd)
- func (v *Viewer) View() tea.View
Examples ¶
Constants ¶
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 ¶
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 ¶
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 ¶
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) Search ¶
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 ¶
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 {
}
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.
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 ¶
NewViewer returns a viewer of book showing page, or the index when book has no such page.
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 ¶
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) SetDimensions ¶
SetDimensions applies the content area inside the frame.