notebook

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Sep 29, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package notebook is a notebook's document: its cells (code cells holding a nushell pipeline, note cells holding Markdown), the names cells give their outputs, what each cell reads, the order cells run in, and what a cell's last run left (its output). It knows nothing of sheets, terminals or nu itself: the engine (internal/sheet) keeps a notebook tab's cells in its undo history and its file, and the UI runs them and draws them.

Index

Constants

View Source
const Selection = "selection"

Selection is the variable that holds the selection.

Variables

View Source
var DefaultCaps = Caps{Cell: 1 << 20, Total: 8 << 20}

DefaultCaps keep 1 MB of a cell's output and 8 MB of a notebook's.

View Source
var Reserved = []string{"in", "env", "nu", "it", "selection", "sheet"}

Reserved are nushell's own variables and the notebook's: $selection is the selection on a sheet, $sheet.A1:C9 a range of one.

Functions

func Dependents

func Dependents(cells []Cell, i int) []int

Dependents returns the cells that read cell i, directly or through others, in the notebook's order.

func Inputs

func Inputs(cells []Cell, i int, output func(id int) *Output) []int

Inputs returns the cells cell i reads, directly or through others, that have no output, in the notebook's order: what has to run before it can.

func Kept

func Kept(cells []Cell, output func(id int) *Output, caps Caps) map[int]bool

Kept returns the cells, by ID, whose outputs a file keeps within caps: in the notebook's order, each that fits in what's left.

func Names

func Names(cells []Cell) map[string]int

Names maps each name to the index of the first code cell giving it.

func Order

func Order(cells []Cell, want []int) (order, cycle []int)

Order returns the code cells of want, each after the cells it reads among them, in want's order otherwise, and the cells left out for reading themselves, directly or through others.

func RangeSpans

func RangeSpans(pipeline string) [][2]int

RangeSpans are where a pipeline reads ranges of sheets, by byte offsets: each $sheet.A1:C9 whole, as Bind renames them.

func Reads

func Reads(cells []Cell, names map[string]int, i int) []int

Reads returns the indices of the cells cell i reads, in the order it names them.

func ReadsSelection

func ReadsSelection(pipeline string) bool

ReadsSelection reports whether a pipeline reads $selection.

func Refs

func Refs(pipeline string) []string

Refs returns the names a pipeline reads as $name, in the order it first names them, leaving out nushell's variables and the notebook's own ($selection, $sheet).

func Settle

func Settle(cells []Cell, outputs map[int]*Output)

Settle makes outputs read from a file what their cells' runs would have left: made from the source as it is, having read the outputs as they are, so none is stale until something changes.

func SplitName

func SplitName(src string) (name, pipeline string)

SplitName splits `name = pipeline` into the name and the pipeline; a source that doesn't start so has no name. `==` isn't a name's `=`.

func Stale

func Stale(cells []Cell, output func(id int) *Output) map[int]bool

Stale returns the cells, by ID, whose output may not be what running them now would give: their source changed since, or an output they read changed or is stale itself.

func Taken

func Taken(cells []Cell, i int) string

Taken says why cell i can't use its name: an earlier cell gives it (names ignore case there, as formulas' nu.name do), or "".

func ValidName

func ValidName(name string) error

ValidName checks that name can name a cell's output: a nushell variable's name of letters, digits and _, not starting with a digit, not one of Reserved, and not starting with __, which the notebook's own variables do.

func WithName

func WithName(src, name string) string

WithName is src given the name name, replacing any name it has; "" takes its name away.

Types

type Caps

type Caps struct{ Cell, Total int }

Caps are how much of the outputs a file keeps, in bytes of NUON: at most Cell of one cell's, Total of all of them.

type Cell

type Cell struct {
	// ID identifies the cell for as long as the workbook is open: its
	// output is kept by it. It isn't saved.
	ID     int
	Kind   Kind
	Source string
}

Cell is one cell of a notebook. Cells are values: a change makes a new one, so the undo history can keep the list it replaced.

func (Cell) Name

func (c Cell) Name() string

Name is the name the cell gives its output, from a source of the form `name = pipeline`, or "".

func (Cell) Pipeline

func (c Cell) Pipeline() string

Pipeline is what runs: the source without its name.

type Kind

type Kind uint8

Kind is what a cell holds.

const (
	// Code cells hold a nushell pipeline, several lines if need be.
	Code Kind = iota
	// Note cells hold Markdown.
	Note
)

func ParseKind

func ParseKind(s string) (Kind, bool)

ParseKind reads a kind as the file names it.

func (Kind) String

func (k Kind) String() string

String is how the file names the kind.

type Output

type Output struct {
	// NUON is what the pipeline printed, as NUON; nil when it failed or
	// its output wasn't saved.
	NUON []byte
	// Err is nu's message when the run failed, and Detail the rest of
	// what it said (its help line).
	Err, Detail string
	// Note says what was left out.
	Note string
	// Count is the run's number in this session, [3]; 0 for an output
	// read from the file.
	Count int
	// Seq identifies the output among the workbook's, for telling
	// whether what a cell read has changed since.
	Seq int
	// Took is how long the run took.
	Took time.Duration
	// Source is the cell's source when it ran.
	Source string
	// Reads are the Seqs of the outputs the run read, by name.
	Reads map[string]int
	// Selection is the range the run read as $selection, "Sheet1!A1:C9".
	Selection string
	// Unsaved is set on an output the file didn't keep, being larger
	// than the caps allowed: there's nothing to show until it runs.
	Unsaved bool
}

Output is what a code cell's last run left: what it printed, as NUON, or why it failed. Outputs are values too, replaced whole.

func (*Output) Failed

func (o *Output) Failed() bool

Failed reports whether the run failed.

type SheetRef

type SheetRef struct {
	// Ref is the range as written after $sheet.: "A1:C9", "Sales!A1:C9".
	Ref string
	// Var is the variable it's read as once renamed: __sheet1.
	Var string
}

SheetRef is a range of a sheet a pipeline reads, $sheet.A1:C9.

func Bind

func Bind(pipeline string) (string, []SheetRef)

Bind renames each range of a sheet the pipeline reads ($sheet.A1:C9) to a variable of its own, returning the pipeline to run and the ranges in the order they appear, the same range read twice once.

Jump to

Keyboard shortcuts

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