helper.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /*
  2. * ECAL
  3. *
  4. * Copyright 2020 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 tool
  11. import (
  12. "fmt"
  13. "regexp"
  14. "strings"
  15. "devt.de/krotik/common/stringutil"
  16. "devt.de/krotik/ecal/interpreter"
  17. )
  18. /*
  19. CLIInputHandler is a handler object for CLI input.
  20. */
  21. type CLIInputHandler interface {
  22. /*
  23. CanHandle checks if a given string can be handled by this handler.
  24. */
  25. CanHandle(s string) bool
  26. /*
  27. Handle handles a given input string.
  28. */
  29. Handle(ot interpreter.OutputTerminal, args []string)
  30. }
  31. /*
  32. matchesFulltextSearch checks if a given text matches a given glob expression. Returns
  33. true if an error occurs.
  34. */
  35. func matchesFulltextSearch(ot interpreter.OutputTerminal, text string, glob string) bool {
  36. var res bool
  37. re, err := stringutil.GlobToRegex(glob)
  38. if err == nil {
  39. res, err = regexp.MatchString(re, text)
  40. }
  41. if err != nil {
  42. ot.WriteString(fmt.Sprintln("Invalid search expression:", err.Error()))
  43. res = true
  44. }
  45. return res
  46. }
  47. /*
  48. fillTableRow fills a table row of a display table.
  49. */
  50. func fillTableRow(tabData []string, key string, value string) []string {
  51. tabData = append(tabData, key)
  52. valSplit := stringutil.ChunkSplit(value, 80, true)
  53. tabData = append(tabData, strings.TrimSpace(valSplit[0]))
  54. for _, valPart := range valSplit[1:] {
  55. tabData = append(tabData, "")
  56. tabData = append(tabData, strings.TrimSpace(valPart))
  57. }
  58. // Insert empty rows to ease reading
  59. tabData = append(tabData, "")
  60. tabData = append(tabData, "")
  61. return tabData
  62. }