draw.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. package editor
  2. import (
  3. "moose/internal/layout"
  4. "moose/internal/buffer"
  5. "moose/internal/util"
  6. "fmt"
  7. "strings"
  8. "strconv"
  9. "github.com/gdamore/tcell/v3/color"
  10. "slices"
  11. "math"
  12. )
  13. func (m *Model) Draw() {
  14. screen := layout.RectFromScren(m.Screen)
  15. main := layout.Rect{
  16. X: screen.X,
  17. Y: screen.Y,
  18. Width: screen.Width,
  19. Height: screen.Height - 2,
  20. }
  21. palette := layout.Rect{
  22. X: screen.X,
  23. Y: main.Height,
  24. Width: screen.Width,
  25. Height: 2,
  26. }
  27. copy := m.LM.Workspaces[m.LM.ActiveIdx]
  28. m.DrawWorkspace(&copy, main)
  29. m.DrawPalette(palette)
  30. }
  31. func (m *Model) generateChordStr() string {
  32. chordStr := ""
  33. if len(m.AM.RCM.Recorded) > 0 {
  34. chordStr += strings.Join(m.AM.RCM.Recorded, "") + " + "
  35. }
  36. if len(m.AM.CM.Recorded) > 0 {
  37. chordStr += strings.Join(m.AM.CM.Recorded, "+") + " +"
  38. }
  39. return chordStr
  40. }
  41. func (m *Model) DrawPalette(rect layout.Rect) {
  42. for col := 0; col < rect.Width; col++ {
  43. m.Screen.SetContent(rect.X + col, rect.Y, ' ', nil, m.Style.Background(color.Black))
  44. }
  45. modeStr := m.Mode.String()
  46. if m.Mode == ModePalette {
  47. if strings.HasPrefix(m.BM.PaletteBuffer.String(), "/") {
  48. modeStr += " (command)"
  49. } else if strings.HasPrefix(m.BM.PaletteBuffer.String(), "?") {
  50. modeStr += " (find)"
  51. } else if strings.HasPrefix(m.BM.PaletteBuffer.String(), "=") {
  52. modeStr += " (replace)"
  53. }
  54. }
  55. m.Screen.PutStrStyled(rect.Width - (len(modeStr) + 1), rect.Y, strings.ToUpper(modeStr), m.Style.Background(color.Black))
  56. splitStr := ""
  57. if m.LM.CurrentSplit == layout.SplitHorizontal {
  58. splitStr = "H"
  59. } else {
  60. splitStr = "V"
  61. }
  62. m.Screen.PutStrStyled(rect.Width - (len(modeStr) + 1) - 5, rect.Y, splitStr, m.Style.Background(color.Black))
  63. populatedWorkspaces := []string{strconv.Itoa(m.LM.ActiveIdx + 1)}
  64. for workspaceIdx, workspace := range m.LM.Workspaces {
  65. if workspaceIdx == m.LM.ActiveIdx {
  66. continue
  67. }
  68. if !workspace.IsEmpty() {
  69. populatedWorkspaces = append(populatedWorkspaces, strconv.Itoa(workspaceIdx + 1))
  70. }
  71. }
  72. slices.Sort(populatedWorkspaces)
  73. workspacesStr := strings.Join(populatedWorkspaces, " ")
  74. m.Screen.PutStrStyled(1, rect.Y, workspacesStr, m.Style.Background(color.Black))
  75. chordStr := m.generateChordStr()
  76. m.Screen.PutStrStyled(len(workspacesStr) + 2, rect.Y, chordStr, m.Style.Background(color.Black))
  77. m.Screen.PutStrStyled(len(chordStr) + 1, rect.Y, m.DebugLog, m.Style.Background(color.Black))
  78. if m.Mode == ModePalette {
  79. m.Screen.PutStrStyled(0, rect.Y + 1, m.BM.PaletteBuffer.String(), m.Style)
  80. for _, cur := range m.BM.PaletteBuffer.CM.Cursors {
  81. line, col := buffer.LineCol(&m.BM.PaletteBuffer, cur.Offset)
  82. if line + rect.Y != rect.Y { continue }
  83. if col + 1 > rect.Width { continue }
  84. ch := buffer.RuneAt(&m.BM.PaletteBuffer, cur.Offset)
  85. m.Screen.SetContent(col, rect.Y + 1, ch, nil, m.Style.Reverse(true))
  86. }
  87. } else if strings.HasPrefix(m.BM.PaletteBuffer.String(), "moose.info:") {
  88. m.Screen.PutStrStyled(0, rect.Y + 1, string([]rune(m.BM.PaletteBuffer.String())[11:]), m.Style.Foreground(color.LightGray))
  89. } else if strings.HasPrefix(m.BM.PaletteBuffer.String(), "moose.warn:") {
  90. m.Screen.PutStrStyled(0, rect.Y + 1, string([]rune(m.BM.PaletteBuffer.String())[11:]), m.Style.Foreground(color.Orange))
  91. } else if strings.HasPrefix(m.BM.PaletteBuffer.String(), "moose.error:") {
  92. m.Screen.PutStrStyled(0, rect.Y + 1, string([]rune(m.BM.PaletteBuffer.String())[12:]), m.Style.Foreground(color.Red))
  93. }
  94. }
  95. func (m *Model) DrawWorkspace(w *layout.Workspace, rect layout.Rect) {
  96. m.DrawContainer(&w.RootContainer, rect)
  97. }
  98. func (m *Model) DrawContainer(c *layout.Container, rect layout.Rect) {
  99. length := util.NonNilLen(c.Children[:])
  100. if length == 0 {
  101. return
  102. }
  103. rect = layout.Rect{
  104. X: rect.X,
  105. Y: rect.Y,
  106. Width: rect.Width,
  107. Height: rect.Height,
  108. }
  109. rect = layout.RectDivide(rect, c.Split, length)
  110. for i, child := range c.Children {
  111. main := layout.RectDisplace(rect, c.Split, i)
  112. switch child.(type) {
  113. case layout.ContainerBuffers: {
  114. cb := child.(layout.ContainerBuffers)
  115. tabs := layout.Rect{
  116. X: main.X,
  117. Y: main.Y,
  118. Width: main.Width,
  119. Height: 1,
  120. }
  121. main.Y += 1
  122. main.Height -= 1
  123. m.DrawContainerTabs(&cb, tabs)
  124. m.DrawContainerBuffers(&cb, main)
  125. }
  126. case layout.Container: {
  127. c := child.(layout.Container)
  128. m.DrawContainer(&c, main)
  129. }
  130. }
  131. }
  132. }
  133. func (m *Model) DrawContainerTabs(c *layout.ContainerBuffers, rect layout.Rect) {
  134. tabs := []string{}
  135. activeTabIdx := -1
  136. for _, bufIdx := range c.Buffers {
  137. if bufIdx >= len(m.BM.Buffers) {
  138. continue
  139. }
  140. if c.ActiveIdx < len(c.Buffers) && c.Buffers[c.ActiveIdx] == bufIdx {
  141. activeTabIdx = len(tabs)
  142. }
  143. buf := m.BM.Buffers[bufIdx]
  144. if buf.Path == "" {
  145. tabs = append(tabs, "Buffer " + strconv.Itoa(bufIdx))
  146. } else {
  147. tabs = append(tabs, buf.Path)
  148. }
  149. }
  150. length := 0
  151. for i, tab := range tabs {
  152. if length + len(tab) + 2 > rect.Width {
  153. m.Screen.PutStrStyled(rect.X + length, rect.Y, ">", m.Style.Foreground(color.White))
  154. return
  155. }
  156. style := m.Style.Background(color.Black).Foreground(color.DarkGray)
  157. if i == activeTabIdx {
  158. style = style.Foreground(color.White)
  159. }
  160. tabStr := strconv.Itoa(int(math.Abs(float64((activeTabIdx - i))))) + ": " + tab
  161. m.Screen.PutStrStyled(rect.X + length, rect.Y, tabStr, style)
  162. length += len(tabStr) + 1
  163. }
  164. }
  165. func (m *Model) DrawContainerBuffers(c *layout.ContainerBuffers, rect layout.Rect) {
  166. if c.ActiveIdx >= len(c.Buffers) {
  167. return
  168. }
  169. if c.Buffers[c.ActiveIdx] >= len(m.BM.Buffers) {
  170. return
  171. }
  172. m.DrawBuffer(&m.BM.Buffers[c.Buffers[c.ActiveIdx]], c.Buffers[c.ActiveIdx] == m.BM.CurrentIdx, rect)
  173. }
  174. func (m *Model) DrawBuffer(buf *buffer.Buffer, isActive bool, rect layout.Rect) {
  175. primary := buf.CM.Cursors[buf.CM.PrimaryIdx]
  176. curLine, _ := buffer.LineCol(buf, primary.Offset)
  177. buf.ScrollToShow(curLine, rect.Height)
  178. startOffset := buffer.OffsetForLine(buf, buf.TopLine)
  179. endOffset := buf.Rope.Len()
  180. if endLine := buf.TopLine + rect.Height; endLine < buffer.LineCount(buf) {
  181. endOffset = buffer.OffsetForLine(buf, endLine)
  182. }
  183. visible := string(buf.Rope.Slice(startOffset, endOffset))
  184. table := strings.SplitAfter(visible, "\n")
  185. for j, line := range table {
  186. if j + 1 > rect.Height { break }
  187. lineNum := j + buf.TopLine
  188. relLine := lineNum - curLine
  189. nums := fmt.Sprintf("%4d ", int(math.Abs(float64(relLine))))
  190. r := []rune(nums + expandTabs(line))
  191. if len(r) + 1 > rect.Width {
  192. r = r[:rect.Width]
  193. }
  194. m.Screen.PutStrStyled(rect.X, rect.Y + j, string(r), m.Style)
  195. }
  196. if isActive {
  197. for _, cur := range buf.CM.Cursors {
  198. line, col := buffer.LineCol(buf, cur.Offset)
  199. screenLine := line - buf.TopLine
  200. if screenLine < 0 || screenLine + 1 > rect.Height { continue }
  201. visCol := visualCol(buffer.LineText(buf, line), col)
  202. if visCol + 1 > rect.Width { continue }
  203. ch := buffer.RuneAt(buf, cur.Offset)
  204. if m.Mode == ModeWrite {
  205. m.Screen.SetContent(rect.X + visCol + 5, rect.Y + screenLine, ch, nil, m.Style.Reverse(true))
  206. } else {
  207. m.Screen.SetContent(rect.X + visCol + 5, rect.Y + screenLine, ch, nil, m.Style.Reverse(true).Foreground(color.Gray))
  208. }
  209. }
  210. }
  211. }
  212. const tabWidth = 4
  213. func expandTabs(s string) string {
  214. var b strings.Builder
  215. col := 0
  216. for _, r := range s {
  217. if r == '\t' {
  218. spaces := tabWidth - (col % tabWidth)
  219. b.WriteString(strings.Repeat(" ", spaces))
  220. col += spaces
  221. } else {
  222. b.WriteRune(r)
  223. col++
  224. }
  225. }
  226. return b.String()
  227. }
  228. func visualCol(lineText string, runeCol int) int {
  229. col := 0
  230. n := 0
  231. for _, r := range lineText {
  232. if n >= runeCol {
  233. break
  234. }
  235. if r == '\t' {
  236. col += tabWidth - (col % tabWidth)
  237. } else {
  238. col++
  239. }
  240. n++
  241. }
  242. return col
  243. }