undo

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: May 1, 2024 License: BSD-3-Clause Imports: 4 Imported by: 1

Documentation

Overview

Package undo package provides a generic undo / redo functionality based on `[]string` representations of any kind of state representation (typically JSON dump of the 'document' state). It stores the compact diffs from one state change to the next, with raw copies saved at infrequent intervals to tradeoff cost of computing diffs.

In addition state (which is optional on any given step), a description of the action and arbitrary string-encoded data can be saved with each record. Thus, for cases where the state doesn't change, you can just save some data about the action sufficient to undo / redo it.

A new record must be saved of the state just *before* an action takes place and the nature of the action taken.

Thus, undoing the action restores the state to that prior state.

Redoing the action means restoring the state *after* the action.

This means that the first Undo action must save the current state before doing the undo.

The Index is always on the last state saved, which will then be the one that would be undone for an undo action.

Index

Constants

This section is empty.

Variables

View Source
var DefaultRawInterval = 50

DefaultRawInterval is interval for saving raw state -- need to do this at some interval to prevent having it take too long to compute patches from all the diffs.

Functions

This section is empty.

Types

type Record added in v0.1.1

type Record struct {

	// description of this action, for user to see
	Action string

	// action data, encoded however you want -- some undo records can just be about this action data that can be interpreted to Undo / Redo a non-state-changing action
	Data string

	// if present, then direct save of full state -- do this at intervals to speed up computing prior states
	Raw []string

	// patch to get from previous record to this one
	Patch textbuf.Patch

	// this record is an UndoSave, when Undo first called from end of stack
	UndoSave bool
}

Record is one undo record, associated with one action that changed state from one to next. The state saved in this record is the state *before* the action took place. The state is either saved as a Raw value or as a diff Patch to the previous state.

func (*Record) Init added in v0.1.1

func (rc *Record) Init(action, data string)

Init sets the action and data in a record -- overwriting any prior values

func (*Record) MemUsed added in v0.1.1

func (rc *Record) MemUsed() int

MemUsed reports the amount of memory used for record

type Stack added in v0.1.1

type Stack struct {

	// current index in the undo records -- this is the record that will be undone if user hits undo
	Index int

	// the list of saved state / action records
	Records []*Record

	// interval for saving raw data -- need to do this at some interval to prevent having it take too long to compute patches from all the diffs.
	RawInterval int

	// mutex that protects updates -- we do diff computation as a separate goroutine so it is instant from perspective of UI
	Mu sync.Mutex
}

Stack is the undo stack manager that manages the undo and redo process.

func (*Stack) HasRedoAvail added in v0.1.1

func (us *Stack) HasRedoAvail() bool

HasRedoAvail returns true if there is at least one redo record available. This does NOT get the lock -- may rarely be inaccurate but is used for GUI enabling so not such a big deal.

func (*Stack) HasUndoAvail added in v0.1.1

func (us *Stack) HasUndoAvail() bool

HasUndoAvail returns true if there is at least one undo record available. This does NOT get the lock -- may rarely be inaccurate but is used for gui enabling so not such a big deal.

func (*Stack) MemStats added in v0.1.1

func (us *Stack) MemStats(details bool) string

MemStats reports the memory usage statistics. if details is true, each record is reported.

func (*Stack) MustSaveUndoStart added in v0.1.1

func (us *Stack) MustSaveUndoStart() bool

MustSaveUndoStart returns true if the current state must be saved as the start of the first Undo action when at the end of the stack. If this returns true, then call SaveUndoStart. It sets a special flag on the record.

func (*Stack) RecState added in v0.1.1

func (us *Stack) RecState(idx int) []string

RecState returns the state for given index, reconstructing from diffs as needed. Must be called under lock.

func (*Stack) Redo added in v0.1.1

func (us *Stack) Redo() (action, data string, state []string)

Redo returns the action, data at the *next* index, and the state at the index *after that*. returning nil if already at end of saved records.

func (*Stack) RedoList added in v0.1.1

func (us *Stack) RedoList() []string

RedoList returns the list actions in order from the current forward to end suitable for a menu of actions to redo

func (*Stack) RedoTo added in v0.1.1

func (us *Stack) RedoTo(idx int) (action, data string, state []string)

RedoTo returns the action, action data, and state at the given index, returning nil if already at end of saved records.

func (*Stack) Reset added in v0.1.1

func (us *Stack) Reset()

Reset resets the undo state

func (*Stack) Save added in v0.1.1

func (us *Stack) Save(action, data string, state []string)

Save saves a new action as next action to be undone, with given action data and current full state of the system (either of which are optional). The state must be available for saving -- we do not copy in case we save the full raw copy.

func (*Stack) SaveReplace added in v0.1.1

func (us *Stack) SaveReplace(action, data string, state []string)

SaveReplace replaces the current Undo record with new state, instead of creating a new record. This is useful for when you have a stream of the same type of manipulations and just want to save the last (it is better to handle that case up front as saving the state can be relatively expensive, but in some cases it is not possible).

func (*Stack) SaveState added in v0.1.1

func (us *Stack) SaveState(nr *Record, idx int, state []string)

SaveState saves given record of state at given index

func (*Stack) SaveUndoStart added in v0.1.1

func (us *Stack) SaveUndoStart(state []string)

SaveUndoStart saves the current state -- call if MustSaveUndoStart is true. Sets a special flag for this record, and action, data are empty. Does NOT increment the index, so next undo is still as expected.

func (*Stack) Undo added in v0.1.1

func (us *Stack) Undo() (action, data string, state []string)

Undo returns the action, action data, and state at the current index and decrements the index to the previous record. This state is the state just prior to the action. If already at the start (Index = -1) then returns empty everything Before calling, first check MustSaveUndoStart() -- if false, then you need to call SaveUndoStart() so that the state just before Undoing can be redone!

func (*Stack) UndoList added in v0.1.1

func (us *Stack) UndoList() []string

UndoList returns the list actions in order from the most recent back in time suitable for a menu of actions to undo.

func (*Stack) UndoTo added in v0.1.1

func (us *Stack) UndoTo(idx int) (action, data string, state []string)

UndoTo returns the action, action data, and state at the given index and decrements the index to the previous record. If idx is out of range then returns empty everything

Jump to

Keyboard shortcuts

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