From afe24beb4d69cd064e368b0113490e4005be97e3 Mon Sep 17 00:00:00 2001 From: Slavi Pantaleev Date: Wed, 24 Aug 2022 11:39:24 +0300 Subject: [PATCH 1/2] Use a command list (not a map) to have a consistent manually-defined commands order --- bot/command.go | 68 +++++++++++++++++++++++++++++++++++++------------- 1 file changed, 51 insertions(+), 17 deletions(-) diff --git a/bot/command.go b/bot/command.go index d3efa5b..d4a5280 100644 --- a/bot/command.go +++ b/bot/command.go @@ -15,23 +15,57 @@ import ( type sanitizerFunc func(string) string +type commandDefinition struct { + key string + description string +} + +type commandList []commandDefinition + +func (c commandList) get(key string) (*commandDefinition, bool) { + for _, command := range c { + if command.key == key { + return &command, true + } + } + return nil, false +} + var ( - commands = map[string]string{ + commands = commandList{ // special commands - "help": "Get help", - "stop": "Disable bridge for the room and clear all configuration", + { + key: "help", + description: "Show this help message", + }, + { + key: "stop", + description: "Disable bridge for the room and clear all configuration", + }, // options commands - optionMailbox: "Get or set mailbox of the room", - optionOwner: "Get or set owner of the room", - optionNoSender: fmt.Sprintf( - "Get or set `%s` of the room (`true` - hide email sender; `false` - show email sender)", - optionNoSender, - ), - optionNoSubject: fmt.Sprintf( - "Get or set `%s` of the room (`true` - hide email subject; `false` - show email subject)", - optionNoSubject, - ), + { + key: optionMailbox, + description: "Get or set mailbox of the room", + }, + { + key: optionOwner, + description: "Get or set owner of the room", + }, + { + key: optionNoSender, + description: fmt.Sprintf( + "Get or set `%s` of the room (`true` - hide email sender; `false` - show email sender)", + optionNoSender, + ), + }, + { + key: optionNoSubject, + description: fmt.Sprintf( + "Get or set `%s` of the room (`true` - hide email subject; `false` - show email subject)", + optionNoSubject, + ), + }, } // sanitizers is map of option name => sanitizer function @@ -43,7 +77,7 @@ var ( ) func (b *Bot) handleCommand(ctx context.Context, evt *event.Event, command []string) { - if _, ok := commands[command[0]]; !ok { + if _, ok := commands.get(command[0]); !ok { return } @@ -82,11 +116,11 @@ func (b *Bot) sendHelp(ctx context.Context, roomID id.RoomID) { var msg strings.Builder msg.WriteString("the following commands are supported:\n\n") - for name, desc := range commands { + for _, command := range commands { msg.WriteString("* **") - msg.WriteString(name) + msg.WriteString(command.key) msg.WriteString("** - ") - msg.WriteString(desc) + msg.WriteString(command.description) msg.WriteString("\n") } From 34735b261443ec6f0f1f93729944d6a7b9439348 Mon Sep 17 00:00:00 2001 From: Aine Date: Wed, 24 Aug 2022 12:02:41 +0300 Subject: [PATCH 2/2] visual fixes --- bot/command.go | 64 ++++++++++---------------------------------------- utils/list.go | 27 +++++++++++++++++++++ 2 files changed, 39 insertions(+), 52 deletions(-) create mode 100644 utils/list.go diff --git a/bot/command.go b/bot/command.go index d4a5280..8b73b46 100644 --- a/bot/command.go +++ b/bot/command.go @@ -15,57 +15,17 @@ import ( type sanitizerFunc func(string) string -type commandDefinition struct { - key string - description string -} - -type commandList []commandDefinition - -func (c commandList) get(key string) (*commandDefinition, bool) { - for _, command := range c { - if command.key == key { - return &command, true - } - } - return nil, false -} - var ( - commands = commandList{ + commands = utils.List{ // special commands - { - key: "help", - description: "Show this help message", - }, - { - key: "stop", - description: "Disable bridge for the room and clear all configuration", - }, + {K: "help", V: "Show this help message"}, + {K: "stop", V: "Disable bridge for the room and clear all configuration"}, // options commands - { - key: optionMailbox, - description: "Get or set mailbox of the room", - }, - { - key: optionOwner, - description: "Get or set owner of the room", - }, - { - key: optionNoSender, - description: fmt.Sprintf( - "Get or set `%s` of the room (`true` - hide email sender; `false` - show email sender)", - optionNoSender, - ), - }, - { - key: optionNoSubject, - description: fmt.Sprintf( - "Get or set `%s` of the room (`true` - hide email subject; `false` - show email subject)", - optionNoSubject, - ), - }, + {K: optionMailbox, V: "Get or set mailbox of the room"}, + {K: optionOwner, V: "Get or set owner of the room"}, + {K: optionNoSender, V: fmt.Sprintf("Get or set `%s` of the room (`true` - hide email sender; `false` - show email sender)", optionNoSender)}, + {K: optionNoSubject, V: fmt.Sprintf("Get or set `%s` of the room (`true` - hide email subject; `false` - show email subject)", optionNoSubject)}, } // sanitizers is map of option name => sanitizer function @@ -77,7 +37,7 @@ var ( ) func (b *Bot) handleCommand(ctx context.Context, evt *event.Event, command []string) { - if _, ok := commands.get(command[0]); !ok { + if _, ok := commands.Get(command[0]); !ok { return } @@ -116,13 +76,13 @@ func (b *Bot) sendHelp(ctx context.Context, roomID id.RoomID) { var msg strings.Builder msg.WriteString("the following commands are supported:\n\n") - for _, command := range commands { + commands.ForEach(func(command, description string) { msg.WriteString("* **") - msg.WriteString(command.key) + msg.WriteString(command) msg.WriteString("** - ") - msg.WriteString(command.description) + msg.WriteString(description) msg.WriteString("\n") - } + }) content := format.RenderMarkdown(msg.String(), true, true) content.MsgType = event.MsgNotice diff --git a/utils/list.go b/utils/list.go new file mode 100644 index 0000000..3e9cd5f --- /dev/null +++ b/utils/list.go @@ -0,0 +1,27 @@ +package utils + +// ListItem with key and value +type ListItem struct { + K string + V string +} + +// List slice +type List []ListItem + +// Get item's value +func (l List) Get(key string) (string, bool) { + for _, item := range l { + if item.K == key { + return item.V, true + } + } + return "", false +} + +// ForEach item +func (l List) ForEach(handler func(key, value string)) { + for _, item := range l { + handler(item.K, item.V) + } +}