undostack

package module
v0.0.6 Latest Latest
Warning

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

Go to latest
Published: Jul 1, 2021 License: MIT Imports: 2 Imported by: 0

README

Go Undo Stack

undostack allows operations to be done (executed) and keeps track of them in order to be able to undo and redo them.

GoDoc

An Operation can consist of one or more Actions, whose Do() methods are called when the opration is done/redone and whose Undo() methods are called in reverse order when the operation is undone.

Action is the interface that wraps the Do() and Undo() method that must be implemented for arbitrary actions:

type Action interface {
  Do() error
  Undo() error
}

Example

Not quite serious example with an operation that holds two actions:

package main

import (
  "fmt"

  "github.com/mfmayer/undostack"
)

// Implement welcome action
type welcomeAction struct{}

func (wa *welcomeAction) Do() error {
  fmt.Println("Welcome my friend.")
  return nil
}

func (wa *welcomeAction) Undo() error {
  fmt.Println("Go away, I don't like you.")
  return nil
}

// Implement action to offer a seat
type offerSeatAction struct{}

func (sfa *offerSeatAction) Do() error {
  fmt.Println("Please have a seat.")
  return nil
}

func (sfa *offerSeatAction) Undo() error {
  fmt.Println("Please stand up.")
  return nil
}

func main() {
  // initalize the undo stack
  undoStack := undostack.UndoStack{}

  // create the receive operation and add the actions welcome and offer a seat
  receiveOperation := undostack.Operation{
    Name: "Receive Guest",
    Actions: []undostack.Action{
      &welcomeAction{},
      &offerSeatAction{},
    },
  }

  // Do, Undo and Redo the opearion
  fmt.Println("# Do receive:")
  undoStack.Do(&receiveOperation)

  fmt.Println("\n# Undo receive:")
  undoStack.Undo()

  fmt.Println("\n# Redo receive:")
  undoStack.Redo()
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Action

type Action interface {
	Do() error
	Undo() error
}

Action interface that wraps Do and Undo methods that are called when an operation is done/redone or undone.

type Operation

type Operation struct {
	Name            string
	Actions         []Action
	DoErrorFormat   func([]error) string
	UndoErrorFormat func([]error) string
}

Operation can consist of one or more actions, whose Do methods are called in order when the opration is done/reonde and whose Undo methods are called in reverse order when the operation is undone.

type UndoStack

type UndoStack struct {
	// contains filtered or unexported fields
}

UndoStack enables operations to be done (executed) and keeps track of them in order to be able to undo and redo them.

func (*UndoStack) Clear

func (us *UndoStack) Clear()

Clear the undo stack and drop all operations

func (*UndoStack) Do

func (us *UndoStack) Do(op *Operation) (err error)

Do (execute) a new operation and put it on the stack.

Previously undone operations are dropped (at least if they haven been redone before).

func (*UndoStack) Redo

func (us *UndoStack) Redo() (err error)

Redo a previously undone operation.

Calls the Do methods of the operation's actions.

func (*UndoStack) State

func (us *UndoStack) State() (canBeUndone int, canBeRedone int)

State returns the number of operations that can be undone and redone

func (*UndoStack) Undo

func (us *UndoStack) Undo() (err error)

Undo a previously done operation.

Calls the Do methods of the operation's actions in reverse order.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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