state

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Sep 19, 2026 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Overview

Package state tracks what each loaded profile changed in the current terminal. The state lives in the __PROFILE_STATE environment variable, so it is per-terminal, inherited by subshells, and never goes stale on disk.

Index

Constants

View Source
const (
	// EnvState holds the encoded State of the current terminal.
	EnvState = "__PROFILE_STATE"
	// EnvSnap carries the pre-load Snapshot from __snapshot to __record.
	EnvSnap = "__PROFILE_SNAP"
)

Variables

This section is empty.

Functions

func AddedEntries

func AddedEntries(before, after string) []string

AddedEntries returns the list entries present in after but not in before.

func EncodeSnapshot

func EncodeSnapshot(s *Snapshot) string

EncodeSnapshot serializes a Snapshot for __PROFILE_SNAP.

func Environ

func Environ() map[string]string

Environ returns the current process environment as a map.

func ListLike

func ListLike(before, after string) bool

ListLike reports whether a change looks like entries were added to a separator-delimited list such as PATH: the before value is still inside the after one as whole entries.

func ParseNames

func ParseNames(lines string) (funcs, aliases []string)

ParseNames reads "f name" / "a name" lines as emitted by the shell hooks.

func Tracked

func Tracked(key string) bool

Tracked reports whether changes to the env var key are recorded.

Types

type Entry

type Entry struct {
	Name    string      `json:"n"`
	File    string      `json:"f"`
	Vars    []VarChange `json:"v,omitempty"`
	Funcs   []string    `json:"fn,omitempty"`
	Aliases []string    `json:"al,omitempty"`
	Hook    string      `json:"h,omitempty"`
}

Entry is everything one loaded profile changed.

func Diff

func Diff(name, file, hook string, before, after *Snapshot) Entry

Diff builds the Entry for a profile from the shell before and after it was sourced. hook is the name the profile's unload function was renamed to.

type Op

type Op struct {
	Kind  OpKind
	Name  string
	Value string
}

Op is one shell operation. Value is only used by OpSetEnv.

func Restore

func Restore(e *Entry, env map[string]string) (ops []Op, warnings []string)

Restore computes the operations that undo e, given the terminal's current env. env is updated in place so several entries can be undone in sequence. Vars changed by something else since the profiles loaded are left alone and reported as warnings.

type OpKind

type OpKind int

OpKind is a shell operation needed to undo a profile.

const (
	OpSetEnv OpKind = iota
	OpUnsetEnv
	OpCallFunc
	OpUnsetFunc
	OpUnsetAlias
)

type Snapshot

type Snapshot struct {
	Env     map[string]string `json:"e"`
	Funcs   []string          `json:"f,omitempty"`
	Aliases []string          `json:"a,omitempty"`
}

Snapshot is the environment, function and alias names of a shell at one point in time.

func DecodeSnapshot

func DecodeSnapshot(raw string) (*Snapshot, error)

DecodeSnapshot parses a value produced by EncodeSnapshot.

type State

type State struct {
	Profiles []Entry `json:"p"`
}

State is the ordered stack of loaded profiles, oldest first.

func Load

func Load() (*State, error)

Load reads the State of the current terminal.

func (*State) Encode

func (s *State) Encode() string

Encode returns the value to store in __PROFILE_STATE, or "" when empty.

func (*State) Find

func (s *State) Find(name string) (*Entry, bool)

Find returns the loaded entry named name.

func (*State) Loaded

func (s *State) Loaded(name string) bool

Loaded reports whether a profile named name is loaded.

func (*State) Remove

func (s *State) Remove(name string)

Remove drops the entry named name.

type VarChange

type VarChange struct {
	Key string  `json:"k"`
	Old *string `json:"o,omitempty"`
	New *string `json:"n,omitempty"`
}

VarChange is one env var a profile touched. Old is nil when the profile added the var; New is nil when the profile removed it.

Jump to

Keyboard shortcuts

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