help.go 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /*
  2. * Rufs - Remote Union File System
  3. *
  4. * Copyright 2017 Matthias Ladkau. All rights reserved.
  5. *
  6. * This Source Code Form is subject to the terms of the MIT
  7. * License, If a copy of the MIT License was not distributed with this
  8. * file, You can obtain one at https://opensource.org/licenses/MIT.
  9. */
  10. package term
  11. import (
  12. "bytes"
  13. "fmt"
  14. "sort"
  15. "unicode/utf8"
  16. "devt.de/krotik/common/stringutil"
  17. )
  18. /*
  19. cmdHelp executes the help command.
  20. */
  21. func cmdHelp(tt *TreeTerm, arg ...string) (string, error) {
  22. var res bytes.Buffer
  23. if len(arg) == 0 {
  24. var maxlen = 0
  25. cmds := make([]string, 0, len(helpMap))
  26. res.WriteString("Available commands:\n")
  27. res.WriteString("----\n")
  28. for c := range helpMap {
  29. if cc := utf8.RuneCountInString(c); cc > maxlen {
  30. maxlen = cc
  31. }
  32. cmds = append(cmds, c)
  33. }
  34. sort.Strings(cmds)
  35. for _, c := range cmds {
  36. cc := utf8.RuneCountInString(c)
  37. spacer := stringutil.GenerateRollingString(" ", maxlen-cc)
  38. res.WriteString(fmt.Sprintf("%v%v : %v\n", c, spacer, helpMap[c]))
  39. }
  40. }
  41. return res.String(), nil
  42. }