render.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. package editor
  2. import (
  3. "strings"
  4. "moose/internal/buffer"
  5. "github.com/gdamore/tcell/v3/color"
  6. )
  7. func (m Model) Render() {
  8. sWidth, sHeight := m.Screen.Size()
  9. maxHeight := sHeight - 2 // TODO: saturating sub?
  10. bLen := len(m.BM.Buffers)
  11. bufWidth := sWidth / bLen
  12. for i, buf := range m.BM.Buffers {
  13. table := strings.SplitAfter(buf.String(), "\n")
  14. for j, line := range table {
  15. if j + 1 > maxHeight { break }
  16. r := []rune(line)
  17. if len(r) + 1 > bufWidth {
  18. r = r[:bufWidth]
  19. }
  20. m.Screen.PutStrStyled(bufWidth*i, j, string(r), m.Style)
  21. }
  22. if i == m.BM.CurrentIdx || m.Mode == ModePalette {
  23. for _, cur := range buf.CM.Cursors {
  24. line, col := buffer.LineCol(buf.Rope, cur.Offset)
  25. if line + 1 > maxHeight { continue }
  26. if col + 1 > bufWidth { continue }
  27. if m.Mode == ModeWrite {
  28. m.Screen.SetContent(bufWidth * i + col, line, ' ', nil, m.Style.Reverse(true))
  29. } else {
  30. m.Screen.SetContent(bufWidth * i + col, line, ' ', nil, m.Style.Reverse(true).Foreground(color.Gray))
  31. }
  32. }
  33. }
  34. }
  35. for col := 0; col < sWidth; col++ {
  36. m.Screen.SetContent(col, maxHeight, ' ', nil, m.Style.Reverse(true))
  37. }
  38. modeStr := m.Mode.String()
  39. if m.Mode == ModePalette {
  40. if strings.HasPrefix(m.BM.PaletteBuffer.String(), "/") {
  41. modeStr = "command"
  42. } else if strings.HasPrefix(m.BM.PaletteBuffer.String(), "?") {
  43. modeStr = "find"
  44. } else if strings.HasPrefix(m.BM.PaletteBuffer.String(), "=") {
  45. modeStr = "replace"
  46. }
  47. }
  48. m.Screen.PutStrStyled(sWidth - (len(modeStr) + 1), maxHeight, strings.ToUpper(modeStr), m.Style.Reverse(true))
  49. if m.Mode == ModePalette {
  50. m.Screen.PutStrStyled(0, maxHeight + 1, m.BM.PaletteBuffer.String(), m.Style)
  51. for _, cur := range m.BM.PaletteBuffer.CM.Cursors {
  52. line, col := buffer.LineCol(m.BM.PaletteBuffer.Rope, cur.Offset)
  53. if line + maxHeight != maxHeight { continue }
  54. if col + 1 > sWidth { continue }
  55. m.Screen.SetContent(col, maxHeight + 1, ' ', nil, m.Style.Reverse(true))
  56. }
  57. } else if strings.HasPrefix(m.BM.PaletteBuffer.String(), "moose.info:") {
  58. m.Screen.PutStrStyled(0, maxHeight + 1, string([]rune(m.BM.PaletteBuffer.String())[11:]), m.Style.Foreground(color.LightGray))
  59. } else if strings.HasPrefix(m.BM.PaletteBuffer.String(), "moose.warn:") {
  60. m.Screen.PutStrStyled(0, maxHeight + 1, string([]rune(m.BM.PaletteBuffer.String())[11:]), m.Style.Foreground(color.Orange))
  61. } else if strings.HasPrefix(m.BM.PaletteBuffer.String(), "moose.error:") {
  62. m.Screen.PutStrStyled(0, maxHeight + 1, string([]rune(m.BM.PaletteBuffer.String())[12:]), m.Style.Foreground(color.Red))
  63. }
  64. }