1
0

draw.go 7.0 KB

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