Skip to content

Commit

Permalink
Add optional Unicode ellipsis to save on screen real estate (#358)
Browse files Browse the repository at this point in the history
* Use Unicode ellipsis to save on screen real estate

* Make Unicode optional via a config
  • Loading branch information
hloeung authored Dec 18, 2020
1 parent 94cf4be commit 6f4f1f3
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
12 changes: 8 additions & 4 deletions bridge/mattermost/mattermost.go
Original file line number Diff line number Diff line change
Expand Up @@ -674,10 +674,14 @@ func (m *Mattermost) wsActionPostSkip(rmsg *model.WebSocketEvent) bool {
// maybeShorten returns a prefix of msg that is approximately newLen
// characters long, followed by "...". Words that start with uncounted
// are included in the result but are not reckoned against newLen.
func maybeShorten(msg string, newLen int, uncounted string) string {
func maybeShorten(msg string, newLen int, uncounted string, unicode bool) string {
if newLen == 0 || len(msg) < newLen {
return msg
}
ellipsis := "..."
if unicode {
ellipsis = "…"
}
newMsg := ""
for _, word := range strings.Split(strings.ReplaceAll(msg, "\n", " "), " ") {
if newMsg == "" {
Expand All @@ -693,15 +697,15 @@ func maybeShorten(msg string, newLen int, uncounted string) string {
// Truncate very long words, but only if they were not skipped, on the
// assumption that such words are important enough to be preserved whole.
if !skipped && len(word) > newLen {
word = fmt.Sprintf("%s[...]", word[0:(newLen*2/3)])
word = fmt.Sprintf("%s[%s]", word[0:(newLen*2/3)], ellipsis)
}
newMsg = fmt.Sprintf("%s %s", newMsg, word)
continue
}
break
}

return fmt.Sprintf("%s ...", newMsg)
return fmt.Sprintf("%s %s", newMsg, ellipsis)
}

// nolint:funlen,gocognit,gocyclo
Expand All @@ -724,7 +728,7 @@ func (m *Mattermost) handleWsActionPost(rmsg *model.WebSocketEvent) {
parentGhost := m.GetUser(parentPost.UserId)

if !m.v.GetBool("mattermost.hidereplies") {
parentMessage := maybeShorten(parentPost.Message, m.v.GetInt("mattermost.ShortenRepliesTo"), "@")
parentMessage := maybeShorten(parentPost.Message, m.v.GetInt("mattermost.ShortenRepliesTo"), "@", m.v.GetBool("mattermost.unicode"))
data.Message = fmt.Sprintf("%s (re @%s: %s)", data.Message, parentGhost.Nick, parentMessage)
}
}
Expand Down
3 changes: 2 additions & 1 deletion matterircd.toml.example
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,10 @@ PreferNickname = false

# Disable showing parent post / replies
HideReplies = false

# Shorten replies to approximately this length
ShortenRepliesTo = 0
# Enable Unicode.
Unicode = false

#Only join direct/group messages when someone talks. This stops from cluttering your
#irc client with lots of windows.
Expand Down

0 comments on commit 6f4f1f3

Please sign in to comment.