comcord/commands/help.go

84 lines
1.7 KiB
Go
Raw Normal View History

2023-07-08 20:51:26 +00:00
package commands
import (
"fmt"
"sort"
2023-07-08 20:51:26 +00:00
"strings"
"unicode"
"unicode/utf8"
2023-07-08 20:51:26 +00:00
2023-07-09 04:42:58 +00:00
"github.com/Cynosphere/comcord/state"
"github.com/mgutz/ansi"
2023-07-08 20:51:26 +00:00
)
2023-07-09 04:42:58 +00:00
const format string = " %s - %s%s"
func lessLower(sa, sb string) bool {
for {
rb, nb := utf8.DecodeRuneInString(sb)
if nb == 0 {
// The number of runes in sa is greater than or
// equal to the number of runes in sb. It follows
// that sa is not less than sb.
return false
}
ra, na := utf8.DecodeRuneInString(sa)
if na == 0 {
// The number of runes in sa is less than the
// number of runes in sb. It follows that sa
// is less than sb.
return true
}
rbl := unicode.ToLower(rb)
ral := unicode.ToLower(ra)
if ral != rbl {
return ral < rbl
} else {
return ra > rb
}
}
}
func HelpCommand() {
2023-07-09 04:42:58 +00:00
noColor := state.HasNoColor()
2023-07-08 20:51:26 +00:00
fmt.Println("\r\nCOMcord (c)left 2023\n\r")
commands := GetAllCommands()
keys := make([]string, 0, len(commands))
for key := range commands {
keys = append(keys, key)
}
sort.Slice(keys, func(i, j int) bool {
return lessLower(keys[i], keys[j])
})
2023-07-08 20:51:26 +00:00
index := 0
for _, key := range keys {
cmd := commands[key]
2023-07-09 04:42:58 +00:00
str := fmt.Sprintf(format, key, cmd.Description, "")
2023-07-08 20:51:26 +00:00
length := len(str)
2023-07-09 04:42:58 +00:00
padding := strings.Repeat(" ", 25 - length)
2023-07-08 20:51:26 +00:00
2023-07-09 04:42:58 +00:00
if noColor {
fmt.Printf(format, key, cmd.Description, padding)
} else {
coloredKey := ansi.Color(key, "yellow+b")
fmt.Printf(format, coloredKey, cmd.Description, padding)
}
2023-07-08 20:51:26 +00:00
index++
if index % 3 == 0 {
fmt.Print("\n\r")
}
}
if index % 3 != 0 {
fmt.Print("\n\r")
}
fmt.Println("\r\nTo begin TALK MODE, press [SPACE]\n\r")
}