48 lines
1.1 KiB
Go
48 lines
1.1 KiB
Go
package main
|
|
|
|
import "net/url"
|
|
|
|
type History struct {
|
|
history []*url.URL
|
|
position int
|
|
}
|
|
|
|
func (history *History) Add (location *url.URL) {
|
|
if len(history.history) == 0 {
|
|
history.history = append(history.history, location)
|
|
history.position = 0
|
|
return
|
|
}
|
|
|
|
history.position ++
|
|
|
|
if len(history.history) > history.position + 1 {
|
|
history.history = history.history[:history.position + 1]
|
|
history.history[history.position] = location
|
|
} else {
|
|
history.history = append(history.history, location)
|
|
}
|
|
}
|
|
|
|
func (history *History) Backward () (location *url.URL) {
|
|
if history.position < 1 || len(history.history) < 1 { return }
|
|
history.position --
|
|
location = history.history[history.position]
|
|
return
|
|
}
|
|
|
|
func (history *History) Forward () (location *url.URL) {
|
|
if history.position + 1 >= len(history.history) { return }
|
|
history.position ++
|
|
location = history.history[history.position]
|
|
return
|
|
}
|
|
|
|
func (history *History) Remove () {
|
|
if len(history.history) <= 1 { return }
|
|
history.history = append (
|
|
history.history[:history.position],
|
|
history.history[history.position + 1:]...)
|
|
history.position --
|
|
}
|