history

package
v1.7.0 Latest Latest
Warning

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

Go to latest
Published: Nov 18, 2026 License: MIT Imports: 13 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 history is what a History terminal needs, as mdsh-gui's History pane and mdsh -i have: a terminal where a command typed runs in the document's shell and is added to the end of the document as a block, #history-N, with its output, and a note, lines starting with #, is added as prose.

It asks the shell through an Asker, such as a *run.Session, so what it sees is what blocks left there: the directory, variables and prompts.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddBlock added in v1.7.0

func AddBlock(d *doc.Document, info, body string) (*doc.Block, error)

AddBlock adds a block typed in the terminal, with info and body, to the end of d, as it was typed. It fails, leaving d as it was, for a block with no body or one d can't take.

func AddNote

func AddNote(d *doc.Document, text string, carry bool) error

AddNote adds text to the end of d as prose. With carry, it carries on the paragraph that ends d, as a note added just before does; else it starts a paragraph. It fails, leaving d as it was, if text would change d's blocks, as it would inside a fence left open.

func Background

func Background(input string) (string, bool)

Background reports whether input ends in a lone &, as a command run in the background does, and returns it without the &.

func Block added in v1.7.0

func Block(input string) (info, body string, ok bool)

Block returns the info string and body of input if it is a whole block typed in the terminal: an opening line, its body, and a closing fence.

func BlockPrompt added in v1.7.0

func BlockPrompt(info string) string

BlockPrompt is the prompt on the lines of a block being typed, in place of PS2, so it is plain the lines are kept, not run: the opening fence and the block's language, as "```bash> ".

func InBlock added in v1.7.0

func InBlock(input string) (info string, ok bool)

InBlock reports whether input opens a block and doesn't close it yet, so enter starts another line of it. It returns the info string the block opened with.

func Info

func Info(lang, id string, background bool) string

Info is the info string a command is added with: lang and id; skip, so running the whole file doesn't run it again, though it still runs by hand; links, so the web addresses it prints are clickable; and background if it was typed with a trailing &.

func KeepRuns added in v1.7.0

func KeepRuns(d *doc.Document, b *doc.Block) error

KeepRuns adds history to b's attributes, unless it has history= already, so that running it again keeps the output it has as an earlier run, as many as the front matter's history: says, else 10.

func Lang

func Lang(shell string) string

Lang is the language commands are added as: zsh if the blocks run in shell zsh, else sh.

func NextID

func NextID(d *doc.Document) string

NextID is the id for the next command in d: one past the highest history-N there. A history block that moved has an id of its own, but its output can still link to a file saved under its old one, so those count too: a new block of that number would write over the file.

func Note

func Note(input string) (string, bool)

Note returns the text of input if it is a note: every line starts with #, which is taken off along with a space after it.

func OpenBlock added in v1.7.0

func OpenBlock(line string) (fence, info string, ok bool)

OpenBlock returns the fence and info string of line if it opens a block typed in the terminal.

func RCScript

func RCScript(path string) string

RCScript reads the file at path, if there is one, in the shell, as an interactive shell reads its rc file. A leading ~/ is the home folder. What it prints is dropped, along with any errors, since an rc file often does things only an interactive shell can.

func Reuse added in v1.7.0

func Reuse(d *doc.Document, lang, body string, bg bool) *doc.Block

Reuse is the #history-N block in d that a command, body typed in lang, repeats: the last whose language and code are body's, and that runs in the background only if bg says the command does. It is nil for a command not typed before. With History's reuse setting on, the command runs that block again rather than adding another.

func Shell

func Shell(d *doc.Document, override string) string

Shell is the shell blocks of d run in: override if set, else what d asks for, else /bin/sh.

func Tilde added in v1.7.0

func Tilde(dir string) string

Tilde is dir with the home folder at its start written as ~.

func Whole

func Whole(shell, input string) bool

Whole reports whether input is a whole command, a note or a block, so pressing enter runs or adds it rather than starting another line. Input ending in \, |, && or ||, inside a quote, an if, a loop or a here-document, or inside a block it opens, isn't. shell checks what the simple rules don't.

Types

type Asker

type Asker interface {
	Ask(ctx context.Context, script string) (string, error)
}

Asker runs script in the shell blocks run in and returns what it printed. *run.Session is one.

type Completion

type Completion struct {
	Word  string   `json:"word"`
	Items []string `json:"items"`
}

Completion is what Tab offers for the word before the cursor: each of Items replaces Word, the end of what was typed.

func Complete

func Complete(ctx context.Context, a Asker, before string, zsh bool) Completion

Complete completes the word at the end of before, what is typed up to the cursor, asking the shell, so it sees the directory and variables blocks left there: a $ word is a variable, the first word of a command is a command, and any other word, or one with a /, a file. A directory ends in /. Items are quoted for the shell, as they would be typed. zsh says the shell is zsh.

type Prompt

type Prompt struct {
	PS1 string `json:"ps1"`
	PS2 string `json:"ps2"`
	// Dir is the shell's working directory, with the home folder as ~,
	// shown on the pane's separator.
	Dir string `json:"dir"`
}

Prompt is the shell's prompts, PS1 and PS2, expanded as the shell would show them, with their colors as ANSI escapes. Empty means the shell has none.

func AskPrompt

func AskPrompt(ctx context.Context, a Asker, zsh bool) (Prompt, error)

AskPrompt asks the shell for its prompts. zsh says the shell is zsh.

func ParsePrompt added in v1.7.0

func ParsePrompt(out string) Prompt

ParsePrompt reads what a prompt script printed: PS1, then PS2 and the working directory, each after its mark. bash's \[ and \], which mark where escapes start and end, are taken out.

Jump to

Keyboard shortcuts

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