draw.go 5.9 KB

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