https://github.com/mfmayer/undostack
Go undo stack that allows operations to be done and keeps track of them in order to be able to undo and redo them.
https://github.com/mfmayer/undostack
go golang redo undo undo-redo
Last synced: 3 months ago
JSON representation
Go undo stack that allows operations to be done and keeps track of them in order to be able to undo and redo them.
- Host: GitHub
- URL: https://github.com/mfmayer/undostack
- Owner: mfmayer
- License: mit
- Created: 2021-05-30T09:46:51.000Z (almost 5 years ago)
- Default Branch: main
- Last Pushed: 2021-07-01T20:45:01.000Z (almost 5 years ago)
- Last Synced: 2023-07-27T22:21:48.759Z (over 2 years ago)
- Topics: go, golang, redo, undo, undo-redo
- Language: Go
- Homepage:
- Size: 8.79 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
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.
[](http://godoc.org/github.com/mfmayer/undostack)
An `Operation` can consist of one or more `Action`s, 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:
```go
type Action interface {
Do() error
Undo() error
}
```
## Example
[Not quite serious example with an operation that holds two actions](examples/main.go):
```go
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()
}
```