/timestamp: time and datetime modes origin/timestamp-both
authorAndrey Petrov <andrey.petrov@shazow.net>
Thu, 21 Mar 2019 21:12:32 +0000 (17:12 -0400)
committerAndrey Petrov <andrey.petrov@shazow.net>
Thu, 21 Mar 2019 21:12:32 +0000 (17:12 -0400)
chat/command.go
chat/message/theme.go
chat/message/user.go

index d8e9a5ff9a009f600186c2cd0b453e7906ac971f..182d00fd553e0a3b94416d38beb80b49cf981f3c 100644 (file)
@@ -94,6 +94,10 @@ func (c Commands) Help(showOp bool) string {
        return help
 }
 
+var timeformatDatetime = "2006-01-02 15:04:05"
+
+var timeformatTime = "15:04"
+
 var defaultCommands *Commands
 
 func init() {
@@ -285,38 +289,56 @@ func InitCommands(c *Commands) {
 
        c.Add(Command{
                Prefix:     "/timestamp",
-               PrefixHelp: "[UTC_OFFSET [LABEL]]",
-               Help:       "Prefix messages with a timestamp. (Offset example: +5h45m)",
+               PrefixHelp: "[time|datetime]",
+               Help:       "Prefix messages with a timestamp. You can also provide the UTC offset: /timestamp time +5h45m",
                Handler: func(room *Room, msg message.CommandMsg) error {
                        u := msg.From()
                        cfg := u.Config()
 
                        args := msg.Args()
+                       mode := ""
                        if len(args) >= 1 {
+                               mode = args[0]
+                       }
+                       if len(args) >= 2 {
                                // FIXME: This is an annoying format to demand from users, but
                                // hopefully we can make it a non-primary flow if we add GeoIP
                                // someday.
-                               offset, err := time.ParseDuration(args[0])
+                               offset, err := time.ParseDuration(args[1])
                                if err != nil {
                                        return err
                                }
-                               label := ""
-                               if len(args) >= 2 {
-                                       label = args[1]
+                               cfg.Timezone = time.FixedZone("", int(offset.Seconds()))
+                       }
+
+                       switch mode {
+                       case "time":
+                               cfg.Timeformat = &timeformatTime
+                       case "datetime":
+                               cfg.Timeformat = &timeformatDatetime
+                       case "":
+                               // Toggle
+                               if cfg.Timeformat != nil {
+                                       cfg.Timeformat = nil
+                               } else {
+                                       cfg.Timeformat = &timeformatTime
                                }
-                               cfg.Timezone = time.FixedZone(label, int(offset.Seconds()))
-                               cfg.Timestamp = true
-                       } else {
-                               cfg.Timestamp = !cfg.Timestamp
+                       case "off":
+                               cfg.Timeformat = nil
+                       default:
+                               return errors.New("timestamp value must be one of: time, datetime, off")
                        }
+
                        u.SetConfig(cfg)
 
                        var body string
-                       if cfg.Timestamp && cfg.Timezone != nil {
-                               tzname := time.Now().In(cfg.Timezone).Format("MST")
-                               body = fmt.Sprintf("Timestamp is toggled ON, timezone is %q", tzname)
-                       } else if cfg.Timestamp {
-                               body = "Timestamp is toggled ON, timezone is UTC"
+                       if cfg.Timeformat != nil {
+                               if cfg.Timezone != nil {
+                                       tzname := time.Now().In(cfg.Timezone).Format("MST")
+                                       body = fmt.Sprintf("Timestamp is toggled ON, timezone is %q", tzname)
+                               } else {
+                                       body = "Timestamp is toggled ON, timezone is UTC"
+                               }
                        } else {
                                body = "Timestamp is toggled OFF"
                        }
index ffed78934786524e171a28e8562080ec26d2a5ff..54886a4f36e390d5ef8a5b876cdf7320f1d4e3d0 100644 (file)
@@ -2,11 +2,8 @@ package message
 
 import (
        "fmt"
-       "time"
 )
 
-const timestampLayout = "2006-01-02 15:04:05"
-
 const (
        // Reset resets the color
        Reset = "\033[0m"
@@ -166,10 +163,9 @@ func (theme Theme) Highlight(s string) string {
        return theme.highlight.Format(s)
 }
 
-// Timestamp formats and colorizes the timestamp.
-func (theme Theme) Timestamp(t time.Time) string {
-       // TODO: Change this per-theme? Or config?
-       return theme.sys.Format(t.Format(timestampLayout))
+// Timestamp colorizes the timestamp.
+func (theme Theme) Timestamp(s string) string {
+       return theme.sys.Format(s)
 }
 
 // List of initialzied themes
index 6ed9acf5c9119e0ac1fe46b055334ebc64077889..faea90c11e926467b936a4d6db5e68a9d44bb9c1 100644 (file)
@@ -175,14 +175,14 @@ func (u *User) render(m Message) string {
        default:
                out += m.Render(cfg.Theme)
        }
-       if cfg.Timestamp {
+       if cfg.Timeformat != nil {
                ts := m.Timestamp()
                if cfg.Timezone != nil {
                        ts = ts.In(cfg.Timezone)
                } else {
                        ts = ts.UTC()
                }
-               return cfg.Theme.Timestamp(ts) + "  " + out + Newline
+               return cfg.Theme.Timestamp(ts.Format(*cfg.Timeformat) + "  " + out + Newline)
        }
        return out + Newline
 }
@@ -223,12 +223,12 @@ func (u *User) Send(m Message) error {
 
 // Container for per-user configurations.
 type UserConfig struct {
-       Highlight *regexp.Regexp
-       Bell      bool
-       Quiet     bool
-       Timestamp bool
-       Timezone  *time.Location
-       Theme     *Theme
+       Highlight  *regexp.Regexp
+       Bell       bool
+       Quiet      bool
+       Timeformat *string
+       Timezone   *time.Location
+       Theme      *Theme
 }
 
 // Default user configuration to use
@@ -236,9 +236,8 @@ var DefaultUserConfig UserConfig
 
 func init() {
        DefaultUserConfig = UserConfig{
-               Bell:      true,
-               Quiet:     false,
-               Timestamp: false,
+               Bell:  true,
+               Quiet: false,
        }
 
        // TODO: Seed random?