helper.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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/common/termutil"
  17. )
  18. /*
  19. matchesFulltextSearch checks if a given text matches a given glob expression. Returns
  20. true if an error occurs.
  21. */
  22. func matchesFulltextSearch(clt termutil.ConsoleLineTerminal, text string, glob string) bool {
  23. var res bool
  24. re, err := stringutil.GlobToRegex(glob)
  25. if err == nil {
  26. res, err = regexp.MatchString(re, text)
  27. }
  28. if err != nil {
  29. clt.WriteString(fmt.Sprintln("Invalid search expression:", err.Error()))
  30. res = true
  31. }
  32. return res
  33. }
  34. /*
  35. fillTableRow fills a table row of a display table.
  36. */
  37. func fillTableRow(tabData []string, key string, value string) []string {
  38. tabData = append(tabData, key)
  39. valSplit := stringutil.ChunkSplit(value, 80, true)
  40. tabData = append(tabData, strings.TrimSpace(valSplit[0]))
  41. for _, valPart := range valSplit[1:] {
  42. tabData = append(tabData, "")
  43. tabData = append(tabData, strings.TrimSpace(valPart))
  44. }
  45. // Insert empty rows to ease reading
  46. tabData = append(tabData, "")
  47. tabData = append(tabData, "")
  48. return tabData
  49. }