@@ -15,6 +15,8 @@ import (
1515const messageBuffer = 5
1616const messageTimeout = 5 * time .Second
1717const reHighlight = `\b(%s)\b`
18+ const timestampTimeout = 30 * time .Minute
19+ const timestampLayout = "2006-01-02 15:04:05 UTC"
1820
1921var ErrUserClosed = errors .New ("user closed" )
2022
@@ -32,7 +34,8 @@ type User struct {
3234
3335 mu sync.Mutex
3436 config UserConfig
35- replyTo * User // Set when user gets a /msg, for replying.
37+ replyTo * User // Set when user gets a /msg, for replying.
38+ lastMsg time.Time // When the last message was rendered
3639}
3740
3841func NewUser (identity Identifier ) * User {
@@ -164,8 +167,9 @@ func (u *User) render(m Message) string {
164167 }
165168}
166169
167- // HandleMsg will render the message to the screen, blocking.
168- func (u * User ) HandleMsg (m Message ) error {
170+ // writeMsg renders the message and attempts to write it, will Close the user
171+ // if it fails.
172+ func (u * User ) writeMsg (m Message ) error {
169173 r := u .render (m )
170174 _ , err := u .screen .Write ([]byte (r ))
171175 if err != nil {
@@ -175,6 +179,26 @@ func (u *User) HandleMsg(m Message) error {
175179 return err
176180}
177181
182+ // HandleMsg will render the message to the screen, blocking.
183+ func (u * User ) HandleMsg (m Message ) error {
184+ u .mu .Lock ()
185+ cfg := u .config
186+ lastMsg := u .lastMsg
187+ u .lastMsg = m .Timestamp ()
188+ injectTimestamp := ! lastMsg .IsZero () && cfg .Timestamp && u .lastMsg .Sub (lastMsg ) > timestampTimeout
189+ u .mu .Unlock ()
190+
191+ if injectTimestamp {
192+ // Inject a timestamp at most once every timestampTimeout between message intervals
193+ ts := NewSystemMsg (fmt .Sprintf ("Timestamp: %s" , m .Timestamp ().UTC ().Format (timestampLayout )), u )
194+ if err := u .writeMsg (ts ); err != nil {
195+ return err
196+ }
197+ }
198+
199+ return u .writeMsg (m )
200+ }
201+
178202// Add message to consume by user
179203func (u * User ) Send (m Message ) error {
180204 select {
@@ -194,6 +218,7 @@ type UserConfig struct {
194218 Highlight * regexp.Regexp
195219 Bell bool
196220 Quiet bool
221+ Timestamp bool
197222 Theme * Theme
198223}
199224
@@ -202,8 +227,9 @@ var DefaultUserConfig UserConfig
202227
203228func init () {
204229 DefaultUserConfig = UserConfig {
205- Bell : true ,
206- Quiet : false ,
230+ Bell : true ,
231+ Quiet : false ,
232+ Timestamp : false ,
207233 }
208234
209235 // TODO: Seed random?
0 commit comments