| 1 |
package vt |
| 2 |
|
| 3 |
import ( |
| 4 |
uv "github.com/charmbracelet/ultraviolet" |
| 5 |
"github.com/charmbracelet/x/ansi" |
| 6 |
) |
| 7 |
|
| 8 |
// nextTab moves the cursor to the next tab stop n times. This respects the |
| 9 |
// horizontal scrolling region. This performs the same function as [ansi.CHT]. |
| 10 |
func (e *Emulator) nextTab(n int) { |
| 11 |
x, y := e.scr.CursorPosition() |
| 12 |
scroll := e.scr.ScrollRegion() |
| 13 |
for range n { |
| 14 |
ts := e.tabstops.Next(x) |
| 15 |
if ts < x { |
| 16 |
break |
| 17 |
} |
| 18 |
x = ts |
| 19 |
} |
| 20 |
|
| 21 |
if x >= scroll.Max.X { |
| 22 |
x = min(scroll.Max.X-1, e.Width()-1) |
| 23 |
} |
| 24 |
|
| 25 |
// NOTE: We use t.scr.setCursor here because we don't want to reset the |
| 26 |
// phantom state. |
| 27 |
e.scr.setCursor(x, y, false) |
| 28 |
} |
| 29 |
|
| 30 |
// prevTab moves the cursor to the previous tab stop n times. This respects the |
| 31 |
// horizontal scrolling region when origin mode is set. If the cursor would |
| 32 |
// move past the leftmost valid column, the cursor remains at the leftmost |
| 33 |
// valid column and the operation completes. |
| 34 |
func (e *Emulator) prevTab(n int) { |
| 35 |
x, _ := e.scr.CursorPosition() |
| 36 |
leftmargin := 0 |
| 37 |
scroll := e.scr.ScrollRegion() |
| 38 |
if e.isModeSet(ansi.DECOM) { |
| 39 |
leftmargin = scroll.Min.X |
| 40 |
} |
| 41 |
|
| 42 |
for range n { |
| 43 |
ts := e.tabstops.Prev(x) |
| 44 |
if ts > x { |
| 45 |
break |
| 46 |
} |
| 47 |
x = ts |
| 48 |
} |
| 49 |
|
| 50 |
if x < leftmargin { |
| 51 |
x = leftmargin |
| 52 |
} |
| 53 |
|
| 54 |
// NOTE: We use t.scr.setCursorX here because we don't want to reset the |
| 55 |
// phantom state. |
| 56 |
e.scr.setCursorX(x, false) |
| 57 |
} |
| 58 |
|
| 59 |
// moveCursor moves the cursor by the given x and y deltas. If the cursor |
| 60 |
// is at phantom, the state will reset and the cursor is back in the screen. |
| 61 |
func (e *Emulator) moveCursor(dx, dy int) { |
| 62 |
e.scr.moveCursor(dx, dy) |
| 63 |
e.atPhantom = false |
| 64 |
} |
| 65 |
|
| 66 |
// setCursor sets the cursor position. This resets the phantom state. |
| 67 |
func (e *Emulator) setCursor(x, y int) { |
| 68 |
e.scr.setCursor(x, y, false) |
| 69 |
e.atPhantom = false |
| 70 |
} |
| 71 |
|
| 72 |
// setCursorPosition sets the cursor position. This respects [ansi.DECOM], |
| 73 |
// Origin Mode. This performs the same function as [ansi.CUP]. |
| 74 |
func (e *Emulator) setCursorPosition(x, y int) { |
| 75 |
mode, ok := e.modes[ansi.DECOM] |
| 76 |
margins := ok && mode.IsSet() |
| 77 |
e.scr.setCursor(x, y, margins) |
| 78 |
e.atPhantom = false |
| 79 |
} |
| 80 |
|
| 81 |
// carriageReturn moves the cursor to the leftmost column. If [ansi.DECOM] is |
| 82 |
// set, the cursor is set to the left margin. If not, and the cursor is on or |
| 83 |
// to the right of the left margin, the cursor is set to the left margin. |
| 84 |
// Otherwise, the cursor is set to the leftmost column of the screen. |
| 85 |
// This performs the same function as [ansi.CR]. |
| 86 |
func (e *Emulator) carriageReturn() { |
| 87 |
mode, ok := e.modes[ansi.DECOM] |
| 88 |
margins := ok && mode.IsSet() |
| 89 |
x, y := e.scr.CursorPosition() |
| 90 |
if margins { |
| 91 |
e.scr.setCursor(0, y, true) |
| 92 |
} else if region := e.scr.ScrollRegion(); uv.Pos(x, y).In(region) { |
| 93 |
e.scr.setCursor(region.Min.X, y, false) |
| 94 |
} else { |
| 95 |
e.scr.setCursor(0, y, false) |
| 96 |
} |
| 97 |
e.atPhantom = false |
| 98 |
} |
| 99 |
|
| 100 |
// repeatPreviousCharacter repeats the previous character n times. This is |
| 101 |
// equivalent to typing the same character n times. This performs the same as |
| 102 |
// [ansi.REP]. |
| 103 |
func (e *Emulator) repeatPreviousCharacter(n int) { |
| 104 |
if e.lastChar == 0 { |
| 105 |
return |
| 106 |
} |
| 107 |
for range n { |
| 108 |
e.handlePrint(e.lastChar) |
| 109 |
} |
| 110 |
} |