history

package
v0.0.0-...-9947234 Latest Latest
Warning

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

Go to latest
Published: Aug 24, 2023 License: MIT Imports: 16 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// Limit is the maximum number of history items to store
	Limit = 1000
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Item

type Item struct {
	// This group of fields are serialized and stored
	ID       xid.ID    `json:"id"`       // The unique ID of the command
	Prompt   string    `json:"prompt"`   // The prompt that was displayed when the command was executed
	Line     string    `json:"line"`     // The command executed
	Started  time.Time `json:"started"`  // The time the command was executed
	Finished time.Time `json:"finished"` // The time the command finished executing
	Status   Status    `json:"status"`   // The status of the command
	Output   string    `json:"output"`   // The output of the command

	// This group of fields are not serialized and are only used
	// for rendering the UI during the current shell session
	StreamedOutput []byte   `json:"-"` // The output of the command as it is streamed
	ItemType       ItemType `json:"-"` // If true then this item is an internal error item and not a user command
	Error          error    `json:"-"` // The error returned from the command
	LoadedHistory  bool     `json:"-"` // If true then this item is a history restored item and not a user command
}

Item represents a single entry in the command history list

It is both a tea.Model as well as a serializable struct to be stored within the history file.

func NewItem

func NewItem(prompt, line string, status Status) Item

NewItem creates a new history item with the given line and status

func (Item) Init

func (i Item) Init() tea.Cmd

Init implements tea.Model init function

func (Item) Update

func (i Item) Update(_ tea.Msg) (Item, tea.Cmd)

Update implements tea.Model update function

Note we return Item here rather than tea.Model as we save on type casting in the history model.

func (Item) View

func (i Item) View(cfg *config.Config, width int) string

View implements tea.Model view function

type ItemType

type ItemType uint8
const (
	Command         ItemType = iota // A command entered by the user
	InternalError                   // An internal error encountered by the shell
	HistoryRestored                 // A message indicating that the history was restored from disk and everything above it is from a previous session
)

type Model

type Model struct {
	Scrollback int    // The number of lines to scroll back
	Items      []Item // The history items we're currently displaying
	// contains filtered or unexported fields
}

Model represents the main history model

func New

func New(cfg *config.Config) Model

New creates a new history model

func (Model) AppendItem

func (m Model) AppendItem(item Item) tea.Cmd

AppendItem adds a new item to the history

func (Model) Init

func (m Model) Init() tea.Cmd

Init implements tea.Model init function

func (Model) Lookback

func (m Model) Lookback(lookback int) Item

Lookback returns the item that is lookback items back in the history

Valid range for lookback is 1 to len(history.Items)

func (Model) ReadHistory

func (m Model) ReadHistory() tea.Cmd

ReadHistory reads the history file and then returns a message to update the history model

func (Model) SaveHistory

func (m Model) SaveHistory(items []Item) tea.Cmd

SaveHistory saves the history file to disk

We pass in the items to save as a parameter so that we can asynchronously save the history file without worrying about commands which might be added to the queue after this one

func (Model) Search

func (m Model) Search(search string, startIdx int, delta int) (foundIdx int, found bool)

Search searches through the history for the given string starting from startIdx moving by delta, it returns the next index that matches the search string, or the startIdx if no more matches are found.

Expected call patterns are: - `Search("foo", 0, 1)` - search from the start of the history going backwards in time - `Search("foo", len(history.Items), -1)` - search from the end of the history going forwards in time

func (Model) StreamOutputFor

func (m Model) StreamOutputFor(cmd Item) func(bytes []byte) tea.Msg

StreamOutputFor returns a function that appends the given bytes to the given item by returning an append message

func (Model) Update

func (m Model) Update(msg tea.Msg) (Model, tea.Cmd)

Update implements tea.Model update function

func (Model) UpdateItem

func (m Model) UpdateItem(item Item) tea.Cmd

UpdateItem updates an item in the history

If the item doesn't exist in the history it will be added

DO NOT rely on this to add items to the history. Use [AppendItem] instead as that will be far more efficient on large histories

func (Model) View

func (m Model) View() string

View implements tea.Model view function

type Status

type Status uint8
const (
	UnknownStatus Status = iota
	RunningStatus
	SuccessStatus
	ErrorStatus
)

Jump to

Keyboard shortcuts

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