helper.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. )
  17. /*
  18. CLIInputHandler is a handler object for CLI input.
  19. */
  20. type CLIInputHandler interface {
  21. /*
  22. CanHandle checks if a given string can be handled by this handler.
  23. */
  24. CanHandle(s string) bool
  25. /*
  26. Handle handles a given input string.
  27. */
  28. Handle(ot OutputTerminal, input string)
  29. }
  30. /*
  31. OutputTerminal is a generic output terminal which can write strings.
  32. */
  33. type OutputTerminal interface {
  34. /*
  35. WriteString write a string on this terminal.
  36. */
  37. WriteString(s string)
  38. }
  39. /*
  40. matchesFulltextSearch checks if a given text matches a given glob expression. Returns
  41. true if an error occurs.
  42. */
  43. func matchesFulltextSearch(ot OutputTerminal, text string, glob string) bool {
  44. var res bool
  45. re, err := stringutil.GlobToRegex(glob)
  46. if err == nil {
  47. res, err = regexp.MatchString(re, text)
  48. }
  49. if err != nil {
  50. ot.WriteString(fmt.Sprintln("Invalid search expression:", err.Error()))
  51. res = true
  52. }
  53. return res
  54. }
  55. /*
  56. fillTableRow fills a table row of a display table.
  57. */
  58. func fillTableRow(tabData []string, key string, value string) []string {
  59. tabData = append(tabData, key)
  60. valSplit := stringutil.ChunkSplit(value, 80, true)
  61. tabData = append(tabData, strings.TrimSpace(valSplit[0]))
  62. for _, valPart := range valSplit[1:] {
  63. tabData = append(tabData, "")
  64. tabData = append(tabData, strings.TrimSpace(valPart))
  65. }
  66. // Insert empty rows to ease reading
  67. tabData = append(tabData, "")
  68. tabData = append(tabData, "")
  69. return tabData
  70. }