package main import ( "time" termboxUtil "git.bullercodeworks.com/brian/termbox-util" termbox "github.com/nsf/termbox-go" ) const MessageNoTimeout = -1 type Message struct { value string timeout time.Duration setTime time.Time fg, bg termbox.Attribute defaultValue string defaultTimeout time.Duration defaultFg termbox.Attribute defaultBg termbox.Attribute } func NewMessage(defaultVal string, defaultFg, defaultBg termbox.Attribute, defaultTO time.Duration) *Message { return &Message{ defaultValue: defaultVal, defaultTimeout: defaultTO, defaultFg: defaultFg, defaultBg: defaultBg, } } func (m *Message) Get() string { m.checkExpiration() return m.value } func (m *Message) Set(v string) { m.value = v m.setTime = time.Now() } func (m *Message) Clear() { m.value = m.defaultValue m.setTime = time.Time{} m.setFromDefaults() } func (m *Message) SetWithNoTimeout(v string) { m.Set(v) m.timeout = MessageNoTimeout } func (m *Message) SetWithTimeout(v string, t time.Duration) { m.Set(v) m.timeout = t } func (m *Message) SetError(v string) { m.value = v m.setTime = time.Now() m.timeout = time.Second * 2 m.bg = termbox.ColorRed m.fg = termbox.ColorWhite | termbox.AttrBold } func (m *Message) DrawAt(x, y int) { termboxUtil.DrawStringAtPoint(m.value, x, y, m.fg, m.bg) } func (m *Message) checkExpiration() { if m.expired() { m.Clear() } } func (m *Message) expired() bool { return m.timeout > 0 && time.Since(m.setTime) > m.timeout } func (m *Message) setFromDefaults() { m.value = m.defaultValue m.timeout = m.defaultTimeout m.fg, m.bg = m.defaultFg, m.defaultBg }