From fd5d8bdf8a5f023e10dbd6541654197efb9fafbc Mon Sep 17 00:00:00 2001 From: erroneousboat Date: Sat, 11 Aug 2018 13:10:36 +0200 Subject: [PATCH] Add optional date/time formatting Fixes #84 --- README.md | 9 ++++---- components/chat.go | 8 +++++++- config/config.go | 7 ++++--- config/theme.go | 7 ++++--- service/slack.go | 51 +++++++++++++++++++++++++--------------------- 5 files changed, 48 insertions(+), 34 deletions(-) diff --git a/README.md b/README.md index b857d11..50c0d06 100644 --- a/README.md +++ b/README.md @@ -35,7 +35,7 @@ Setup 1. Get a slack token, click [here](https://api.slack.com/docs/oauth-test-tokens) 2. Create a `.slack-term` file, and place it in your home directory. Below is - an an example file, you can leave out the `OPTIONAL` parts, you are only + an example file, you can leave out the `OPTIONAL` parts, you are only required to specify a `slack_token`. Remember that your file should be a valid json file so don't forget to remove the comments. @@ -47,7 +47,7 @@ Setup "sidebar_width": 1, // OPTIONAL: turn on desktop notifications for all incoming messages, set - // the value as: "all", and for only mentions and im messages set the + // the value as: "all". For only mentions and im messages set the // value as: "mention", default is turned off: "" "notify": "", @@ -112,7 +112,8 @@ Setup "message": { "time": "", "name": "", - "text": "" + "text": "", + "time_format": "15:04" } } } @@ -139,7 +140,7 @@ $ slack-term -config [path-to-config-file] Default Key Mapping ------------------- -Below are the default key-mapping for `slack-term`, you can change them +Below are the default key-mappings for `slack-term`, you can change them in your `slack-term.json` file. | mode | key | action | diff --git a/components/chat.go b/components/chat.go index d130da4..d1a8a86 100644 --- a/components/chat.go +++ b/components/chat.go @@ -19,6 +19,8 @@ type Message struct { StyleTime string StyleName string StyleText string + + FormatTime string } // Chat is the definition of a Chat component @@ -54,7 +56,11 @@ func (c *Chat) Buffer() termui.Buffer { if (msg.Time != time.Time{} && msg.Name != "") { // Time cells = append(cells, termui.DefaultTxBuilder.Build( - fmt.Sprintf("[[%s]](%s) ", msg.Time.Format("15:04"), msg.StyleTime), + fmt.Sprintf( + "[[%s]](%s) ", + msg.Time.Format(msg.FormatTime), + msg.StyleTime, + ), termui.ColorDefault, termui.ColorDefault)..., ) diff --git a/config/config.go b/config/config.go index abde6e4..caf3318 100644 --- a/config/config.go +++ b/config/config.go @@ -124,9 +124,10 @@ func getDefaultConfig() Config { Text: "", }, Message: Message{ - Time: "", - Name: "", - Text: "", + Time: "", + TimeFormat: "15:04", + Name: "", + Text: "", }, }, } diff --git a/config/theme.go b/config/theme.go index 0b113d9..38b06a3 100644 --- a/config/theme.go +++ b/config/theme.go @@ -16,9 +16,10 @@ type View struct { } type Message struct { - Time string `json:"time"` - Name string `json:"name"` - Text string `json:"text"` + Time string `json:"time"` + Name string `json:"name"` + Text string `json:"text"` + TimeFormat string `json:"time_format"` } type Channel struct { diff --git a/service/slack.go b/service/slack.go index 0d424b5..93093a5 100644 --- a/service/slack.go +++ b/service/slack.go @@ -453,12 +453,13 @@ func (s *SlackService) CreateMessage(message slack.Message) []components.Message // Format message msg := components.Message{ - Time: time.Unix(intTime, 0), - Name: name, - Content: parseMessage(s, message.Text), - StyleTime: s.Config.Theme.Message.Time, - StyleName: s.Config.Theme.Message.Name, - StyleText: s.Config.Theme.Message.Text, + Time: time.Unix(intTime, 0), + Name: name, + Content: parseMessage(s, message.Text), + StyleTime: s.Config.Theme.Message.Time, + StyleName: s.Config.Theme.Message.Name, + StyleText: s.Config.Theme.Message.Text, + FormatTime: s.Config.Theme.Message.TimeFormat, } msgs = append(msgs, msg) @@ -525,12 +526,13 @@ func (s *SlackService) CreateMessageFromMessageEvent(message *slack.MessageEvent // Format message msg := components.Message{ - Time: time.Unix(intTime, 0), - Name: name, - Content: parseMessage(s, message.Text), - StyleTime: s.Config.Theme.Message.Time, - StyleName: s.Config.Theme.Message.Name, - StyleText: s.Config.Theme.Message.Text, + Time: time.Unix(intTime, 0), + Name: name, + Content: parseMessage(s, message.Text), + StyleTime: s.Config.Theme.Message.Time, + StyleName: s.Config.Theme.Message.Name, + StyleText: s.Config.Theme.Message.Text, + FormatTime: s.Config.Theme.Message.TimeFormat, } msgs = append(msgs, msg) @@ -663,9 +665,10 @@ func (s *SlackService) CreateMessageFromAttachments(atts []slack.Attachment) []c att.Fields[i].Title, att.Fields[i].Value, ), - StyleTime: s.Config.Theme.Message.Time, - StyleName: s.Config.Theme.Message.Name, - StyleText: s.Config.Theme.Message.Text, + StyleTime: s.Config.Theme.Message.Time, + StyleName: s.Config.Theme.Message.Name, + StyleText: s.Config.Theme.Message.Text, + FormatTime: s.Config.Theme.Message.TimeFormat, }, ) } @@ -674,10 +677,11 @@ func (s *SlackService) CreateMessageFromAttachments(atts []slack.Attachment) []c msgs = append( msgs, components.Message{ - Content: fmt.Sprintf("%s", att.Text), - StyleTime: s.Config.Theme.Message.Time, - StyleName: s.Config.Theme.Message.Name, - StyleText: s.Config.Theme.Message.Text, + Content: fmt.Sprintf("%s", att.Text), + StyleTime: s.Config.Theme.Message.Time, + StyleName: s.Config.Theme.Message.Name, + StyleText: s.Config.Theme.Message.Text, + FormatTime: s.Config.Theme.Message.TimeFormat, }, ) } @@ -686,10 +690,11 @@ func (s *SlackService) CreateMessageFromAttachments(atts []slack.Attachment) []c msgs = append( msgs, components.Message{ - Content: fmt.Sprintf("%s", att.Title), - StyleTime: s.Config.Theme.Message.Time, - StyleName: s.Config.Theme.Message.Name, - StyleText: s.Config.Theme.Message.Text, + Content: fmt.Sprintf("%s", att.Title), + StyleTime: s.Config.Theme.Message.Time, + StyleName: s.Config.Theme.Message.Name, + StyleText: s.Config.Theme.Message.Text, + FormatTime: s.Config.Theme.Message.TimeFormat, }, ) }