| 1 |
package vt |
| 2 |
|
| 3 |
import ( |
| 4 |
uv "github.com/charmbracelet/ultraviolet" |
| 5 |
) |
| 6 |
|
| 7 |
// Screen represents a virtual terminal screen. |
| 8 |
type Screen struct { |
| 9 |
// cb is the callbacks struct to use. |
| 10 |
cb *Callbacks |
| 11 |
// The buffer of the screen. |
| 12 |
buf uv.Buffer |
| 13 |
// The cur of the screen. |
| 14 |
cur, saved Cursor |
| 15 |
// scroll is the scroll region. |
| 16 |
scroll uv.Rectangle |
| 17 |
} |
| 18 |
|
| 19 |
// NewScreen creates a new screen. |
| 20 |
func NewScreen(w, h int) *Screen { |
| 21 |
s := Screen{} |
| 22 |
s.Resize(w, h) |
| 23 |
return &s |
| 24 |
} |
| 25 |
|
| 26 |
// Reset resets the screen. |
| 27 |
// It clears the screen, sets the cursor to the top left corner, reset the |
| 28 |
// cursor styles, and resets the scroll region. |
| 29 |
func (s *Screen) Reset() { |
| 30 |
s.buf.Clear() |
| 31 |
s.cur = Cursor{} |
| 32 |
s.saved = Cursor{} |
| 33 |
s.scroll = s.buf.Bounds() |
| 34 |
} |
| 35 |
|
| 36 |
// Bounds returns the bounds of the screen. |
| 37 |
func (s *Screen) Bounds() uv.Rectangle { |
| 38 |
return s.buf.Bounds() |
| 39 |
} |
| 40 |
|
| 41 |
// Touched returns touched lines in the screen buffer. |
| 42 |
func (s *Screen) Touched() []*uv.LineData { |
| 43 |
return s.buf.Touched |
| 44 |
} |
| 45 |
|
| 46 |
// CellAt returns the cell at the given x, y position. |
| 47 |
func (s *Screen) CellAt(x int, y int) *uv.Cell { |
| 48 |
return s.buf.CellAt(x, y) |
| 49 |
} |
| 50 |
|
| 51 |
// SetCell sets the cell at the given x, y position. |
| 52 |
func (s *Screen) SetCell(x, y int, c *uv.Cell) { |
| 53 |
s.buf.SetCell(x, y, c) |
| 54 |
} |
| 55 |
|
| 56 |
// Height returns the height of the screen. |
| 57 |
func (s *Screen) Height() int { |
| 58 |
return s.buf.Height() |
| 59 |
} |
| 60 |
|
| 61 |
// Resize resizes the screen. |
| 62 |
func (s *Screen) Resize(width int, height int) { |
| 63 |
s.buf.Resize(width, height) |
| 64 |
s.scroll = s.buf.Bounds() |
| 65 |
} |
| 66 |
|
| 67 |
// Width returns the width of the screen. |
| 68 |
func (s *Screen) Width() int { |
| 69 |
return s.buf.Width() |
| 70 |
} |
| 71 |
|
| 72 |
// Clear clears the screen with blank cells. |
| 73 |
func (s *Screen) Clear() { |
| 74 |
s.ClearArea(s.Bounds()) |
| 75 |
} |
| 76 |
|
| 77 |
// ClearArea clears the given area. |
| 78 |
func (s *Screen) ClearArea(area uv.Rectangle) { |
| 79 |
s.buf.ClearArea(area) |
| 80 |
} |
| 81 |
|
| 82 |
// Fill fills the screen or part of it. |
| 83 |
func (s *Screen) Fill(c *uv.Cell) { |
| 84 |
s.FillArea(c, s.Bounds()) |
| 85 |
} |
| 86 |
|
| 87 |
// FillArea fills the given area with the given cell. |
| 88 |
func (s *Screen) FillArea(c *uv.Cell, area uv.Rectangle) { |
| 89 |
s.buf.FillArea(c, area) |
| 90 |
} |
| 91 |
|
| 92 |
// setHorizontalMargins sets the horizontal margins. |
| 93 |
func (s *Screen) setHorizontalMargins(left, right int) { |
| 94 |
s.scroll.Min.X = left |
| 95 |
s.scroll.Max.X = right |
| 96 |
} |
| 97 |
|
| 98 |
// setVerticalMargins sets the vertical margins. |
| 99 |
func (s *Screen) setVerticalMargins(top, bottom int) { |
| 100 |
s.scroll.Min.Y = top |
| 101 |
s.scroll.Max.Y = bottom |
| 102 |
} |
| 103 |
|
| 104 |
// setCursorX sets the cursor X position. If margins is true, the cursor is |
| 105 |
// only set if it is within the scroll margins. |
| 106 |
func (s *Screen) setCursorX(x int, margins bool) { |
| 107 |
s.setCursor(x, s.cur.Y, margins) |
| 108 |
} |
| 109 |
|
| 110 |
// setCursor sets the cursor position. If margins is true, the cursor is only |
| 111 |
// set if it is within the scroll margins. This follows how [ansi.CUP] works. |
| 112 |
func (s *Screen) setCursor(x, y int, margins bool) { |
| 113 |
old := s.cur.Position |
| 114 |
if !margins { |
| 115 |
y = clamp(y, 0, s.buf.Height()-1) |
| 116 |
x = clamp(x, 0, s.buf.Width()-1) |
| 117 |
} else { |
| 118 |
y = clamp(s.scroll.Min.Y+y, s.scroll.Min.Y, s.scroll.Max.Y-1) |
| 119 |
x = clamp(s.scroll.Min.X+x, s.scroll.Min.X, s.scroll.Max.X-1) |
| 120 |
} |
| 121 |
s.cur.X, s.cur.Y = x, y |
| 122 |
|
| 123 |
if s.cb.CursorPosition != nil && (old.X != x || old.Y != y) { |
| 124 |
s.cb.CursorPosition(old, uv.Pos(x, y)) |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
// moveCursor moves the cursor by the given x and y deltas. If the cursor |
| 129 |
// position is inside the scroll region, it is bounded by the scroll region. |
| 130 |
// Otherwise, it is bounded by the screen bounds. |
| 131 |
// This follows how [ansi.CUU], [ansi.CUD], [ansi.CUF], [ansi.CUB], [ansi.CNL], |
| 132 |
// [ansi.CPL]. |
| 133 |
func (s *Screen) moveCursor(dx, dy int) { |
| 134 |
scroll := s.scroll |
| 135 |
old := s.cur.Position |
| 136 |
if old.X < scroll.Min.X { |
| 137 |
scroll.Min.X = 0 |
| 138 |
} |
| 139 |
if old.X >= scroll.Max.X { |
| 140 |
scroll.Max.X = s.buf.Width() |
| 141 |
} |
| 142 |
|
| 143 |
pt := uv.Pos(s.cur.X+dx, s.cur.Y+dy) |
| 144 |
|
| 145 |
var x, y int |
| 146 |
if old.In(scroll) { |
| 147 |
y = clamp(pt.Y, scroll.Min.Y, scroll.Max.Y-1) |
| 148 |
x = clamp(pt.X, scroll.Min.X, scroll.Max.X-1) |
| 149 |
} else { |
| 150 |
y = clamp(pt.Y, 0, s.buf.Height()-1) |
| 151 |
x = clamp(pt.X, 0, s.buf.Width()-1) |
| 152 |
} |
| 153 |
|
| 154 |
s.cur.X, s.cur.Y = x, y |
| 155 |
|
| 156 |
if s.cb.CursorPosition != nil && (old.X != x || old.Y != y) { |
| 157 |
s.cb.CursorPosition(old, uv.Pos(x, y)) |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
// Cursor returns the cursor. |
| 162 |
func (s *Screen) Cursor() Cursor { |
| 163 |
return s.cur |
| 164 |
} |
| 165 |
|
| 166 |
// CursorPosition returns the cursor position. |
| 167 |
func (s *Screen) CursorPosition() (x, y int) { |
| 168 |
return s.cur.X, s.cur.Y |
| 169 |
} |
| 170 |
|
| 171 |
// ScrollRegion returns the scroll region. |
| 172 |
func (s *Screen) ScrollRegion() uv.Rectangle { |
| 173 |
return s.scroll |
| 174 |
} |
| 175 |
|
| 176 |
// SaveCursor saves the cursor. |
| 177 |
func (s *Screen) SaveCursor() { |
| 178 |
s.saved = s.cur |
| 179 |
} |
| 180 |
|
| 181 |
// RestoreCursor restores the cursor. |
| 182 |
func (s *Screen) RestoreCursor() { |
| 183 |
old := s.cur.Position |
| 184 |
s.cur = s.saved |
| 185 |
|
| 186 |
if s.cb.CursorPosition != nil && (old.X != s.cur.X || old.Y != s.cur.Y) { |
| 187 |
s.cb.CursorPosition(old, s.cur.Position) |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
// setCursorHidden sets the cursor hidden. |
| 192 |
func (s *Screen) setCursorHidden(hidden bool) { |
| 193 |
changed := s.cur.Hidden != hidden |
| 194 |
s.cur.Hidden = hidden |
| 195 |
if changed && s.cb.CursorVisibility != nil { |
| 196 |
s.cb.CursorVisibility(!hidden) |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
// setCursorStyle sets the cursor style. |
| 201 |
func (s *Screen) setCursorStyle(style CursorStyle, blink bool) { |
| 202 |
changed := s.cur.Style != style || s.cur.Steady != !blink |
| 203 |
s.cur.Style = style |
| 204 |
s.cur.Steady = !blink |
| 205 |
if changed && s.cb.CursorStyle != nil { |
| 206 |
s.cb.CursorStyle(style, !blink) |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
// cursorPen returns the cursor pen. |
| 211 |
func (s *Screen) cursorPen() uv.Style { |
| 212 |
return s.cur.Pen |
| 213 |
} |
| 214 |
|
| 215 |
// cursorLink returns the cursor link. |
| 216 |
func (s *Screen) cursorLink() uv.Link { |
| 217 |
return s.cur.Link |
| 218 |
} |
| 219 |
|
| 220 |
// ShowCursor shows the cursor. |
| 221 |
func (s *Screen) ShowCursor() { |
| 222 |
s.setCursorHidden(false) |
| 223 |
} |
| 224 |
|
| 225 |
// HideCursor hides the cursor. |
| 226 |
func (s *Screen) HideCursor() { |
| 227 |
s.setCursorHidden(true) |
| 228 |
} |
| 229 |
|
| 230 |
// InsertCell inserts n blank characters at the cursor position pushing out |
| 231 |
// cells to the right and out of the screen. |
| 232 |
func (s *Screen) InsertCell(n int) { |
| 233 |
if n <= 0 { |
| 234 |
return |
| 235 |
} |
| 236 |
|
| 237 |
x, y := s.cur.X, s.cur.Y |
| 238 |
s.buf.InsertCellArea(x, y, n, s.blankCell(), s.scroll) |
| 239 |
} |
| 240 |
|
| 241 |
// DeleteCell deletes n cells at the cursor position moving cells to the left. |
| 242 |
// This has no effect if the cursor is outside the scroll region. |
| 243 |
func (s *Screen) DeleteCell(n int) { |
| 244 |
if n <= 0 { |
| 245 |
return |
| 246 |
} |
| 247 |
|
| 248 |
x, y := s.cur.X, s.cur.Y |
| 249 |
s.buf.DeleteCellArea(x, y, n, s.blankCell(), s.scroll) |
| 250 |
} |
| 251 |
|
| 252 |
// ScrollUp scrolls the content up n lines within the given region. Lines |
| 253 |
// scrolled past the top margin are lost. This is equivalent to [ansi.SU] which |
| 254 |
// moves the cursor to the top margin and performs a [ansi.DL] operation. |
| 255 |
func (s *Screen) ScrollUp(n int) { |
| 256 |
x, y := s.CursorPosition() |
| 257 |
s.setCursor(s.cur.X, 0, true) |
| 258 |
s.DeleteLine(n) |
| 259 |
s.setCursor(x, y, false) |
| 260 |
} |
| 261 |
|
| 262 |
// ScrollDown scrolls the content down n lines within the given region. Lines |
| 263 |
// scrolled past the bottom margin are lost. This is equivalent to [ansi.SD] |
| 264 |
// which moves the cursor to top margin and performs a [ansi.IL] operation. |
| 265 |
func (s *Screen) ScrollDown(n int) { |
| 266 |
x, y := s.CursorPosition() |
| 267 |
s.setCursor(s.cur.X, 0, true) |
| 268 |
s.InsertLine(n) |
| 269 |
s.setCursor(x, y, false) |
| 270 |
} |
| 271 |
|
| 272 |
// InsertLine inserts n blank lines at the cursor position Y coordinate. |
| 273 |
// Only operates if cursor is within scroll region. Lines below cursor Y |
| 274 |
// are moved down, with those past bottom margin being discarded. |
| 275 |
// It returns true if the operation was successful. |
| 276 |
func (s *Screen) InsertLine(n int) bool { |
| 277 |
if n <= 0 { |
| 278 |
return false |
| 279 |
} |
| 280 |
|
| 281 |
x, y := s.cur.X, s.cur.Y |
| 282 |
|
| 283 |
// Only operate if cursor Y is within scroll region |
| 284 |
if y < s.scroll.Min.Y || y >= s.scroll.Max.Y || |
| 285 |
x < s.scroll.Min.X || x >= s.scroll.Max.X { |
| 286 |
return false |
| 287 |
} |
| 288 |
|
| 289 |
s.buf.InsertLineArea(y, n, s.blankCell(), s.scroll) |
| 290 |
|
| 291 |
return true |
| 292 |
} |
| 293 |
|
| 294 |
// DeleteLine deletes n lines at the cursor position Y coordinate. |
| 295 |
// Only operates if cursor is within scroll region. Lines below cursor Y |
| 296 |
// are moved up, with blank lines inserted at the bottom of scroll region. |
| 297 |
// It returns true if the operation was successful. |
| 298 |
func (s *Screen) DeleteLine(n int) bool { |
| 299 |
if n <= 0 { |
| 300 |
return false |
| 301 |
} |
| 302 |
|
| 303 |
scroll := s.scroll |
| 304 |
x, y := s.cur.X, s.cur.Y |
| 305 |
|
| 306 |
// Only operate if cursor Y is within scroll region |
| 307 |
if y < scroll.Min.Y || y >= scroll.Max.Y || |
| 308 |
x < scroll.Min.X || x >= scroll.Max.X { |
| 309 |
return false |
| 310 |
} |
| 311 |
|
| 312 |
s.buf.DeleteLineArea(y, n, s.blankCell(), scroll) |
| 313 |
|
| 314 |
return true |
| 315 |
} |
| 316 |
|
| 317 |
// blankCell returns the cursor blank cell with the background color set to the |
| 318 |
// current pen background color. If the pen background color is nil, the return |
| 319 |
// value is nil. |
| 320 |
func (s *Screen) blankCell() *uv.Cell { |
| 321 |
if s.cur.Pen.Bg == nil { |
| 322 |
return nil |
| 323 |
} |
| 324 |
|
| 325 |
c := uv.EmptyCell |
| 326 |
c.Style.Bg = s.cur.Pen.Bg |
| 327 |
return &c |
| 328 |
} |