1
0

draw.go 6.3 KB

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