mirror of
https://github.com/shazow/ssh-chat.git
synced 2025-04-15 00:20:37 +03:00
Padded help output, because why not.
This commit is contained in:
parent
fb7cd83821
commit
3b5f4faf76
@ -3,7 +3,6 @@ package chat
|
|||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"sort"
|
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
)
|
)
|
||||||
@ -23,22 +22,25 @@ var ErrMissingPrefix = errors.New("command missing prefix")
|
|||||||
|
|
||||||
// Command is a definition of a handler for a command.
|
// Command is a definition of a handler for a command.
|
||||||
type Command struct {
|
type Command struct {
|
||||||
Prefix string
|
// The command's key, such as /foo
|
||||||
|
Prefix string
|
||||||
|
// Extra help regarding arguments
|
||||||
PrefixHelp string
|
PrefixHelp string
|
||||||
Help string
|
// If omitted, command is hidden from /help
|
||||||
Handler func(*Channel, CommandMsg) error
|
Help string
|
||||||
|
Handler func(*Channel, CommandMsg) error
|
||||||
}
|
}
|
||||||
|
|
||||||
// Commands is a registry of available commands.
|
// Commands is a registry of available commands.
|
||||||
type Commands struct {
|
type Commands struct {
|
||||||
commands map[string]Command
|
commands map[string]*Command
|
||||||
sync.RWMutex
|
sync.RWMutex
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewCommands returns a new Commands registry.
|
// NewCommands returns a new Commands registry.
|
||||||
func NewCommands() *Commands {
|
func NewCommands() *Commands {
|
||||||
return &Commands{
|
return &Commands{
|
||||||
commands: map[string]Command{},
|
commands: map[string]*Command{},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -52,7 +54,7 @@ func (c *Commands) Add(cmd Command) error {
|
|||||||
return ErrMissingPrefix
|
return ErrMissingPrefix
|
||||||
}
|
}
|
||||||
|
|
||||||
c.commands[cmd.Prefix] = cmd
|
c.commands[cmd.Prefix] = &cmd
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -91,13 +93,9 @@ func (c *Commands) Help() string {
|
|||||||
c.RLock()
|
c.RLock()
|
||||||
defer c.RUnlock()
|
defer c.RUnlock()
|
||||||
|
|
||||||
r := []string{}
|
// TODO: Could cache this...
|
||||||
for _, cmd := range c.commands {
|
help := NewCommandsHelp(c)
|
||||||
r = append(r, fmt.Sprintf("%s %s - %s", cmd.Prefix, cmd.PrefixHelp, cmd.Help))
|
return help.String()
|
||||||
}
|
|
||||||
sort.Strings(r)
|
|
||||||
|
|
||||||
return strings.Join(r, Newline)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var defaultCmdHandlers *Commands
|
var defaultCmdHandlers *Commands
|
||||||
|
58
chat/help.go
Normal file
58
chat/help.go
Normal file
@ -0,0 +1,58 @@
|
|||||||
|
package chat
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"sort"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
type helpItem struct {
|
||||||
|
Prefix string
|
||||||
|
Text string
|
||||||
|
}
|
||||||
|
|
||||||
|
type help struct {
|
||||||
|
items []helpItem
|
||||||
|
prefixWidth int
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewCommandsHelp creates a help container from a commands container.
|
||||||
|
func NewCommandsHelp(c *Commands) *help {
|
||||||
|
lookup := map[string]struct{}{}
|
||||||
|
h := help{
|
||||||
|
items: []helpItem{},
|
||||||
|
}
|
||||||
|
for _, cmd := range c.commands {
|
||||||
|
if cmd.Help == "" {
|
||||||
|
// Skip hidden commands.
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
_, exists := lookup[cmd.Prefix]
|
||||||
|
if exists {
|
||||||
|
// Duplicate (alias)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
lookup[cmd.Prefix] = struct{}{}
|
||||||
|
prefix := fmt.Sprintf("%s %s", cmd.Prefix, cmd.PrefixHelp)
|
||||||
|
h.add(helpItem{prefix, cmd.Help})
|
||||||
|
}
|
||||||
|
return &h
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *help) add(item helpItem) {
|
||||||
|
h.items = append(h.items, item)
|
||||||
|
if len(item.Prefix) > h.prefixWidth {
|
||||||
|
h.prefixWidth = len(item.Prefix)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h help) String() string {
|
||||||
|
r := []string{}
|
||||||
|
format := fmt.Sprintf("%%-%ds - %%s", h.prefixWidth)
|
||||||
|
for _, item := range h.items {
|
||||||
|
r = append(r, fmt.Sprintf(format, item.Prefix, item.Text))
|
||||||
|
}
|
||||||
|
|
||||||
|
sort.Strings(r)
|
||||||
|
return strings.Join(r, Newline)
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user