draw.go 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. package editor
  2. import (
  3. "fmt"
  4. "math"
  5. "moose/internal/buffer"
  6. "moose/internal/layout"
  7. "moose/internal/util"
  8. "slices"
  9. "strconv"
  10. "strings"
  11. "github.com/gdamore/tcell/v3"
  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.Config.StyleDefault.Background(tcell.GetColor(m.Config.Style.PaletteBarBackground)).Foreground(tcell.GetColor(m.Config.Style.PaletteBarForeground)))
  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.Config.StyleDefault.Foreground(tcell.GetColor(m.Config.Style.PaletteBarForeground)).Background(tcell.GetColor(m.Config.Style.PaletteBarBackground)))
  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.Config.StyleDefault.Foreground(tcell.GetColor(m.Config.Style.PaletteBarForeground)).Background(tcell.GetColor(m.Config.Style.PaletteBarBackground)))
  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.Config.StyleDefault.Foreground(tcell.GetColor(m.Config.Style.WorkspaceForeground)).Background(tcell.GetColor(m.Config.Style.WorkspaceBackground)))
  75. chordStr := m.generateChordStr()
  76. m.Screen.PutStrStyled(len(workspacesStr)+2, rect.Y, chordStr, m.Config.StyleDefault.Foreground(tcell.GetColor(m.Config.Style.PaletteBarForeground)).Background(tcell.GetColor(m.Config.Style.PaletteBarBackground)))
  77. m.Screen.PutStrStyled(len(chordStr)+1, rect.Y, m.DebugLog, m.Config.StyleDefault.Foreground(tcell.GetColor(m.Config.Style.PaletteBarForeground)).Background(tcell.GetColor(m.Config.Style.PaletteInputBackground)))
  78. if m.Mode == ModePalette {
  79. m.Screen.PutStrStyled(0, rect.Y+1, m.BM.PaletteBuffer.String(), m.Config.StyleDefault.Foreground(tcell.GetColor(m.Config.Style.PaletteInputForeground)).Background(tcell.GetColor(m.Config.Style.PaletteInputBackground)))
  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 {
  83. continue
  84. }
  85. if col+1 > rect.Width {
  86. continue
  87. }
  88. ch := buffer.RuneAt(&m.BM.PaletteBuffer, cur.Offset)
  89. m.Screen.SetContent(col, rect.Y+1, ch, nil, m.Config.StyleDefault.Background(tcell.GetColor(m.Config.Style.CursorColorWrite)))
  90. }
  91. } else if strings.HasPrefix(m.BM.PaletteBuffer.String(), "moose.info:") {
  92. m.Screen.PutStrStyled(0, rect.Y+1, string([]rune(m.BM.PaletteBuffer.String())[11:]), m.Config.StyleDefault.Foreground(tcell.GetColor(m.Config.Style.InfoMsgForeground)).Background(tcell.GetColor(m.Config.Style.InfoMsgBackground)))
  93. } else if strings.HasPrefix(m.BM.PaletteBuffer.String(), "moose.warn:") {
  94. m.Screen.PutStrStyled(0, rect.Y+1, string([]rune(m.BM.PaletteBuffer.String())[11:]), m.Config.StyleDefault.Foreground(tcell.GetColor(m.Config.Style.WarnMsgForeground)).Background(tcell.GetColor(m.Config.Style.WarnMsgBackground)))
  95. } else if strings.HasPrefix(m.BM.PaletteBuffer.String(), "moose.error:") {
  96. m.Screen.PutStrStyled(0, rect.Y+1, string([]rune(m.BM.PaletteBuffer.String())[12:]), m.Config.StyleDefault.Foreground(tcell.GetColor(m.Config.Style.ErrorMsgForeground)).Background(tcell.GetColor(m.Config.Style.ErrorMsgBackground)))
  97. }
  98. }
  99. func (m *Model) DrawWorkspace(w *layout.Workspace, rect layout.Rect) {
  100. m.DrawContainer(&w.RootContainer, rect)
  101. }
  102. func (m *Model) DrawContainer(c *layout.Container, rect layout.Rect) {
  103. length := util.NonNilLen(c.Children[:])
  104. if length == 0 {
  105. return
  106. }
  107. rect = layout.Rect{
  108. X: rect.X,
  109. Y: rect.Y,
  110. Width: rect.Width,
  111. Height: rect.Height,
  112. }
  113. rect = layout.RectDivide(rect, c.Split, length)
  114. for i, child := range c.Children {
  115. main := layout.RectDisplace(rect, c.Split, i)
  116. switch child.(type) {
  117. case layout.ContainerBuffers:
  118. {
  119. cb := child.(layout.ContainerBuffers)
  120. tabs := layout.Rect{
  121. X: main.X,
  122. Y: main.Y,
  123. Width: main.Width,
  124. Height: 1,
  125. }
  126. main.Y += 1
  127. main.Height -= 1
  128. m.DrawContainerTabs(&cb, tabs)
  129. m.DrawContainerBuffers(&cb, main)
  130. }
  131. case layout.Container:
  132. {
  133. c := child.(layout.Container)
  134. m.DrawContainer(&c, main)
  135. }
  136. }
  137. }
  138. }
  139. func (m *Model) DrawContainerTabs(c *layout.ContainerBuffers, rect layout.Rect) {
  140. tabs := []string{}
  141. activeTabIdx := -1
  142. for _, bufIdx := range c.Buffers {
  143. if bufIdx >= len(m.BM.Buffers) {
  144. continue
  145. }
  146. if c.ActiveIdx < len(c.Buffers) && c.Buffers[c.ActiveIdx] == bufIdx {
  147. activeTabIdx = len(tabs)
  148. }
  149. buf := m.BM.Buffers[bufIdx]
  150. if buf.Path == "" {
  151. tabs = append(tabs, "Buffer "+strconv.Itoa(bufIdx))
  152. } else {
  153. tabs = append(tabs, buf.Path)
  154. }
  155. }
  156. length := 0
  157. for i, tab := range tabs {
  158. if length+len(tab)+2 > rect.Width {
  159. m.Screen.PutStrStyled(rect.X+length, rect.Y, ">", m.Config.StyleDefault.Foreground(tcell.GetColor(m.Config.Style.TabForeground)).Background(tcell.GetColor(m.Config.Style.TabBackground)))
  160. return
  161. }
  162. style := m.Config.StyleDefault.Background(tcell.GetColor(m.Config.Style.TabBackground)).Foreground(tcell.GetColor(m.Config.Style.TabForeground))
  163. if i == activeTabIdx {
  164. style = style.Foreground(tcell.GetColor(m.Config.Style.TabForegroundActive)).Background(tcell.GetColor(m.Config.Style.TabBackgroundActive))
  165. }
  166. tabStr := strconv.Itoa(int(math.Abs(float64((activeTabIdx - i))))) + ": " + tab
  167. m.Screen.PutStrStyled(rect.X+length, rect.Y, tabStr, style)
  168. length += len(tabStr) + 1
  169. }
  170. }
  171. func (m *Model) DrawContainerBuffers(c *layout.ContainerBuffers, rect layout.Rect) {
  172. if c.ActiveIdx >= len(c.Buffers) {
  173. return
  174. }
  175. if c.Buffers[c.ActiveIdx] >= len(m.BM.Buffers) {
  176. return
  177. }
  178. m.DrawBuffer(&m.BM.Buffers[c.Buffers[c.ActiveIdx]], c.Buffers[c.ActiveIdx] == m.BM.CurrentIdx, rect)
  179. }
  180. func (m *Model) DrawBuffer(buf *buffer.Buffer, isActive bool, rect layout.Rect) {
  181. primary := buf.CM.Cursors[buf.CM.PrimaryIdx]
  182. curLine, _ := buffer.LineCol(buf, primary.Offset)
  183. buf.ScrollToShow(curLine, rect.Height)
  184. startOffset := buffer.OffsetForLine(buf, buf.TopLine)
  185. endOffset := buf.Rope.Len()
  186. if endLine := buf.TopLine + rect.Height; endLine < buffer.LineCount(buf) {
  187. endOffset = buffer.OffsetForLine(buf, endLine)
  188. }
  189. visible := string(buf.Rope.Slice(startOffset, endOffset))
  190. table := strings.SplitAfter(visible, "\n")
  191. for j, line := range table {
  192. if j+1 > rect.Height {
  193. break
  194. }
  195. lineNum := j + buf.TopLine
  196. relLine := lineNum - curLine
  197. nums := fmt.Sprintf("%4d ", int(math.Abs(float64(relLine))))
  198. r := []rune(nums + expandTabs(line))
  199. if len(r)+1 > rect.Width {
  200. r = r[:rect.Width]
  201. }
  202. m.Screen.PutStrStyled(rect.X, rect.Y+j, string(r), m.Config.StyleDefault.Foreground(tcell.GetColor(m.Config.Style.MainForeground)).Background(tcell.GetColor(m.Config.Style.MainBackground)))
  203. }
  204. if isActive {
  205. for _, cur := range buf.CM.Cursors {
  206. line, col := buffer.LineCol(buf, cur.Offset)
  207. screenLine := line - buf.TopLine
  208. if screenLine < 0 || screenLine+1 > rect.Height {
  209. continue
  210. }
  211. visCol := visualCol(buffer.LineText(buf, line), col)
  212. if visCol+1 > rect.Width {
  213. continue
  214. }
  215. ch := buffer.RuneAt(buf, cur.Offset)
  216. if m.Mode == ModeWrite {
  217. m.Screen.SetContent(rect.X+visCol+5, rect.Y+screenLine, ch, nil, m.Config.StyleDefault.Background(tcell.GetColor(m.Config.Style.CursorColorWrite)))
  218. } else {
  219. m.Screen.SetContent(rect.X+visCol+5, rect.Y+screenLine, ch, nil, m.Config.StyleDefault.Background(tcell.GetColor(m.Config.Style.CursorColor)))
  220. }
  221. }
  222. }
  223. }
  224. const tabWidth = 4
  225. func expandTabs(s string) string {
  226. var b strings.Builder
  227. col := 0
  228. for _, r := range s {
  229. if r == '\t' {
  230. spaces := tabWidth - (col % tabWidth)
  231. b.WriteString(strings.Repeat(" ", spaces))
  232. col += spaces
  233. } else {
  234. b.WriteRune(r)
  235. col++
  236. }
  237. }
  238. return b.String()
  239. }
  240. func visualCol(lineText string, runeCol int) int {
  241. col := 0
  242. n := 0
  243. for _, r := range lineText {
  244. if n >= runeCol {
  245. break
  246. }
  247. if r == '\t' {
  248. col += tabWidth - (col % tabWidth)
  249. } else {
  250. col++
  251. }
  252. n++
  253. }
  254. return col
  255. }