From 1c780ab0000890760bc923fc9322dd8462e83fa1 Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Sun, 25 Dec 2016 14:31:37 +0100 Subject: Split last --- last.go | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 last.go (limited to 'last.go') diff --git a/last.go b/last.go new file mode 100644 index 0000000..34a3022 --- /dev/null +++ b/last.go @@ -0,0 +1,49 @@ +package main + +import ( + "container/ring" + "fmt" + "time" +) + +type Msg struct { + Time time.Time + Nick string + Text string +} + +type LastBuf struct { + *ring.Ring +} + +func NewLastBuf(n int) *LastBuf { + return &LastBuf{ring.New(n)} +} + +func (v *LastBuf) Push(t time.Time, nick, text string) { + v.Value = Msg{Time: t, Nick: nick, Text: text} + v.Ring = v.Next() +} + +// walk through buffer and find last message +func (v *LastBuf) Last(nick string) string { + var msg string + v.Do(func(v interface{}) { + if l, ok := v.(Msg); ok { + if l.Nick == nick { + msg = l.Text + } + } + }) + return msg +} + +func (v *LastBuf) Dump(c chan string) { + v.Do(func(v interface{}) { + if l, ok := v.(Msg); ok { + c <- fmt.Sprintf("%v <%v> %v", + l.Time.UTC().Format(time.Kitchen), + l.Nick, l.Text) + } + }) +} -- cgit v1.2.3