1
0

render.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package editor
  2. import (
  3. "strings"
  4. "moose/internal/buffer"
  5. "github.com/gdamore/tcell/v3"
  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. for _, cur := range buf.CM.Cursors {
  23. line, col := buffer.LineCol(buf.Rope, cur.Offset)
  24. if line + 1 > maxHeight { continue }
  25. if col + 1 > bufWidth { continue }
  26. if i == m.BM.CurrentIdx && m.Mode != ModePalette {
  27. m.Screen.SetContent(bufWidth * i + col, line, ' ', nil, m.Style.Reverse(true))
  28. } else {
  29. m.Screen.SetContent(bufWidth * i + col, line, ' ', nil, m.Style.Reverse(true).Foreground(tcell.ColorGray))
  30. }
  31. }
  32. }
  33. for col := 0; col < sWidth; col++ {
  34. m.Screen.SetContent(col, maxHeight, ' ', nil, m.Style.Reverse(true))
  35. }
  36. modeStr := m.Mode.String()
  37. m.Screen.PutStrStyled(sWidth - (len(modeStr) + 1), maxHeight, strings.ToUpper(modeStr), m.Style.Reverse(true))
  38. if m.Mode == ModePalette {
  39. m.Screen.PutStrStyled(0, maxHeight + 1, m.BM.PaletteBuffer.String(), m.Style)
  40. for _, cur := range m.BM.PaletteBuffer.CM.Cursors {
  41. line, col := buffer.LineCol(m.BM.PaletteBuffer.Rope, cur.Offset)
  42. if line + maxHeight != maxHeight { continue }
  43. if col + 1 > sWidth { continue }
  44. m.Screen.SetContent(col, maxHeight + 1, ' ', nil, m.Style.Reverse(true))
  45. }
  46. } else if strings.HasPrefix(m.BM.PaletteBuffer.String(), "moose.error:") {
  47. m.Screen.PutStrStyled(0, maxHeight + 1, string([]rune(m.BM.PaletteBuffer.String())[12:]), m.Style.Foreground(tcell.ColorRed))
  48. }
  49. }