Bombabillo is a non-web client for the terminal, supporting Gopher, Gemini and much more. https://bombadillo.colorfield.space
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

149 lines
3.9 KiB

  1. package main
  2. import (
  3. "fmt"
  4. "strings"
  5. "tildegit.org/sloum/bombadillo/cui"
  6. )
  7. //------------------------------------------------\\
  8. // + + + T Y P E S + + + \\
  9. //--------------------------------------------------\\
  10. // Bookmarks represents the contents of the bookmarks
  11. // bar, as well as its visibility, focus, and scroll
  12. // state.
  13. type Bookmarks struct {
  14. IsOpen bool
  15. IsFocused bool
  16. Position int
  17. Length int
  18. Titles []string
  19. Links []string
  20. }
  21. //------------------------------------------------\\
  22. // + + + R E C E I V E R S + + + \\
  23. //--------------------------------------------------\\
  24. // Add a bookmark to the bookmarks struct
  25. func (b *Bookmarks) Add(v []string) (string, error) {
  26. if len(v) < 2 {
  27. return "", fmt.Errorf("Received %d arguments, expected 2+", len(v))
  28. }
  29. b.Titles = append(b.Titles, strings.Join(v[1:], " "))
  30. b.Links = append(b.Links, v[0])
  31. b.Length = len(b.Titles)
  32. return "Bookmark added successfully", nil
  33. }
  34. // Delete a bookmark from the bookmarks struct
  35. func (b *Bookmarks) Delete(i int) (string, error) {
  36. if i < len(b.Titles) && len(b.Titles) == len(b.Links) {
  37. b.Titles = append(b.Titles[:i], b.Titles[i+1:]...)
  38. b.Links = append(b.Links[:i], b.Links[i+1:]...)
  39. b.Length = len(b.Titles)
  40. return "Bookmark deleted successfully", nil
  41. }
  42. return "", fmt.Errorf("Bookmark %d does not exist", i)
  43. }
  44. // ToggleOpen toggles visibility state of the bookmarks bar
  45. func (b *Bookmarks) ToggleOpen() {
  46. b.IsOpen = !b.IsOpen
  47. if b.IsOpen {
  48. b.IsFocused = true
  49. } else {
  50. b.IsFocused = false
  51. }
  52. }
  53. // ToggleFocused toggles the focal state of the bookmarks bar
  54. func (b *Bookmarks) ToggleFocused() {
  55. if b.IsOpen {
  56. b.IsFocused = !b.IsFocused
  57. }
  58. }
  59. // IniDump returns a string representing the current bookmarks
  60. // in the format that .bombadillo.ini uses
  61. func (b Bookmarks) IniDump() string {
  62. if len(b.Titles) < 1 {
  63. return ""
  64. }
  65. out := "[BOOKMARKS]\n"
  66. for i := 0; i < len(b.Titles); i++ {
  67. out += b.Titles[i]
  68. out += "="
  69. out += b.Links[i]
  70. out += "\n"
  71. }
  72. return out
  73. }
  74. // List returns a list, including link nums, of bookmarks
  75. // as a string slice
  76. func (b Bookmarks) List() []string {
  77. var out []string
  78. for i, t := range b.Titles {
  79. out = append(out, fmt.Sprintf("[%d] %s", i, t))
  80. }
  81. return out
  82. }
  83. // Render returns a string slice with the contents of each
  84. // visual row of the bookmark bar.
  85. func (b Bookmarks) Render(termwidth, termheight int) []string {
  86. width := 40
  87. termheight -= 3
  88. var walll, wallr, floor, ceil, tr, tl, br, bl string
  89. if termwidth < 40 {
  90. width = termwidth
  91. }
  92. if b.IsFocused {
  93. walll = cui.Shapes["awalll"]
  94. wallr = cui.Shapes["awallr"]
  95. ceil = cui.Shapes["aceiling"]
  96. floor = cui.Shapes["afloor"]
  97. tr = cui.Shapes["atr"]
  98. br = cui.Shapes["abr"]
  99. tl = cui.Shapes["atl"]
  100. bl = cui.Shapes["abl"]
  101. } else {
  102. walll = cui.Shapes["walll"]
  103. wallr = cui.Shapes["wallr"]
  104. ceil = cui.Shapes["ceiling"]
  105. floor = cui.Shapes["floor"]
  106. tr = cui.Shapes["tr"]
  107. br = cui.Shapes["br"]
  108. tl = cui.Shapes["tl"]
  109. bl = cui.Shapes["bl"]
  110. }
  111. out := make([]string, 0, 5)
  112. contentWidth := width - 2
  113. top := fmt.Sprintf("%s%s%s", tl, strings.Repeat(ceil, contentWidth), tr)
  114. out = append(out, top)
  115. marks := b.List()
  116. for i := 0; i < termheight-2; i++ {
  117. if i+b.Position >= len(b.Titles) {
  118. out = append(out, fmt.Sprintf("%s%-*.*s%s", walll, contentWidth, contentWidth, "", wallr))
  119. } else {
  120. out = append(out, fmt.Sprintf("%s%-*.*s%s", walll, contentWidth, contentWidth, marks[i+b.Position], wallr))
  121. }
  122. }
  123. bottom := fmt.Sprintf("%s%s%s", bl, strings.Repeat(floor, contentWidth), br)
  124. out = append(out, bottom)
  125. return out
  126. }
  127. //------------------------------------------------\\
  128. // + + + F U N C T I O N S + + + \\
  129. //--------------------------------------------------\\
  130. // MakeBookmarks creates a Bookmark struct with default values
  131. func MakeBookmarks() Bookmarks {
  132. return Bookmarks{false, false, 0, 0, make([]string, 0), make([]string, 0)}
  133. }