Add better help for /toggle

This commit is contained in:
Tulir Asokan 2020-04-16 20:05:03 +03:00
parent ae770a97e2
commit bb5f9156c9

View File

@ -30,6 +30,7 @@ import (
"strconv" "strconv"
"strings" "strings"
"time" "time"
"unicode"
"github.com/lucasb-eyer/go-colorful" "github.com/lucasb-eyer/go-colorful"
"github.com/russross/blackfriday/v2" "github.com/russross/blackfriday/v2"
@ -560,6 +561,7 @@ func cmdSetState(cmd *Command) {
} }
type ToggleMessage interface { type ToggleMessage interface {
Name() string
Format(state bool) string Format(state bool) string
} }
@ -573,6 +575,10 @@ func (hm HideMessage) Format(state bool) string {
} }
} }
func (hm HideMessage) Name() string {
return string(hm)
}
type SimpleToggleMessage string type SimpleToggleMessage string
func (stm SimpleToggleMessage) Format(state bool) string { func (stm SimpleToggleMessage) Format(state bool) string {
@ -583,9 +589,13 @@ func (stm SimpleToggleMessage) Format(state bool) string {
} }
} }
func (stm SimpleToggleMessage) Name() string {
return string(unicode.ToUpper(rune(stm[0]))) + string(stm[1:])
}
var toggleMsg = map[string]ToggleMessage{ var toggleMsg = map[string]ToggleMessage{
"rooms": HideMessage("room list sidebar"), "rooms": HideMessage("Room list sidebar"),
"users": HideMessage("user list sidebar"), "users": HideMessage("User list sidebar"),
"baremessages": SimpleToggleMessage("bare message view"), "baremessages": SimpleToggleMessage("bare message view"),
"images": SimpleToggleMessage("image rendering"), "images": SimpleToggleMessage("image rendering"),
"typingnotif": SimpleToggleMessage("typing notifications"), "typingnotif": SimpleToggleMessage("typing notifications"),
@ -596,13 +606,24 @@ var toggleMsg = map[string]ToggleMessage{
"notifications": SimpleToggleMessage("desktop notifications"), "notifications": SimpleToggleMessage("desktop notifications"),
} }
func makeUsage() string {
var buf strings.Builder
buf.WriteString("Usage: /toggle <things...>\n\n")
buf.WriteString("List of Things:\n")
for key, value := range toggleMsg {
_, _ = fmt.Fprintf(&buf, "* %s - %s\n", key, value.Name())
}
return buf.String()[:buf.Len()-1]
}
func cmdToggle(cmd *Command) { func cmdToggle(cmd *Command) {
if len(cmd.Args) == 0 { if len(cmd.Args) == 0 {
cmd.Reply("Usage: /toggle <rooms/users/baremessages/images/typingnotif/emojis>") cmd.Reply(makeUsage())
return return
} }
for _, thing := range cmd.Args {
var val *bool var val *bool
switch cmd.Args[0] { switch thing {
case "rooms": case "rooms":
val = &cmd.Config.Preferences.HideRoomList val = &cmd.Config.Preferences.HideRoomList
case "users": case "users":
@ -624,11 +645,13 @@ func cmdToggle(cmd *Command) {
case "notifications": case "notifications":
val = &cmd.Config.Preferences.DisableNotifications val = &cmd.Config.Preferences.DisableNotifications
default: default:
cmd.Reply("Usage: /toggle <rooms/users/baremessages/images/typingnotif/emojis>") cmd.Reply("Unknown toggle %s. Use /toggle without arguments for a list of togglable things.", thing)
return return
} }
*val = !(*val) *val = !(*val)
cmd.Reply(toggleMsg[cmd.Args[0]].Format(*val)) debug.Print(thing, *val)
cmd.Reply(toggleMsg[thing].Format(*val))
}
cmd.UI.Render() cmd.UI.Render()
go cmd.Matrix.SendPreferencesToMatrix() go cmd.Matrix.SendPreferencesToMatrix()
} }