1
0

render.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 += " (cmd)"
  42. } else if strings.HasPrefix(m.BM.PaletteBuffer.String(), "?") {
  43. modeStr += " (find)"
  44. }
  45. }
  46. m.Screen.PutStrStyled(sWidth - (len(modeStr) + 1), maxHeight, strings.ToUpper(modeStr), m.Style.Reverse(true))
  47. if m.Mode == ModePalette {
  48. m.Screen.PutStrStyled(0, maxHeight + 1, m.BM.PaletteBuffer.String(), m.Style)
  49. for _, cur := range m.BM.PaletteBuffer.CM.Cursors {
  50. line, col := buffer.LineCol(m.BM.PaletteBuffer.Rope, cur.Offset)
  51. if line + maxHeight != maxHeight { continue }
  52. if col + 1 > sWidth { continue }
  53. m.Screen.SetContent(col, maxHeight + 1, ' ', nil, m.Style.Reverse(true))
  54. }
  55. } else if strings.HasPrefix(m.BM.PaletteBuffer.String(), "moose.info:") {
  56. m.Screen.PutStrStyled(0, maxHeight + 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, maxHeight + 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, maxHeight + 1, string([]rune(m.BM.PaletteBuffer.String())[12:]), m.Style.Foreground(color.Red))
  61. }
  62. }