mirror of
https://github.com/shazow/ssh-chat.git
synced 2025-04-12 23:27:17 +03:00
/timestamp: time and datetime modes
This commit is contained in:
parent
2089eda486
commit
c5cc4b0594
@ -94,6 +94,10 @@ func (c Commands) Help(showOp bool) string {
|
|||||||
return help
|
return help
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var timeformatDatetime = "2006-01-02 15:04:05"
|
||||||
|
|
||||||
|
var timeformatTime = "15:04"
|
||||||
|
|
||||||
var defaultCommands *Commands
|
var defaultCommands *Commands
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
@ -285,38 +289,56 @@ func InitCommands(c *Commands) {
|
|||||||
|
|
||||||
c.Add(Command{
|
c.Add(Command{
|
||||||
Prefix: "/timestamp",
|
Prefix: "/timestamp",
|
||||||
PrefixHelp: "[UTC_OFFSET [LABEL]]",
|
PrefixHelp: "[time|datetime]",
|
||||||
Help: "Prefix messages with a timestamp. (Offset example: +5h45m)",
|
Help: "Prefix messages with a timestamp. You can also provide the UTC offset: /timestamp time +5h45m",
|
||||||
Handler: func(room *Room, msg message.CommandMsg) error {
|
Handler: func(room *Room, msg message.CommandMsg) error {
|
||||||
u := msg.From()
|
u := msg.From()
|
||||||
cfg := u.Config()
|
cfg := u.Config()
|
||||||
|
|
||||||
args := msg.Args()
|
args := msg.Args()
|
||||||
|
mode := ""
|
||||||
if len(args) >= 1 {
|
if len(args) >= 1 {
|
||||||
|
mode = args[0]
|
||||||
|
}
|
||||||
|
if len(args) >= 2 {
|
||||||
// FIXME: This is an annoying format to demand from users, but
|
// FIXME: This is an annoying format to demand from users, but
|
||||||
// hopefully we can make it a non-primary flow if we add GeoIP
|
// hopefully we can make it a non-primary flow if we add GeoIP
|
||||||
// someday.
|
// someday.
|
||||||
offset, err := time.ParseDuration(args[0])
|
offset, err := time.ParseDuration(args[1])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
label := ""
|
cfg.Timezone = time.FixedZone("", int(offset.Seconds()))
|
||||||
if len(args) >= 2 {
|
|
||||||
label = args[1]
|
|
||||||
}
|
|
||||||
cfg.Timezone = time.FixedZone(label, int(offset.Seconds()))
|
|
||||||
cfg.Timestamp = true
|
|
||||||
} else {
|
|
||||||
cfg.Timestamp = !cfg.Timestamp
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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)
|
u.SetConfig(cfg)
|
||||||
|
|
||||||
var body string
|
var body string
|
||||||
if cfg.Timestamp && cfg.Timezone != nil {
|
if cfg.Timeformat != nil {
|
||||||
tzname := time.Now().In(cfg.Timezone).Format("MST")
|
if cfg.Timezone != nil {
|
||||||
body = fmt.Sprintf("Timestamp is toggled ON, timezone is %q", tzname)
|
tzname := time.Now().In(cfg.Timezone).Format("MST")
|
||||||
} else if cfg.Timestamp {
|
body = fmt.Sprintf("Timestamp is toggled ON, timezone is %q", tzname)
|
||||||
body = "Timestamp is toggled ON, timezone is UTC"
|
} else {
|
||||||
|
body = "Timestamp is toggled ON, timezone is UTC"
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
body = "Timestamp is toggled OFF"
|
body = "Timestamp is toggled OFF"
|
||||||
}
|
}
|
||||||
|
@ -2,11 +2,8 @@ package message
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"time"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const timestampLayout = "2006-01-02 15:04:05"
|
|
||||||
|
|
||||||
const (
|
const (
|
||||||
// Reset resets the color
|
// Reset resets the color
|
||||||
Reset = "\033[0m"
|
Reset = "\033[0m"
|
||||||
@ -166,10 +163,9 @@ func (theme Theme) Highlight(s string) string {
|
|||||||
return theme.highlight.Format(s)
|
return theme.highlight.Format(s)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Timestamp formats and colorizes the timestamp.
|
// Timestamp colorizes the timestamp.
|
||||||
func (theme Theme) Timestamp(t time.Time) string {
|
func (theme Theme) Timestamp(s string) string {
|
||||||
// TODO: Change this per-theme? Or config?
|
return theme.sys.Format(s)
|
||||||
return theme.sys.Format(t.Format(timestampLayout))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// List of initialzied themes
|
// List of initialzied themes
|
||||||
|
@ -175,14 +175,14 @@ func (u *User) render(m Message) string {
|
|||||||
default:
|
default:
|
||||||
out += m.Render(cfg.Theme)
|
out += m.Render(cfg.Theme)
|
||||||
}
|
}
|
||||||
if cfg.Timestamp {
|
if cfg.Timeformat != nil {
|
||||||
ts := m.Timestamp()
|
ts := m.Timestamp()
|
||||||
if cfg.Timezone != nil {
|
if cfg.Timezone != nil {
|
||||||
ts = ts.In(cfg.Timezone)
|
ts = ts.In(cfg.Timezone)
|
||||||
} else {
|
} else {
|
||||||
ts = ts.UTC()
|
ts = ts.UTC()
|
||||||
}
|
}
|
||||||
return cfg.Theme.Timestamp(ts) + " " + out + Newline
|
return cfg.Theme.Timestamp(ts.Format(*cfg.Timeformat) + " " + out + Newline)
|
||||||
}
|
}
|
||||||
return out + Newline
|
return out + Newline
|
||||||
}
|
}
|
||||||
@ -223,12 +223,12 @@ func (u *User) Send(m Message) error {
|
|||||||
|
|
||||||
// Container for per-user configurations.
|
// Container for per-user configurations.
|
||||||
type UserConfig struct {
|
type UserConfig struct {
|
||||||
Highlight *regexp.Regexp
|
Highlight *regexp.Regexp
|
||||||
Bell bool
|
Bell bool
|
||||||
Quiet bool
|
Quiet bool
|
||||||
Timestamp bool
|
Timeformat *string
|
||||||
Timezone *time.Location
|
Timezone *time.Location
|
||||||
Theme *Theme
|
Theme *Theme
|
||||||
}
|
}
|
||||||
|
|
||||||
// Default user configuration to use
|
// Default user configuration to use
|
||||||
@ -236,9 +236,8 @@ var DefaultUserConfig UserConfig
|
|||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
DefaultUserConfig = UserConfig{
|
DefaultUserConfig = UserConfig{
|
||||||
Bell: true,
|
Bell: true,
|
||||||
Quiet: false,
|
Quiet: false,
|
||||||
Timestamp: false,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Seed random?
|
// TODO: Seed random?
|
||||||
|
Loading…
x
Reference in New Issue
Block a user