/timestamp: time and datetime modes

This commit is contained in:
Andrey Petrov 2019-03-21 17:12:32 -04:00
parent 2089eda486
commit c5cc4b0594
3 changed files with 51 additions and 34 deletions

View 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(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"
}

View 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

View 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?