From c5cc4b05949d35460152f58b730f1ab2f112ec32 Mon Sep 17 00:00:00 2001 From: Andrey Petrov Date: Thu, 21 Mar 2019 17:12:32 -0400 Subject: [PATCH] /timestamp: time and datetime modes --- chat/command.go | 54 ++++++++++++++++++++++++++++++------------- chat/message/theme.go | 10 +++----- chat/message/user.go | 21 ++++++++--------- 3 files changed, 51 insertions(+), 34 deletions(-) diff --git a/chat/command.go b/chat/command.go index d8e9a5f..182d00f 100644 --- a/chat/command.go +++ b/chat/command.go @@ -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(label, int(offset.Seconds())) - cfg.Timestamp = true - } else { - cfg.Timestamp = !cfg.Timestamp + 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 + } + 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" } diff --git a/chat/message/theme.go b/chat/message/theme.go index ffed789..54886a4 100644 --- a/chat/message/theme.go +++ b/chat/message/theme.go @@ -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 diff --git a/chat/message/user.go b/chat/message/user.go index 6ed9acf..faea90c 100644 --- a/chat/message/user.go +++ b/chat/message/user.go @@ -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?