pgenr.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. package pgenr
  2. const (
  3. TextThemeSuccess = "success"
  4. TextThemeDanger = "danger"
  5. )
  6. type (
  7. TextOption func(t *Text)
  8. ButtonOption func(btn *Button)
  9. Text struct {
  10. Content string
  11. Theme string
  12. Color string
  13. }
  14. Button struct {
  15. Url string
  16. Text string
  17. Color string
  18. }
  19. Entry struct {
  20. Title string
  21. Items map[string]*Text
  22. }
  23. Action struct {
  24. Instructions string
  25. Button *Button
  26. InviteCode string
  27. }
  28. Page struct {
  29. Title string
  30. Head string
  31. Intros []string
  32. Entries []*Entry
  33. Actions []Action
  34. Outros []string
  35. Copyright string
  36. }
  37. )
  38. func (e *Entry) AddItem(label string, txt *Text) *Entry {
  39. if e.Items == nil {
  40. e.Items = make(map[string]*Text)
  41. }
  42. e.Items[label] = txt
  43. return e
  44. }
  45. func (page *Page) SetHead(s string) *Page {
  46. page.Head = s
  47. return page
  48. }
  49. func (page *Page) SetCopyright(s string) *Page {
  50. page.Copyright = s
  51. return page
  52. }
  53. func (page *Page) AddIntro(s string) *Page {
  54. page.Intros = append(page.Intros, s)
  55. return page
  56. }
  57. func (page *Page) AddEntry(e *Entry) *Page {
  58. page.Entries = append(page.Entries, e)
  59. return page
  60. }
  61. func (page *Page) AddButtonAction(s string, btn *Button) *Page {
  62. page.Actions = append(page.Actions, Action{Instructions: s, Button: btn})
  63. return page
  64. }
  65. func (page *Page) AddInviteCodeAction(s string, code string) *Page {
  66. page.Actions = append(page.Actions, Action{Instructions: s, InviteCode: code})
  67. return page
  68. }
  69. func (page *Page) AddOutro(s string) *Page {
  70. page.Outros = append(page.Outros, s)
  71. return page
  72. }
  73. func NewPage(title string) *Page {
  74. return &Page{
  75. Title: title,
  76. Intros: make([]string, 0),
  77. Entries: make([]*Entry, 0),
  78. Actions: make([]Action, 0),
  79. Outros: make([]string, 0),
  80. }
  81. }
  82. func NewEntry(title string) *Entry {
  83. return &Entry{
  84. Title: title,
  85. Items: make(map[string]*Text),
  86. }
  87. }
  88. func WithTextTheme(s string) TextOption {
  89. return func(t *Text) {
  90. t.Theme = s
  91. }
  92. }
  93. func WithTextColor(color string) TextOption {
  94. return func(t *Text) {
  95. t.Color = color
  96. }
  97. }
  98. func NewButton(label, link string, opts ...ButtonOption) *Button {
  99. btn := &Button{Text: label, Url: link}
  100. for _, cb := range opts {
  101. cb(btn)
  102. }
  103. return btn
  104. }
  105. func NewText(s string, opts ...TextOption) *Text {
  106. txt := &Text{Content: s}
  107. for _, cb := range opts {
  108. cb(txt)
  109. }
  110. return txt
  111. }