draw.go 5.9 KB

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